9
9
#endif
10
10
11
11
#include " TMVA/RModel.hxx"
12
+ #include " TMVA/RModelProfiler.hxx"
12
13
#include " TMVA/SOFIE_common.hxx"
13
14
14
15
namespace TMVA {
@@ -871,7 +872,7 @@ void RModel::GenerateSessionCode()
871
872
CheckAndFlushIntermediateMemory (fOperators [op_idx]->GetOpInputTensors (), op_idx);
872
873
}
873
874
874
- // to check remaining unused fragments after memory allocation (lesser the better)
875
+ // to check remaining unused fragments after memory allocation (lesser the better)
875
876
// for (const auto &it: fIntermediateMemoryInfo.available_stack){
876
877
// std::cout<<"chunk_idx: "<<it.first<<", chunk_size: "<<it.second<<"\n";
877
878
// }
@@ -899,13 +900,13 @@ void RModel::GenerateSessionCode()
899
900
// Generate code for Session constructor
900
901
if (fUseSession ) {
901
902
std::string sessionName = " Session" ;
902
- if (fIsSubGraph )
903
+ if (fIsSubGraph )
903
904
sessionName += " _" + fName ;
904
905
// add here specific operator code that needs to define session data members
905
906
fGC += " \n " ;
906
907
for (size_t id = 0 ; id < fOperators .size (); id++) {
907
908
std::string opName = std::to_string (id);
908
- fGC += fOperators [id]->GenerateSessionMembersCode (opName);
909
+ fGC += fOperators [id]->GenerateSessionMembersCode (opName);
909
910
}
910
911
fGC += " \n " ;
911
912
// here add initialization and reading of weight tensors
@@ -950,35 +951,41 @@ void RModel::GenerateSessionCode()
950
951
951
952
fGC += " }\n\n " ;
952
953
}
953
-
954
- fGC += doInferSignature + " {\n " ;
955
- fGC += " \n " ;
956
-
957
- // generate the inference code
958
- if (fVerbose )
959
- std::cout << " Generating main inference code for " << fName << std::endl;
960
954
961
- if (fOutputTensorNames .size () == 0 )
962
- throw std::runtime_error (" TMVA-SOFIE: output size=0 are not supported" );
955
+ if (fProfile ) {
956
+ RModelProfiler profiler (*this );
957
+ profiler.Generate ();
958
+ fGC += fProfilerGC ;
959
+ } else {
960
+ fGC += doInferSignature + " {\n " ;
961
+ fGC += " \n " ;
963
962
964
- for ( size_t op_idx = 0 ; op_idx < fOperators . size (); ++op_idx) {
963
+ // generate the inference code
965
964
if (fVerbose )
965
+ std::cout << " Generating main inference code for " << fName << std::endl;
966
+
967
+ if (fOutputTensorNames .size () == 0 )
968
+ throw std::runtime_error (" TMVA-SOFIE: output size=0 are not supported" );
969
+
970
+ for (size_t op_idx = 0 ; op_idx < fOperators .size (); ++op_idx) {
971
+ if (fVerbose )
966
972
std::cout << " Generating code for operator .... " << op_idx << std::endl;
967
- fGC += (fOperators [op_idx]->Generate (std::to_string (op_idx)));
968
- }
973
+ fGC += (fOperators [op_idx]->Generate (std::to_string (op_idx)));
974
+ }
969
975
970
- fGC += SP + " using TMVA::Experimental::SOFIE::UTILITY::FillOutput;\n\n " ;
976
+ fGC += SP + " using TMVA::Experimental::SOFIE::UTILITY::FillOutput;\n\n " ;
971
977
972
- for (std::string const &name : fOutputTensorNames ) {
973
- // need to check is size is the same (don't want to return a vector with
974
- // larger size) in that case better to copy
975
- bool isIntermediate = fIntermediateTensorInfos .count (name) > 0 ;
976
- std::string n = isIntermediate ? std::to_string (ConvertShapeToLength (GetTensorShape (name)))
977
- : ConvertDynamicShapeToLength (GetDynamicTensorShape (name));
978
- fGC += SP + " FillOutput(tensor_" + name + " , output_tensor_" + name + " , " + n + " );\n " ;
979
- }
978
+ for (std::string const &name : fOutputTensorNames ) {
979
+ // need to check is size is the same (don't want to return a vector with
980
+ // larger size) in that case better to copy
981
+ bool isIntermediate = fIntermediateTensorInfos .count (name) > 0 ;
982
+ std::string n = isIntermediate ? std::to_string (ConvertShapeToLength (GetTensorShape (name)))
983
+ : ConvertDynamicShapeToLength (GetDynamicTensorShape (name));
984
+ fGC += SP + " FillOutput(tensor_" + name + " , output_tensor_" + name + " , " + n + " );\n " ;
985
+ }
980
986
981
- fGC += " }\n\n " ;
987
+ fGC += " }\n\n " ;
988
+ }
982
989
983
990
// generate the inference overload that returns an output struct
984
991
GenerateOutput ();
@@ -991,9 +998,11 @@ void RModel::GenerateSessionCode()
991
998
992
999
void RModel::Generate (std::underlying_type_t <Options> options, int batchSize, long pos, bool verbose)
993
1000
{
1001
+ bool profile = (options & static_cast <std::underlying_type_t <Options>>(Options::kProfile ));
994
1002
fVerbose = verbose;
995
1003
fBatchSize = batchSize;
996
1004
fReadPos = pos;
1005
+ fProfile = profile;
997
1006
998
1007
// session flag is used in operator initialize
999
1008
if (static_cast <std::underlying_type_t <Options>>(Options::kNoSession ) & options) {
@@ -1013,9 +1022,9 @@ void RModel::Generate(std::underlying_type_t<Options> options, int batchSize, lo
1013
1022
" TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class" );
1014
1023
}
1015
1024
1016
- if (static_cast <std::underlying_type_t <Options>>(Options::kGNN ) & options)
1025
+ if (static_cast <std::underlying_type_t <Options>>(Options::kGNN ) & options)
1017
1026
fIsGNN = true ;
1018
- if (static_cast <std::underlying_type_t <Options>>(Options::kGNNComponent ) & options)
1027
+ if (static_cast <std::underlying_type_t <Options>>(Options::kGNNComponent ) & options)
1019
1028
fIsGNNComponent = true ;
1020
1029
1021
1030
// initialize the model including all operators and sub-graphs
@@ -1029,13 +1038,13 @@ void RModel::Generate(std::underlying_type_t<Options> options, int batchSize, lo
1029
1038
1030
1039
// generate first code for the subgraphs
1031
1040
for (auto &graph : fSubGraphs ) {
1032
- if (fVerbose )
1041
+ if (fVerbose )
1033
1042
std::cout << " generate session code for subgraph " << graph->fName << std::endl;
1034
1043
graph->GenerateSessionCode ();
1035
1044
fGC += graph->fGC ;
1036
1045
}
1037
1046
1038
- if (fVerbose )
1047
+ if (fVerbose )
1039
1048
std::cout << " generate Main session code - model " << fName << std::endl;
1040
1049
1041
1050
// generate main session code
0 commit comments