Skip to content

Commit

Permalink
Merge pull request #1 from cms-hls4ml/jmduarte-patch-1
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
jmduarte authored Dec 21, 2023
2 parents d7f10cc + 6900e90 commit 8d6c73a
Show file tree
Hide file tree
Showing 71 changed files with 9,701 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TOoLLip_v1/NN/
.gitignore
*.so
*.o
*~
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "TOoLLip_v1/L1JetTagger"]
path = TOoLLip_v1/L1JetTagger
path = TOoLLiP_v1/L1JetTagger
url = https://github.com/ucsd-hep-ex/L1JetTagger
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
CPP_STANDARD := c++17
CXXFLAGS := -O3 -fPIC -std=$(CPP_STANDARD)
PREFIX:=.
EMULATOR_EXTRAS := /cvmfs/cms.cern.ch/el8_amd64_gcc11/external/hls4mlEmulatorExtras/1.1.1-6933fcc7cdb4cdd5a649bd6579151d1b/
EMULATOR_EXTRAS := ../../hls4mlEmulatorExtras
AP_TYPES := $(EMULATOR_EXTRAS)/include/ap_types
HLS_ROOT := /cvmfs/cms.cern.ch/el8_amd64_gcc11/external/hls/2019.08-fd724004387c2a6770dc3517446d30d9
HLS_ROOT := ../../hls
HLS4ML_INCLUDE := $(EMULATOR_EXTRAS)/include/hls4ml
INCLUDES := -I$(HLS4ML_INCLUDE) -I$(AP_TYPES) -I$(HLS_ROOT)/include
LD_FLAGS := -L$(EMULATOR_EXTRAS)/lib64 -lemulator_interface
ALL_VERSIONS:=TOoLLip_v1/TOoLLip_emulator_v1.so
ALL_VERSIONS:=TOoLLiP_v1/TOoLLiP_v1.so

.DEFAULT_GOAL := all
.PHONY: all clean install
Expand All @@ -19,10 +19,10 @@ all: $(ALL_VERSIONS)
install: all
@rm -rf $(PREFIX)/lib64
@mkdir -p $(PREFIX)/lib64
cp TOoLLip_*.so $(PREFIX)/lib64
cp TOoLLiP_*.so $(PREFIX)/lib64

%.so:
$(MAKE) -C $(@D) INCLUDES="$(INCLUDES)" LD_FLAGS="$(LD_FLAGS)" CXXFLAGS="$(CXXFLAGS)"

clean:
rm TOoLLip_*.so
rm TOoLLiP_*.so
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# TOoLLiP

- Model training and conversion code: https://github.com/ucsd-hep-ex/L1JetTag
- Emulator tutorial: https://gitlab.cern.ch/fastmachinelearning/cms_mlatl1t_tutorial/-/tree/master/part3

## v1
- Based on QKeras model: https://github.com/ucsd-hep-ex/L1JetTagger/blob/eb93e812690dcf1431bf5cb27bf063d154daed1e/qkL1JetTagModel.h5
- Convert script: https://github.com/ucsd-hep-ex/L1JetTagger/blob/eb93e812690dcf1431bf5cb27bf063d154daed1e/HLS_qk_Roc.py
- Converted HLS project: https://github.com/ucsd-hep-ex/L1JetTagger/tree/eb93e812690dcf1431bf5cb27bf063d154daed1e
- hls4ml version/commit used: https://github.com/jmduarte/hls4ml/tree/8dea6ad51f78be4662513c1d4482b4b0ab94c5de
- Model:
![model_summary](https://github.com/cms-hls4ml/TOoLLiP/assets/4932543/788b4d4b-4268-4ad4-94ab-26b4edadb0de)

- Expected performance:

![hls_Qk_ROCCurve](https://github.com/cms-hls4ml/TOoLLiP/assets/4932543/1470ab5b-ec51-4741-a500-ace2a7a14b5d)

![profiling_sigmoid](https://github.com/cms-hls4ml/TOoLLiP/assets/4932543/48042a16-5a9c-4697-b891-a1b57fa899ac)
1 change: 1 addition & 0 deletions TOoLLiP_v1/L1JetTagger
Submodule L1JetTagger added at eb93e8
4 changes: 2 additions & 2 deletions TOoLLip_v1/Makefile → TOoLLiP_v1/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: clean

TOoLLip_emulator_v1.so: TOoLLip_emulator_v1.o myproject.o
TOoLLiP_v1.so: TOoLLiP_emulator_v1.o TOoLLiP_v1.o
$(CXX) $(CXXFLAGS) $(LD_FLAGS) -shared $^ -o $@

%.o: NN/%.cpp
Expand All @@ -10,4 +10,4 @@ TOoLLip_emulator_v1.so: TOoLLip_emulator_v1.o myproject.o
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

clean:
rm TOoLLip_emulator_v1.so TOoLLip_emulator_v1.o myproject.o
rm TOoLLiP_emulator_v1.o TOoLLiP_v1.o TOoLLiP_v1.so
73 changes: 73 additions & 0 deletions TOoLLiP_v1/NN/TOoLLiP_v1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>

#include "TOoLLiP_v1.h"
#include "parameters.h"

void TOoLLiP_v1(
input_t input_1[N_INPUT_1_1*N_INPUT_2_1],
result_t layer14_out[N_LAYER_12]
) {

// hls-fpga-machine-learning insert IO
#pragma HLS ARRAY_RESHAPE variable=input_1 complete dim=0
#pragma HLS ARRAY_PARTITION variable=layer14_out complete dim=0
#pragma HLS INTERFACE ap_vld port=input_1,layer14_out
#pragma HLS DATAFLOW

#ifdef __HLS4ML_LOAD_TXT_WEIGHTS__
static bool loaded_weights = false;
if (!loaded_weights) {
// hls-fpga-machine-learning insert load weights
nnet::load_weights_from_txt<weight15_t, 140>(w15, "w15.txt");
nnet::load_weights_from_txt<bias15_t, 10>(b15, "b15.txt");
nnet::load_weights_from_txt<weight16_t, 100>(w16, "w16.txt");
nnet::load_weights_from_txt<bias16_t, 10>(b16, "b16.txt");
nnet::load_weights_from_txt<weight9_t, 100>(w9, "w9.txt");
nnet::load_weights_from_txt<bias9_t, 10>(b9, "b9.txt");
nnet::load_weights_from_txt<weight12_t, 10>(w12, "w12.txt");
nnet::load_weights_from_txt<bias12_t, 1>(b12, "b12.txt");
loaded_weights = true;
}
#endif

// ****************************************
// NETWORK INSTANTIATION
// ****************************************

// hls-fpga-machine-learning insert layers

layer15_t layer15_out[N_OUTPUTS_15*N_FILT_15];
#pragma HLS ARRAY_PARTITION variable=layer15_out complete dim=0
nnet::pointwise_conv_1d_cl<input_t, layer15_t, config15>(input_1, layer15_out, w15, b15); // q_conv1d

layer4_t layer4_out[N_OUTPUTS_2*N_FILT_2];
#pragma HLS ARRAY_PARTITION variable=layer4_out complete dim=0
nnet::relu<layer15_t, layer4_t, relu_config4>(layer15_out, layer4_out); // q_activation

layer16_t layer16_out[N_OUTPUTS_16*N_FILT_16];
#pragma HLS ARRAY_PARTITION variable=layer16_out complete dim=0
nnet::pointwise_conv_1d_cl<layer4_t, layer16_t, config16>(layer4_out, layer16_out, w16, b16); // q_conv1d_1

layer7_t layer7_out[N_OUTPUTS_5*N_FILT_5];
#pragma HLS ARRAY_PARTITION variable=layer7_out complete dim=0
nnet::relu<layer16_t, layer7_t, relu_config7>(layer16_out, layer7_out); // q_activation_1

layer8_t layer8_out[N_FILT_8];
#pragma HLS ARRAY_PARTITION variable=layer8_out complete dim=0
nnet::global_pooling1d_cl<layer7_t, layer8_t, config8>(layer7_out, layer8_out); // global_average_pooling1d

layer9_t layer9_out[N_LAYER_9];
#pragma HLS ARRAY_PARTITION variable=layer9_out complete dim=0
nnet::dense<layer8_t, layer9_t, config9>(layer8_out, layer9_out, w9, b9); // q_dense

layer11_t layer11_out[N_LAYER_9];
#pragma HLS ARRAY_PARTITION variable=layer11_out complete dim=0
nnet::relu<layer9_t, layer11_t, relu_config11>(layer9_out, layer11_out); // q_activation_2

layer12_t layer12_out[N_LAYER_12];
#pragma HLS ARRAY_PARTITION variable=layer12_out complete dim=0
nnet::dense<layer11_t, layer12_t, config12>(layer11_out, layer12_out, w12, b12); // q_dense_1

nnet::sigmoid<layer12_t, result_t, sigmoid_config14>(layer12_out, layer14_out); // sigmoid

}
16 changes: 16 additions & 0 deletions TOoLLiP_v1/NN/TOoLLiP_v1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TOOLLIP_V1_H_
#define TOOLLIP_V1_H_

#include "ap_fixed.h"
#include "ap_int.h"
#include "hls_stream.h"

#include "defines.h"

// Prototype of top level function for C-synthesis
void TOoLLiP_v1(
input_t input_1[N_INPUT_1_1*N_INPUT_2_1],
result_t layer14_out[N_LAYER_12]
);

#endif
54 changes: 54 additions & 0 deletions TOoLLiP_v1/NN/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef DEFINES_H_
#define DEFINES_H_

#include "ap_fixed.h"
#include "ap_int.h"
#include "nnet_utils/nnet_types.h"
#include <cstddef>
#include <cstdio>

// hls-fpga-machine-learning insert numbers
#define N_INPUT_1_1 10
#define N_INPUT_2_1 14
#define N_OUTPUTS_15 10
#define N_FILT_15 10
#define N_OUTPUTS_2 10
#define N_FILT_2 10
#define N_OUTPUTS_16 10
#define N_FILT_16 10
#define N_OUTPUTS_5 10
#define N_FILT_5 10
#define N_FILT_8 10
#define N_LAYER_9 10
#define N_LAYER_9 10
#define N_LAYER_12 1
#define N_LAYER_12 1

// hls-fpga-machine-learning insert layer-precision
typedef ap_fixed<16,6> input_t;
typedef ap_fixed<16,6> model_default_t;
typedef ap_fixed<16,6> layer15_t;
typedef ap_fixed<8,4> weight15_t;
typedef ap_fixed<8,4> bias15_t;
typedef ap_ufixed<8,0,AP_RND_CONV,AP_SAT> layer4_t;
typedef ap_fixed<18,8> q_activation_table_t;
typedef ap_fixed<16,6> layer16_t;
typedef ap_fixed<8,4> weight16_t;
typedef ap_fixed<8,4> bias16_t;
typedef ap_ufixed<8,0,AP_RND_CONV,AP_SAT> layer7_t;
typedef ap_fixed<18,8> q_activation_1_table_t;
typedef ap_fixed<16,6> layer8_t;
typedef ap_fixed<16,6> layer9_t;
typedef ap_fixed<8,4> weight9_t;
typedef ap_fixed<8,4> bias9_t;
typedef ap_uint<1> layer9_index;
typedef ap_ufixed<8,0,AP_RND_CONV,AP_SAT> layer11_t;
typedef ap_fixed<18,8> q_activation_2_table_t;
typedef ap_fixed<16,6> layer12_t;
typedef ap_fixed<8,4> weight12_t;
typedef ap_fixed<8,4> bias12_t;
typedef ap_uint<1> layer12_index;
typedef ap_fixed<16,6> result_t;
typedef ap_fixed<18,8> sigmoid_table_t;

#endif
Loading

0 comments on commit 8d6c73a

Please sign in to comment.