8#ifdef SOFIE_SUPPORT_ROOT_BINARY
19const std::string
SP =
" ";
21void ReplaceAll(std::string &str,
const std::string &from,
const std::string &to)
24 while ((pos = str.find(from, pos)) != std::string::npos) {
25 str.replace(pos, from.length(), to);
30bool IsIdentifierChar(
char c)
32 return std::isalnum(
static_cast<unsigned char>(
c)) ||
c ==
'_';
38bool IsIdentifier(
const std::string &s)
40 if (s.empty() || std::isdigit(
static_cast<unsigned char>(s[0])))
43 if (!IsIdentifierChar(
c))
49std::string TensorMember(std::string
const &
name)
51 return "tensor_" +
name;
57 return static_cast<std::underlying_type_t<Options>
>(opA) |
static_cast<std::underlying_type_t<Options>
>(opB);
59std::underlying_type_t<Options>
operator|(std::underlying_type_t<Options> opA,
Options opB) {
60 return opA |
static_cast<std::underlying_type_t<Options>
>(opB);
67 return f->second.shape;
71 return f2->second.shape();
75 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] is an input tensor with unspecified dimension parameter");
79 return f4->second.shape;
85 if (f5->second.second)
86 return std::vector<size_t>{};
88 return std::vector<size_t>{f5->second.first.size()};
92 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] is a dynamic tensor. Use GetDynamicTensorShape instead of GetTensorShape");
97 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the shape is requested is not found");
102 return f->second.shape;
105 return f->second.shape;
113 return f->second.shape;
116 return f->second.shape;
120 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the shape is requested is not dynamic");
122 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the shape is requested is not found");
128 return f->second.type;
132 return f2->second.type();
136 return f3->second.type;
140 return f4->second.type;
144 return f5->second.type;
154 throw std::runtime_error(
"TMVA SOFIE tensor [" +
name +
"] for which the type is requested is not found, model name: " +
fName);
171 throw std::runtime_error(
"TMVA-SOFIE: input tensor with name " + input_name +
" already exists \n");
181 throw std::runtime_error(
"TMVA-SOFIE: input tensor with name " + input_name +
" already exists \n");
194 auto libs = op->GetStdLibs();
195 auto op_input_tensors = op->GetOpInputTensors();
196 for (
auto &stdlib : libs) {
199 if (order_execution >= 0) {
216 std::cout <<
"adding order execution for " << op_input_tensors[
index] <<
" order " << order_execution
226 throw std::runtime_error(
"TMVA-SOFIE: initialized tensor with name " + tensor_name +
" already exists \n");
236 throw std::runtime_error(
"TMVA-SOFIE: constant tensor with name " + tensor_name +
" already exists \n");
245 throw std::runtime_error(
"TMVA-SOFIE: shape tensor with name " + tensor_name +
" already exists \n");
247 fShapeTensors[tensor_name] = std::make_pair(shape_values, scalar);
255 throw std::runtime_error(
"TMVA-SOFIE: alias tensor with name " + tensor_name +
" already exists \n");
282 return itr->second.IsConstantTensor();
303 if (!int_shape.empty())
312 throw std::runtime_error(
"TMVA-SOFIE: intermediate tensor with name " + tensor_name +
" already exists \n");
321 throw std::runtime_error(
"TMVA-SOFIE: intermediate tensor with name " + tensor_name +
" already exists \n");
326 for (
auto &
d : shape) {
328 if (
d.dim !=
size_t(-1)) {
345 for(
auto& it : outputtensornames) {
351 for(
auto& it:curr_output_tensors) {
360 throw std::runtime_error(
"TMVA-SOFIE: tensor " + tensor_name +
" not found when trying to update it");
369 throw std::runtime_error(
"TMVA-SOFIE: tensor " + tensor_name +
" not found when trying to get its data");
371 return f->second.sharedptr();
378 throw std::runtime_error(
"TMVA-SOFIE: initialized tensor " + tensor_name +
" not found when trying to get its info");
380 t->second.SetNotWritable();
385 std::stringstream code;
388 std::cout <<
"Total chunks allocated\n";
390 std::cout <<
"..... chunk " << chunk->first <<
" size " << chunk->second.tensor_size <<
" " << chunk->second.tensor_name << std::endl;
394 auto declareIntermediateTensor = [
this, &code](std::string
const &
name,
size_t size,
size_t location) {
396 code <<
"\n // Allocating memory for intermediate tensor " <<
name <<
" with size " <<
size <<
" bytes";
398 << typeName <<
"* " << TensorMember(
name) <<
" = reinterpret_cast<" << typeName
399 <<
"*>(fIntermediateMemoryPool.data() + " << location <<
");\n";
402 if (
fVerbose) std::cout <<
"*** AllocateIntermediateMemory: Loop on op output tensors\n";
404 std::vector<TensorMemoryInfo> ordered_output_tensors;
406 for (
auto &it : op_output_tensors) {
407 auto name = std::string(it);
420 ordered_output_tensors.push_back(tmi);
422 std::sort(ordered_output_tensors.begin(), ordered_output_tensors.end(),
425 for (
auto &it : ordered_output_tensors) {
426 bool allocated =
false;
427 std::string
name = std::string{it.tensor_name};
428 size_t tensor_size = it.tensor_size;
430 std::cout <<
"output tensor " <<
name <<
" size " << tensor_size << std::endl;
435 if (
fVerbose) std::cout <<
".. available chunk " << chunk->first <<
" with size = " << chunk->second;
437 if (chunk->second >= tensor_size) {
441 auto new_chunk_location = chunk->first + chunk->second - tensor_size;
444 declareIntermediateTensor(
name, tensor_size, new_chunk_location);
445 chunk->second -= tensor_size;
449 if (
fVerbose) std::cout <<
" is re-used and split in a new of size " << new_chunk.tensor_size <<
" at " << new_chunk_location;
451 if (chunk->second == 0) {
452 if (
fVerbose) std::cout <<
" and deleted since size matches";
455 if (
fVerbose) std::cout << std::endl;
461 declareIntermediateTensor(
name, tensor_size, chunk->first);
464 if (
fVerbose) std::cout <<
" is extended with a bigger one of size " << tensor_size << std::endl;
468 if (
fVerbose) std::cout << std::endl;
479 declareIntermediateTensor(
name, tensor_size, chunk_idx);
481 if (
fVerbose) std::cout <<
"no chunk available - add in total stack a new chunk with size of tensor and idx : " << chunk_idx
489 if (
fVerbose) std::cout <<
"*** CheckAndFlushIntermediateMemory: Loop on input tensors for op " << op_idx <<
"\n";
491 if (
fVerbose) std::cout <<
"available chunks before freeing them : \n";
494 if (
fVerbose) std::cout <<
"-- free chunk " << chunk->first <<
" size = " << chunk->second << std::endl;
496 for (
auto &iv : op_input_tensors) {
498 if (
fVerbose) std::cout <<
".. input tensors : " << iv;
505 if (
fVerbose) std::cout <<
" flash condition is met - looping on chunks to find matching one \n";
508 if (
fVerbose) std::cout <<
"--- chunk " << chunk->first <<
" , " << chunk->second.tensor_name <<
" size " << chunk->second.tensor_size;
509 if (chunk->second.tensor_name == it) {
510 if (
fVerbose) std::cout <<
" -- Found chunk corresponding to input tensor: " << chunk->first;
516 : std::prev(first_greater);
521 last_smaller->first + last_smaller->second == chunk->first) {
523 last_smaller->second += chunk->second.tensor_size;
525 if (
fVerbose) std::cout <<
" is adjacent in memory with previous one - merge ";
527 last_smaller->first + last_smaller->second == first_greater->first) {
529 last_smaller->second += first_greater->second;
535 if (
fVerbose) std::cout <<
" merge also with following that is free ";
538 if (
fVerbose) std::cout << std::endl;
541 chunk->first + chunk->second.tensor_size == first_greater->first) {
543 if (
fVerbose) std::cout <<
" is adjacent in memory with following one - merge \n";
545 size_t new_size = chunk->second.tensor_size + first_greater->second;
546 size_t first_greater_idx = first_greater->first;
555 if (
fVerbose) std::cout <<
" insert in the available stack the chunk with size " << chunk->second.tensor_size << std::endl;
557 chunk->second.tensor_name =
"free";
562 if (
fVerbose) std::cout << std::endl;
568 std::map<std::string, size_t> inputParams;
570 inputParams[
"input_size"] = batchSize;
571 inputParams[
"batch_size"] = batchSize;
572 inputParams[
"bs"] = batchSize;
583 std::cout <<
"Model is already initialized - skip initialization " << std::endl;
594 for (
auto &
input : originalInputTensorInfos) {
595 if (verbose) std::cout <<
"looking at the tensor " <<
input.first << std::endl;
597 if (!inputParams.empty()) {
598 for (
auto &
d :
input.second.shape) {
600 std::string pname =
d.param;
601 if (pname ==
input.first +
"_size") pname =
"input_size";
602 auto itr = inputParams.find(pname);
603 if (itr != inputParams.end() ) {
604 d =
Dim{ itr->second };
606 std::cout <<
"Tensor: " <<
input.first <<
" - fix parametric shape " << itr->first <<
" to " << itr->second << std::endl;
616 if (!shape.empty()) {
628 for (
auto &
d :
input.second.shape) {
647 std::vector<size_t> temp_available_stack;
650 std::unordered_set<std::string> runtimeInitializedInputs;
651 for(
size_t op_idx = 0; op_idx <
fOperators.size(); ++op_idx){
654 std::cout <<
"Initializing operator " << i <<
" " <<
typeid(
r).
name() << std::endl;
657 for(
auto &it:
fOperators[op_idx]->GetOpOutputTensors()){
658 std::string
name = std::string{it};
668 if (!
fOperators[op_idx]->IsOutputConstant()) {
669 for (
auto &it :
fOperators[op_idx]->GetOpInputTensors()) {
670 std::string
name = std::string{it};
672 runtimeInitializedInputs.insert(
name);
685 if (it.second.IsNotWritable() && runtimeInitializedInputs.find(it.first) != runtimeInitializedInputs.end()) {
686 it.second.SetWritable();
688 std::cout <<
"Initialized tensor " << it.first <<
" is flagged as not writable but is used by non constant operators, set it as writable \n";
694 it.second.SetConstant();
700 bool modelHasWeights =
false;
702 if (it.second.IsWeightTensor()) {
703 modelHasWeights =
true;
707 if (!modelHasWeights)
729 graph->fParentGraph =
this;
730 graph->fIsSubGraph =
true;
738 std::vector<std::string> blasRoutines;
739 for (
auto &
e : graph->fNeededBlasRoutines)
740 blasRoutines.push_back(
e);
742 for (
auto e : graph->fNeededStdLib)
747 graph->fInputTensorNames.emplace_back(
name);
759 std::stringstream strs;
764 bool allocateOnStack = (
length > 100 || t.second.IsWeightTensor()) ? false :
true;
766 const T *
data = t.second.data<T>();
769 bool sameData =
false;
772 if (!allocateOnStack &&
length > 1) {
775 sameData = (
data[idx] ==
data[idx - 1]);
777 }
while (sameData && idx <
length);
779 if (allocateOnStack) {
781 strs <<
type <<
" * " << TensorMember(t.first) <<
" = fTensor_" + t.first +
";\n";
783 strs <<
"std::vector<" <<
type <<
"> fTensor_" << t.first <<
" = ";
789 strs <<
type <<
" * " << TensorMember(t.first) <<
" = fTensor_" + t.first +
".data();\n";
797 fGC +=
"// initialized (weights and constant) tensors\n";
801 if (i.second.IsNotWritable())
continue;
806 bool hasInfOrNaN =
false;
807 const float *
data = i.second.data<
float>();
808 for (
size_t idx = 0; idx <
length; idx++) {
809 if (std::is_floating_point<float>::value) {
810 if (std::isinf(
data[idx]) || std::isnan(
data[idx])) {
835 fGC +=
"std::vector<float> fTensor_" + i.first +
" = std::vector<float>(" + std::to_string(
length) +
");\n";
836 fGC +=
"float * " + TensorMember(i.first) +
" = fTensor_" + i.first +
".data();\n";
845 fGC +=
"\n//--- Allocating session memory pool to be used for allocating intermediate tensors\n";
850 const size_t memPoolSize = totalStack.rbegin()->first + totalStack.rbegin()->second.tensor_size;
851 fGC +=
"std::vector<char> fIntermediateMemoryPool = std::vector<char>(" + std::to_string(memPoolSize) +
");\n\n";
856 std::string tensor_declaration_block =
"";
860 tensor_declaration_block +=
"std::vector<std::uint8_t> fTensor_" + i.first +
" = std::vector<std::uint8_t>(" + std::to_string(
ConvertShapeToLength(i.second.shape)) +
");\n";
861 tensor_declaration_block +=
"std::uint8_t * " + TensorMember(i.first) +
" = fTensor_" + i.first +
".data();\n";
865 bool not_in_freq_map =
867 bool not_in_output_names =
870 if (((not_in_freq_map && not_in_output_names) || (!not_in_freq_map && !is_extended && not_in_output_names) ) && !is_alias) {
874 tensor_declaration_block +=
"std::vector<float> fTensor_" + i.first +
" = std::vector<float>(" + std::to_string(
length) +
");\n";
875 tensor_declaration_block +=
"float * " + TensorMember(i.first) +
" = fTensor_" + i.first +
".data();\n";
879 tensor_declaration_block +=
"std::vector<double> fTensor_" + i.first +
" = std::vector<double>(" + std::to_string(
length) +
");\n";
880 tensor_declaration_block +=
"double * " + TensorMember(i.first) +
" = fTensor_" + i.first +
".data();\n";
884 tensor_declaration_block +=
"std::vector<int64_t> fTensor_" + i.first +
" = std::vector<int64_t>(" + std::to_string(
length) +
");\n";
885 tensor_declaration_block +=
"int64_t * " + TensorMember(i.first) +
" = fTensor_" + i.first +
".data();\n";
890 tensor_declaration_block +=
ConvertTypeToString(i.second.type) +
" * " + TensorMember(i.first) +
" = nullptr;\n";
895 if (tensor_declaration_block.length()) {
896 fGC +=
"\n//--- declare and allocate the intermediate tensors\n" + tensor_declaration_block;
901 fGC +=
"//--- declare the dynamic tensors\n";
905 fGC +=
"//--- dynamic tensors pool\n";
906 fGC +=
"std::vector<char> fDynamicMemoryPool;\n";
914 strcode += op->GenerateDeclCode();
916 if (strcode.empty())
return;
917 fGC +=
"\n//---- operator declarations \n";
929 std::cout <<
"generating code for dynamic tensor management" << std::endl;
933 std::stringstream out;
934 out <<
"// dynamic tensor memory management\n";
935 out <<
SP <<
"std::vector<TMVA::Experimental::SOFIE::TensorLifeInfo> dynamicTensorInfos;\n";
940 std::vector<std::pair<std::string, ETensorType>> tensors;
944 for (
auto &it : op->GetOpOutputTensors()) {
946 auto op_ptr = op.get();
947 std::cout <<
"Looping on operator " << op_index <<
" " <<
typeid(*op_ptr).name() << std::endl;
950 std::string
name = std::string(it);
956 int begin = op_index;
961 end = it_lookup->second + 1;
966 std::cout <<
"op " << op_index <<
"tensor_" <<
name <<
" begin " << begin <<
" " <<
" end " << end << std::endl;
967 throw std::runtime_error(
"TMVA-SOFIE: RModel::GenerateDynamicTensorInfo: tensor_" +
name +
" has end before begin");
971 out <<
SP <<
"dynamicTensorInfos.push_back( {" << begin <<
", " << end <<
", " << type_size <<
"* (" << tensor_size <<
") });"
972 <<
" // tensor_" <<
name << std::endl;
978 out <<
"\n" <<
SP <<
"auto memory_result = OrganizeMemory(dynamicTensorInfos);\n\n";
979 out <<
"// allocating now the memory\n";
980 out <<
SP <<
"fDynamicMemoryPool = std::vector<char>(memory_result.total_bytes);\n";
981 out <<
SP <<
"int idx = 0;\n";
982 for (
auto & it : tensors) {
983 out <<
SP <<
"tensor_" << it.first <<
" = reinterpret_cast<" <<
ConvertTypeToString(it.second) <<
" *>(fDynamicMemoryPool.data() + memory_result.offsets[idx++]);\n";
986 bool missingTensor =
false;
990 if (std::find(tensors.begin(), tensors.end(), std::pair<std::string,ETensorType>{i.first, i.second.type}) == tensors.end()) {
991 std::cout <<
"Dynamic tensors " << i.first <<
" is not in list of operator input/output " << std::endl;
992 missingTensor =
true;
996 throw std::runtime_error(
"TMVA-SOFIE: RModel::GenerateDynamicTensorInfo - some tensors are not in input/output list");
1007 for (
auto &
d : shape) {
1008 if (
d.param == paramName)
1022 const std::string
target =
"tensor_";
1024 std::vector<std::string>
result;
1026 for (
size_t i = 0; i <
input.size();) {
1028 bool doCollect =
false;
1031 (i == 0 || !IsIdentifierChar(
input[i - 1]))) {
1035 std::size_t j = i +
target.size();
1038 while (j <
input.size() && IsIdentifierChar(
input[j]))
1041 std::string fullName =
input.substr(i, j - i);
1062 result.push_back(fullName);
1082 std::unordered_map<std::string, int> inputParams;
1088 for (
auto &
d : shape) {
1089 std::string pName =
d.param;
1091 if (
d.isParam && inputParams.count(pName) == 0) {
1092 if (isdecl) rGC +=
"size_t ";
1093 rGC +=
d.param +
",";
1094 inputParams[pName] = i_input;
1100 if (
type ==
"other")
1101 throw std::runtime_error(
"TMVA-SOFIE: input tensor " +
name +
1102 " is of a data type which is not yet supported.");
1103 rGC +=
type +
" const* ";
1105 rGC +=
"tensor_" +
name +
",";
1122std::string memberNameForDimShape(std::string
name)
1124 if (!
name.empty()) {
1125 name[0] = std::toupper(
static_cast<unsigned char>(
name[0]));
1138 bool sameOutputTypes =
true;
1139 std::string inferReturnType;
1142 if (outputSize == 1) {
1143 fGC +=
"std::vector<" + typeForOutput(eFirstOutputType) +
">";
1148 sameOutputTypes =
false;
1150 if (sameOutputTypes)
1151 fGC +=
"std::vector<std::vector<" + typeForOutput(eFirstOutputType) +
">>";
1153 inferReturnType =
"std::tuple<";
1154 for (
size_t i = 0; i < outputSize; i++) {
1156 if (i < outputSize - 1)
1157 inferReturnType +=
",";
1159 inferReturnType +=
">";
1160 fGC += inferReturnType;
1167 if (!doInferArgs.empty())
1180 bool hasRuntimeParam =
false;
1183 hasRuntimeParam =
true;
1185 n = hasRuntimeParam ? memberNameForDimShape(dimLen) : dimLen;
1187 std::string outputName =
"output_tensor_" +
name;
1189 doInferArgs +=
" " + outputName +
".data(),";
1193 fGC +=
SP +
"size_t " + dim.param +
" = 0;\n";
1194 doInferArgs +=
" " + dim.param +
",";
1199 if (!doInferArgs.empty())
1200 doInferArgs.back() =
' ';
1203 std::unordered_set<std::string> input_params_checked;
1204 std::string dynamic_parameters_check =
"";
1208 for (
auto &
d : shape) {
1209 std::string pName =
d.param;
1210 if (
d.isParam && input_params_checked.count(pName) == 0) {
1211 std::string memberName = memberNameForDimShape(
d.param);
1212 dynamic_parameters_check +=
d.param +
" > " + memberName +
" || ";
1213 input_params_checked.insert(pName);
1214 fGC +=
SP +
"if (" +
d.param +
" > " + memberName +
") {\n";
1215 fGC +=
SP +
SP +
"throw std::runtime_error(\"TMVA-SOFIE: dynamic input tensor shape parameter " +
1216 d.param +
" exceeds the initialized maximum allowed shape.\");\n";
1224 fGC +=
SP +
"doInfer(*this, " + doInferArgs +
");\n";
1226 fGC +=
SP +
"doInfer(" + doInferArgs +
");\n";
1233 std::string outputName =
"output_tensor_" +
name;
1235 fGC +=
SP + outputName +
".resize(" + tensor_size +
");\n";
1239 fGC +=
SP +
"return {";
1255 fGC +=
"struct " + sessionName +
";\n";
1260 if (!doInferSignature.empty())
1261 doInferSignature +=
", ";
1268 doInferSignature +=
" size_t &" + dim.param +
"_output,";
1272 doInferSignature.back() =
' ';
1275 doInferSignature = sessionName +
" const &session, " + doInferSignature;
1278 doInferSignature =
"inline void doInfer(" + doInferSignature +
")";
1282 fGC += doInferSignature +
";\n";
1287 fGC +=
"struct " + sessionName +
" {\n";
1295 std::string intermediate_memory_alloc_string =
"";
1296 intermediate_memory_alloc_string +=
"\n// --- Positioning intermediate tensor memory --";
1297 for (
size_t op_idx = 0; op_idx <
fOperators.size(); ++op_idx) {
1300 std::cout <<
"\n******************\n analyzing input/output operator " << op_idx <<
" "
1301 <<
typeid(*op).name() << std::endl;
1316 fGC += intermediate_memory_alloc_string;
1326 fGC +=
"\n// dynamic shape parameters\n";
1328 std::sort(dimShapeNames.begin(), dimShapeNames.end());
1329 for (
const auto &p : dimShapeNames) {
1330 fGC +=
"size_t " + memberNameForDimShape(p) +
";\n";
1337 fGC +=
"Session_" + graph->fName +
" fSession_" + graph->fName +
";\n";
1344 for (
size_t id = 0;
id <
fOperators.size();
id++) {
1345 std::string opName = std::to_string(
id);
1351 std::string fileName =
fName;
1356 fileName +=
".root";
1358 fGC += sessionName +
"(std::string filename =\"" + fileName +
"\"";
1362 fGC += sessionName +
"(std::string = \"\"";
1380 fGC +=
" " + memberNameForDimShape(p) +
" = " + p +
";\n";
1387 fGC +=
"\n//--- reading weights from file\n";
1397 for (
size_t id = 0;
id <
fOperators.size();
id++) {
1409 fGC +=
"}; // end of Session\n\n";
1414 fGC += doInferSignature +
" {\n";
1419 std::cout <<
"Generating main inference code for " <<
fName << std::endl;
1422 throw std::runtime_error(
"TMVA-SOFIE: output size=0 are not supported");
1424 std::string allOperatorCode;
1426 for (
size_t op_idx = 0; op_idx <
fOperators.size(); ++op_idx) {
1428 std::cout <<
"Generating code for operator .... " << op_idx << std::endl;
1429 std::string operatorCode =
fOperators[op_idx]->Generate(std::to_string(op_idx));
1430 allOperatorCode += operatorCode;
1435 ReplaceAll(allOperatorCode,
"this->",
"session.");
1440 for (
auto const&
name: tensorMemberNames) {
1441 fGC +=
" auto &" +
name +
" = session." +
name +
";\n";
1446 fGC += allOperatorCode;
1453 fGC +=
" " + dim.param +
"_output = " + dim.param +
";\n";
1457 std::string t =
"session.tensor_" +
name;
1459 fGC +=
" std::copy(" + t +
", " + t +
" + " + std::to_string(
length) +
", tensor_" +
name +
");\n";
1467void RModel::Generate(std::underlying_type_t<Options> options,
int batchSize,
long pos,
bool verbose)
1487 throw std::runtime_error(
1488 "TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class");
1491 if (
static_cast<std::underlying_type_t<Options>
>(
Options::kGNN) & options)
1503 std::cout <<
"Warning: Force having a Session since model has dynamic tensors " << std::endl;
1515 std::cout <<
"generate session code for subgraph " << graph->fName << std::endl;
1516 graph->GenerateSessionCode();
1521 std::cout <<
"generate Main session code - model " <<
fName << std::endl;
1527 fGC += (
"} //TMVA_SOFIE_" +
fName +
"\n");
1528 fGC +=
"\n#endif // " + hgname +
"\n";
1539 fGC +=
" std::ifstream f;\n";
1540 fGC +=
" f.open(filename);\n";
1541 fGC +=
" if (!f.is_open()) {\n";
1542 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open file \" + filename + \" for input weights\");\n";
1546 fGC +=
" f.seekg(" + std::to_string(pos) +
");\n";
1549 fGC +=
" using TMVA::Experimental::SOFIE::ReadTensorFromStream;\n";
1554 if (!i.second.IsWeightTensor())
continue;
1555 std::string tensor_name =
"tensor_" + i.first;
1558 fGC +=
" ReadTensorFromStream(f, " + tensor_name +
", \"" + tensor_name +
"\", " +
length +
");\n";
1560 throw std::runtime_error(
"tmva-sofie tensor " + tensor_name +
" with type " +
ConvertTypeToString(i.second.type()) +
" cannot be read from a file");
1563 fGC +=
" f.close();\n";
1568#ifdef SOFIE_SUPPORT_ROOT_BINARY
1570 fGC +=
" std::unique_ptr<TFile> rootFile(TFile::Open(filename.c_str(), \"READ\"));\n";
1571 fGC +=
" if (!rootFile->IsOpen()) {\n";
1572 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open ROOT file for input weights\");\n";
1575 std::string dirName =
fName +
"_weights";
1576 fGC +=
" if (!rootFile->GetKey(\"" + dirName +
"\")) {\n";
1577 fGC +=
" throw std::runtime_error(\"tmva-sofie failed to open ROOT directory for input weights\");\n";
1582 if (!i.second.IsWeightTensor())
continue;
1584 std::string tensor_name =
"tensor_" + i.first;
1586 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<float>*>(rootFile->Get(\"";
1587 fGC += dirName +
"/" + tensor_name +
"\"));\n";
1589 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<double>*>(rootFile->Get(\"";
1590 fGC += dirName + +
"/" + tensor_name +
"\"));\n";
1592 fGC +=
" fTensor_" + i.first +
" = *reinterpret_cast<std::vector<int64_t>*>(rootFile->Get(\"";
1593 fGC += dirName +
"/" + tensor_name +
"\"));\n";
1595 throw std::runtime_error(
"tmva-sofie tensor " + tensor_name +
" with type " +
ConvertTypeToString(i.second.type()) +
" cannot be read from a ROOT file");
1601 throw std::runtime_error(
"SOFIE was not built with ROOT file support.");
1608 std::string fileExtension;
1611 fileExtension =
".dat";
1614 fileExtension =
".root";
1617 fileExtension =
".dat";
1628#ifdef SOFIE_SUPPORT_ROOT_BINARY
1630 throw std::runtime_error(
"SOFIE-GNN yet not supports writing to a ROOT file.");
1634 std::string dirName =
fName +
"_weights";
1636 if (outputFile->GetKey(dirName.c_str()))
1637 outputFile->rmdir(dirName.c_str());
1639 auto outputDir = outputFile->mkdir(dirName.c_str());
1643 if (!item.second.IsWeightTensor())
continue;
1644 std::string tensorName =
"tensor_" + item.first;
1648 const float*
data = item.second.data<
float>();
1650 outputDir->WriteObjectAny(&tensorDataVector,
"std::vector<float>", tensorName.c_str());
1653 const double*
data = item.second.data<
double>();
1655 outputDir->WriteObjectAny(&tensorDataVector,
"std::vector<double>", tensorName.c_str());
1658 const int64_t*
data = item.second.data<int64_t>();
1660 outputDir->WriteObjectAny(&tensorDataVector,
"std::vector<int64_t>", tensorName.c_str());
1663 throw std::runtime_error(
"tmva-sofie tensor " + tensorName +
" with type " +
ConvertTypeToString(item.second.type()) +
1664 " cannot be written to a ROOT file");
1667 outputFile->Write(
filename.c_str());
1673 throw std::runtime_error(
"SOFIE was not built with ROOT file support.");
1685 std::runtime_error(
"tmva-sofie failed to open file " +
filename +
" for tensor weight data");
1688 if (!i.second.IsWeightTensor()) {
1692 std::string tensor_name =
"tensor_" + i.first;
1693 f << tensor_name <<
" " <<
length <<
"\n";
1695 const float *
data = i.second.data<
float>();
1696 for (
size_t idx = 0; idx <
length; idx++) {
1699 if (
value != 0. && std::abs(
value) < std::numeric_limits<float>::min() )
value = 0;
1701 if (std::isinf(
value))
1702 f << (
value > 0 ?
"inf" :
"-inf");
1703 else if (std::isnan(
value))
1706 f << std::setprecision(std::numeric_limits<float>::max_digits10) <<
value;
1707 f << ( (idx <
length-1) ?
" " :
"\n" );
1711 throw std::runtime_error(
"tmva-sofie tensor " + tensor_name +
" with type " +
ConvertTypeToString(i.second.type()) +
" cannot be written to a file");
1714 throw std::runtime_error(
"tmva-sofie failed to write tensor data to file for " + tensor_name);
1716 long curr_pos =
f.tellp();
1725 std::cout <<
"Summary of model " <<
GetName() << std::endl;
1726 for(
size_t op_idx = 0; op_idx <
fOperators.size(); ++op_idx){
1728 std::string raw_name =
typeid(
r).
name();
1730 std::string
name = raw_name.substr(raw_name.find(
"ROperator_")+10, raw_name.size());
1731 std::cout << op_idx <<
" " <<
name <<
" : ";
1732 for (
auto & t_in :
r.GetOpInputTensors()) std::cout << t_in <<
" ";
1733 std::cout <<
" ----> ";
1734 for (
auto & t_out :
r.GetOpOutputTensors()) std::cout << t_out <<
" ";
1735 std::cout << std::endl;
1743 fGC +=
"\n// Input tensor dimensions\n";
1744 fGC +=
"using TMVA::Experimental::SOFIE::SingleDim;\n";
1745 fGC +=
"using TMVA::Experimental::SOFIE::TensorDims;\n";
1746 fGC +=
"using TMVA::Experimental::SOFIE::makeDims;\n\n";
1747 bool hasDynamicInputTensors =
false;
1752 hasDynamicInputTensors =
true;
1755 fGC +=
"constexpr std::array<SingleDim, " + std::to_string(shape.size()) +
"> dim_" +
name +
"{";
1756 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1757 auto const &dim = shape[iDim];
1759 fGC +=
"SingleDim{\"" + dim.GetVal() +
"\"}";
1761 fGC +=
"SingleDim{" + dim.GetVal() +
"}";
1763 if (iDim != shape.size() - 1) {
1769 fGC +=
"\nconstexpr std::array<TensorDims, " + std::to_string(
fInputTensorNames.size()) +
"> inputTensorDims{\n";
1772 fGC +=
SP +
"makeDims(dim_" +
name +
")";
1782 "\nconstexpr bool hasDynamicInputTensors{" + std::string{hasDynamicInputTensors ?
"true" :
"false"} +
"};\n\n";
1784 fGC +=
"\n// Output tensor dimensions\n";
1785 bool hasDynamicOutputTensors =
false;
1789 hasDynamicOutputTensors =
true;
1792 fGC +=
"constexpr std::array<SingleDim, " + std::to_string(shape.size()) +
"> dim_" +
name +
"{";
1793 for (std::size_t iDim = 0; iDim < shape.size(); ++iDim) {
1794 auto const &dim = shape[iDim];
1796 fGC +=
"SingleDim{\"" + dim.GetVal() +
"\"}";
1798 fGC +=
"SingleDim{" + dim.GetVal() +
"}";
1800 if (iDim != shape.size() - 1) {
1806 fGC +=
"\nconstexpr std::array<TensorDims, " + std::to_string(
fOutputTensorNames.size()) +
"> outputTensorDims{\n";
1809 fGC +=
SP +
"makeDims(dim_" +
name +
")";
1818 "\nconstexpr bool hasDynamicOutputTensors{" + std::string{hasDynamicOutputTensors ?
"true" :
"false"} +
"};\n\n";
1822 std::cout <<
"Model requires following inputs:\n";
1824 std::cout <<
"Parametrised Tensor name: " << inputInfo.first <<
"\t";
1826 std::cout <<
"shape: [";
1827 for (
size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1828 if (inputInfo.second.shape[i].isParam) {
1829 std::cout << inputInfo.second.shape[i].param;
1831 std::cout << inputInfo.second.shape[i].dim ;
1833 if (i < inputInfo.second.shape.size() - 1) std::cout <<
",";
1835 std::cout <<
"]" << std::endl;
1839 std::cout <<
"Fully Specified Tensor name: " << inputInfo.first <<
"\t";
1841 std::cout <<
"shape: [";
1842 for (
size_t i = 0; i < inputInfo.second.shape.size(); i++) {
1843 std::cout << inputInfo.second.shape[i];
1844 if (i < inputInfo.second.shape.size() - 1) std::cout <<
",";
1846 std::cout <<
"]" << std::endl;
1852 std::cout <<
"Model initialized the following tensors:\n";
1854 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
1856 std::cout <<
"shape: [";
1857 for (
size_t i = 0; i < it.second.shape().
size(); i++) {
1858 std::cout << it.second.shape()[i];
1859 if (i < it.second.shape().size() - 1) std::cout <<
",";
1862 if (it.second.IsConstantTensor()) std::cout <<
" (Constant)";
1863 if (it.second.IsNotWritable()) std::cout <<
" (Not Writable)";
1864 std::cout << std::endl;
1870 std::cout <<
"Model specify the following intermediate tensors:\n";
1872 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
1874 std::cout <<
"shape: [";
1875 for (
size_t i = 0; i < it.second.shape.size(); i++) {
1876 std::cout << it.second.shape[i];
1877 if (i < it.second.shape.size() - 1) std::cout <<
",";
1879 std::cout <<
"]" << std::endl;
1885 std::cout <<
"Model specify the following dynamic tensors:\n";
1887 std::cout <<
"Tensor name: \"" << it.first <<
"\"\t";
1889 std::cout <<
"shape: [";
1890 for (
size_t i = 0; i < it.second.shape.size(); i++) {
1891 std::cout << it.second.shape[i].GetVal();
1892 if (i < it.second.shape.size() - 1) std::cout <<
",";
1894 std::cout <<
"]" << std::endl;
1900 std::cout <<
"Model specify the following output tensors:\n";
1902 std::cout <<
"Tensor name: \"" << it <<
"\"\t";
1907 std::cout <<
"with shape not yet defined" << std::endl;
1916 std::cout <<
"Tensor " <<
name <<
" not found in model's initialized tensor list" << std::endl;
1920 std::cout <<
"Tensor name: " << it->first <<
"\t";
1923 std::cout <<
"shape: [";
1924 for (
size_t i = 0; i < it->second.shape().
size(); i++) {
1925 std::cout << it->second.shape()[i];
1926 length *= it->second.shape()[i];
1927 if (i < it->second.shape().size() - 1) std::cout <<
",";
1929 std::cout <<
"]" << std::endl;
1930 bool ellipsis =
true;
1936 std::cout <<
"data: [" << std::endl;
1938 auto converted_data = it->second.data<
float>();
1939 for (
int i =0; i < n_print; i++) {
1940 std::cout << converted_data[i];
1941 if (i < n_print - 1) std::cout <<
" ,";
1944 if (ellipsis) std::cout <<
", ...";
1945 std::cout <<
"]" << std::endl;
1956 size_t pos =
filename.find(
".hxx");
1971void RModel::Streamer(
TBuffer &R__b) {
1973 RModel::Class()->ReadBuffer(R__b,
this);
1975 i.second.CastPersistentToShared();
1980 i.second.CastSharedToPersistent();
1982 RModel::Class()->WriteBuffer(R__b,
this);
true
Register systematic variations for multiple existing columns using auto-generated tags.
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 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 target
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 result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
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
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)
void OutputGenerated(std::string filename="", bool append=false)
const std::string & GetName() const
WeightFileType fWeightFile
void AddBlasRoutines(std::vector< std::string > routines)
void AddNeededStdLib(std::string libname)
void AddShapeParam(const std::string &name, size_t def_value=0)
std::vector< size_t > GetTensorShape(const std::string &name) const
void PrintIntermediateTensors() const
std::vector< Dim > GetDimTensorShape(const std::string &name) const
std::unordered_map< std::string, DynamicTensorInfo > fDynamicTensorInfos
bool IsDynamicTensor(const std::string &name) const
void AddAliasTensor(const std::string &tensor_name, const std::string &orig_tensor_name)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
void GenerateIntermediateTensorInfo()
std::string GenerateInferSignature(bool isdecl=true)
std::string fExtraCodeForDimShapes
void GenerateOperatorDeclarations()
size_t fWeightsTensorSize
bool CheckIfTensorAlreadyExist(std::string tensor_name)
std::vector< std::unique_ptr< ROperator > > fOperators
void GenerateRequiredInputTensorInfo()
To emit the dimensions of the input tensors as a data member of a session, which is helpful when vali...
void OutputGenerated(std::string filename="", bool append=false)
std::unordered_map< std::string, std::string > fAliasTensors
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)
std::vector< std::string > fDimShapeNames
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
std::unordered_map< std::string_view, size_t > fIntermediateTensorFrequencyLookup
! lookup table for intermediate tensor frequency (transient)
void AddInputTensorName(std::string name)
std::vector< std::string > fOutputTensorNames
void PrintRequiredInputTensors() const
void GenerateSessionCode()
bool IsDimInputTensor(const std::string &name) const
void GenerateDynamicTensorInfo()
void PrintDynamicTensors() const
bool IsShapeTensor(const std::string &name) const
check if a tensor is a shape tensor
bool IsInitializedTensor(const std::string &name) const
bool IsAliasTensor(const std::string &name) const
check if a tensor is a alias tensor
size_t fConstantTensorSize
void CheckAndFlushIntermediateMemory(std::span< const std::string_view > op_output_tensors, const size_t &op_idx)
void AddOperator(std::unique_ptr< ROperator > op, int order_execution=-1)
void PrintOutputTensors() const
void HeadInitializedTensors(std::string name, int n_print=50)
bool IsConstantTensor(const std::string &name) const
void Initialize(int batchSize=-1, bool verbose=false)
long WriteInitializedTensorsToFile(std::string filename="")
OptimizationLevel fOptimizationLevel
void Generate(std::underlying_type_t< Options > options, int batchSize=-1, long pos=0, bool verbose=false)
void PrintSummary() const
std::vector< std::string > CollectTensorMemberNames(const std::string &input)
Collects all identifiers starting with "tensor_" in the input code, provided that the occurrence is n...
std::vector< Dim > GetDynamicTensorShape(const std::string &name) const
void PrintInitializedTensors() const
std::unordered_map< std::string, InputTensorInfo > fInputTensorInfos
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
MemoryPoolInfo fIntermediateMemoryInfo
! intermediate memory info (transient)
void GenerateIntermediateMemoryPool()
void ReadInitializedTensorsFromFile(long)
std::string AllocateIntermediateMemory(std::span< const std::string_view > op_output_tensors)
std::unordered_map< std::string, std::pair< std::vector< Dim >, bool > > fShapeTensors
void InitializeSubGraph(std::shared_ptr< RModel > graph)
std::unordered_map< std::string, std::string > fShapeParams
void SetNotWritableInitializedTensor(const std::string &tensor_name)
ETensorType GetTensorType(std::string name) const
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)
const std::vector< Dim > & GetShapeTensorValues(const std::string &tensor_name) const
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 AddShapeTensor(const std::string &name, const std::vector< Dim > &shapeValues, bool scalar=false)
bool IsInputTensorShapeParam(std::string const &name) const
Check if a given parameter is used for the shape of an input tensor.
std::string Clean_name(std::string input_tensor_name)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
constexpr size_t GetTypeSize(ETensorType type)
std::string GenerateConstantTensorCode(const std::pair< std::string, InitializedTensor > &t)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
std::underlying_type_t< Options > operator|(Options opA, Options opB)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
std::string ConvertValToString(T value)