From 28760dc901751a4433f72084b707b663549540c9 Mon Sep 17 00:00:00 2001 From: Sanjiban Sengupta Date: Wed, 23 Aug 2023 23:20:09 +0530 Subject: [PATCH] [tmva][sofie-gnn] Fix on loading required packages (sonnet, graph_nets) and restricting numpy version avoid trying to load sonnet and graph_nets if not installed Co-Authored-By: moneta [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: https://github.com/numpy/numpy/pull/14882 https://github.com/numpy/numpy/pull/22607 fix: definition of OutputGenerated in RModel_Base [tmva][sofie-gnn] Suppress warnings for cases other than .dat file in method WriteInitializedTensorsToFile in RModel --- .../pyroot/pythonizations/test/CMakeLists.txt | 6 +- .../pyroot/pythonizations/test/sofie_gnn.py | 4 +- tmva/sofie/inc/TMVA/RModel_Base.hxx | 2 +- tmva/sofie/src/RModel.cxx | 126 +++++++++--------- 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/bindings/pyroot/pythonizations/test/CMakeLists.txt b/bindings/pyroot/pythonizations/test/CMakeLists.txt index b31e4f613c39e..9f72b3506f0e6 100644 --- a/bindings/pyroot/pythonizations/test/CMakeLists.txt +++ b/bindings/pyroot/pythonizations/test/CMakeLists.txt @@ -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() diff --git a/bindings/pyroot/pythonizations/test/sofie_gnn.py b/bindings/pyroot/pythonizations/test/sofie_gnn.py index aa861efa90d0a..a59766627e0e6 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" 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 diff --git a/tmva/sofie/inc/TMVA/RModel_Base.hxx b/tmva/sofie/inc/TMVA/RModel_Base.hxx index 724d00324dd4c..c4fd259ed601f 100644 --- a/tmva/sofie/inc/TMVA/RModel_Base.hxx +++ b/tmva/sofie/inc/TMVA/RModel_Base.hxx @@ -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; } diff --git a/tmva/sofie/src/RModel.cxx b/tmva/sofie/src/RModel.cxx index 20d42d873a683..a39256e40f845 100644 --- a/tmva/sofie/src/RModel.cxx +++ b/tmva/sofie/src/RModel.cxx @@ -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 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; - } + 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()); - // 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 @@ -629,6 +627,8 @@ long RModel::WriteInitializedTensorsToFile(std::string filename) { long curr_pos = f.tellp(); f.close(); return curr_pos; + } else { + return -1; } } @@ -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