Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Tools/ML/model.cxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.

Check failure on line 1 in Tools/ML/model.cxx

View workflow job for this annotation

GitHub Actions / O2 linter

[name/workflow-file]

Name of a workflow file must match the name of the main struct in it (without the PWG prefix). (Class implementation files should be in "Core" directories.)
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
Expand Down Expand Up @@ -42,17 +42,17 @@
std::string OnnxModel::printShape(const std::vector<int64_t>& v)
{
std::stringstream ss("");
for (size_t i = 0; i < v.size() - 1; i++)
for (std::size_t i = 0; i < v.size() - 1; i++)
ss << v[i] << "x";
ss << v[v.size() - 1];
return ss.str();
}

bool OnnxModel::checkHyperloop(bool verbose)
bool OnnxModel::checkHyperloop(const bool verbose)
{
/// Testing hyperloop core settings
const char* alienCores = gSystem->Getenv("ALIEN_JDL_CPUCORES");
bool alienCoresFound = (alienCores != NULL);
const bool alienCoresFound = (alienCores != NULL);
if (alienCoresFound) {
if (verbose) {
LOGP(info, "Hyperloop test/Grid job detected! Number of cores = {}. Setting threads anyway to 1.", alienCores);
Expand All @@ -68,7 +68,7 @@
return alienCoresFound;
}

void OnnxModel::initModel(std::string localPath, bool enableOptimizations, int threads, uint64_t from, uint64_t until)
void OnnxModel::initModel(const std::string& localPath, const bool enableOptimizations, const int threads, const uint64_t from, const uint64_t until)
{

assert(from <= until);
Expand All @@ -90,26 +90,26 @@
mEnv = std::make_shared<Ort::Env>(ORT_LOGGING_LEVEL_WARNING, "onnx-model");
mSession = std::make_shared<Ort::Session>(*mEnv, modelPath.c_str(), sessionOptions);

Ort::AllocatorWithDefaultOptions tmpAllocator;
for (size_t i = 0; i < mSession->GetInputCount(); ++i) {
Ort::AllocatorWithDefaultOptions const tmpAllocator;
for (std::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) {
for (std::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) {
for (std::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) {
for (std::size_t i = 0; i < mSession->GetOutputCount(); ++i) {
mOutputShapes.emplace_back(mSession->GetOutputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
}
LOG(info) << "Input Nodes:";
for (size_t i = 0; i < mInputNames.size(); i++) {
for (std::size_t i = 0; i < mInputNames.size(); i++) {
LOG(info) << "\t" << mInputNames[i] << " : " << printShape(mInputShapes[i]);
}

LOG(info) << "Output Nodes:";
for (size_t i = 0; i < mOutputNames.size(); i++) {
for (std::size_t i = 0; i < mOutputNames.size(); i++) {
LOG(info) << "\t" << mOutputNames[i] << " : " << printShape(mOutputShapes[i]);
}

Expand All @@ -121,7 +121,7 @@
LOG(info) << "--- Model initialized! ---";
}

void OnnxModel::setActiveThreads(int threads)
void OnnxModel::setActiveThreads(const int threads)
{
activeThreads = threads;
if (!checkHyperloop(false)) {
Expand Down
16 changes: 8 additions & 8 deletions Tools/ML/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OnnxModel
~OnnxModel() = default;

// Inferencing
void initModel(std::string, bool = false, int = 0, uint64_t = 0, uint64_t = 0);
void initModel(const std::string&, const bool = false, const int = 0, const uint64_t = 0, const uint64_t = 0);

// template methods -- best to define them in header
template <typename T>
Expand All @@ -57,7 +57,7 @@ class OnnxModel
// assert(input[0].GetTensorTypeAndShapeInfo().GetShape() == getNumInputNodes()); --> Fails build in debug mode, TODO: assertion should be checked somehow

try {
Ort::RunOptions runOptions;
const 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(); });
Expand Down Expand Up @@ -87,7 +87,7 @@ class OnnxModel
template <typename T>
T* evalModel(std::vector<T>& input)
{
int64_t size = input.size();
const int64_t size = input.size();
assert(size % mInputShapes[0][1] == 0);
std::vector<int64_t> inputShape{size / mInputShapes[0][1], mInputShapes[0][1]};
std::vector<Ort::Value> inputTensors;
Expand All @@ -106,16 +106,16 @@ class OnnxModel

Ort::MemoryInfo memInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);

for (size_t iinput = 0; iinput < input.size(); iinput++) {
for (std::size_t iinput = 0; iinput < input.size(); iinput++) {
[[maybe_unused]] int totalSize = 1;
int64_t size = input[iinput].size();
for (size_t idim = 1; idim < mInputShapes[iinput].size(); idim++) {
for (std::size_t idim = 1; idim < mInputShapes[iinput].size(); idim++) {
totalSize *= mInputShapes[iinput][idim];
}
assert(size % totalSize == 0);

std::vector<int64_t> inputShape{static_cast<int64_t>(size / totalSize)};
for (size_t idim = 1; idim < mInputShapes[iinput].size(); idim++) {
for (std::size_t idim = 1; idim < mInputShapes[iinput].size(); idim++) {
inputShape.push_back(mInputShapes[iinput][idim]);
}

Expand All @@ -142,7 +142,7 @@ class OnnxModel
int getNumOutputNodes() const { return mOutputShapes[0][1]; }
uint64_t getValidityFrom() const { return validFrom; }
uint64_t getValidityUntil() const { return validUntil; }
void setActiveThreads(int);
void setActiveThreads(const int);

private:
// Environment variables for the ONNX runtime
Expand All @@ -164,7 +164,7 @@ class OnnxModel

// Internal function for printing the shape of tensors
std::string printShape(const std::vector<int64_t>&);
bool checkHyperloop(bool = true);
bool checkHyperloop(const bool = true);
};

} // namespace ml
Expand Down
Loading