Skip to content

Commit

Permalink
fix: numpy version for sofie-gnn test should be restricted within <=1…
Browse files Browse the repository at this point in the history
….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
  • Loading branch information
sanjibansg committed Aug 25, 2023
1 parent 1b8b84c commit 6c99ac3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
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" 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
Expand Down
118 changes: 59 additions & 59 deletions tmva/sofie/src/RModel.cxx
Expand Up @@ -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<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());
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;
}
}

// 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
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 6c99ac3

Please sign in to comment.