CMS 3D CMS Logo

OnlineDQMDigiAD_cmssw.h
Go to the documentation of this file.
1 /*
2  * OnlineDQMDigiAD_cmssw.cpp
3  *
4  * Created on: Jun 10, 2023
5  * Author: Mulugeta W.Asres, UiA, Norway
6  *
7  * The implementation follows https://github.com/cms-sw/cmssw/tree/master/PhysicsTools/ONNXRuntime
8  */
9 #ifndef OnlineDQMDigiAD_cmssw_H_
10 #define OnlineDQMDigiAD_cmssw_H_
11 
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <functional>
19 #include <iostream>
20 #include <memory>
21 #include <numeric>
22 
23 // Declare OnlineDQMDigiAD class
25 public:
31  OnlineDQMDigiAD(const std::string model_system_name,
32  const std::string &modelFilepath,
34 
39 
43  void InitializeState();
44 
54  std::vector<std::vector<float>> Inference(std::vector<float> &digiHcalMapTW,
55  std::vector<float> &numEvents,
56  std::vector<float> &adThr,
57  std::vector<float> &input_model_state_memory_e_0_0,
58  std::vector<float> &input_model_state_memory_e_0_1,
59  std::vector<float> &input_model_state_memory_e_1_0,
60  std::vector<float> &input_model_state_memory_e_1_1,
61  std::vector<float> &input_model_state_memory_d_0_0,
62  std::vector<float> &input_model_state_memory_d_0_1,
63  std::vector<float> &input_model_state_memory_d_1_0,
64  std::vector<float> &input_model_state_memory_d_1_1);
78  std::vector<std::vector<float>> Inference_CMSSW(const std::vector<std::vector<float>> &digiHcal2DHist_depth_1,
79  const std::vector<std::vector<float>> &digiHcal2DHist_depth_2,
80  const std::vector<std::vector<float>> &digiHcal2DHist_depth_3,
81  const std::vector<std::vector<float>> &digiHcal2DHist_depth_4,
82  const std::vector<std::vector<float>> &digiHcal2DHist_depth_5,
83  const std::vector<std::vector<float>> &digiHcal2DHist_depth_6,
84  const std::vector<std::vector<float>> &digiHcal2DHist_depth_7,
85  const float LS_numEvents,
86  const float flagDecisionThr = 20);
87 
94  std::vector<std::vector<std::vector<float>>> ONNXOutputToDQMHistMap(
95  const std::vector<std::vector<float>> &ad_model_output_vectors,
96  const int numDepth,
97  const int numDIeta = 64,
98  const int selOutputIdx = 7);
99 
100 private:
101  // onnx session
102  const std::vector<std::string> hcal_modeled_systems = {"he", "hb"};
104  std::unique_ptr<cms::Ort::ONNXRuntime> ort_mSession = nullptr;
105  std::string model_path; // onnx model path
106 
107  // names of onnx model input vectors; do not change
108  const std::vector<std::string> input_names = {
109  "input_data",
110  "input_data_exo",
111  "anomaly_std_th",
112  "e_rnn_hidden__layer_0_state_0",
113  "e_rnn_hidden__layer_0_state_1",
114  "e_rnn_hidden__layer_1_state_0",
115  "e_rnn_hidden__layer_1_state_1",
116  "d_rnn_hidden__layer_0_state_0",
117  "d_rnn_hidden__layer_0_state_1",
118  "d_rnn_hidden__layer_1_state_0",
119  "d_rnn_hidden__layer_1_state_1",
120  };
121 
122  // names of onnx model outputs vectors; do not change
123  const std::vector<std::string> output_names = {
124  "target_data",
125  "pred_data",
126  "pred_err_spatial",
127  "pred_err_window_spatial",
128  "pred_err_spatial_scaled",
129  "pred_err_window_spatial_scaled",
130  "pred_err_spatial_scaled_aml",
131  "pred_err_window_spatial_scaled_aml",
132  "e_rnn_hidden__layer_0_state_0_o",
133  "e_rnn_hidden__layer_0_state_1_o",
134  "e_rnn_hidden__layer_1_state_0_o",
135  "e_rnn_hidden__layer_1_state_1_o",
136  "d_rnn_hidden__layer_0_state_0_o",
137  "d_rnn_hidden__layer_0_state_1_o",
138  "d_rnn_hidden__layer_1_state_0_o",
139  "d_rnn_hidden__layer_1_state_1_o",
140  };
141 
142  // model state network config declaration : encoder and decoder have each two lstm layers(each hold two state vectors, h0, c0)
143  const size_t num_state_vectors = 8;
144  const unsigned int model_state_inner_dim = 2; // do not change
145  const std::vector<std::vector<unsigned int>> model_state_layer_dims = {
146  {128, 32}, {128, 640}}; // do not change, encoder[layer_0, layer_1] and decoder [layer_0, layer_1]
147  const std::vector<std::vector<unsigned int>> model_state_layer_serialized_dims = {
148  {256, 64},
149  {256,
150  1280}}; // do not change, model_state_inner_dim*encoder[layer_0, layer_1] and model_state_inner_dim*decoder [layer_0, layer_1]
151  // unsigned model_state_refresh_counter = 15; // do not change for now. set due to onnx double datatype handling limitation that might cause precision error to propagate.
153  1; // DQM multithread returns non-sequential LS. Hence, the model will not keep states (experimental)
154 
155  std::vector<float> input_model_state_memory_e_0_0{std::vector<float>(model_state_layer_serialized_dims[0][0])};
156  std::vector<float> input_model_state_memory_e_0_1{std::vector<float>(model_state_layer_serialized_dims[0][0])};
157  std::vector<float> input_model_state_memory_e_1_0{std::vector<float>(model_state_layer_serialized_dims[0][1])};
158  std::vector<float> input_model_state_memory_e_1_1{std::vector<float>(model_state_layer_serialized_dims[0][1])};
159  std::vector<float> input_model_state_memory_d_0_0{std::vector<float>(model_state_layer_serialized_dims[1][0])};
160  std::vector<float> input_model_state_memory_d_0_1{std::vector<float>(model_state_layer_serialized_dims[1][0])};
161  std::vector<float> input_model_state_memory_d_1_0{std::vector<float>(model_state_layer_serialized_dims[1][1])};
162  std::vector<float> input_model_state_memory_d_1_1{std::vector<float>(model_state_layer_serialized_dims[1][1])};
163 
164  // input and outputs
165  int64_t batch_size = 1; // number maps to be evaluated at once, a single time-window
166  std::vector<std::vector<float>> input_values, output_values;
167  std::vector<std::vector<int64_t>> input_shapes;
168 
172  std::vector<float> Serialize2DVector(const std::vector<std::vector<float>> &input_2d_vec);
173 
177  std::vector<std::vector<float>> Map1DTo2DVector(const std::vector<float> &input_1d_vec, const int numSplits);
178 
183  std::vector<float> PrepareONNXDQMMapVectors(std::vector<std::vector<std::vector<float>>> &digiHcal2DHist_depth_all);
184 };
185 
186 #endif // OnlineDQMDigiAD_cmssw_H_
std::vector< float > input_model_state_memory_e_0_0
const std::vector< std::string > output_names
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.
unsigned model_state_refresh_counter
std::vector< float > input_model_state_memory_e_1_0
OnlineDQMDigiAD(const std::string model_system_name, const std::string &modelFilepath, cms::Ort::Backend backend=cms::Ort::Backend::cpu)
Constructor.
const std::vector< std::vector< unsigned int > > model_state_layer_serialized_dims
std::vector< float > input_model_state_memory_d_1_1
const size_t num_state_vectors
std::string hcal_subsystem_name
const unsigned int model_state_inner_dim
std::vector< std::vector< float > > input_values
std::vector< float > input_model_state_memory_d_0_0
void InitializeState()
Resets ml model memory states to default and function needs to be called when new collision run start...
std::vector< std::vector< float > > output_values
std::vector< float > input_model_state_memory_d_0_1
const std::vector< std::string > hcal_modeled_systems
std::unique_ptr< cms::Ort::ONNXRuntime > ort_mSession
const std::vector< std::string > input_names
std::vector< float > input_model_state_memory_e_1_1
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< std::vector< int64_t > > input_shapes
std::vector< float > input_model_state_memory_e_0_1
const std::vector< std::vector< unsigned int > > model_state_layer_dims
std::vector< float > input_model_state_memory_d_1_0
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.