Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RModel_Base.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_RMODEL_BASE
2#define TMVA_SOFIE_RMODEL_BASE
3
4#include <type_traits>
5#include <unordered_set>
6#include <vector>
7#include <unordered_map>
8#include <memory>
9#include <ctime>
10#include <set>
11#include <iomanip>
12#include <fstream>
13#include <sstream>
14#include "TMVA/SOFIE_common.hxx"
15#include "TMVA/ROperator.hxx"
16#include "TBuffer.h"
17
18namespace TMVA {
19namespace Experimental {
20namespace SOFIE {
21
22enum class Options {
23 kDefault = 0x0,
24 kNoSession = 0x1,
25 kNoWeightFile = 0x2,
27 kGNN = 0x8,
28 kGNNComponent = 0x10,
29};
30
31// Optimization levels inspired by ONNXRuntime.
32// We only get Operator Fusion with the Basic, and
33// memory reuse with Extended. kExtended is enabled
34// by default
36 kBasic = 0x0,
37 kExtended = 0x1,
38};
39
41
42std::underlying_type_t<Options> operator|(Options opA, Options opB);
43std::underlying_type_t<Options> operator|(std::underlying_type_t<Options> opA, Options opB);
44
46
47protected:
48 std::string fFileName; // file name of original model file for identification
49 std::string fParseTime; // UTC date and time string at parsing
50
52
53 std::unordered_set<std::string> fNeededBlasRoutines;
54 // Set to true once GenerateHeaderInfo has emitted the extern "C" declaration
55 // of the BLAS sgemm_ routine (from fNeededBlasRoutines). It lets the
56 // standalone Gemm_Call helper skip emitting a second, duplicate declaration.
57 bool fBlasSgemmDeclared = false; //!
58
59 std::unordered_set<std::string> fNeededStdLib = {"vector"};
60 std::unordered_set<std::string> fCustomOpHeaders;
61
62 // Inference helper functions (from SOFIE_common) that the generated code
63 // needs. Their standalone definitions are emitted into the generated header
64 // so that it does not depend on including TMVA/SOFIE_common.hxx.
65 std::set<std::string> fNeededHelperFunctions;
66
67 std::string fName = "UnnamedModel";
68 std::string fGC; // generated code
69 bool fUseWeightFile = true;
70 bool fUseSession = true;
71 bool fIsGNN = false;
72 bool fIsGNNComponent = false;
73
74public:
75 /**
76 Default constructor. Needed to allow serialization of ROOT objects. See
77 https://root.cern/manual/io_custom_classes/#restrictions-on-types-root-io-can-handle
78 */
79 RModel_Base() = default;
80
81 RModel_Base(std::string name, std::string parsedtime);
82
83 // For GNN Functions usage
85
86 void AddBlasRoutines(std::vector<std::string> routines)
87 {
88 for (auto &routine : routines) {
90 }
91 }
92 void AddNeededStdLib(std::string libname)
93 {
94 // if the library is already in the set, insert does nothing, so we don't need to check before inserting
95 fNeededStdLib.insert(libname);
96 }
98 {
100 }
101 // Register an inference helper function that the generated code needs. See
102 // GenerateHelperFunctionsCode for the list of recognised keys.
104 {
105 fNeededHelperFunctions.insert(std::move(name));
106 }
107 const std::set<std::string> &GetNeededHelperFunctions() const { return fNeededHelperFunctions; }
108
109 // Placeholder tokens emitted by GenerateHeaderInfo and later replaced by
110 // EmitHelperFunctionsCode with the actual helper includes / definitions.
111 // This two-step approach is needed because the full set of required helpers
112 // is only known once all operators (and sub-graphs) have been generated.
113 static constexpr const char *kHelperIncludesMarker = "//@SOFIE_HELPER_INCLUDES@\n";
114 static constexpr const char *kHelperFunctionsMarker = "//@SOFIE_HELPER_FUNCTIONS@\n";
115
116 void GenerateHeaderInfo(std::string &hgname);
117 // Replace the helper markers in the generated code with the standalone
118 // definitions of the helper functions collected in fNeededHelperFunctions.
120 void PrintGenerated(std::ostream &os=std::cout) { os << fGC; }
121
122 std::string ReturnGenerated() { return fGC; }
123 void OutputGenerated(std::string filename = "", bool append = false);
124 void SetFilename(std::string filename) { fName = filename; }
125 std::string GetFilename() { return fName; }
126 const std::string & GetName() const { return fName;}
127};
128
129enum class GraphType { INVALID = 0, GNN = 1, GraphIndependent = 2 };
130
131enum class FunctionType { UPDATE = 0, AGGREGATE = 1 };
132enum class FunctionTarget { INVALID = 0, NODES = 1, EDGES = 2, GLOBALS = 3 };
133enum class FunctionReducer { INVALID = 0, SUM = 1, MEAN = 2 };
135
137public:
138 virtual void Generate() = 0;
139 virtual ~RModel_GNNBase() = default;
140};
141
142} // namespace SOFIE
143} // namespace Experimental
144} // namespace TMVA
145
146#endif // TMVA_SOFIE_RMODEL_BASE
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
char name[80]
Definition TGX11.cxx:148
void GenerateHeaderInfo(std::string &hgname)
RModel_Base()=default
Default constructor.
static constexpr const char * kHelperIncludesMarker
void PrintGenerated(std::ostream &os=std::cout)
std::set< std::string > fNeededHelperFunctions
std::unordered_set< std::string > fNeededBlasRoutines
static constexpr const char * kHelperFunctionsMarker
const std::set< std::string > & GetNeededHelperFunctions() const
void AddNeededHelperFunction(std::string name)
RModel_Base(std::string function_name)
std::unordered_set< std::string > fCustomOpHeaders
void OutputGenerated(std::string filename="", bool append=false)
std::unordered_set< std::string > fNeededStdLib
const std::string & GetName() const
void AddBlasRoutines(std::vector< std::string > routines)
void AddNeededStdLib(std::string libname)
void AddNeededCustomHeader(std::string filename)
void SetFilename(std::string filename)
std::underlying_type_t< Options > operator|(Options opA, Options opB)
Definition RModel.cxx:56
create variable transformations