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>
19class ROperator_Transpose final : public ROperator
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)) {
35 }
36
37 ROperator_Transpose(std::string nameData, std::string nameOutput):
38 fNData(UTILITY::Clean_name(nameData)), fNOutput(UTILITY::Clean_name(nameOutput)) {
39 }
40
41 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
42 return input;
43 }
44
45 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
46 if (input.size() > 1) throw std::runtime_error("TMVA SOFIE Tranpose Op Shape Inference only need 1 input tensor");
47 auto& data = input[0];
48 if (fAttrPerm.size() != data.size() )
49 throw std::runtime_error("TMVA SOFIE Tranpose Op - Invalid axes attributes");
50
51 std::vector<size_t> output_shape(fAttrPerm.size());
52 for (size_t i = 0; i < fAttrPerm.size(); i++){
53 output_shape[i] = data[fAttrPerm[i]];
54 }
55 std::vector<std::vector<size_t>> ret;
56 ret.push_back(output_shape);
57 return ret;
58 }
59
60
61 void Initialize(RModel& model){
62 if (model.CheckIfTensorAlreadyExist(fNData) == false){ //input must be a graph input, or already initialized intermediate tensor
63 throw std::runtime_error("TMVA SOFIE Tranpose Op Input Tensor is not found in model");
64 }
66 if (fAttrPerm.empty()){
67 fAttrPerm.reserve(fShapeData.size());
68 for (int i = fShapeData.size() - 1; i >= 0; i--){
69 fAttrPerm.push_back(i);
70 }
71 }
72 std::vector<std::vector<size_t>> inputs = { fShapeData };
73 fShapeOutput = ShapeInference(inputs).front();
75 }
76
77 std::string Generate(std::string OpName){
78 OpName = "op_" + OpName;
79 if (fShapeData.empty() || fShapeOutput.empty()){
80 throw std::runtime_error("TMVA SOFIE Transpose Op called to Generate without being initialized first");
81 }
82 int dim = fShapeData.size();
85 size_t length = inStrides[0]*fShapeData[0]; // total tensor size
86 assert (length == outStrides[0]*fShapeOutput[0]);
87
88 std::stringstream out;
89 // Implement transpose operator using consecutive read inputs.
90 // But
91 // tensorOut[id] = tensorInput[ inStrides[0]*i0 + inStrides[1]*i1 + inStrides[2]*i2 + ...]
92 // now if (j0,j1,j2) are the output indices
93 // j0 = id / outStrides[0]
94 // j1 = (id % outStrides[0])/outStrides[1]
95 // j2 = (id % outStrides[1])/outStrides[2]
96 //......
97 // and we have j_k = i_fAttrPerm[k]
98 // since we are using consecutive writes we should find the inverse of fAttrPerm
99 out << SP << "///------- Transpose operator\n" << std::endl;
100 out << SP << "for (size_t id = 0; id < " << length << " ; id++){\n";
101 out << SP << SP << "tensor_" << fNOutput << "[id] = tensor_" << fNData << "[ ";
102 // compute output j indices
103 std::vector<std::string> i_out(dim);
104 for (int k =0; k < dim; k++){
105 if (k == 0)
106 i_out[k] = "id";
107 else
108 i_out[k] = "(id % " + std::to_string(outStrides[k-1]) + ")";
109 if (k < dim-1)
110 i_out[k] += " / " + std::to_string(outStrides[k]);
111 }
112 // use now them for input tensors
113 // need to invert the fAttrPerm[k]
114 for (int k =0; k < dim; k++){
115 // find value in fAtrrPerm corresponding to k
116 int l = std::find(fAttrPerm.begin(), fAttrPerm.end(), k) - fAttrPerm.begin();
117 assert(l >= 0 && l < dim);
118 out << "( " << i_out[l] << " )";
119 if (k < dim-1) {
120 out << " * " << inStrides[k];
121 out << " + ";
122 }
123 }
124 out << "];\n";
125 out << SP << "}\n";
126 return out.str();
127 }
128
129
130};
131
132}//SOFIE
133}//Experimental
134}//TMVA
135
136
137#endif //TMVA_SOFIE_ROPERATOR_TRANSPOSE
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:80
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape)
Definition RModel.cxx:160
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:101
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:59
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)
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
create variable transformations
TLine l
Definition textangle.C:4