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, const int selOutputIdx = 7);
96 
97 private:
98  // onnx session
99  const std::vector<std::string> hcal_modeled_systems = {"he", "hb"};
101  std::unique_ptr<cms::Ort::ONNXRuntime> ort_mSession = nullptr;
102  std::string model_path; // onnx model path
103 
104  // names of onnx model input vectors; do not change
105  const std::vector<std::string> input_names = {
106  "input_data",
107  "input_data_exo",
108  "anomaly_std_th",
109  "e_rnn_hidden__layer_0_state_0",
110  "e_rnn_hidden__layer_0_state_1",
111  "e_rnn_hidden__layer_1_state_0",
112  "e_rnn_hidden__layer_1_state_1",
113  "d_rnn_hidden__layer_0_state_0",
114  "d_rnn_hidden__layer_0_state_1",
115  "d_rnn_hidden__layer_1_state_0",
116  "d_rnn_hidden__layer_1_state_1",
117  };
118 
119  // names of onnx model outputs vectors; do not change
120  const std::vector<std::string> output_names = {
121  "target_data",
122  "pred_data",
123  "pred_err_spatial",
124  "pred_err_window_spatial",
125  "pred_err_spatial_scaled",
126  "pred_err_window_spatial_scaled",
127  "pred_err_spatial_scaled_aml",
128  "pred_err_window_spatial_scaled_aml",
129  "e_rnn_hidden__layer_0_state_0_o",
130  "e_rnn_hidden__layer_0_state_1_o",
131  "e_rnn_hidden__layer_1_state_0_o",
132  "e_rnn_hidden__layer_1_state_1_o",
133  "d_rnn_hidden__layer_0_state_0_o",
134  "d_rnn_hidden__layer_0_state_1_o",
135  "d_rnn_hidden__layer_1_state_0_o",
136  "d_rnn_hidden__layer_1_state_1_o",
137  };
138 
139  // model state network config declaration : encoder and decoder have each two lstm layers(each hold two state vectors, h0, c0)
140  const size_t num_state_vectors = 8;
141  const unsigned int model_state_inner_dim = 2; // do not change
142  const std::vector<std::vector<unsigned int>> model_state_layer_dims = {
143  {128, 32}, {128, 640}}; // do not change, encoder[layer_0, layer_1] and decoder [layer_0, layer_1]
144  const std::vector<std::vector<unsigned int>> model_state_layer_serialized_dims = {
145  {256, 64},
146  {256,
147  1280}}; // do not change, model_state_inner_dim*encoder[layer_0, layer_1] and model_state_inner_dim*decoder [layer_0, layer_1]
148  // 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.
150  1; // DQM multithread returns non-sequential LS. Hence, the model will not keep states (experimental)
151 
152  std::vector<float> input_model_state_memory_e_0_0{std::vector<float>(model_state_layer_serialized_dims[0][0])};
153  std::vector<float> input_model_state_memory_e_0_1{std::vector<float>(model_state_layer_serialized_dims[0][0])};
154  std::vector<float> input_model_state_memory_e_1_0{std::vector<float>(model_state_layer_serialized_dims[0][1])};
155  std::vector<float> input_model_state_memory_e_1_1{std::vector<float>(model_state_layer_serialized_dims[0][1])};
156  std::vector<float> input_model_state_memory_d_0_0{std::vector<float>(model_state_layer_serialized_dims[1][0])};
157  std::vector<float> input_model_state_memory_d_0_1{std::vector<float>(model_state_layer_serialized_dims[1][0])};
158  std::vector<float> input_model_state_memory_d_1_0{std::vector<float>(model_state_layer_serialized_dims[1][1])};
159  std::vector<float> input_model_state_memory_d_1_1{std::vector<float>(model_state_layer_serialized_dims[1][1])};
160 
161  // input and outputs
162  int64_t batch_size = 1; // number maps to be evaluated at once, a single time-window
163  std::vector<std::vector<float>> input_values, output_values;
164  std::vector<std::vector<int64_t>> input_shapes;
165 
169  std::vector<float> Serialize2DVector(const std::vector<std::vector<float>> &input_2d_vec);
170 
174  std::vector<std::vector<float>> Map1DTo2DVector(const std::vector<float> &input_1d_vec, const int numSplits);
175 
180  std::vector<float> PrepareONNXDQMMapVectors(std::vector<std::vector<std::vector<float>>> &digiHcal2DHist_depth_all);
181 };
182 
183 #endif // OnlineDQMDigiAD_cmssw_H_
std::vector< float > input_model_state_memory_e_0_0
std::vector< std::vector< std::vector< float > > > ONNXOutputToDQMHistMap(const std::vector< std::vector< float >> &ad_model_output_vectors, const int selOutputIdx=7)
Converts 1D serialized vector output of the onnx into 3d hcal-hehp vector.
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.