13namespace Experimental {
17 return static_cast<std::underlying_type_t<Options>
>(opA) |
static_cast<std::underlying_type_t<Options>
>(opB);
19std::underlying_type_t<Options>
operator|(std::underlying_type_t<Options> opA,
Options opB) {
20 return opA |
static_cast<std::underlying_type_t<Options>
>(opB);
59 return f->second.shape;
63 return f2->second.fShape;
67 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] is an input tensor with unspecified dimension parameter");
71 return f4->second.shape;
74 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] is a dynamic tensor. Use GetDynamicTensorShape instead of GetTensorShape");
76 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the shape is requested is not found");
81 return f->second.shape;
84 return f->second.shape;
94 return f->second.type;
98 return f2->second.fType;
102 return f3->second.type;
106 return f4->second.type;
110 return f5->second.type;
113 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the type is requested is not found");
128 throw std::runtime_error(
"TMVA-SOFIE: input tensor with name " + input_name +
" already exists \n");
138 throw std::runtime_error(
"TMVA-SOFIE: input tensor with name " + input_name +
" already exists \n");
150 auto libs = op->GetStdLibs();
151 for (
auto& stdlib : libs) {
154 if (order_execution >= 0) {
165 throw std::runtime_error(
"TMVA-SOFIE: initialized tensor with name " + tensor_name +
" already exists \n");
189 if (!int_shape.empty())
198 throw std::runtime_error(
"TMVA-SOFIE: intermediate tensor with name " + tensor_name +
" already exists \n");
207 throw std::runtime_error(
"TMVA-SOFIE: intermediate tensor with name " + tensor_name +
" already exists \n");
212 for (
auto &
d : shape) {
217 if (
d.dim !=
size_t(-1)) {
227 for(
auto& it : outputtensornames) {
233 for(
auto& it:curr_output_tensors) {
242 throw std::runtime_error(
"TMVA-SOFIE: tensor " + tensor_name +
" not found when trying to update it");
251 throw std::runtime_error(
"TMVA-SOFIE: tensor " + tensor_name +
" not found when trying to get its data");
253 return f->second.fData;
271 for (
auto &
d :
input.second.shape) {
272 if (
d.isParam && (
d.param ==
"bs" ||
d.param ==
"batch_size")) {
273 d =
Dim{
static_cast<size_t>(batchSize)};
278 if (!shape.empty()) {
287 for (
auto &
d :
input.second.shape) {
302 bool modelHasWeights =
false;
305 modelHasWeights =
true;
309 if (!modelHasWeights)
317 std::cout <<
"Initializing operator " << i <<
" " <<
typeid(
r).
name() << std::endl;
319 op->Initialize(*
this);
328 for (
auto & dim: i.second.fShape) {
332 fGC +=
"float tensor_" + i.first +
"[" + std::to_string(
length) +
"] = {";
333 std::shared_ptr<float>
data = std::static_pointer_cast<float>(i.second.fData);
334 std::stringstream floats;
335 for (
size_t idx = 0; idx <
length-1; idx++) {
336 floats << std::setprecision(std::numeric_limits<float>::max_digits10) <<
data.get()[idx] <<
", ";
338 floats << std::setprecision(std::numeric_limits<float>::max_digits10) <<
data.get()[
length-1];
343 fGC +=
"std::vector<float> fTensor_" + i.first +
" = std::vector<float>(" + std::to_string(
length) +
");\n";
344 fGC +=
"float * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
353 fGC +=
"\n//--- declare and allocate the intermediate tensors\n";
357 fGC +=
"std::vector<float> fTensor_" + i.first +
" = std::vector<float>(" + std::to_string(
length) +
");\n";
358 fGC +=
"float * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
361 fGC +=
"std::vector<double> fTensor_" + i.first +
" = std::vector<double>(" + std::to_string(
length) +
");\n";
362 fGC +=
"double * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
365 fGC +=
"std::vector<int64_t> fTensor_" + i.first +
" = std::vector<int64_t>(" + std::to_string(
length) +
");\n";
366 fGC +=
"int64_t * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
369 fGC +=
"std::vector<bool> fTensor_" + i.first +
" = std::vector<bool>(" + std::to_string(
length) +
");\n";
376 fGC +=
"//--- declare the dynamic tensors\n";
379 fGC +=
"std::vector<float> fTensor_" + i.first +
";\n";
380 fGC +=
"float * tensor_" + i.first +
" = nullptr;\n";
382 fGC +=
"std::vector<double> fTensor_" + i.first +
";\n";
383 fGC +=
"double * tensor_" + i.first +
" = nullptr;\n";
385 fGC +=
"std::vector<int64_t> fTensor_" + i.first +
";\n";
386 fGC +=
"int64_t * tensor_" + i.first +
" = nullptr;\n";
393 fGC +=
"//---- allocate the intermediate dynamic tensors\n";
394 std::stringstream out;
397 out <<
SP <<
"if (" <<
length <<
" > 0) {\n";
398 out <<
SP <<
SP <<
"fTensor_" << i.first <<
".resize(" <<
length <<
");\n";
399 out <<
SP <<
SP <<
"tensor_" << i.first <<
" = fTensor_" << i.first <<
".data();\n";
410 throw std::runtime_error(
"TMVA-SOFIE: output size=0 are not supported");
412 std::string outputType;
416 if (outputSize == 1) {
417 fGC +=
"std::vector<" + outputType +
"> ";
420 for (
size_t i = 1; i < outputSize; i++) {
422 throw std::runtime_error(
"TMVA-SOFIE: different output tensor types are not supported");
424 fGC +=
"std::vector<std::vector<" + outputType +
">> ";
429 std::unordered_map<std::string, int> inputParams;
435 for (
auto &
d : shape) {
436 std::string pName =
d.param;
438 if (
d.isParam && inputParams.count(pName) == 0) {
439 fGC +=
"size_t " +
d.param +
",";
440 inputParams[pName] = i_input;
446 fGC +=
"float* tensor_" +
name +
",";
450 fGC +=
"int32_t* tensor_" +
name +
",";
454 fGC +=
"int64_t* tensor_" +
name +
",";
458 fGC +=
"double* tensor_" +
name +
",";
462 fGC +=
"bool* tensor_" +
name +
",";
466 throw std::runtime_error(
"TMVA-SOFIE: input tensor " +
name +
467 " is of a data type which is not yet supported.");
476 for (
size_t id = 0;
id <
fOperators.size();
id++) {
480 if (outputSize == 1) {
485 fGC +=
SP +
"return fTensor_" + tensorName +
";\n";
491 fGC +=
SP +
"std::vector<bool> ret (fTensor_" + tensorName +
".begin(), fTensor_" + tensorName +
492 ".begin() + " + outputLength +
");\n";
494 fGC +=
SP +
"std::vector<" + outputType +
"> ret (tensor_" + tensorName +
", tensor_" + tensorName +
" + " +
495 outputLength +
");\n";
497 fGC +=
SP +
"return ret;\n";
501 fGC +=
SP +
"std::vector<std::vector<" + outputType +
">> ret({";
502 for (
size_t i = 0; i < outputSize; i++) {
504 if (!tensorName.empty()) {
506 fGC +=
"fTensor_" + tensorName;
510 fGC +=
"std::vector<bool>(fTensor_" + tensorName +
".begin(), fTensor_" + tensorName +
".begin() + " +
511 outputLength +
");\n";
513 fGC +=
"std::vector<" + outputType +
">(tensor_" + tensorName +
", tensor_" + tensorName +
" + " +
517 if (i < outputSize - 1)
524 fGC +=
SP +
"return ret;\n";
545 std::runtime_error(
"TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class");
548 if (
static_cast<std::underlying_type_t<Options>
>(
Options::kGNN) & options)
559 fGC +=
"struct Session {\n";
569 for (
size_t id = 0;
id <
fOperators.size();
id++) {
570 std::string opName = std::to_string(
id);
576 std::string fileName =
fName;
583 fGC +=
"Session(std::string filename =\"" + fileName +
"\"";
587 fGC +=
"Session(std::string = \"\"";
594 fGC +=
" size_t " +
p.first +
" = " +
p.second;
600 fGC +=
"\n//--- reading weights from file\n";
610 for (
size_t id = 0;
id <
fOperators.size() ;
id++) {
623 fGC += (
"} //TMVA_SOFIE_" +
fName +
"\n");
624 fGC +=
"\n#endif // " + hgname +
"\n";
633 fGC +=
" std::ifstream f;\n";
634 fGC +=
" f.open(filename);\n";
635 fGC +=
" if (!f.is_open()) {\n";
636 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open file for input weights\");\n";
640 fGC +=
" f.seekg(" + std::to_string(pos) +
");\n";
643 fGC +=
" std::string tensor_name;\n";
644 fGC +=
" size_t length;\n";
651 std::string tensor_name =
"tensor_" + i.first;
652 std::string slength = std::to_string(
length);
653 fGC +=
" f >> tensor_name >> length;\n";
654 fGC +=
" if (tensor_name != \"" + tensor_name +
"\" ) {\n";
655 fGC +=
" std::string err_msg = \"TMVA-SOFIE failed to read the correct tensor name; expected name is " +
656 tensor_name +
" , read \" + tensor_name;\n";
657 fGC +=
" throw std::runtime_error(err_msg);\n";
659 fGC +=
" if (length != " + slength +
") {\n";
660 fGC +=
" std::string err_msg = \"TMVA-SOFIE failed to read the correct tensor size; expected size is " +
661 slength +
" , read \" + std::to_string(length) ;\n";
662 fGC +=
" throw std::runtime_error(err_msg);\n";
664 fGC +=
" for (size_t i = 0; i < length; ++i)\n";
665 fGC +=
" f >> " + tensor_name +
"[i];\n";
668 fGC +=
" f.close();\n";
674 fGC +=
" std::unique_ptr<TFile> rootFile(TFile::Open(filename.c_str(), \"READ\"));\n";
675 fGC +=
" if (!rootFile->IsOpen()) {\n";
676 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open ROOT file for input weights\");\n";
679 std::string dirName =
fName +
"_weights";
680 fGC +=
" if (!rootFile->GetKey(\"" + dirName +
"\")) {\n";
681 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open ROOT directory for input weights\");\n";
686 std::string tensor_name =
"tensor_" + i.first;
688 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<float>*>(rootFile->Get(\"";
689 fGC += dirName +
"/" + tensor_name +
"\"));\n";
691 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<double>*>(rootFile->Get(\"";
692 fGC += dirName + +
"/" + tensor_name +
"\"));\n";
694 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<int64_t>*>(rootFile->Get(\"";
695 fGC += dirName +
"/" + tensor_name +
"\"));\n";
705 std::string fileExtension;
708 fileExtension =
".dat";
711 fileExtension =
".root";
714 fileExtension =
".dat";
726 throw std::runtime_error(
"SOFIE-GNN yet not supports writing to a ROOT file.");
730 std::string dirName =
fName +
"_weights";
732 if (outputFile->GetKey(dirName.c_str()))
733 outputFile->rmdir(dirName.c_str());
735 auto outputDir = outputFile->mkdir(dirName.c_str());
738 std::string tensorName =
"tensor_" + item.first;
742 const std::shared_ptr<void> ptr = item.second.fData;
743 const float*
data = (std::static_pointer_cast<float>(item.second.fData)).get();
745 outputDir->WriteObjectAny(&tensorDataVector,
"std::vector<float>", tensorName.c_str());
748 const std::shared_ptr<void> ptr = item.second.fData;
749 const double*
data = (std::static_pointer_cast<double>(item.second.fData)).get();
751 outputDir->WriteObjectAny(&tensorDataVector,
"std::vector<double>", tensorName.c_str());
754 const std::shared_ptr<void> ptr = item.second.fData;
755 const int64_t*
data = (std::static_pointer_cast<int64_t>(item.second.fData)).get();
757 outputDir->WriteObjectAny(&tensorDataVector,
"std::vector<int64_t>", tensorName.c_str());
760 outputFile->Write(
filename.c_str());
775 std::runtime_error(
"tmva-sofie failed to open file for tensor weight data");
779 for (
auto &dim : i.second.fShape) {
782 std::string tensor_name =
"tensor_" + i.first;
783 f << tensor_name <<
" " <<
length <<
"\n";
784 const float *
data = (std::static_pointer_cast<float>(i.second.fData)).get();
785 for (
size_t idx = 0; idx <
length - 1; idx++) {
786 f << std::setprecision(std::numeric_limits<float>::max_digits10) <<
data[idx] <<
" ";
788 f << std::setprecision(std::numeric_limits<float>::max_digits10) <<
data[
length - 1];
792 long curr_pos =
f.tellp();
801 std::cout <<
"Model requires following inputs:\n";
803 std::cout <<
"Parameterised Tensor name: " << inputInfo.first <<
"\t";
805 std::cout <<
"shape: [";
806 for (
size_t i = 0; i < inputInfo.second.shape.size(); i++) {
807 if (inputInfo.second.shape[i].isParam) {
808 std::cout << inputInfo.second.shape[i].param;
810 std::cout << inputInfo.second.shape[i].dim ;
812 if (i < inputInfo.second.shape.size() - 1) std::cout <<
",";
814 std::cout <<
"]" << std::endl;
818 std::cout <<
"Fully Specified Tensor name: " << inputInfo.first <<
"\t";
820 std::cout <<
"shape: [";
821 for (
size_t i = 0; i < inputInfo.second.shape.size(); i++) {
822 std::cout << inputInfo.second.shape[i];
823 if (i < inputInfo.second.shape.size() - 1) std::cout <<
",";
825 std::cout <<
"]" << std::endl;
831 std::cout <<
"Model initialized the following tensors:\n";
833 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
835 std::cout <<
"shape: [";
836 for (
size_t i = 0; i < it.second.fShape.size(); i++) {
837 std::cout << it.second.fShape[i];
838 if (i < it.second.fShape.size() - 1) std::cout <<
",";
840 std::cout <<
"]" << std::endl;
846 std::cout <<
"Model specify the following intermediate tensors:\n";
848 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
850 std::cout <<
"shape: [";
851 for (
size_t i = 0; i < it.second.shape.size(); i++) {
852 std::cout << it.second.shape[i];
853 if (i < it.second.shape.size() - 1) std::cout <<
",";
855 std::cout <<
"]" << std::endl;
861 std::cout <<
"Model specify the following dynamic tensors:\n";
863 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
865 std::cout <<
"shape: [";
866 for (
size_t i = 0; i < it.second.shape.size(); i++) {
867 std::cout << it.second.shape[i].GetVal();
868 if (i < it.second.shape.size() - 1) std::cout <<
",";
870 std::cout <<
"]" << std::endl;
876 std::cout <<
"Model specify the following output tensors:\n";
878 std::cout <<
"Tensor name: \"" << it <<
"\"\t";
890 std::cout <<
"Tensor " <<
name <<
" not found in model's initialized tensor list" << std::endl;
894 std::cout <<
"Tensor name: " << it->first <<
"\t";
897 std::cout <<
"shape: [";
898 for (
size_t i = 0; i < it->second.fShape.size(); i++) {
899 std::cout << it->second.fShape[i];
900 length *= it->second.fShape[i];
901 if (i < it->second.fShape.size() - 1) std::cout <<
",";
903 std::cout <<
"]" << std::endl;
904 bool ellipsis =
true;
910 std::cout <<
"data: [" << std::endl;
912 auto converted_data = std::static_pointer_cast<float>(it->second.fData).get();
913 for (
int i =0; i < n_print; i++) {
914 std::cout << converted_data[i];
915 if (i < n_print - 1) std::cout <<
" ,";
918 if (ellipsis) std::cout <<
", ...";
919 std::cout <<
"]" << std::endl;
949 i->second.CastPersistentToShared();
954 i->second.CastSharedToPersistent();
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Buffer base class used for serializing objects.
Int_t ReadBuffer(TBuffer &b, void *pointer, Int_t version, UInt_t start, UInt_t count)
Function called by the Streamer functions to deserialize information from buffer b into object at p.
Int_t WriteBuffer(TBuffer &b, void *pointer, const char *info="")
Function called by the Streamer functions to serialize object at p to buffer b.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
void GenerateHeaderInfo(std::string &hgname)
std::unordered_set< std::string > fNeededBlasRoutines
void OutputGenerated(std::string filename="", bool append=false)
std::unordered_set< std::string > fNeededStdLib
WeightFileType fWeightFile
void AddBlasRoutines(std::vector< std::string > routines)
void AddNeededStdLib(std::string libname)
const ETensorType & GetTensorType(std::string name)
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
bool IsDynamicTensor(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
void GenerateIntermediateTensorInfo()
std::vector< Dim > GetDynamicTensorShape(std::string name)
void PrintIntermediateTensors()
void PrintOutputTensors()
bool CheckIfTensorAlreadyExist(std::string tensor_name)
std::vector< std::unique_ptr< ROperator > > fOperators
void OutputGenerated(std::string filename="", bool append=false)
void AddInputTensorInfo(std::string input_name, ETensorType type, std::vector< Dim > shape)
std::unordered_map< std::string, TensorInfo > fIntermediateTensorInfos
void AddOutputTensorNameList(std::vector< std::string > output_tensor_names)
std::unordered_map< std::string, TensorInfo > fReadyInputTensorInfos
void AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
RModel & operator=(RModel &&other)
void AddInputTensorName(std::string name)
void PrintDynamicTensors()
std::vector< std::string > fOutputTensorNames
void GenerateDynamicTensorInfo()
bool IsInitializedTensor(const std::string &name) const
void PrintInitializedTensors()
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
void HeadInitializedTensors(std::string name, int n_print=50)
void Initialize(int batchSize=-1, bool verbose=false)
const std::vector< size_t > & GetTensorShape(std::string name)
bool IsInputTensor(const std::string &name) const
long WriteInitializedTensorsToFile(std::string filename="")
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, long pos=0)
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
void ReadInitializedTensorsFromFile(long)
std::unordered_map< std::string, std::string > fShapeParams
void GenerateInitializedTensorInfo()
std::vector< std::string > fInputTensorNames
std::unordered_map< std::string, InitializedTensor > fInitializedTensors
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
void PrintRequiredInputTensors()
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
std::string Clean_name(std::string input_tensor_name)
std::vector< Dim > ConvertShapeToDim(std::vector< size_t > shape)
Convert shape from integer format to dynamic one (based on Dim)
std::string ConvertDynamicShapeToLength(std::vector< Dim > shape)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
std::underlying_type_t< Options > operator|(Options opA, Options opB)
std::vector< size_t > ConvertShapeToInt(std::vector< Dim > shape)
Convert shape based on Dim to integer format.
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations