File indexing completed on 2024-04-06 12:07:34
0001
0002
0003
0004
0005
0006
0007
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
0024 class OnlineDQMDigiAD {
0025 public:
0026
0027
0028
0029
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
0037
0038 void IsModelExist(std::string hcal_subsystem_name);
0039
0040
0041
0042
0043 void InitializeState();
0044
0045
0046
0047
0048
0049
0050
0051
0052
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
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
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
0090
0091
0092
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
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;
0106
0107
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
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
0143 const size_t num_state_vectors = 8;
0144 const unsigned int model_state_inner_dim = 2;
0145 const std::vector<std::vector<unsigned int>> model_state_layer_dims = {
0146 {128, 32}, {128, 640}};
0147 const std::vector<std::vector<unsigned int>> model_state_layer_serialized_dims = {
0148 {256, 64},
0149 {256,
0150 1280}};
0151
0152 unsigned model_state_refresh_counter =
0153 1;
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
0165 int64_t batch_size = 1;
0166 std::vector<std::vector<float>> input_values, output_values;
0167 std::vector<std::vector<int64_t>> input_shapes;
0168
0169
0170
0171
0172 std::vector<float> Serialize2DVector(const std::vector<std::vector<float>> &input_2d_vec);
0173
0174
0175
0176
0177 std::vector<std::vector<float>> Map1DTo2DVector(const std::vector<float> &input_1d_vec, const int numSplits);
0178
0179
0180
0181
0182
0183 std::vector<float> PrepareONNXDQMMapVectors(std::vector<std::vector<std::vector<float>>> &digiHcal2DHist_depth_all);
0184 };
0185
0186 #endif