Skip to content

Commit e6488a9

Browse files
committed
Revert "Merge pull request #49179 from pallabidas/EMTF_NN_hls"
This reverts commit 3351dfb, reversing changes made to 6a7bc96.
1 parent 1750035 commit e6488a9

File tree

10 files changed

+52
-40
lines changed

10 files changed

+52
-40
lines changed

L1Trigger/L1TMuonEndCap/BuildFile.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@
88
<use name="DataFormats/L1TMuon"/>
99
<use name="L1Trigger/L1TMuon"/>
1010
<use name="PhysicsTools/TensorFlow"/>
11-
<use name="hls"/>
12-
<use name="hls4mlEmulatorExtras"/>

L1Trigger/L1TMuonEndCap/interface/PtAssignment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PtAssignment {
2424
bool promoteMode7,
2525
int modeQualVer,
2626
std::vector<int> promoteMode7Sectors,
27-
std::string nnModel);
27+
std::string pbFileName);
2828

2929
void process(EMTFTrackCollection& best_tracks);
3030

L1Trigger/L1TMuonEndCap/interface/PtAssignmentEngineDxy.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,33 @@
1010

1111
#include "L1Trigger/L1TMuonEndCap/interface/Common.h"
1212
#include "L1Trigger/L1TMuonEndCap/interface/PtAssignmentEngineAux2017.h"
13+
#include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
1314
#include "FWCore/ParameterSet/interface/FileInPath.h"
14-
#include "ap_fixed.h"
15-
#include "hls4ml/emulator.h"
1615

1716
class PtAssignmentEngineDxy {
1817
public:
1918
explicit PtAssignmentEngineDxy();
2019
virtual ~PtAssignmentEngineDxy();
2120

22-
void configure(int verbose, const std::string nnModel);
21+
void configure(int verbose, const std::string pbFileNameDxy);
2322

2423
const PtAssignmentEngineAux2017& aux() const;
2524

2625
virtual void calculate_pt_dxy(const EMTFTrack& track, emtf::Feature& feature, emtf::Prediction& prediction) const;
2726

2827
virtual void preprocessing_dxy(const EMTFTrack& track, emtf::Feature& feature) const;
2928

30-
virtual void call_hls_dxy(const emtf::Feature& feature, emtf::Prediction& prediction) const;
29+
virtual void call_tensorflow_dxy(const emtf::Feature& feature, emtf::Prediction& prediction) const;
3130

3231
protected:
3332
int verbose_;
34-
std::string nnModel_;
35-
std::unique_ptr<hls4mlEmulator::ModelLoader> loader;
36-
std::shared_ptr<hls4mlEmulator::Model> model;
33+
34+
tensorflow::GraphDef* graphDefDxy_;
35+
tensorflow::Session* sessionDxy_;
36+
std::string pbFileNameDxy_;
37+
std::string pbFilePathDxy_;
38+
std::string inputNameDxy_;
39+
std::vector<std::string> outputNamesDxy_;
3740
};
3841

3942
#endif

L1Trigger/L1TMuonEndCap/interface/VersionControl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class VersionControl {
5858
bool bug9BitDPhi_, bugMode7CLCT_, bugNegPt_, bugGMTPhi_, promoteMode7_;
5959
int modeQualVer_;
6060
std::vector<int> promoteMode7Sectors_; // Sectors to promote mode 7 tracks in Run 2 and Run 3, -1 for all sectors
61-
std::string nnModel_;
61+
std::string pbFileName_;
6262
};
6363

6464
#endif
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<library file="*.cc" name="L1TriggerL1TMuonEndCapPlugins">
22
<use name="L1Trigger/L1TMuonEndCap"/>
3-
<use name="hls"/>
4-
<use name="hls4mlEmulatorExtras"/>
53
<flags EDM_PLUGIN="1"/>
64
</library>

L1Trigger/L1TMuonEndCap/python/simEmtfDigis_cfi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
PromoteMode7 = cms.bool(False), # Assign station 2-3-4 tracks with |eta| > 1.6 SingleMu quality
127127
ModeQualVer = cms.int32(2), # Version 2 contains modified mode-quality mapping for 2018
128128
PromoteMode7Sectors = cms.vint32(-1), # Sectors to promote mode 7 tracks in Run 2 and Run 3, -1 for all sectors
129-
NNModel = cms.string('EMTFnn_v1'), # HLS model name to be used by NN based pT assignment NNv16 is online since 26.06.2023
129+
ProtobufFileName = cms.string('model_graph.displ.16.pb'), # Protobuf file name to be used by NN based pT assignment NNv16 is online since 26.06.2023
130130
),
131131

132132
)

L1Trigger/L1TMuonEndCap/src/PtAssignment.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void PtAssignment::configure(PtAssignmentEngine* pt_assign_engine,
1818
bool promoteMode7,
1919
int modeQualVer,
2020
std::vector<int> promoteMode7Sectors,
21-
std::string nnModel) {
21+
std::string pbFileName) {
2222
emtf_assert(pt_assign_engine != nullptr);
2323
emtf_assert(pt_assign_engine_dxy != nullptr);
2424

@@ -33,7 +33,7 @@ void PtAssignment::configure(PtAssignmentEngine* pt_assign_engine,
3333

3434
pt_assign_engine_->configure(verbose_, readPtLUTFile, fixMode15HighPt, bug9BitDPhi, bugMode7CLCT, bugNegPt);
3535

36-
pt_assign_engine_dxy_->configure(verbose_, nnModel);
36+
pt_assign_engine_dxy_->configure(verbose_, pbFileName);
3737

3838
bugGMTPhi_ = bugGMTPhi;
3939
promoteMode7_ = promoteMode7;

L1Trigger/L1TMuonEndCap/src/PtAssignmentEngineDxy.cc

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,34 @@
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

2139
const 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
}

L1Trigger/L1TMuonEndCap/src/SectorProcessor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void SectorProcessor::process_single_bx(int bx,
169169
cfg.promoteMode7_,
170170
cfg.modeQualVer_,
171171
cfg.promoteMode7Sectors_,
172-
cfg.nnModel_);
172+
cfg.pbFileName_);
173173

174174
std::map<int, TriggerPrimitiveCollection> selected_dt_map;
175175
std::map<int, TriggerPrimitiveCollection> selected_csc_map;

L1Trigger/L1TMuonEndCap/src/VersionControl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ VersionControl::VersionControl(const edm::ParameterSet& iConfig) : config_(iConf
6464
promoteMode7_ = spPAParams16.getParameter<bool>("PromoteMode7");
6565
modeQualVer_ = spPAParams16.getParameter<int>("ModeQualVer");
6666
promoteMode7Sectors_ = spPAParams16.getParameter<std::vector<int> >("PromoteMode7Sectors");
67-
nnModel_ = spPAParams16.getParameter<std::string>("NNModel");
67+
pbFileName_ = spPAParams16.getParameter<std::string>("ProtobufFileName");
6868
}
6969

7070
VersionControl::~VersionControl() {}

0 commit comments

Comments
 (0)