66
77#include " helper.h" // assert_no_abort
88
9- PtAssignmentEngineDxy::PtAssignmentEngineDxy () {}
9+ PtAssignmentEngineDxy::PtAssignmentEngineDxy () : graphDefDxy_( nullptr ), sessionDxy_( nullptr ) {}
1010
11- PtAssignmentEngineDxy::~PtAssignmentEngineDxy () {}
11+ PtAssignmentEngineDxy::~PtAssignmentEngineDxy () {
12+ if (sessionDxy_ != nullptr ) {
13+ tensorflow::closeSession (sessionDxy_);
14+ }
15+ delete graphDefDxy_;
16+ }
1217
13- void PtAssignmentEngineDxy::configure (int verbose, const std::string nnModel) {
14- nnModel_ = nnModel;
18+ void PtAssignmentEngineDxy::configure (int verbose, const std::string pbFileNameDxy) {
1519 verbose_ = verbose;
16- std::string nnModelDxy_ = nnModel_;
17- loader = std::make_unique<hls4mlEmulator::ModelLoader>(nnModelDxy_);
18- model = loader->load_model ();
20+
21+ pbFileNameDxy_ = pbFileNameDxy;
22+ std::string pbFilePathDxy_ = " L1Trigger/L1TMuon/data/emtf_luts/" + pbFileNameDxy_;
23+
24+ inputNameDxy_ = " input1" ;
25+ outputNamesDxy_ = {" Identity" };
26+
27+ if (graphDefDxy_ == nullptr ) {
28+ graphDefDxy_ = tensorflow::loadGraphDef (edm::FileInPath (pbFilePathDxy_).fullPath ());
29+ }
30+ emtf_assert (graphDefDxy_ != nullptr );
31+
32+ if (sessionDxy_ == nullptr ) {
33+ sessionDxy_ = tensorflow::createSession (graphDefDxy_);
34+ }
35+
36+ emtf_assert (sessionDxy_ != nullptr );
1937}
2038
2139const PtAssignmentEngineAux2017& PtAssignmentEngineDxy::aux () const {
@@ -28,7 +46,7 @@ void PtAssignmentEngineDxy::calculate_pt_dxy(const EMTFTrack& track,
2846 emtf::Prediction& prediction) const {
2947 // This is called for each track instead of for entire track collection as was done in Phase-2 implementation
3048 preprocessing_dxy (track, feature);
31- call_hls_dxy (feature, prediction);
49+ call_tensorflow_dxy (feature, prediction);
3250 return ;
3351}
3452
@@ -135,24 +153,19 @@ void PtAssignmentEngineDxy::preprocessing_dxy(const EMTFTrack& track, emtf::Feat
135153 return ;
136154}
137155
138- void PtAssignmentEngineDxy::call_hls_dxy (const emtf::Feature& feature, emtf::Prediction& prediction) const {
156+ void PtAssignmentEngineDxy::call_tensorflow_dxy (const emtf::Feature& feature, emtf::Prediction& prediction) const {
157+ tensorflow::Tensor input (tensorflow::DT_FLOAT, {1 , emtf::NUM_FEATURES});
158+ std::vector<tensorflow::Tensor> outputs;
139159 emtf_assert (feature.size () == emtf::NUM_FEATURES);
140160
141- ap_uint<13 > nn_input[29 ];
142- for (size_t i = 0 ; i < feature.size (); i++) {
143- nn_input[i] = feature[i];
144- }
145- ap_uint<8 > nn_output[2 ];
146- model->prepare_input (nn_input);
147- model->predict ();
148- model->read_result (nn_output);
149- ap_uint<8 > pT = nn_output[0 ];
150- ap_uint<7 > dxy = (nn_output[1 ] > 127 ) ? ap_uint<7 >(127 ) : ap_uint<7 >(nn_output[1 ]);
151-
161+ float * d = input.flat <float >().data ();
162+ std::copy (feature.begin (), feature.end (), d);
163+ tensorflow::run (sessionDxy_, {{inputNameDxy_, input}}, outputNamesDxy_, &outputs);
164+ emtf_assert (outputs.size () == 1 );
152165 emtf_assert (prediction.size () == emtf::NUM_PREDICTIONS);
153166
154- prediction.at (0 ) = pT. to_float ( );
155- prediction.at (1 ) = dxy. to_float ( );
167+ prediction.at (0 ) = outputs[ 0 ]. matrix < float >()( 0 , 0 );
168+ prediction.at (1 ) = outputs[ 0 ]. matrix < float >()( 0 , 1 );
156169
157170 return ;
158171}
0 commit comments