Skip to content

Commit

Permalink
[tmva][sofie-gnn] Fix on loading required packages (sonnet, graph_net…
Browse files Browse the repository at this point in the history
…s) and restricting numpy version

avoid trying to load sonnet and graph_nets if not installed

Co-Authored-By: moneta <lorenzo.moneta@cern.ch>

[tmva][sofie-gnn] numpy version for sofie-gnn test should be restricted within <=1.19 or >=1.24

Because of the changed behavior of np.bool and similar aliases for builtin
data types, we need to restrict the numpy version to the stated range for sonnet.

For more information, refer here:
numpy/numpy#14882
numpy/numpy#22607

fix: definition of OutputGenerated in RModel_Base

[tmva][sofie-gnn] Suppress warnings for cases other than .dat file in method WriteInitializedTensorsToFile in RModel
  • Loading branch information
sanjibansg committed Sep 1, 2023
1 parent 071b0d8 commit 28760dc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 67 deletions.
6 changes: 5 additions & 1 deletion bindings/pyroot/pythonizations/test/CMakeLists.txt
Expand Up @@ -128,7 +128,11 @@ endif()
# SOFIE-GNN pythonizations
if (dataframe AND tmva)
if(NOT MSVC OR CMAKE_SIZEOF_VOID_P EQUAL 4 OR win_broken_tests AND PYTHON_VERSION_MAJOR_Development_Main EQUAL 3 )
ROOT_ADD_PYUNITTEST(pyroot_pyz_sofie_gnn sofie_gnn.py PYTHON_DEPS numpy sonnet graph_nets)
find_python_module(sonnet QUIET)
find_python_module(graph_nets QUIET)
if (PY_SONNET_FOUND AND PY_GRAPH_NETS_FOUND)
ROOT_ADD_PYUNITTEST(pyroot_pyz_sofie_gnn sofie_gnn.py PYTHON_DEPS numpy sonnet graph_nets)
endif()
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions bindings/pyroot/pythonizations/test/sofie_gnn.py
Expand Up @@ -4,8 +4,8 @@
import numpy as np
from numpy.testing import assert_almost_equal

if np.__version__ > "1.19":
raise RuntimeError(f"This test requires NumPy version 1.19 or lower")
if np.__version__ >= "1.20" and np.__version__ < "1.24":
raise RuntimeError(f"This test requires NumPy version <=1.19 or >=1.24")

import graph_nets as gn
from graph_nets import utils_tf
Expand Down
2 changes: 1 addition & 1 deletion tmva/sofie/inc/TMVA/RModel_Base.hxx
Expand Up @@ -84,7 +84,7 @@ public:
std::string ReturnGenerated() {
return fGC;
}
void OutputGenerated(std::string filename = "");
void OutputGenerated(std::string filename = "", bool append = false);
void SetFilename(std::string filename) {
fName = filename;
}
Expand Down
126 changes: 63 additions & 63 deletions tmva/sofie/src/RModel.cxx
Expand Up @@ -536,70 +536,68 @@ void RModel::ReadInitializedTensorsFromFile(long pos) {
}

long RModel::WriteInitializedTensorsToFile(std::string filename) {
// Determine the file extension based on the weight file type
std::string fileExtension;
switch (fWeightFile) {
case WeightFileType::None:
fileExtension = ".dat";
break;
case WeightFileType::RootBinary:
fileExtension = ".root";
break;
case WeightFileType::Text:
fileExtension = ".dat";
break;
}

// If filename is empty, use the model name as the base filename
if (filename.empty()) {
filename = fFileName + fileExtension;
}

// Write the initialized tensors to the file
if (fWeightFile == WeightFileType::RootBinary) {
// Determine the file extension based on the weight file type
std::string fileExtension;
switch (fWeightFile) {
case WeightFileType::None:
fileExtension = ".dat";
break;
case WeightFileType::RootBinary:
fileExtension = ".root";
break;
case WeightFileType::Text:
fileExtension = ".dat";
break;
}

// If filename is empty, use the model name as the base filename
if (filename.empty()) {
filename = fFileName + fileExtension;
}

// Write the initialized tensors to the file
if (fWeightFile == WeightFileType::RootBinary) {
if(fIsGNNComponent || fIsGNN) {
throw std::runtime_error("SOFIE-GNN yet not supports writing to a ROOT file.")
}
std::unique_ptr<TFile> outputFile(TFile::Open(filename.c_str(), "UPDATE"));

std::string dirName = fName + "_weights";
// check if directory exists, in case delete to replace with new one
if (outputFile->GetKey(dirName.c_str()))
outputFile->rmdir(dirName.c_str());

auto outputDir = outputFile->mkdir(dirName.c_str());

for (const auto& item : fInitializedTensors) {
std::string tensorName = "tensor_" + item.first;
size_t length = 1;
length = ConvertShapeToLength(item.second.fShape);
if(item.second.fType == ETensorType::FLOAT){
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const float* data = (std::static_pointer_cast<float>(item.second.fData)).get();
std::vector<float> tensorDataVector(data , data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<float>", tensorName.c_str());
}
else if(item.second.fType == ETensorType::DOUBLE){
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const double* data = (std::static_pointer_cast<double>(item.second.fData)).get();
std::vector<double> tensorDataVector(data , data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<double>", tensorName.c_str());
}
else if(item.second.fType == ETensorType::INT64) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const int64_t* data = (std::static_pointer_cast<int64_t>(item.second.fData)).get();
std::vector<int64_t> tensorDataVector(data , data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<int64_t>", tensorName.c_str());
}
}
outputFile->Write(filename.c_str());

// this needs to be changed, similar to the text file
return 0;
}
throw std::runtime_error("SOFIE-GNN yet not supports writing to a ROOT file.");
}
std::unique_ptr<TFile> outputFile(TFile::Open(filename.c_str(), "UPDATE"));

std::string dirName = fName + "_weights";
// check if directory exists, in case delete to replace with new one
if (outputFile->GetKey(dirName.c_str()))
outputFile->rmdir(dirName.c_str());

auto outputDir = outputFile->mkdir(dirName.c_str());

for (const auto& item : fInitializedTensors) {
std::string tensorName = "tensor_" + item.first;
size_t length = 1;
length = ConvertShapeToLength(item.second.fShape);
if(item.second.fType == ETensorType::FLOAT) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const float* data = (std::static_pointer_cast<float>(item.second.fData)).get();
std::vector<float> tensorDataVector(data, data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<float>", tensorName.c_str());
}
else if(item.second.fType == ETensorType::DOUBLE) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const double* data = (std::static_pointer_cast<double>(item.second.fData)).get();
std::vector<double> tensorDataVector(data, data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<double>", tensorName.c_str());
}
else if(item.second.fType == ETensorType::INT64) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const int64_t* data = (std::static_pointer_cast<int64_t>(item.second.fData)).get();
std::vector<int64_t> tensorDataVector(data, data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<int64_t>", tensorName.c_str());
}
}
outputFile->Write(filename.c_str());

// Write the initialized tensors to a text file
if (fWeightFile == WeightFileType::Text) {
// this needs to be changed, similar to the text file
return -1;

} else if (fWeightFile == WeightFileType::Text) {
std::ofstream f;
if(fIsGNNComponent) {
// appending all GNN components into the same file
Expand Down Expand Up @@ -629,6 +627,8 @@ long RModel::WriteInitializedTensorsToFile(std::string filename) {
long curr_pos = f.tellp();
f.close();
return curr_pos;
} else {
return -1;
}
}

Expand Down Expand Up @@ -731,7 +731,7 @@ void RModel::HeadInitializedTensors(std::string name, int n_print) {
}

void RModel::OutputGenerated(std::string filename, bool append) {

RModel_Base::OutputGenerated(filename, append);

// write weights in a text file
Expand Down

0 comments on commit 28760dc

Please sign in to comment.