66
77#include " helper.h" // assert_no_abort
88
9- PtAssignmentEngineDxy::PtAssignmentEngineDxy () : graphDefDxy_( nullptr ), sessionDxy_( nullptr ) { }
9+ PtAssignmentEngineDxy::PtAssignmentEngineDxy (std::shared_ptr<hls4mlEmulator::Model> model) { model_ = model; }
1010
11- PtAssignmentEngineDxy::~PtAssignmentEngineDxy () {
12- if (sessionDxy_ != nullptr ) {
13- tensorflow::closeSession (sessionDxy_);
14- }
15- delete graphDefDxy_;
16- }
17-
18- void PtAssignmentEngineDxy::configure (int verbose, const std::string pbFileNameDxy) {
19- verbose_ = verbose;
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- }
11+ PtAssignmentEngineDxy::~PtAssignmentEngineDxy () {}
3512
36- emtf_assert (sessionDxy_ != nullptr );
37- }
13+ void PtAssignmentEngineDxy::configure (int verbose) { verbose_ = verbose; }
3814
3915const PtAssignmentEngineAux2017& PtAssignmentEngineDxy::aux () const {
4016 static const PtAssignmentEngineAux2017 instance;
@@ -46,7 +22,7 @@ void PtAssignmentEngineDxy::calculate_pt_dxy(const EMTFTrack& track,
4622 emtf::Prediction& prediction) const {
4723 // This is called for each track instead of for entire track collection as was done in Phase-2 implementation
4824 preprocessing_dxy (track, feature);
49- call_tensorflow_dxy (feature, prediction);
25+ call_hls_dxy (feature, prediction);
5026 return ;
5127}
5228
@@ -153,19 +129,24 @@ void PtAssignmentEngineDxy::preprocessing_dxy(const EMTFTrack& track, emtf::Feat
153129 return ;
154130}
155131
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;
132+ void PtAssignmentEngineDxy::call_hls_dxy (const emtf::Feature& feature, emtf::Prediction& prediction) const {
159133 emtf_assert (feature.size () == emtf::NUM_FEATURES);
160134
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 );
135+ ap_uint<13 > nn_input[29 ];
136+ for (size_t i = 0 ; i < feature.size (); i++) {
137+ nn_input[i] = feature[i];
138+ }
139+ ap_uint<8 > nn_output[2 ];
140+ model_->prepare_input (nn_input);
141+ model_->predict ();
142+ model_->read_result (nn_output);
143+ ap_uint<8 > pT = nn_output[0 ];
144+ ap_uint<7 > dxy = (nn_output[1 ] > 127 ) ? ap_uint<7 >(127 ) : ap_uint<7 >(nn_output[1 ]);
145+
165146 emtf_assert (prediction.size () == emtf::NUM_PREDICTIONS);
166147
167- prediction.at (0 ) = outputs[ 0 ]. matrix < float >()( 0 , 0 );
168- prediction.at (1 ) = outputs[ 0 ]. matrix < float >()( 0 , 1 );
148+ prediction.at (0 ) = pT. to_float ( );
149+ prediction.at (1 ) = dxy. to_float ( );
169150
170151 return ;
171152}
0 commit comments