Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Transpose.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_TRANSPOSE
2#define TMVA_SOFIE_ROPERATOR_TRANSPOSE
3
5#include "TMVA/ROperator.hxx"
6#include "TMVA/RModel.hxx"
7
8#include <sstream>
9#include <cassert>
10
11namespace TMVA{
12namespace Experimental{
13namespace SOFIE{
14
15
16
17
18template <typename T>
20{
21
22private:
23 std::vector<int_t> fAttrPerm;
24
25 std::string fNData;
26 std::string fNOutput;
27 std::vector<size_t> fShapeData;
28 std::vector<size_t> fShapeOutput;
29
30public:
31
33 ROperator_Transpose(std::vector<int_t> attr_perm, std::string nameData, std::string nameOutput):
34 fAttrPerm(attr_perm), fNData(UTILITY::Clean_name(nameData)), fNOutput(UTILITY::Clean_name(nameOutput)) {
37 }
38
39 ROperator_Transpose(std::string nameData, std::string nameOutput):
40 fNData(UTILITY::Clean_name(nameData)), fNOutput(UTILITY::Clean_name(nameOutput)) {
43 }
44
45 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
46 return input;
47 }
48
49 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
50 if (input.size() > 1) throw std::runtime_error("TMVA SOFIE Tranpose Op Shape Inference only need 1 input tensor");
51 auto& data = input[0];
52 if (fAttrPerm.size() != data.size() )
53 throw std::runtime_error("TMVA SOFIE Tranpose Op - Invalid axes attributes");
54
55 std::vector<size_t> output_shape(fAttrPerm.size());
56 for (size_t i = 0; i < fAttrPerm.size(); i++){
58 }
59 std::vector<std::vector<size_t>> ret;
60 ret.push_back(output_shape);
61 return ret;
62 }
63
64
65 void Initialize(RModel& model) override {
66 if (model.CheckIfTensorAlreadyExist(fNData) == false){ //input must be a graph input, or already initialized intermediate tensor
67 std::cout<<"Input tensor for transpose: "<<fNData<<'\n';
68 throw std::runtime_error("TMVA SOFIE Tranpose Op Input Tensor is not found in model");
69 }
71 if (fAttrPerm.empty()){
72 fAttrPerm.reserve(fShapeData.size());
73 for (int i = fShapeData.size() - 1; i >= 0; i--){
74 fAttrPerm.push_back(i);
75 }
76 }
77 std::vector<std::vector<size_t>> inputs = { fShapeData };
79 if (model.IsInitializedTensor(fNData)) {
80 fIsOutputConstant = true;
81 // case input is a constant or initialized tensor we perform here the transpose
85 auto inputData = static_cast<T*>(model.GetInitializedTensorData(fNData).get());
86 size_t dim = fShapeData.size();
87 std::vector<size_t> outputIdx(dim);
88 std::vector<T> outputData(length);
89 for (size_t i = 0; i < length; i++) {
90 outputIdx[0] = i / outStrides[0];
91 for (size_t j = 1; j < dim; j++) {
92 outputIdx[j] = (i % outStrides[j-1]) / outStrides[j];
93 }
94 // compute input index
95 size_t inputIndex = 0;
96 for (size_t j = 0; j < dim; j++) {
97 // find value in fAtrrPerm corresponding to j
98 int k = std::find(fAttrPerm.begin(), fAttrPerm.end(), j) - fAttrPerm.begin();
100 }
102 }
104 if (model.Verbose()) {
105 std::cout << "Transpose: output is a constant tensor " << ConvertShapeToString(fShapeOutput) << " : "
106 << ConvertValuesToString(outputData) << std::endl;
107 }
108 } else {
110 if (model.Verbose()) {
111 std::cout << "Transpose ---> " << fNOutput << " " << ConvertShapeToString(fShapeOutput) << std::endl;
112 }
113 }
114 }
115
116 std::string Generate(std::string OpName){
117 if (fIsOutputConstant) return ""; //no op for constant tensors
118 OpName = "op_" + OpName;
119 if (fShapeData.empty() || fShapeOutput.empty()){
120 throw std::runtime_error("TMVA SOFIE Transpose Op called to Generate without being initialized first");
121 }
122 int dim = fShapeData.size();
125 size_t length = inStrides[0]*fShapeData[0]; // total tensor size
127
128 std::stringstream out;
129 // Implement transpose operator using consecutive read inputs.
130 // But
131 // tensorOut[id] = tensorInput[ inStrides[0]*i0 + inStrides[1]*i1 + inStrides[2]*i2 + ...]
132 // now if (j0,j1,j2) are the output indices
133 // j0 = id / outStrides[0]
134 // j1 = (id % outStrides[0])/outStrides[1]
135 // j2 = (id % outStrides[1])/outStrides[2]
136 //......
137 // and we have j_k = i_fAttrPerm[k]
138 // since we are using consecutive writes we should find the inverse of fAttrPerm
139 out << SP << "///------- Transpose operator\n" << std::endl;
140 out << SP << "for (size_t id = 0; id < " << length << " ; id++){\n";
141 out << SP << SP << "tensor_" << fNOutput << "[id] = tensor_" << fNData << "[ ";
142 // compute output j indices
143 std::vector<std::string> i_out(dim);
144 for (int k =0; k < dim; k++){
145 if (k == 0)
146 i_out[k] = "id";
147 else
148 i_out[k] = "(id % " + std::to_string(outStrides[k-1]) + ")";
149 if (k < dim-1)
150 i_out[k] += " / " + std::to_string(outStrides[k]);
151 }
152 // use now them for input tensors
153 // need to invert the fAttrPerm[k]
154 for (int k =0; k < dim; k++){
155 // find value in fAtrrPerm corresponding to k
156 int l = std::find(fAttrPerm.begin(), fAttrPerm.end(), k) - fAttrPerm.begin();
157 assert(l >= 0 && l < dim);
158 out << "( " << i_out[l] << " )";
159 if (k < dim-1) {
160 out << " * " << inStrides[k];
161 out << " + ";
162 }
163 }
164 out << "];\n";
165 out << SP << "}\n";
166 return out.str();
167 }
168
169
170};
171
172}//SOFIE
173}//Experimental
174}//TMVA
175
176
177#endif //TMVA_SOFIE_ROPERATOR_TRANSPOSE
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 data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
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 Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
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:227
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
Definition RModel.cxx:192
bool IsInitializedTensor(const std::string &name) const
Definition RModel.cxx:202
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:288
ROperator_Transpose(std::string nameData, std::string nameOutput)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
ROperator_Transpose(std::vector< int_t > attr_perm, std::string nameData, std::string nameOutput)
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
Definition ROperator.hxx:44
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::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertValuesToString(size_t n, const T *data)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations
TLine l
Definition textangle.C:4