Skip to content

Commit 77c1691

Browse files
committed
CCDB fetching of NNs ported to GPUWorkflowSpec
1 parent 710993a commit 77c1691

File tree

8 files changed

+148
-70
lines changed

8 files changed

+148
-70
lines changed

Detectors/TPC/calibration/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ o2_add_library(TPCCalibration
2525
src/CalibPadGainTracksBase.cxx
2626
src/CalibLaserTracks.cxx
2727
src/LaserTracksCalibrator.cxx
28+
src/NeuralNetworkClusterizer.cxx
2829
src/SACDecoder.cxx
2930
src/IDCAverageGroup.cxx
3031
src/IDCAverageGroupBase.cxx
@@ -82,6 +83,7 @@ o2_target_root_dictionary(TPCCalibration
8283
include/TPCCalibration/FastHisto.h
8384
include/TPCCalibration/CalibLaserTracks.h
8485
include/TPCCalibration/LaserTracksCalibrator.h
86+
include/TPCCalibration/NeuralNetworkClusterizer.h
8587
include/TPCCalibration/SACDecoder.h
8688
include/TPCCalibration/IDCAverageGroup.h
8789
include/TPCCalibration/IDCAverageGroupBase.h
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
/// \file NeuralNetworkClusterizer.h
13+
/// \brief Fetching neural networks for clusterization from CCDB
14+
/// \author Christian Sonnabend
15+
16+
#ifndef AliceO2_TPC_NeuralNetworkClusterizer_h
17+
#define AliceO2_TPC_NeuralNetworkClusterizer_h
18+
19+
#include "CCDB/CcdbApi.h"
20+
21+
namespace o2::tpc
22+
{
23+
24+
class NeuralNetworkClusterizer
25+
{
26+
public:
27+
NeuralNetworkClusterizer() = default;
28+
void initCcdbApi(std::string url);
29+
void loadIndividualFromCCDB(std::map<std::string, std::string> settings);
30+
31+
private:
32+
o2::ccdb::CcdbApi ccdbApi;
33+
std::map<std::string, std::string> metadata;
34+
std::map<std::string, std::string> headers;
35+
36+
};
37+
38+
} // namespace o2::tpc
39+
#endif
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
/// \file NeuralNetworkClusterizer.cxx
13+
/// \brief Fetching neural networks for clusterization from CCDB
14+
/// \author Christian Sonnabend
15+
16+
#include <CommonUtils/StringUtils.h>
17+
#include "TPCCalibration/NeuralNetworkClusterizer.h"
18+
19+
using namespace o2::tpc;
20+
21+
void NeuralNetworkClusterizer::initCcdbApi(std::string url) {
22+
ccdbApi.init(url);
23+
}
24+
25+
void NeuralNetworkClusterizer::loadIndividualFromCCDB(std::map<std::string, std::string> settings)
26+
{
27+
metadata["inputDType"] = settings["inputDType"];
28+
metadata["outputDType"] = settings["outputDType"];
29+
metadata["nnCCDBEvalType"] = settings["nnCCDBEvalType"]; // classification_1C, classification_2C, regression_1C, regression_2C
30+
metadata["nnCCDBWithMomentum"] = settings["nnCCDBWithMomentum"]; // 0, 1 -> Only for regression model
31+
metadata["nnCCDBLayerType"] = settings["nnCCDBLayerType"]; // FC, CNN
32+
if (settings["nnCCDBInteractionRate"] != "" && std::stoi(settings["nnCCDBInteractionRate"]) > 0) {
33+
metadata["nnCCDBInteractionRate"] = settings["nnCCDBInteractionRate"];
34+
}
35+
if (settings["nnCCDBBeamType"] != "") {
36+
metadata["nnCCDBBeamType"] = settings["nnCCDBBeamType"];
37+
}
38+
39+
bool retrieveSuccess = ccdbApi.retrieveBlob(settings["nnCCDBPath"], settings["outputFolder"], metadata, 1, false, settings["outputFile"]);
40+
// headers = ccdbApi.retrieveHeaders(settings["nnPathCCDB"], metadata, 1); // potentially needed to init some local variables
41+
42+
if (retrieveSuccess) {
43+
LOG(info) << "Network " << settings["nnCCDBPath"] << " retrieved from CCDB, stored at " << settings["outputFile"];
44+
} else {
45+
LOG(error) << "Failed to retrieve network from CCDB";
46+
}
47+
}

GPU/GPUTracking/Definitions/GPUSettingsList.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ AddOption(nnClassificationPath, std::string, "network_class.onnx", "", 0, "The c
268268
AddOption(nnClassThreshold, float, 0.5, "", 0, "The cutoff at which clusters will be accepted / rejected.")
269269
AddOption(nnRegressionPath, std::string, "network_reg.onnx", "", 0, "The regression network path")
270270
AddOption(nnSigmoidTrafoClassThreshold, int, 1, "", 0, "If true (default), then the classification threshold is transformed by an inverse sigmoid function. This depends on how the network was trained (with a sigmoid as acitvation function in the last layer or not).")
271+
AddOption(nnEvalMode, std::string, "c1:r1", "", 0, "Concatention of modes, e.g. c1:r1 (classification class 1, regression class 1)")
271272
// CCDB
272273
AddOption(nnLoadFromCCDB, int, 1, "", 0, "If 1 networks are fetched from ccdb, else locally")
274+
AddOption(nnLocalFolder, std::string, ".", "", 0, "Local folder in which the networks will be fetched")
273275
AddOption(nnCCDBURL, std::string, "http://ccdb-test.cern.ch:8080", "", 0, "The CCDB URL from where the network files are fetched")
274276
AddOption(nnCCDBPath, std::string, "Users/c/csonnabe/TPC/Clusterization", "", 0, "Folder path containing the networks")
275-
AddOption(nnCCDBFetchMode, std::string, "c1:r1", "", 0, "Concatention of modes, e.g. c1:r1 (classification class 1, regression class 1)")
276277
AddOption(nnCCDBWithMomentum, int, 1, "", 0, "Distinguishes between the network with and without momentum output for the regression")
277278
AddOption(nnCCDBClassificationLayerType, std::string, "FC", "", 0, "Distinguishes between network with different layer types. Options: FC, CNN")
278279
AddOption(nnCCDBRegressionLayerType, std::string, "CNN", "", 0, "Distinguishes between network with different layer types. Options: FC, CNN")

GPU/GPUTracking/TPCClusterFinder/GPUTPCNNClusterizerHost.cxx

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,27 @@
1616

1717
#include "GPUTPCNNClusterizerHost.h"
1818
#include "GPUTPCNNClusterizer.h"
19-
#include "CCDB/CcdbApi.h"
2019
#include "GPUSettings.h"
2120
#include "ML/3rdparty/GPUORTFloat16.h"
2221

2322
using namespace o2::gpu;
2423

25-
void GPUTPCNNClusterizerHost::loadFromCCDB(std::map<std::string, std::string> settings)
26-
{
27-
o2::ccdb::CcdbApi ccdbApi;
28-
ccdbApi.init(settings["nnCCDBURL"]);
29-
30-
metadata["inputDType"] = settings["inputDType"];
31-
metadata["outputDType"] = settings["outputDType"];
32-
metadata["nnCCDBEvalType"] = settings["nnCCDBEvalType"]; // classification_1C, classification_2C, regression_1C, regression_2C
33-
metadata["nnCCDBWithMomentum"] = settings["nnCCDBWithMomentum"]; // 0, 1 -> Only for regression model
34-
metadata["nnCCDBLayerType"] = settings["nnCCDBLayerType"]; // FC, CNN
35-
if (settings["nnCCDBInteractionRate"] != "" && std::stoi(settings["nnCCDBInteractionRate"]) > 0) {
36-
metadata["nnCCDBInteractionRate"] = settings["nnCCDBInteractionRate"];
37-
}
38-
if (settings["nnCCDBBeamType"] != "") {
39-
metadata["nnCCDBBeamType"] = settings["nnCCDBBeamType"];
40-
}
41-
42-
bool retrieveSuccess = ccdbApi.retrieveBlob(settings["nnCCDBPath"], ".", metadata, 1, false, settings["outputFile"]);
43-
// headers = ccdbApi.retrieveHeaders(settings["nnPathCCDB"], metadata, 1); // potentially needed to init some local variables
44-
45-
if (retrieveSuccess) {
46-
LOG(info) << "Network " << settings["nnCCDBPath"] << " retrieved from CCDB, stored at " << settings["outputFile"];
47-
} else {
48-
LOG(error) << "Failed to retrieve network from CCDB";
49-
}
50-
}
51-
5224
void GPUTPCNNClusterizerHost::init(const GPUSettingsProcessingNNclusterizer& settings)
5325
{
5426
std::string class_model_path = settings.nnClassificationPath, reg_model_path = settings.nnRegressionPath;
5527
std::vector<std::string> reg_model_paths;
56-
57-
if (settings.nnLoadFromCCDB) {
58-
std::map<std::string, std::string> ccdbSettings = {
59-
{"nnCCDBURL", settings.nnCCDBURL},
60-
{"nnCCDBPath", settings.nnCCDBPath},
61-
{"inputDType", settings.nnInferenceInputDType},
62-
{"outputDType", settings.nnInferenceOutputDType},
63-
{"nnCCDBWithMomentum", std::to_string(settings.nnCCDBWithMomentum)},
64-
{"nnCCDBBeamType", settings.nnCCDBBeamType},
65-
{"nnCCDBInteractionRate", std::to_string(settings.nnCCDBInteractionRate)}};
66-
67-
std::string nnFetchFolder = "";
68-
std::vector<std::string> fetchMode = o2::utils::Str::tokenize(settings.nnCCDBFetchMode, ':');
69-
std::map<std::string, std::string> networkRetrieval = ccdbSettings;
70-
71-
if (fetchMode[0] == "c1") {
72-
networkRetrieval["nnCCDBLayerType"] = settings.nnCCDBClassificationLayerType;
73-
networkRetrieval["nnCCDBEvalType"] = "classification_c1";
74-
networkRetrieval["outputFile"] = nnFetchFolder + "net_classification_c1.onnx";
75-
loadFromCCDB(networkRetrieval);
76-
} else if (fetchMode[0] == "c2") {
77-
networkRetrieval["nnCCDBLayerType"] = settings.nnCCDBClassificationLayerType;
78-
networkRetrieval["nnCCDBEvalType"] = "classification_c2";
79-
networkRetrieval["outputFile"] = nnFetchFolder + "net_classification_c2.onnx";
80-
loadFromCCDB(networkRetrieval);
28+
std::vector<std::string> evalMode = o2::utils::Str::tokenize(settings.nnEvalMode, ':');
29+
30+
if(settings.nnLoadFromCCDB) {
31+
reg_model_path = settings.nnLocalFolder + "/net_regression_c1.onnx"; // Needs to be set identical to NeuralNetworkClusterizer.cxx, otherwise the networks might be loaded from the wrong place
32+
if (evalMode[0] == "c1") {
33+
class_model_path = settings.nnLocalFolder + "/net_classification_c1.onnx";
34+
} else if (evalMode[0] == "c2") {
35+
class_model_path = settings.nnLocalFolder + "/net_classification_c2.onnx";
8136
}
82-
class_model_path = networkRetrieval["outputFile"]; // Setting the proper path from the where the models will be initialized locally
8337

84-
networkRetrieval["nnCCDBLayerType"] = settings.nnCCDBRegressionLayerType;
85-
networkRetrieval["nnCCDBEvalType"] = "regression_c1";
86-
networkRetrieval["outputFile"] = nnFetchFolder + "net_regression_c1.onnx";
87-
loadFromCCDB(networkRetrieval);
88-
reg_model_path = networkRetrieval["outputFile"];
89-
if (fetchMode[1] == "r2") {
90-
networkRetrieval["nnCCDBLayerType"] = settings.nnCCDBRegressionLayerType;
91-
networkRetrieval["nnCCDBEvalType"] = "regression_c2";
92-
networkRetrieval["outputFile"] = nnFetchFolder + "net_regression_c2.onnx";
93-
loadFromCCDB(networkRetrieval);
94-
reg_model_path += ":", networkRetrieval["outputFile"];
38+
if (evalMode[1] == "r2") {
39+
reg_model_path += ":" + settings.nnLocalFolder + "/net_regression_c2.onnx";
9540
}
9641
}
9742

GPU/GPUTracking/TPCClusterFinder/GPUTPCNNClusterizerHost.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ class GPUTPCNNClusterizerHost
4848
std::vector<bool> modelsUsed = {false, false, false}; // 0: class, 1: reg_1, 2: reg_2
4949
int32_t deviceId = -1;
5050
std::vector<std::string> reg_model_paths;
51-
52-
private:
53-
std::map<std::string, std::string> metadata;
54-
std::map<std::string, std::string> headers;
5551
}; // class GPUTPCNNClusterizerHost
5652

5753
} // namespace o2::gpu

GPU/Workflow/include/GPUWorkflow/GPUWorkflowSpec.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class GPUO2Interface;
8383
struct TPCPadGainCalib;
8484
struct TPCZSLinkMapping;
8585
struct GPUSettingsO2;
86+
struct GPUSettingsProcessingNNclusterizer;
8687
class GPUO2InterfaceQA;
8788
struct GPUTrackingInOutPointers;
8889
struct GPUTrackingInOutZS;
@@ -224,6 +225,8 @@ class GPURecoWorkflowSpec : public o2::framework::Task
224225
uint32_t mNextThreadIndex = 0;
225226
bool mUpdateGainMapCCDB = true;
226227
std::unique_ptr<o2::gpu::GPUSettingsTF> mTFSettings;
228+
std::unique_ptr<o2::gpu::GPUSettingsProcessingNNclusterizer> mNNClusterizerSettings;
229+
227230
Config mSpecConfig;
228231
std::shared_ptr<o2::base::GRPGeomRequest> mGGR;
229232
bool mGRPGeomUpdated = false;

GPU/Workflow/src/GPUWorkflowSpec.cxx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "DetectorsRaw/RDHUtils.h"
7979
#include "ITStracking/TrackingInterface.h"
8080
#include "GPUWorkflowInternal.h"
81+
#include "TPCCalibration/NeuralNetworkClusterizer.h"
8182
// #include "Framework/ThreadPool.h"
8283

8384
#include <TStopwatch.h>
@@ -118,6 +119,7 @@ GPURecoWorkflowSpec::GPURecoWorkflowSpec(GPURecoWorkflowSpec::CompletionPolicyDa
118119
mConfig.reset(new GPUO2InterfaceConfiguration);
119120
mConfParam.reset(new GPUSettingsO2);
120121
mTFSettings.reset(new GPUSettingsTF);
122+
mNNClusterizerSettings.reset(new GPUSettingsProcessingNNclusterizer);
121123
mTimer.reset(new TStopwatch);
122124
mPipeline.reset(new GPURecoWorkflowSpec_PipelineInternals);
123125

@@ -133,6 +135,49 @@ void GPURecoWorkflowSpec::init(InitContext& ic)
133135
GRPGeomHelper::instance().setRequest(mGGR);
134136
GPUO2InterfaceConfiguration& config = *mConfig.get();
135137

138+
if (mNNClusterizerSettings->nnLoadFromCCDB){
139+
LOG(info) << "Loading neural networks from CCDB";
140+
o2::tpc::NeuralNetworkClusterizer nnClusterizerFetcher;
141+
nnClusterizerFetcher.initCcdbApi(mNNClusterizerSettings->nnCCDBURL);
142+
std::map<std::string, std::string> ccdbSettings = {
143+
{"nnCCDBURL", mNNClusterizerSettings->nnCCDBURL},
144+
{"nnCCDBPath", mNNClusterizerSettings->nnCCDBPath},
145+
{"inputDType", mNNClusterizerSettings->nnInferenceInputDType},
146+
{"outputDType", mNNClusterizerSettings->nnInferenceOutputDType},
147+
{"outputFolder", mNNClusterizerSettings->nnLocalFolder},
148+
{"nnCCDBPath", mNNClusterizerSettings->nnCCDBPath},
149+
{"nnCCDBWithMomentum", std::to_string(mNNClusterizerSettings->nnCCDBWithMomentum)},
150+
{"nnCCDBBeamType", mNNClusterizerSettings->nnCCDBBeamType},
151+
{"nnCCDBInteractionRate", std::to_string(mNNClusterizerSettings->nnCCDBInteractionRate)}};
152+
153+
std::string nnFetchFolder = mNNClusterizerSettings->nnLocalFolder;
154+
std::vector<std::string> evalMode = o2::utils::Str::tokenize(mNNClusterizerSettings->nnEvalMode, ':');
155+
156+
if (evalMode[0] == "c1") {
157+
ccdbSettings["nnCCDBLayerType"] = mNNClusterizerSettings->nnCCDBClassificationLayerType;
158+
ccdbSettings["nnCCDBEvalType"] = "classification_c1";
159+
ccdbSettings["outputFile"] = "net_classification_c1.onnx";
160+
nnClusterizerFetcher.loadIndividualFromCCDB(ccdbSettings);
161+
} else if (evalMode[0] == "c2") {
162+
ccdbSettings["nnCCDBLayerType"] = mNNClusterizerSettings->nnCCDBClassificationLayerType;
163+
ccdbSettings["nnCCDBEvalType"] = "classification_c2";
164+
ccdbSettings["outputFile"] = "net_classification_c2.onnx";
165+
nnClusterizerFetcher.loadIndividualFromCCDB(ccdbSettings);
166+
}
167+
168+
ccdbSettings["nnCCDBLayerType"] = mNNClusterizerSettings->nnCCDBRegressionLayerType;
169+
ccdbSettings["nnCCDBEvalType"] = "regression_c1";
170+
ccdbSettings["outputFile"] = "net_regression_c1.onnx";
171+
nnClusterizerFetcher.loadIndividualFromCCDB(ccdbSettings);
172+
if (evalMode[1] == "r2") {
173+
ccdbSettings["nnCCDBLayerType"] = mNNClusterizerSettings->nnCCDBRegressionLayerType;
174+
ccdbSettings["nnCCDBEvalType"] = "regression_c2";
175+
ccdbSettings["outputFile"] = "net_regression_c2.onnx";
176+
nnClusterizerFetcher.loadIndividualFromCCDB(ccdbSettings);
177+
}
178+
LOG(info) << "Neural network loading done!";
179+
}
180+
136181
// Create configuration object and fill settings
137182
mConfig->configGRP.solenoidBzNominalGPU = 0;
138183
mTFSettings->hasSimStartOrbit = 1;

0 commit comments

Comments
 (0)