Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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)){}
30
31 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
32 return input;
33 }
34
35 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
36 std::vector<size_t> ret = input[0];
37
38 for(size_t i=0; i < input[1].size(); i++) {
39 ret[i]=ret[i]*input[1][i];
40 }
41 return {ret};
42 }
43
44 void Initialize(RModel& model){
45 //input must be a graph input, or already initialized intermediate tensor
46 if (model.CheckIfTensorAlreadyExist(fNInput) == false){
47 throw std::runtime_error("TMVA SOFIE Tile Op Input Tensor is not found in model");
48 }
49 if (model.CheckIfTensorAlreadyExist(fNRepeats) == false){
50 throw std::runtime_error("TMVA SOFIE Tile Op Input Tensor is not found in model");
51 }
53
54 // if repeats vector is not initialized we cannot deduce shape of output
55 // not support for time being this case
56 if (!model.IsInitializedTensor(fNRepeats)) {
57 throw std::runtime_error("TMVA SOFIE Tile Op: non-initialized repeats input is not supported");
58 }
59
60 // Retrieve the data pointer for the repeats tensor
62 // Cast the raw pointer to the appropriate type (size_t*)
63 auto repeats_data = static_cast<int64_t*>(repptr.get());
64 if (repeats_data == nullptr) {
65 throw std::runtime_error("Failed to retrieve the data for the repeats tensor.");
66 }
67 // Get the shape of the repeats tensor to determine the number of elements
69 // Ensure the repeats tensor is 1D and get the number of elements
70 if (repeats_shape.size() != 1) {
71 throw std::runtime_error("Repeats tensor is not 1D.");
72 }
73 size_t num_elements = repeats_shape[0];
74 // Convert the data to a vector of size_t
75 std::vector<size_t> repeats_vector(num_elements);
77
78
80
82
83 if (model.Verbose())
84 std::cout << "Tile: " << fNInput << " " << ConvertShapeToString(fShapeInput) << " -> " << fNY << " with shape " << ConvertShapeToString(fShapeY)
85 << " given repeats " << ConvertShapeToString(repeats_vector) << std::endl;
86 }
87
88 std::string Generate(std::string OpName){
89 OpName = "op_" + OpName;
90 if (fShapeInput.empty() || fShapeY.empty()) {
91 throw std::runtime_error("TMVA SOFIE Tile Op called to Generate without being initialized first");
92 }
93
94 //size_t input_length = ConvertShapeToLength(fShapeInput);
95 //size_t output_length = ConvertShapeToLength(fShapeY);
96
97
98 std::stringstream out;
99 std::string input = "tensor_" + fNInput;
100 std::string output = "tensor_" + fNY;
101 out << "///-------- Tile operator\n";
102 out << "{\n"; // add scope to re-use same names
103 out << "const int input_shape[" << fShapeInput.size() << "] = " << ConvertShapeToString(fShapeInput) << ";\n";
104
105 out << "int inputLength = " << ConvertShapeToLength(fShapeInput) << ";\n";
106 out << "int s = 1;\n";
107 // loop from inverse dim order
108 out << "for (int i = " << fShapeInput.size()-1 << "; i >=0; i--) {\n";
109 out << SP << "int r = tensor_" << fNRepeats << "[i];\n";
110 // we cannot exclude case where repeats=1 since we need offset
111 //out << SP << "if (r == 1 && i < " << fShapeInput.size()-1 << ") continue;\n";
112 out << SP << "int i_offset = 0, o_offset = 0;\n";
113 out << SP << "s = s * input_shape[i];\n";
114 // case we have first copy
115 out << SP << "if (i == " << fShapeInput.size()-1 << ") {\n";
116 out << SP << SP << "for (int j = 0; j < inputLength/s ; j++) {\n";
117 out << SP << SP << SP << "for (int k = 0; k < r ; k++) {\n";
118 out << SP << SP << SP << SP << "std::copy(" << input << "+ i_offset, "
119 << input << "+ i_offset + s, " << output << "+ o_offset);\n";
120 out << SP << SP << SP << SP << "o_offset += s;\n";
121 out << SP << SP << SP << "}\n"; // end k loop
122 out << SP << SP << SP << "i_offset += s;\n";
123 out << SP << SP << "}\n"; // end j loop
124 out << SP << "} else {\n"; // second copy we do from output to output
125 // and we need to loop on j from reverse order to avoir re-writing in output tensor
126 out << SP << SP << "for (int j = inputLength/s - 1 ; j>=0; j--) {\n";
127 out << SP << SP << SP << "o_offset = j*s*r;\n";
128 out << SP << SP << SP << "i_offset = j*s;\n";
129 out << SP << SP << SP << "for (int k = 0; k < r ; k++) {\n";
130 out << SP << SP << SP << SP << "std::copy(" << output << "+ i_offset, "
131 << output << "+ i_offset + s, " << output << "+ o_offset);\n";
132 out << SP << SP << SP << SP << "o_offset += s;\n";
133 out << SP << SP << SP << "}\n"; // end k loop
134 out << SP << SP << "}\n"; // end j loop
135 out << SP << "}\n"; // end if
136 out << SP << "s *= r;\n";
137 out << SP << "inputLength *= r;\n";
138 out << "}\n"; // end i loop
139 out << "}\n"; // end of scope
140 return out.str();
141 }
142};
143
144}//SOFIE
145}//Experimental
146}//TMVA
147
148#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
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:94
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:203
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:188
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:56
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
Definition RModel.cxx:264
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
ROperator_Tile(std::string nameRepeat, std::string nameInput, std::string nameY)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::string Generate(std::string OpName)
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations
static void output()