Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Tile.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Tile
2#define TMVA_SOFIE_ROPERATOR_Tile
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14template <typename T>
16{
17
18private:
19
20 std::string fNRepeats;
21 std::string fNInput;
22 std::string fNY;
23 std::vector<size_t>fShapeInput;
24 std::vector<size_t> fShapeY;
25
26public:
28 ROperator_Tile(std::string nameRepeat, std::string nameInput, std::string nameY):
29 fNRepeats(UTILITY::Clean_name(nameRepeat)),fNInput(UTILITY::Clean_name(nameInput)), fNY(UTILITY::Clean_name(nameY)){
32 }
33
34 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
35 return input;
36 }
37
38 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
39 std::vector<size_t> ret = input[0];
40
41 for(size_t i=0; i < input[1].size(); i++) {
42 ret[i]=ret[i]*input[1][i];
43 }
44 return {ret};
45 }
46
47 void Initialize(RModel& model) override {
48 //input must be a graph input, or already initialized intermediate tensor
49 if (model.CheckIfTensorAlreadyExist(fNInput) == false){
50 throw std::runtime_error("TMVA SOFIE Tile Op Input Tensor is not found in model");
51 }
52 if (model.CheckIfTensorAlreadyExist(fNRepeats) == false){
53 throw std::runtime_error("TMVA SOFIE Tile Op Input Tensor is not found in model");
54 }
56
57 // if repeats vector is not initialized we cannot deduce shape of output
58 // not support for time being this case
59 if (!model.IsInitializedTensor(fNRepeats)) {
60 throw std::runtime_error("TMVA SOFIE Tile Op: non-initialized repeats input is not supported");
61 }
62
63 // Retrieve the data pointer for the repeats tensor
65 // Cast the raw pointer to the appropriate type (size_t*)
66 auto repeats_data = static_cast<int64_t*>(repptr.get());
67 if (repeats_data == nullptr) {
68 throw std::runtime_error("Failed to retrieve the data for the repeats tensor.");
69 }
70 // Get the shape of the repeats tensor to determine the number of elements
72 // Ensure the repeats tensor is 1D and get the number of elements
73 if (repeats_shape.size() != 1) {
74 throw std::runtime_error("Repeats tensor is not 1D.");
75 }
76 size_t num_elements = repeats_shape[0];
77 // Convert the data to a vector of size_t
78 std::vector<size_t> repeats_vector(num_elements);
80
81
83
85
86 if (model.Verbose())
87 std::cout << "Tile: " << fNInput << " " << ConvertShapeToString(fShapeInput) << " -> " << fNY << " with shape " << ConvertShapeToString(fShapeY)
88 << " given repeats " << ConvertShapeToString(repeats_vector) << std::endl;
89 }
90
91 std::string Generate(std::string OpName) override {
92 OpName = "op_" + OpName;
93 if (fShapeInput.empty() || fShapeY.empty()) {
94 throw std::runtime_error("TMVA SOFIE Tile Op called to Generate without being initialized first");
95 }
96
97 //size_t input_length = ConvertShapeToLength(fShapeInput);
98 //size_t output_length = ConvertShapeToLength(fShapeY);
99
100
101 std::stringstream out;
102 std::string input = "tensor_" + fNInput;
103 std::string output = "tensor_" + fNY;
104 out << "///-------- Tile operator\n";
105 out << "{\n"; // add scope to re-use same names
106 out << "const int input_shape[" << fShapeInput.size() << "] = " << ConvertShapeToString(fShapeInput) << ";\n";
107
108 out << "int inputLength = " << ConvertShapeToLength(fShapeInput) << ";\n";
109 out << "int s = 1;\n";
110 // loop from inverse dim order
111 out << "for (int i = " << fShapeInput.size()-1 << "; i >=0; i--) {\n";
112 out << SP << "int r = tensor_" << fNRepeats << "[i];\n";
113 // we cannot exclude case where repeats=1 since we need offset
114 //out << SP << "if (r == 1 && i < " << fShapeInput.size()-1 << ") continue;\n";
115 out << SP << "int i_offset = 0, o_offset = 0;\n";
116 out << SP << "s = s * input_shape[i];\n";
117 // case we have first copy
118 out << SP << "if (i == " << fShapeInput.size()-1 << ") {\n";
119 out << SP << SP << "for (int j = 0; j < inputLength/s ; j++) {\n";
120 out << SP << SP << SP << "for (int k = 0; k < r ; k++) {\n";
121 out << SP << SP << SP << SP << "std::copy(" << input << "+ i_offset, "
122 << input << "+ i_offset + s, " << output << "+ o_offset);\n";
123 out << SP << SP << SP << SP << "o_offset += s;\n";
124 out << SP << SP << SP << "}\n"; // end k loop
125 out << SP << SP << SP << "i_offset += s;\n";
126 out << SP << SP << "}\n"; // end j loop
127 out << SP << "} else {\n"; // second copy we do from output to output
128 // and we need to loop on j from reverse order to avoir re-writing in output tensor
129 out << SP << SP << "for (int j = inputLength/s - 1 ; j>=0; j--) {\n";
130 out << SP << SP << SP << "o_offset = j*s*r;\n";
131 out << SP << SP << SP << "i_offset = j*s;\n";
132 out << SP << SP << SP << "for (int k = 0; k < r ; k++) {\n";
133 out << SP << SP << SP << SP << "std::copy(" << output << "+ i_offset, "
134 << output << "+ i_offset + s, " << output << "+ o_offset);\n";
135 out << SP << SP << SP << SP << "o_offset += s;\n";
136 out << SP << SP << SP << "}\n"; // end k loop
137 out << SP << SP << "}\n"; // end j loop
138 out << SP << "}\n"; // end if
139 out << SP << "s *= r;\n";
140 out << SP << "inputLength *= r;\n";
141 out << "}\n"; // end i loop
142 out << "}\n"; // end of scope
143 return out.str();
144 }
145};
146
147}//SOFIE
148}//Experimental
149}//TMVA
150
151#endif //TMVA_SOFIE_ROPERATOR_Tile
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 input
const_iterator begin() const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:200
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:95
const ETensorType & GetTensorType(std::string name) const
Definition RModel.cxx:67
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:175
const std::vector< size_t > & GetTensorShape(std::string name) const
Definition RModel.cxx:29
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:261
ROperator_Tile(std::string nameRepeat, std::string nameInput, std::string nameY)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::string Generate(std::string OpName) override
void Initialize(RModel &model) override
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
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:47
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations
static void output()