From 6c99ac31158bfa5661f86ba45387fc8422a59c41 Mon Sep 17 00:00:00 2001 From: Sanjiban Sengupta Date: Wed, 23 Aug 2023 23:29:56 +0530 Subject: [PATCH] fix: 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: https://github.com/numpy/numpy/pull/14882 https://github.com/numpy/numpy/pull/22607 --- .../pyroot/pythonizations/test/sofie_gnn.py | 4 +- tmva/sofie/src/RModel.cxx | 118 +++++++++--------- 2 files changed, 61 insertions(+), 61 deletions(-) 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