34 std::string instanceName{
"DESMOD Digioccupancy Map AD inference"};
44 auto uOrtSession = std::make_unique<ONNXRuntime>(
model_path, &session_options);
48 hcal_subsystem_name = model_system_name;
50 IsModelExist(hcal_subsystem_name);
52 if (hcal_subsystem_name ==
"he") {
53 std::vector<std::vector<int64_t>> input_shapes_ = {
54 {batch_size, 64, 72, 7, 1},
57 {batch_size, model_state_inner_dim, model_state_layer_dims[0][0]},
58 {batch_size, model_state_inner_dim, model_state_layer_dims[0][0]},
59 {batch_size, model_state_inner_dim, model_state_layer_dims[0][1]},
60 {batch_size, model_state_inner_dim, model_state_layer_dims[0][1]},
61 {batch_size, model_state_inner_dim, model_state_layer_dims[1][0]},
62 {batch_size, model_state_inner_dim, model_state_layer_dims[1][0]},
63 {batch_size, model_state_inner_dim, model_state_layer_dims[1][1]},
64 {batch_size, model_state_inner_dim, model_state_layer_dims[1][1]}};
65 input_shapes = input_shapes_;
68 else if (hcal_subsystem_name ==
"hb") {
69 std::vector<std::vector<int64_t>> input_shapes_ = {
70 {batch_size, 64, 72, 4, 1},
73 {batch_size, model_state_inner_dim, model_state_layer_dims[0][0]},
74 {batch_size, model_state_inner_dim, model_state_layer_dims[0][0]},
75 {batch_size, model_state_inner_dim, model_state_layer_dims[0][1]},
76 {batch_size, model_state_inner_dim, model_state_layer_dims[0][1]},
77 {batch_size, model_state_inner_dim, model_state_layer_dims[1][0]},
78 {batch_size, model_state_inner_dim, model_state_layer_dims[1][0]},
79 {batch_size, model_state_inner_dim, model_state_layer_dims[1][1]},
80 {batch_size, model_state_inner_dim, model_state_layer_dims[1][1]}};
81 input_shapes = input_shapes_;
86 if (
std::find(hcal_modeled_systems.begin(), hcal_modeled_systems.end(), hcal_subsystem_name) ==
87 hcal_modeled_systems.end()) {
89 "ML for OnlineDQM is not currently supported for the selected " + hcal_subsystem_name +
" system!\n";
90 throw std::invalid_argument(
err);
96 std::fill(input_model_state_memory_e_0_0.begin(),
97 input_model_state_memory_e_0_0.end(),
99 std::fill(input_model_state_memory_e_0_1.begin(),
100 input_model_state_memory_e_0_1.end(),
102 std::fill(input_model_state_memory_e_1_0.begin(),
103 input_model_state_memory_e_1_0.end(),
105 std::fill(input_model_state_memory_e_1_1.begin(),
106 input_model_state_memory_e_1_1.end(),
108 std::fill(input_model_state_memory_d_0_0.begin(),
109 input_model_state_memory_d_0_0.end(),
111 std::fill(input_model_state_memory_d_0_1.begin(),
112 input_model_state_memory_d_0_1.end(),
114 std::fill(input_model_state_memory_d_1_0.begin(),
115 input_model_state_memory_d_1_0.end(),
117 std::fill(input_model_state_memory_d_1_1.begin(),
118 input_model_state_memory_d_1_1.end(),
122 model_state_refresh_counter =
127 std::vector<float>
output;
128 for (
const auto &row : input_2d_vec) {
129 for (
const auto &element : row) {
130 output.push_back(element);
137 const int numSplits) {
139 throw std::invalid_argument(
"numSplits must be greater than 0.");
141 std::size_t
const splitted_size = input_1d_vec.size() / numSplits;
143 if (splitted_size * numSplits != input_1d_vec.size())
144 throw std::invalid_argument(
"Conversion is not allowed! The input vector length " +
145 std::to_string(input_1d_vec.size()) +
" must be divisible by the numSplits " +
148 std::vector<std::vector<float>> output_2d_vec;
150 for (
int i = 0;
i < numSplits;
i++) {
151 std::vector<float> chunch_vec(input_1d_vec.begin() +
i * splitted_size,
152 input_1d_vec.begin() + (
i + 1) * splitted_size);
153 output_2d_vec.push_back(chunch_vec);
155 return output_2d_vec;
160 std::vector<float> digi3DHistVector_serialized;
162 for (
const std::vector<std::vector<float>> &digiHcal2DHist_depth : digiHcal2DHist_depth_all) {
163 std::vector<float> digiHcalDHist_serialized_depth = Serialize2DVector(digiHcal2DHist_depth);
164 digi3DHistVector_serialized.insert(digi3DHistVector_serialized.end(),
165 digiHcalDHist_serialized_depth.begin(),
166 digiHcalDHist_serialized_depth.end());
169 return digi3DHistVector_serialized;
173 const std::vector<std::vector<float>> &ad_model_output_vectors,
176 const int selOutputIdx) {
179 const std::vector<float> &output_vector = ad_model_output_vectors[selOutputIdx];
180 std::vector<std::vector<float>> output_2d_vec = Map1DTo2DVector(output_vector, numDepth);
182 std::vector<std::vector<std::vector<float>>> digiHcal3DHist;
183 for (
const std::vector<float> &output_vector_depth : output_2d_vec) {
184 std::vector<std::vector<float>> digiHcal2DHist_depth = Map1DTo2DVector(output_vector_depth, numDIeta);
185 digiHcal3DHist.push_back(digiHcal2DHist_depth);
188 return digiHcal3DHist;
194 std::vector<float> &adThr,
195 std::vector<float> &input_model_state_memory_e_0_0,
196 std::vector<float> &input_model_state_memory_e_0_1,
197 std::vector<float> &input_model_state_memory_e_1_0,
198 std::vector<float> &input_model_state_memory_e_1_1,
199 std::vector<float> &input_model_state_memory_d_0_0,
200 std::vector<float> &input_model_state_memory_d_0_1,
201 std::vector<float> &input_model_state_memory_d_1_0,
202 std::vector<float> &input_model_state_memory_d_1_1) {
209 input_values.clear();
210 input_values.emplace_back(digiHcalMapTW);
212 input_values.emplace_back(adThr);
213 input_values.emplace_back(input_model_state_memory_e_0_0);
214 input_values.emplace_back(input_model_state_memory_e_0_1);
215 input_values.emplace_back(input_model_state_memory_e_1_0);
216 input_values.emplace_back(input_model_state_memory_e_1_1);
217 input_values.emplace_back(input_model_state_memory_d_0_0);
218 input_values.emplace_back(input_model_state_memory_d_0_1);
219 input_values.emplace_back(input_model_state_memory_d_1_0);
220 input_values.emplace_back(input_model_state_memory_d_1_1);
226 return output_values;
231 const std::vector<std::vector<float>> &digiHcal2DHist_depth_1,
232 const std::vector<std::vector<float>> &digiHcal2DHist_depth_2,
233 const std::vector<std::vector<float>> &digiHcal2DHist_depth_3,
234 const std::vector<std::vector<float>> &digiHcal2DHist_depth_4,
235 const std::vector<std::vector<float>> &digiHcal2DHist_depth_5,
236 const std::vector<std::vector<float>> &digiHcal2DHist_depth_6,
237 const std::vector<std::vector<float>> &digiHcal2DHist_depth_7,
238 const float LS_numEvents,
239 const float flagDecisionThr)
244 std::vector<std::vector<std::vector<float>>> digiHcal2DHist_depth_all;
246 if (hcal_subsystem_name ==
"he") {
247 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_1);
248 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_2);
249 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_3);
250 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_4);
251 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_5);
252 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_6);
253 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_7);
256 else if (hcal_subsystem_name ==
"hb") {
257 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_1);
258 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_2);
259 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_3);
260 digiHcal2DHist_depth_all.push_back(digiHcal2DHist_depth_4);
264 std::vector<float> digiHcalMapTW = PrepareONNXDQMMapVectors(digiHcal2DHist_depth_all);
266 std::vector<float> adThr{flagDecisionThr};
267 std::vector<float>
numEvents{LS_numEvents};
271 std::vector<std::vector<float>> output_tensors = Inference(digiHcalMapTW,
274 input_model_state_memory_e_0_0,
275 input_model_state_memory_e_0_1,
276 input_model_state_memory_e_1_0,
277 input_model_state_memory_e_1_1,
278 input_model_state_memory_d_0_0,
279 input_model_state_memory_d_0_1,
280 input_model_state_memory_d_1_0,
281 input_model_state_memory_d_1_1);
289 std::vector<std::vector<float>> ad_model_output_vectors, ad_model_state_vectors;
290 for (
size_t i = 0;
i < output_tensors.size();
i++) {
292 2, state_output_name_tag.length());
293 if (output_names_startstr == state_output_name_tag) {
294 ad_model_state_vectors.emplace_back(output_tensors[
i]);
296 ad_model_output_vectors.emplace_back(output_tensors[
i]);
300 if (ad_model_output_vectors.size() == num_state_vectors) {
301 input_model_state_memory_e_0_0 = ad_model_state_vectors[0];
302 input_model_state_memory_e_0_1 = ad_model_state_vectors[1];
303 input_model_state_memory_e_1_0 = ad_model_state_vectors[2];
304 input_model_state_memory_e_1_1 = ad_model_state_vectors[3];
305 input_model_state_memory_d_0_0 = ad_model_state_vectors[4];
306 input_model_state_memory_d_0_1 = ad_model_state_vectors[5];
307 input_model_state_memory_d_1_0 = ad_model_state_vectors[6];
308 input_model_state_memory_d_1_1 = ad_model_state_vectors[7];
310 std::cout <<
"Warning: the number of output state vectors does NOT equals to expected!. The states are set to " 325 if (--model_state_refresh_counter == 0)
328 return ad_model_output_vectors;
::Ort::SessionOptions defaultSessionOptions(Backend backend=Backend::cpu)
std::string fullPath() const
std::vector< std::vector< float > > Inference_CMSSW(const std::vector< std::vector< float >> &digiHcal2DHist_depth_1, const std::vector< std::vector< float >> &digiHcal2DHist_depth_2, const std::vector< std::vector< float >> &digiHcal2DHist_depth_3, const std::vector< std::vector< float >> &digiHcal2DHist_depth_4, const std::vector< std::vector< float >> &digiHcal2DHist_depth_5, const std::vector< std::vector< float >> &digiHcal2DHist_depth_6, const std::vector< std::vector< float >> &digiHcal2DHist_depth_7, const float LS_numEvents, const float flagDecisionThr=20)
Perform inference on a single image.
std::vector< std::vector< float > > Map1DTo2DVector(const std::vector< float > &input_1d_vec, const int numSplits)
Converts serialized 1d vectors into 2d.
std::vector< std::vector< float > > Inference(std::vector< float > &digiHcalMapTW, std::vector< float > &numEvents, std::vector< float > &adThr, std::vector< float > &input_model_state_memory_e_0_0, std::vector< float > &input_model_state_memory_e_0_1, std::vector< float > &input_model_state_memory_e_1_0, std::vector< float > &input_model_state_memory_e_1_1, std::vector< float > &input_model_state_memory_d_0_0, std::vector< float > &input_model_state_memory_d_0_1, std::vector< float > &input_model_state_memory_d_1_0, std::vector< float > &input_model_state_memory_d_1_1)
Perform inference on a single image.
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
static std::string to_string(const XMLCh *ch)
OnlineDQMDigiAD(const std::string model_system_name, const std::string &modelFilepath, cms::Ort::Backend backend=cms::Ort::Backend::cpu)
Constructor.
void InitializeState()
Resets ml model memory states to default and function needs to be called when new collision run start...
std::vector< float > PrepareONNXDQMMapVectors(std::vector< std::vector< std::vector< float >>> &digiHcal2DHist_depth_all)
Prepares model input serialized dqm histogram from 2D histogram inputs from the cmssw.
void IsModelExist(std::string hcal_subsystem_name)
check whether onnx model integration is added for the selected hcal system
std::vector< float > Serialize2DVector(const std::vector< std::vector< float >> &input_2d_vec)
Serializes 2d vectors into 1d.
std::vector< std::vector< std::vector< float > > > ONNXOutputToDQMHistMap(const std::vector< std::vector< float >> &ad_model_output_vectors, const int numDepth, const int numDIeta=64, const int selOutputIdx=7)
Converts 1D serialized vector output of the onnx into 3d hcal-hehp vector.