forked from AliceO2Group/O2Physics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpidOnnxModel.h
More file actions
346 lines (294 loc) · 13.4 KB
/
pidOnnxModel.h
File metadata and controls
346 lines (294 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file pidONNXModel.h
/// \brief A class that wraps PID ML ONNX model. See README.md for more detailed instructions.
///
/// \author Maja Kabus <mkabus@cern.ch>
#ifndef TOOLS_PIDML_PIDONNXMODEL_H_
#define TOOLS_PIDML_PIDONNXMODEL_H_
#include <Framework/ASoA.h>
#include <array>
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <limits>
#include <optional>
#include <string>
#include <map>
#include <type_traits>
#include <utility>
#include <memory>
#include <vector>
#if __has_include(<onnxruntime/core/session/onnxruntime_cxx_api.h>)
#include <onnxruntime/core/session/experimental_onnxruntime_cxx_api.h>
#else
#include <onnxruntime_cxx_api.h>
#endif
#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
#include "CCDB/CcdbApi.h"
#include "Tools/PIDML/pidUtils.h"
using namespace pidml::pidutils;
enum PidMLDetector {
kTPCOnly = 0,
kTPCTOF,
kTPCTOFTRD,
kNDetectors ///< number of available detectors configurations
};
namespace pidml_pt_cuts
{
// TODO: for now first limit wouldn't be used,
// network needs TPC, so we can either do not cut it by p or return 0.0f as prediction
constexpr std::array<double, kNDetectors> defaultModelPLimits({0.0, 0.5, 0.8});
} // namespace pidml_pt_cuts
// TODO: Copied from cefpTask, shall we put it in some common utils code?
namespace
{
bool readJsonFile(const std::string& config, rapidjson::Document& d)
{
FILE* fp = fopen(config.data(), "rb");
if (!fp) {
LOG(error) << "Missing configuration json file: " << config;
return false;
}
char readBuffer[65536];
rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
d.ParseStream(is);
fclose(fp);
return true;
}
} // namespace
template <typename T>
struct PidONNXModel {
public:
PidONNXModel(std::string& localPath, std::string& ccdbPath, bool useCCDB, o2::ccdb::CcdbApi& ccdbApi, uint64_t timestamp,
int pid, double minCertainty, const double* pLimits = &pidml_pt_cuts::defaultModelPLimits[0])
: mPid(pid), mMinCertainty(minCertainty), mPLimits(pLimits, pLimits + kNDetectors)
{
assert(mPLimits.size() == kNDetectors);
std::string modelFile;
loadInputFiles(localPath, ccdbPath, useCCDB, ccdbApi, timestamp, pid, modelFile);
Ort::SessionOptions sessionOptions;
mEnv = std::make_shared<Ort::Env>(ORT_LOGGING_LEVEL_WARNING, "pid-onnx-inferer");
LOG(info) << "Loading ONNX model from file: " << modelFile;
#if __has_include(<onnxruntime/core/session/onnxruntime_cxx_api.h>)
mSession.reset(new Ort::Experimental::Session{*mEnv, modelFile, sessionOptions});
#else
mSession.reset(new Ort::Session{*mEnv, modelFile.c_str(), sessionOptions});
#endif
LOG(info) << "ONNX model loaded";
#if __has_include(<onnxruntime/core/session/onnxruntime_cxx_api.h>)
mInputNames = mSession->GetInputNames();
mInputShapes = mSession->GetInputShapes();
mOutputNames = mSession->GetOutputNames();
mOutputShapes = mSession->GetOutputShapes();
#else
Ort::AllocatorWithDefaultOptions tmpAllocator;
for (size_t i = 0; i < mSession->GetInputCount(); ++i) {
mInputNames.push_back(mSession->GetInputNameAllocated(i, tmpAllocator).get());
}
for (size_t i = 0; i < mSession->GetInputCount(); ++i) {
mInputShapes.emplace_back(mSession->GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
}
for (size_t i = 0; i < mSession->GetOutputCount(); ++i) {
mOutputNames.push_back(mSession->GetOutputNameAllocated(i, tmpAllocator).get());
}
for (size_t i = 0; i < mSession->GetOutputCount(); ++i) {
mOutputShapes.emplace_back(mSession->GetOutputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
}
#endif
LOG(debug) << "Input Node Name/Shape (" << mInputNames.size() << "):";
for (size_t i = 0; i < mInputNames.size(); i++) {
LOG(debug) << "\t" << mInputNames[i] << " : " << printShape(mInputShapes[i]);
}
LOG(debug) << "Output Node Name/Shape (" << mOutputNames.size() << "):";
for (size_t i = 0; i < mOutputNames.size(); i++) {
LOG(debug) << "\t" << mOutputNames[i] << " : " << printShape(mOutputShapes[i]);
}
// Assume model has 1 input node and 1 output node.
assert(mInputNames.size() == 1 && mOutputNames.size() == 1);
}
PidONNXModel() = default;
PidONNXModel(PidONNXModel&&) = default;
PidONNXModel& operator=(PidONNXModel&&) = default;
PidONNXModel(const PidONNXModel&) = delete;
PidONNXModel& operator=(const PidONNXModel&) = delete;
~PidONNXModel() = default;
float applyModel(const typename T::iterator& track)
{
return getModelOutput(track);
}
bool applyModelBoolean(const typename T::iterator& track)
{
return getModelOutput(track) >= mMinCertainty;
}
int mPid;
double mMinCertainty;
private:
void getModelPaths(std::string const& path, std::string& modelDir, std::string& modelFile, std::string& modelPath, int pid, std::string const& ext)
{
modelDir = path;
modelFile = "attention_model_";
if (pid < 0) {
modelFile += "0" + std::to_string(-pid);
} else {
modelFile += std::to_string(pid);
}
modelFile += ext;
modelPath = modelDir + "/" + modelFile;
}
void downloadFromCCDB(o2::ccdb::CcdbApi& ccdbApi, std::string const& ccdbFile, uint64_t timestamp, std::string const& localDir, std::string const& localFile)
{
std::map<std::string, std::string> metadata;
bool retrieveSuccess = ccdbApi.retrieveBlob(ccdbFile, localDir, metadata, timestamp, false, localFile);
if (retrieveSuccess) {
std::map<std::string, std::string> headers = ccdbApi.retrieveHeaders(ccdbFile, metadata, timestamp);
LOG(info) << "Network file downloaded from: " << ccdbFile << " to: " << localDir << "/" << localFile;
} else {
LOG(fatal) << "Error encountered while fetching/loading the network from CCDB! Maybe the network doesn't exist yet for this run number/timestamp?";
}
}
void loadInputFiles(std::string const& localPath, std::string const& ccdbPath, bool useCCDB, o2::ccdb::CcdbApi& ccdbApi, uint64_t timestamp, int pid, std::string& modelPath)
{
rapidjson::Document trainColumnsDoc;
rapidjson::Document scalingParamsDoc;
std::string localDir, localModelFile;
std::string trainColumnsFile = "columns_for_training";
std::string scalingParamsFile = "scaling_params";
getModelPaths(localPath, localDir, localModelFile, modelPath, pid, ".onnx");
std::string localTrainColumnsPath = localDir + "/" + trainColumnsFile + ".json";
std::string localScalingParamsPath = localDir + "/" + scalingParamsFile + ".json";
if (useCCDB) {
std::string ccdbDir, ccdbModelFile, ccdbModelPath;
getModelPaths(ccdbPath, ccdbDir, ccdbModelFile, ccdbModelPath, pid, "");
std::string ccdbTrainColumnsPath = ccdbDir + "/" + trainColumnsFile;
std::string ccdbScalingParamsPath = ccdbDir + "/" + scalingParamsFile;
downloadFromCCDB(ccdbApi, ccdbModelPath, timestamp, localDir, localModelFile);
downloadFromCCDB(ccdbApi, ccdbTrainColumnsPath, timestamp, localDir, "columns_for_training.json");
downloadFromCCDB(ccdbApi, ccdbScalingParamsPath, timestamp, localDir, "scaling_params.json");
}
LOG(info) << "Using configuration files: " << localTrainColumnsPath << ", " << localScalingParamsPath;
if (readJsonFile(localTrainColumnsPath, trainColumnsDoc)) {
for (auto& param : trainColumnsDoc["columns_for_training"].GetArray()) {
auto columnLabel = param.GetString();
mTrainColumns.emplace_back(columnLabel);
mGetters.emplace_back(o2::soa::row_helpers::getColumnGetterByLabel<float, T>(columnLabel));
}
}
if (readJsonFile(localScalingParamsPath, scalingParamsDoc)) {
for (auto& param : scalingParamsDoc["data"].GetArray()) {
mScalingParams[param[0].GetString()] = std::make_pair(param[1].GetFloat(), param[2].GetFloat());
}
}
}
static float scale(float value, const std::pair<float, float>& scalingParams)
{
return (value - scalingParams.first) / scalingParams.second;
}
std::vector<float> getValues(const typename T::iterator& track)
{
std::vector<float> output;
output.reserve(mTrainColumns.size());
bool useTOF = !tofMissing(track) && inPLimit(track, mPLimits[kTPCTOF]);
bool useTRD = !trdMissing(track) && inPLimit(track, mPLimits[kTPCTOFTRD]);
for (uint32_t i = 0; i < mTrainColumns.size(); ++i) {
auto& columnLabel = mTrainColumns[i];
if (
((columnLabel == "fTRDSignal" || columnLabel == "fTRDPattern") && !useTRD) ||
((columnLabel == "fTOFSignal" || columnLabel == "fBeta") && !useTOF)) {
output.push_back(std::numeric_limits<float>::quiet_NaN());
continue;
}
std::optional<std::pair<float, float>> scalingParams = std::nullopt;
auto scalingParamsEntry = mScalingParams.find(columnLabel);
if (scalingParamsEntry != mScalingParams.end()) {
scalingParams = scalingParamsEntry->second;
}
float value = mGetters[i](track);
if (scalingParams) {
value = scale(value, scalingParams.value());
}
output.push_back(value);
}
return output;
}
float getModelOutput(const typename T::iterator& track)
{
// First rank of the expected model input is -1 which means that it is dynamic axis.
// Axis is exported as dynamic to make it possible to run model inference with the batch of
// tracks at once in the future (batch would need to have the same amount of quiet_NaNs in each row).
// For now we hardcode 1.
static constexpr int64_t batch_size = 1;
auto input_shape = mInputShapes[0];
input_shape[0] = batch_size;
std::vector<float> inputTensorValues = getValues(track);
std::vector<Ort::Value> inputTensors;
#if __has_include(<onnxruntime/core/session/onnxruntime_cxx_api.h>)
inputTensors.emplace_back(Ort::Experimental::Value::CreateTensor<float>(inputTensorValues.data(), inputTensorValues.size(), input_shape));
#else
Ort::MemoryInfo mem_info = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
inputTensors.emplace_back(Ort::Value::CreateTensor<float>(mem_info, inputTensorValues.data(), inputTensorValues.size(), input_shape.data(), input_shape.size()));
#endif
// Double-check the dimensions of the input tensor
assert(inputTensors[0].IsTensor() &&
inputTensors[0].GetTensorTypeAndShapeInfo().GetShape() == input_shape);
LOG(debug) << "input tensor shape: " << printShape(inputTensors[0].GetTensorTypeAndShapeInfo().GetShape());
try {
#if __has_include(<onnxruntime/core/session/onnxruntime_cxx_api.h>)
auto outputTensors = mSession->Run(mInputNames, inputTensors, mOutputNames);
#else
Ort::RunOptions runOptions;
std::vector<const char*> inputNamesChar(mInputNames.size(), nullptr);
std::transform(std::begin(mInputNames), std::end(mInputNames), std::begin(inputNamesChar),
[&](const std::string& str) { return str.c_str(); });
std::vector<const char*> outputNamesChar(mOutputNames.size(), nullptr);
std::transform(std::begin(mOutputNames), std::end(mOutputNames), std::begin(outputNamesChar),
[&](const std::string& str) { return str.c_str(); });
auto outputTensors = mSession->Run(runOptions, inputNamesChar.data(), inputTensors.data(), inputTensors.size(), outputNamesChar.data(), outputNamesChar.size());
#endif
// Double-check the dimensions of the output tensors
// The number of output tensors is equal to the number of output nodes specified in the Run() call
assert(outputTensors.size() == mOutputNames.size() && outputTensors[0].IsTensor());
LOG(debug) << "output tensor shape: " << printShape(outputTensors[0].GetTensorTypeAndShapeInfo().GetShape());
const float* output_value = outputTensors[0].GetTensorData<float>();
float certainty = *output_value;
return certainty;
} catch (const Ort::Exception& exception) {
LOG(error) << "Error running model inference: " << exception.what();
}
return false; // unreachable code
}
// Pretty prints a shape dimension vector
std::string printShape(const std::vector<int64_t>& v)
{
std::stringstream ss("");
for (size_t i = 0; i < v.size() - 1; i++)
ss << v[i] << "x";
ss << v[v.size() - 1];
return ss.str();
}
std::vector<std::string> mTrainColumns;
std::vector<float (*)(const typename T::iterator&)> mGetters;
std::map<std::string, std::pair<float, float>> mScalingParams;
std::shared_ptr<Ort::Env> mEnv = nullptr;
// No empty constructors for Session, we need a pointer
#if __has_include(<onnxruntime/core/session/onnxruntime_cxx_api.h>)
std::shared_ptr<Ort::Experimental::Session> mSession = nullptr;
#else
std::shared_ptr<Ort::Session> mSession = nullptr;
#endif
std::vector<double> mPLimits;
std::vector<std::string> mInputNames;
std::vector<std::vector<int64_t>> mInputShapes;
std::vector<std::string> mOutputNames;
std::vector<std::vector<int64_t>> mOutputShapes;
};
#endif // TOOLS_PIDML_PIDONNXMODEL_H_