13namespace Experimental {
17 return static_cast<std::underlying_type_t<Options>
>(
opA) |
static_cast<std::underlying_type_t<Options>
>(
opB);
20 return opA |
static_cast<std::underlying_type_t<Options>
>(
opB);
59 return f->second.shape;
63 return f2->second.shape();
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");
79 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the shape is requested is not found");
84 return f->second.shape;
87 return f->second.shape;
97 return f->second.type;
101 return f2->second.type();
105 return f3->second.type;
109 return f4->second.type;
113 return f5->second.type;
119 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the type is requested is not found");
135 throw std::runtime_error(
"TMVA-SOFIE: input tensor with name " +
input_name +
" already exists \n");
145 throw std::runtime_error(
"TMVA-SOFIE: input tensor with name " +
input_name +
" already exists \n");
157 auto libs =
op->GetStdLibs();
172 throw std::runtime_error(
"TMVA-SOFIE: initialized tensor with name " +
tensor_name +
" already exists \n");
182 throw std::runtime_error(
"TMVA-SOFIE: initialized tensor with name " +
tensor_name +
" already exists \n");
196 return itr->second.IsConstantTensor();
224 throw std::runtime_error(
"TMVA-SOFIE: intermediate tensor with name " +
tensor_name +
" already exists \n");
233 throw std::runtime_error(
"TMVA-SOFIE: intermediate tensor with name " +
tensor_name +
" already exists \n");
238 for (
auto &
d : shape) {
243 if (
d.dim !=
size_t(-1)) {
268 throw std::runtime_error(
"TMVA-SOFIE: tensor " +
tensor_name +
" not found when trying to update it");
277 throw std::runtime_error(
"TMVA-SOFIE: tensor " +
tensor_name +
" not found when trying to get its data");
279 return f->second.sharedptr();
286 throw std::runtime_error(
"TMVA-SOFIE: initialized tensor " +
tensor_name +
" not found when trying to get its info");
288 t->second.SetNotWritable();
306 std::cout <<
"Model is already initialized - skip initialization " << std::endl;
317 if (verbose) std::cout <<
"looking at the tensor " <<
input.first << std::endl;
320 for (
auto &
d :
input.second.shape) {
322 std::string
pname =
d.param;
328 std::cout <<
"Tensor: " <<
input.first <<
" - fix parametric shape " <<
itr->first <<
" to " <<
itr->second << std::endl;
338 if (!shape.empty()) {
350 for (
auto &
d :
input.second.shape) {
380 std::cout <<
"Initializing operator " << i <<
" " <<
typeid(
r).
name() << std::endl;
382 op->Initialize(*
this);
392 graph->fParentGraph =
this;
393 graph->fIsSubGraph =
true;
402 for (
auto &
e : graph->fNeededBlasRoutines)
405 for (
auto e : graph->fNeededStdLib)
410 graph->fInputTensorNames.push_back(
name);
422 std::stringstream
strs;
428 const T *
data = t.second.data<T>();
443 strs <<
"std::vector<" <<
type <<
"> fTensor_" << t.first <<
" = ";
449 strs <<
"const " <<
type <<
" * tensor_" + t.first +
" = fTensor_" + t.first +
".data();\n";
457 fGC +=
"// initialized tensors\n";
470 fGC +=
"std::vector<float> fTensor_" + i.first +
" = std::vector<float>(" + std::to_string(
length) +
");\n";
471 fGC +=
"float * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
479 fGC +=
"\n//--- declare and allocate the intermediate tensors\n";
483 fGC +=
"std::vector<float> fTensor_" + i.first +
" = std::vector<float>(" + std::to_string(
length) +
");\n";
484 fGC +=
"float * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
487 fGC +=
"std::vector<double> fTensor_" + i.first +
" = std::vector<double>(" + std::to_string(
length) +
");\n";
488 fGC +=
"double * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
491 fGC +=
"std::vector<int64_t> fTensor_" + i.first +
" = std::vector<int64_t>(" + std::to_string(
length) +
");\n";
492 fGC +=
"int64_t * tensor_" + i.first +
" = fTensor_" + i.first +
".data();\n";
495 fGC +=
"std::vector<bool> fTensor_" + i.first +
" = std::vector<bool>(" + std::to_string(
length) +
");\n";
502 fGC +=
"//--- declare the dynamic tensors\n";
505 fGC +=
"std::vector<float> fTensor_" + i.first +
";\n";
506 fGC +=
"float * tensor_" + i.first +
" = nullptr;\n";
508 fGC +=
"std::vector<double> fTensor_" + i.first +
";\n";
509 fGC +=
"double * tensor_" + i.first +
" = nullptr;\n";
511 fGC +=
"std::vector<int64_t> fTensor_" + i.first +
";\n";
512 fGC +=
"int64_t * tensor_" + i.first +
" = nullptr;\n";
525 fGC +=
"\n//---- operator declarations \n";
531 fGC +=
"//---- allocate the intermediate dynamic tensors\n";
532 std::stringstream out;
535 out <<
SP <<
"if (" <<
length <<
" > 0) {\n";
536 out <<
SP <<
SP <<
"fTensor_" << i.first <<
".resize(" <<
length <<
");\n";
537 out <<
SP <<
SP <<
"tensor_" << i.first <<
" = fTensor_" << i.first <<
".data();\n";
553 for (
auto &
d : shape) {
554 std::string
pName =
d.param;
558 rGC +=
d.param +
",";
566 throw std::runtime_error(
"TMVA-SOFIE: input tensor " +
name +
567 " is of a data type which is not yet supported.");
581 std::cout <<
"Generating main inference code for " <<
fName << std::endl;
586 throw std::runtime_error(
"TMVA-SOFIE: output size=0 are not supported");
592 if (outputSize == 1) {
596 for (
size_t i = 1; i < outputSize; i++) {
598 throw std::runtime_error(
"TMVA-SOFIE: different output tensor types are not supported");
609 for (
size_t id = 0;
id <
fOperators.size();
id++) {
610 if (
fVerbose) std::cout <<
"Generating code for operator .... " <<
id << std::endl;
614 if (outputSize == 1) {
631 fGC +=
SP +
"return ret;\n";
636 for (
size_t i = 0; i < outputSize; i++) {
651 if (i < outputSize - 1)
658 fGC +=
SP +
"return ret;\n";
669 fGC +=
"struct Session {\n";
671 fGC +=
"struct Session_" +
fName +
" {\n";
684 fGC +=
"Session_" + graph->fName +
" fSession_" + graph->fName +
";\n";
694 for (
size_t id = 0;
id <
fOperators.size();
id++) {
695 std::string
opName = std::to_string(
id);
701 std::string fileName =
fName;
708 fGC +=
sessionName +
"(std::string filename =\"" + fileName +
"\"";
719 fGC +=
" size_t " +
p.first +
" = " +
p.second;
725 fGC +=
"\n//--- reading weights from file\n";
735 for (
size_t id = 0;
id <
fOperators.size();
id++) {
746 fGC +=
"}; // end of Session\n";
750void RModel::Generate(std::underlying_type_t<Options> options,
int batchSize,
long pos,
bool verbose)
770 throw std::runtime_error(
771 "TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class");
774 if (
static_cast<std::underlying_type_t<Options>
>(
Options::kGNN) & options)
791 std::cout <<
"generate session code for subgraph " << graph->fName << std::endl;
792 graph->GenerateSessionCode();
797 std::cout <<
"generate Main session code - model " <<
fName << std::endl;
803 fGC += (
"} //TMVA_SOFIE_" +
fName +
"\n");
813 fGC +=
" std::ifstream f;\n";
814 fGC +=
" f.open(filename);\n";
815 fGC +=
" if (!f.is_open()) {\n";
816 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open file \" + filename + \" for input weights\");\n";
820 fGC +=
" f.seekg(" + std::to_string(pos) +
");\n";
823 fGC +=
" std::string tensor_name;\n";
824 fGC +=
" size_t length;\n";
829 if (!i.second.IsWeightTensor())
continue;
835 fGC +=
" f >> tensor_name >> length;\n";
837 fGC +=
" std::string err_msg = \"TMVA-SOFIE failed to read the correct tensor name; expected name is " +
839 fGC +=
" throw std::runtime_error(err_msg);\n";
842 fGC +=
" std::string err_msg = \"TMVA-SOFIE failed to read the correct tensor size; expected size is " +
843 slength +
" , read \" + std::to_string(length) ;\n";
844 fGC +=
" throw std::runtime_error(err_msg);\n";
846 fGC +=
" for (size_t i = 0; i < length; ++i)\n";
848 fGC +=
" if (f.fail()) {\n";
849 fGC +=
" throw std::runtime_error(\"TMVA-SOFIE failed to read the values for tensor " +
tensor_name +
"\");\n";
855 fGC +=
" f.close();\n";
861 fGC +=
" std::unique_ptr<TFile> rootFile(TFile::Open(filename.c_str(), \"READ\"));\n";
862 fGC +=
" if (!rootFile->IsOpen()) {\n";
863 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open ROOT file for input weights\");\n";
867 fGC +=
" if (!rootFile->GetKey(\"" +
dirName +
"\")) {\n";
868 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open ROOT directory for input weights\");\n";
873 if (!i.second.IsWeightTensor())
continue;
877 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<float>*>(rootFile->Get(\"";
880 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<double>*>(rootFile->Get(\"";
883 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<int64_t>*>(rootFile->Get(\"";
886 std::runtime_error(
"tmva-sofie tensor " +
tensor_name +
" with type " +
ConvertTypeToString(i.second.type()) +
" cannot be read from a ROOT file");
917 throw std::runtime_error(
"SOFIE-GNN yet not supports writing to a ROOT file.");
930 if (!item.second.IsWeightTensor())
continue;
931 std::string
tensorName =
"tensor_" + item.first;
935 const float*
data = item.second.data<
float>();
940 const double*
data = item.second.data<
double>();
945 const int64_t*
data = item.second.data<int64_t>();
951 " cannot be written to a ROOT file");
969 std::runtime_error(
"tmva-sofie failed to open file " +
filename +
" for tensor weight data");
972 if (!i.second.IsWeightTensor()) {
979 const float *
data = i.second.data<
float>();
980 for (
size_t idx = 0; idx <
length; idx++) {
983 if (
value != 0. && std::abs(
value) < std::numeric_limits<float>::min() )
value = 0;
984 f << std::setprecision(std::numeric_limits<float>::max_digits10) <<
value;
985 f << ( (idx <
length-1) ?
" " :
"\n" );
992 std::runtime_error(
"tmva-sofie failed to write tensor data to file for " +
tensor_name);
1003 std::cout <<
"Model requires following inputs:\n";
1005 std::cout <<
"Parametrised Tensor name: " <<
inputInfo.first <<
"\t";
1007 std::cout <<
"shape: [";
1008 for (
size_t i = 0; i <
inputInfo.second.shape.size(); i++) {
1009 if (
inputInfo.second.shape[i].isParam) {
1010 std::cout <<
inputInfo.second.shape[i].param;
1012 std::cout <<
inputInfo.second.shape[i].dim ;
1014 if (i <
inputInfo.second.shape.size() - 1) std::cout <<
",";
1016 std::cout <<
"]" << std::endl;
1020 std::cout <<
"Fully Specified Tensor name: " <<
inputInfo.first <<
"\t";
1022 std::cout <<
"shape: [";
1023 for (
size_t i = 0; i <
inputInfo.second.shape.size(); i++) {
1025 if (i <
inputInfo.second.shape.size() - 1) std::cout <<
",";
1027 std::cout <<
"]" << std::endl;
1033 std::cout <<
"Model initialized the following tensors:\n";
1035 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
1037 std::cout <<
"shape: [";
1038 for (
size_t i = 0; i < it.second.shape().
size(); i++) {
1039 std::cout << it.second.shape()[i];
1040 if (i < it.second.shape().size() - 1) std::cout <<
",";
1043 if (it.second.IsConstantTensor()) std::cout <<
" (Constant)";
1044 else if (!it.second.IsWeightTensor()) std::cout <<
" (Not Writable)";
1045 std::cout << std::endl;
1051 std::cout <<
"Model specify the following intermediate tensors:\n";
1053 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
1055 std::cout <<
"shape: [";
1056 for (
size_t i = 0; i < it.second.shape.size(); i++) {
1057 std::cout << it.second.shape[i];
1058 if (i < it.second.shape.size() - 1) std::cout <<
",";
1060 std::cout <<
"]" << std::endl;
1066 std::cout <<
"Model specify the following dynamic tensors:\n";
1068 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
1070 std::cout <<
"shape: [";
1071 for (
size_t i = 0; i < it.second.shape.size(); i++) {
1072 std::cout << it.second.shape[i].GetVal();
1073 if (i < it.second.shape.size() - 1) std::cout <<
",";
1075 std::cout <<
"]" << std::endl;
1081 std::cout <<
"Model specify the following output tensors:\n";
1083 std::cout <<
"Tensor name: \"" << it <<
"\"\t";
1095 std::cout <<
"Tensor " <<
name <<
" not found in model's initialized tensor list" << std::endl;
1099 std::cout <<
"Tensor name: " << it->first <<
"\t";
1102 std::cout <<
"shape: [";
1103 for (
size_t i = 0; i < it->second.shape().
size(); i++) {
1104 std::cout << it->second.shape()[i];
1105 length *= it->second.shape()[i];
1106 if (
i < it->second.shape().size() - 1) std::cout <<
",";
1108 std::cout <<
"]" << std::endl;
1115 std::cout <<
"data: [" << std::endl;
1118 for (
int i =0; i <
n_print; i++) {
1120 if (i <
n_print - 1) std::cout <<
" ,";
1123 if (
ellipsis) std::cout <<
", ...";
1124 std::cout <<
"]" << std::endl;
1135 size_t pos =
filename.find(
".hxx");
1151 if (
R__b.IsReading()) {
1152 RModel::Class()->ReadBuffer(
R__b,
this);
1154 i->second.CastPersistentToShared();
1159 i->second.CastSharedToPersistent();
1161 RModel::Class()->WriteBuffer(
R__b,
this);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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 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 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 value
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
const_iterator begin() const
const_iterator end() const
Buffer base class used for serializing objects.
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()
std::string GenerateInferSignature(bool isdecl=true)
void GenerateOperatorDeclarations()
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 AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
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 GenerateSessionCode()
bool IsDimInputTensor(const std::string &name) const
void GenerateDynamicTensorInfo()
bool IsInitializedTensor(const std::string &name) const
void PrintInitializedTensors()
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
RModel()=default
Default constructor.
void HeadInitializedTensors(std::string name, int n_print=50)
bool IsConstantTensor(const std::string &name) const
void Initialize(int batchSize=-1, bool verbose=false)
const std::vector< size_t > & GetTensorShape(std::string name)
long WriteInitializedTensorsToFile(std::string filename="")
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, long pos=0, bool verbose=false)
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
void ReadInitializedTensorsFromFile(long)
void InitializeSubGraph(std::shared_ptr< RModel > graph)
std::unordered_map< std::string, std::string > fShapeParams
void SetNotWritableInitializedTensor(const std::string &tensor_name)
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)
std::vector< std::shared_ptr< RModel > > fSubGraphs
! sub-graph models (transient)
bool IsReadyInputTensor(const std::string &name) const
void UpdateOutputTensorList(std::vector< std::string > curr_output_tensor, std::vector< std::string > modify_output_tensor)
void PrintRequiredInputTensors()
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 ConvertValuesToString(size_t n, const T *data)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string GenerateConstantTensorCode(const std::pair< std::string, InitializedTensor > &t)
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::string ConvertValToString(T value)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations