Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Custom.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Custom
2#define TMVA_SOFIE_ROPERATOR_Custom
3
4
6#include "TMVA/ROperator.hxx"
7#include "TMVA/RModel.hxx"
8
10
11
12template<typename T>
14{
15
16private:
17 std::string fOpName;
18 std::vector<std::string> fInputNames;
19 std::vector<std::string> fOutputNames;
20 std::vector<std::vector<std::size_t>> fOutputShapes;
21 std::vector<std::size_t> fInputSizes;
22 std::string fHeaderName;
24
25public:
27 ROperator_Custom(std::string OpName, std::vector<std::string>Inputs, std::vector<std::string>Outputs, std::vector<std::vector<std::size_t>> OutputShapes, std::string HeaderName){
31 for(auto& it:Inputs){
32 fInputNames.emplace_back(UTILITY::Clean_name(it));
33 fInputTensorNames.emplace_back(fInputNames.back());
34 }
35 for(auto& it:Outputs){
36 fOutputNames.emplace_back(UTILITY::Clean_name(it));
37 fOutputTensorNames.emplace_back(fOutputNames.back());
38 }
39 }
40
41 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>>) override {return {{}};};
42 std::vector<ETensorType> TypeInference(std::vector<ETensorType>) override { return {};};
43
44 void Initialize(RModel& model) override {
45 model.AddNeededCustomHeader(fHeaderName);
46 fInputType = model.GetTensorType(fInputNames[0]);
47
48 for(auto& it:fInputNames){
49 if (model.CheckIfTensorAlreadyExist(it) == false){
50 throw std::runtime_error("TMVA SOFIE Custom " + fOpName + " Op Input Tensor " + it + " is not found in model");
51 }
52 fInputSizes.push_back(ConvertShapeToLength(model.GetTensorShape(it)));
53 }
54
55 if(fOutputNames.size() != fOutputShapes.size()){
56 throw std::runtime_error("TMVA SOFIE Custom "+ fOpName + " Op was not intialized with the names/shapes of all the output tensors");
57 }
58
59 for(long unsigned int i=0; i<fOutputNames.size(); ++i){
60 model.AddIntermediateTensor(std::string(fOutputNames[i]), ETensorType::FLOAT, fOutputShapes[i]);
61 }
62
63
64 model.UpdateOutputTensorList(fInputNames, fOutputNames);
65
66 if (model.Verbose()) {
67 std::cout << "Custom operator using " << fHeaderName;
68 for (auto & i : fInputNames) std::cout << " " << i;
69 std::cout << " ---> ";
70 for (auto & i : fOutputNames) std::cout << " " << i;
71 std::cout << "\n";
72 }
73 model.AddNeededCustomHeader("ROOT/RSpan.hxx");
74 }
75
76 std::string Generate(std::string OpName) override {
77 OpName = "op_" + OpName;
78 std::stringstream out;
79 out << "\n//------ "<<fOpName<<" \n";
80 std::string args;
81 for(long unsigned int i = 0; i<fInputNames.size(); ++i){
82 args+="std::span<const "+ConvertTypeToString(fInputType)+">(tensor_"+std::string(fInputNames[i])+", "+std::to_string(fInputSizes[i])+"),";
83 }
84
85 for(long unsigned int i = 0; i<fOutputNames.size(); ++i){
86 args+="std::span<"+TensorType<T>::Name()+">(tensor_"+std::string(fOutputNames[i])+", "+std::to_string(ConvertShapeToLength(fOutputShapes[i]))+"),";
87 }
88 args.pop_back();
89 out << SP << fOpName<<"::Compute("+args+");\n";
90 return out.str();
91 }
92
93};
94
95
96} // namespace TMVA::Experimental::SOFIE
97
98
99#endif //TMVA_SOFIE_ROPERATOR_Custom
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
ROperator_Custom(std::string OpName, std::vector< std::string >Inputs, std::vector< std::string >Outputs, std::vector< std::vector< std::size_t > > OutputShapes, std::string HeaderName)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > >) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType >) override
std::vector< std::vector< std::size_t > > fOutputShapes
std::string Generate(std::string OpName) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:47
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:42
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:48
std::string Clean_name(std::string input_tensor_name)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertTypeToString(ETensorType type)