Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:34

0001 /*
0002  * OnlineDQMDigiAD_cmssw.cpp
0003  *
0004  * Created on: Jun 10, 2023
0005  * Author: Mulugeta W.Asres, UiA, Norway
0006  *
0007  * The implementation follows https://github.com/cms-sw/cmssw/tree/master/PhysicsTools/ONNXRuntime
0008  */
0009 #ifndef OnlineDQMDigiAD_cmssw_H_
0010 #define OnlineDQMDigiAD_cmssw_H_
0011 
0012 #include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h"
0013 #include "FWCore/Utilities/interface/Exception.h"
0014 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0015 
0016 #include <algorithm>
0017 #include <cassert>
0018 #include <functional>
0019 #include <iostream>
0020 #include <memory>
0021 #include <numeric>
0022 
0023 // Declare OnlineDQMDigiAD class
0024 class OnlineDQMDigiAD {
0025 public:
0026   /**
0027      * @brief Constructor
0028      * @param modelFilepath: path to the .onnx file
0029      * @param Backend: backend selection cpu or gpu
0030      */
0031   OnlineDQMDigiAD(const std::string model_system_name,
0032                   const std::string &modelFilepath,
0033                   cms::Ort::Backend backend = cms::Ort::Backend::cpu);
0034 
0035   /**
0036      * @brief check whether onnx model integration is added for the selected hcal system
0037      */
0038   void IsModelExist(std::string hcal_subsystem_name);
0039 
0040   /**
0041      * @brief Resets ml model memory states to default and function needs to be called when new collision run starts
0042      */
0043   void InitializeState();
0044 
0045   /**
0046      * @brief Perform inference on a single image
0047      * @param digiHcalMapTW: The input digipccupany maps in time window
0048      * @param numEvents: The input number of events for map renormalization in time window
0049      * @param adThr: The anomaly detection decision threshold
0050      * @param input_model_state_memory_: The model memory states
0051      * @param output_tensors: output arrays
0052      * @return the list of  multidimensional arrays
0053      */
0054   std::vector<std::vector<float>> Inference(std::vector<float> &digiHcalMapTW,
0055                                             std::vector<float> &numEvents,
0056                                             std::vector<float> &adThr,
0057                                             std::vector<float> &input_model_state_memory_e_0_0,
0058                                             std::vector<float> &input_model_state_memory_e_0_1,
0059                                             std::vector<float> &input_model_state_memory_e_1_0,
0060                                             std::vector<float> &input_model_state_memory_e_1_1,
0061                                             std::vector<float> &input_model_state_memory_d_0_0,
0062                                             std::vector<float> &input_model_state_memory_d_0_1,
0063                                             std::vector<float> &input_model_state_memory_d_1_0,
0064                                             std::vector<float> &input_model_state_memory_d_1_1);
0065   /**
0066      * @brief Perform inference on a single image
0067      * @param digiHcal2DHist_depth_1: 2D histogram digioccupancy of the 1st depth of the hcal-hehb
0068      * @param digiHcal2DHist_depth_2: 2D histogram digioccupancy of the 2nd depth of the hcal-hehb
0069      * @param digiHcal2DHist_depth_3: 2D histogram digioccupancy of the 3rd depth of the hcal-hehb
0070      * @param digiHcal2DHist_depth_4: 2D histogram digioccupancy of the 4th depth of the hcal-hehb
0071      * @param digiHcal2DHist_depth_5: 2D histogram digioccupancy of the 5th depth of the hcal-hehb
0072      * @param digiHcal2DHist_depth_5: 2D histogram digioccupancy of the 6th depth of the hcal-hehb
0073      * @param digiHcal2DHist_depth_7: 2D histogram digioccupancy of the 7th depth of the hcal-hehb
0074      * @param LS_numEvents: The input number of events for digioccupancy map renormalization
0075      * @param flagDecisionThr: The anomaly detection decision threshold, decrease to increase sensitivity
0076      * @return ad_model_output_vectors: the vectors of multidimensional arrays: output_data_0, output_data_1, ...
0077      */
0078   std::vector<std::vector<float>> Inference_CMSSW(const std::vector<std::vector<float>> &digiHcal2DHist_depth_1,
0079                                                   const std::vector<std::vector<float>> &digiHcal2DHist_depth_2,
0080                                                   const std::vector<std::vector<float>> &digiHcal2DHist_depth_3,
0081                                                   const std::vector<std::vector<float>> &digiHcal2DHist_depth_4,
0082                                                   const std::vector<std::vector<float>> &digiHcal2DHist_depth_5,
0083                                                   const std::vector<std::vector<float>> &digiHcal2DHist_depth_6,
0084                                                   const std::vector<std::vector<float>> &digiHcal2DHist_depth_7,
0085                                                   const float LS_numEvents,
0086                                                   const float flagDecisionThr = 20);
0087 
0088   /**
0089      @brief Converts 1D serialized vector output of the onnx into 3d hcal-hehp vector
0090     @param ad_model_output_vectors: vector of 3D histogram maps the hcal-hehb, each vector output from the onnx. e.g 3d map of anomaly score and 3d map of anomaly flag or label
0091     @param selOutputIdx: index to select of the onnx output. e.g. 5 is the anomaly score and 7 is the anomaly flag (1 is with anomaly, 0 is healthy)
0092     @return ad_model_output_vectors: the vectors of multidimensional arrays: output_data_0, output_data_1, ...
0093     */
0094   std::vector<std::vector<std::vector<float>>> ONNXOutputToDQMHistMap(
0095       const std::vector<std::vector<float>> &ad_model_output_vectors,
0096       const int numDepth,
0097       const int numDIeta = 64,
0098       const int selOutputIdx = 7);
0099 
0100 private:
0101   // onnx session
0102   const std::vector<std::string> hcal_modeled_systems = {"he", "hb"};
0103   std::string hcal_subsystem_name;
0104   std::unique_ptr<cms::Ort::ONNXRuntime> ort_mSession = nullptr;
0105   std::string model_path;  // onnx model path
0106 
0107   // names of onnx model input vectors; do not change
0108   const std::vector<std::string> input_names = {
0109       "input_data",
0110       "input_data_exo",
0111       "anomaly_std_th",
0112       "e_rnn_hidden__layer_0_state_0",
0113       "e_rnn_hidden__layer_0_state_1",
0114       "e_rnn_hidden__layer_1_state_0",
0115       "e_rnn_hidden__layer_1_state_1",
0116       "d_rnn_hidden__layer_0_state_0",
0117       "d_rnn_hidden__layer_0_state_1",
0118       "d_rnn_hidden__layer_1_state_0",
0119       "d_rnn_hidden__layer_1_state_1",
0120   };
0121 
0122   // names of onnx model outputs vectors; do not change
0123   const std::vector<std::string> output_names = {
0124       "target_data",
0125       "pred_data",
0126       "pred_err_spatial",
0127       "pred_err_window_spatial",
0128       "pred_err_spatial_scaled",
0129       "pred_err_window_spatial_scaled",
0130       "pred_err_spatial_scaled_aml",
0131       "pred_err_window_spatial_scaled_aml",
0132       "e_rnn_hidden__layer_0_state_0_o",
0133       "e_rnn_hidden__layer_0_state_1_o",
0134       "e_rnn_hidden__layer_1_state_0_o",
0135       "e_rnn_hidden__layer_1_state_1_o",
0136       "d_rnn_hidden__layer_0_state_0_o",
0137       "d_rnn_hidden__layer_0_state_1_o",
0138       "d_rnn_hidden__layer_1_state_0_o",
0139       "d_rnn_hidden__layer_1_state_1_o",
0140   };
0141 
0142   // model state network config declaration : encoder and decoder have each two lstm layers(each hold two state vectors, h0, c0)
0143   const size_t num_state_vectors = 8;
0144   const unsigned int model_state_inner_dim = 2;  // do not change
0145   const std::vector<std::vector<unsigned int>> model_state_layer_dims = {
0146       {128, 32}, {128, 640}};  // do not change, encoder[layer_0, layer_1] and decoder [layer_0, layer_1]
0147   const std::vector<std::vector<unsigned int>> model_state_layer_serialized_dims = {
0148       {256, 64},
0149       {256,
0150        1280}};  // do not change, model_state_inner_dim*encoder[layer_0, layer_1] and model_state_inner_dim*decoder [layer_0, layer_1]
0151   // 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.
0152   unsigned model_state_refresh_counter =
0153       1;  // DQM multithread returns non-sequential LS. Hence, the model will not keep states (experimental)
0154 
0155   std::vector<float> input_model_state_memory_e_0_0{std::vector<float>(model_state_layer_serialized_dims[0][0])};
0156   std::vector<float> input_model_state_memory_e_0_1{std::vector<float>(model_state_layer_serialized_dims[0][0])};
0157   std::vector<float> input_model_state_memory_e_1_0{std::vector<float>(model_state_layer_serialized_dims[0][1])};
0158   std::vector<float> input_model_state_memory_e_1_1{std::vector<float>(model_state_layer_serialized_dims[0][1])};
0159   std::vector<float> input_model_state_memory_d_0_0{std::vector<float>(model_state_layer_serialized_dims[1][0])};
0160   std::vector<float> input_model_state_memory_d_0_1{std::vector<float>(model_state_layer_serialized_dims[1][0])};
0161   std::vector<float> input_model_state_memory_d_1_0{std::vector<float>(model_state_layer_serialized_dims[1][1])};
0162   std::vector<float> input_model_state_memory_d_1_1{std::vector<float>(model_state_layer_serialized_dims[1][1])};
0163 
0164   // input and outputs
0165   int64_t batch_size = 1;  // number maps to  be evaluated at once, a single time-window
0166   std::vector<std::vector<float>> input_values, output_values;
0167   std::vector<std::vector<int64_t>> input_shapes;
0168 
0169   /**
0170      * @brief Serializes 2d vectors into 1d
0171      */
0172   std::vector<float> Serialize2DVector(const std::vector<std::vector<float>> &input_2d_vec);
0173 
0174   /**
0175      * @brief Converts serialized 1d vectors into 2d
0176      */
0177   std::vector<std::vector<float>> Map1DTo2DVector(const std::vector<float> &input_1d_vec, const int numSplits);
0178 
0179   /**
0180      * @brief Prepares model input serialized dqm histogram from 2D histogram inputs from the cmssw
0181      *  @param digiHcal2DHist_depth_all: 3D vector (depth[ieta[iphi]]) of combined 2D histogram digioccupancy of the any depth of the hcal
0182      */
0183   std::vector<float> PrepareONNXDQMMapVectors(std::vector<std::vector<std::vector<float>>> &digiHcal2DHist_depth_all);
0184 };
0185 
0186 #endif  // OnlineDQMDigiAD_cmssw_H_