diff --git a/bindings/pyroot/pythonizations/test/sofie_gnn.py b/bindings/pyroot/pythonizations/test/sofie_gnn.py index aa861efa90d0a..210f14378058e 100644 --- a/bindings/pyroot/pythonizations/test/sofie_gnn.py +++ b/bindings/pyroot/pythonizations/test/sofie_gnn.py @@ -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" or 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 diff --git a/tmva/sofie/src/RModel.cxx b/tmva/sofie/src/RModel.cxx index 20d42d873a683..d4e0b13a78b98 100644 --- a/tmva/sofie/src/RModel.cxx +++ b/tmva/sofie/src/RModel.cxx @@ -536,70 +536,70 @@ 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 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 ptr = item.second.fData; // shared_ptr instance - const float* data = (std::static_pointer_cast(item.second.fData)).get(); - std::vector tensorDataVector(data , data + length); - outputDir->WriteObjectAny(&tensorDataVector, "std::vector", tensorName.c_str()); - } - else if(item.second.fType == ETensorType::DOUBLE){ - const std::shared_ptr ptr = item.second.fData; // shared_ptr instance - const double* data = (std::static_pointer_cast(item.second.fData)).get(); - std::vector tensorDataVector(data , data + length); - outputDir->WriteObjectAny(&tensorDataVector, "std::vector", tensorName.c_str()); - } - else if(item.second.fType == ETensorType::INT64) { - const std::shared_ptr ptr = item.second.fData; // shared_ptr instance - const int64_t* data = (std::static_pointer_cast(item.second.fData)).get(); - std::vector tensorDataVector(data , data + length); - outputDir->WriteObjectAny(&tensorDataVector, "std::vector", tensorName.c_str()); - } - } - outputFile->Write(filename.c_str()); - + std::unique_ptr 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 ptr = item.second.fData; // shared_ptr instance + const float* data = (std::static_pointer_cast(item.second.fData)).get(); + std::vector tensorDataVector(data, data + length); + outputDir->WriteObjectAny(&tensorDataVector, "std::vector", tensorName.c_str()); + } + else if(item.second.fType == ETensorType::DOUBLE) { + const std::shared_ptr ptr = item.second.fData; // shared_ptr instance + const double* data = (std::static_pointer_cast(item.second.fData)).get(); + std::vector tensorDataVector(data, data + length); + outputDir->WriteObjectAny(&tensorDataVector, "std::vector", tensorName.c_str()); + } + else if(item.second.fType == ETensorType::INT64) { + const std::shared_ptr ptr = item.second.fData; // shared_ptr instance + const int64_t* data = (std::static_pointer_cast(item.second.fData)).get(); + std::vector tensorDataVector(data, data + length); + outputDir->WriteObjectAny(&tensorDataVector, "std::vector", tensorName.c_str()); + } + } + outputFile->Write(filename.c_str()); + // this needs to be changed, similar to the text file return 0; - } + } - // Write the initialized tensors to a text file - if (fWeightFile == WeightFileType::Text) { + // Write the initialized tensors to a text file + if (fWeightFile == WeightFileType::Text) { std::ofstream f; if(fIsGNNComponent) { // appending all GNN components into the same file @@ -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