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 std::cout<<"Input tensor for transspose: "<<fNData<<'\n';
64 throw std::runtime_error("TMVA SOFIE Tranpose Op Input Tensor is not found in model");
65 }
67 if (fAttrPerm.empty()){
68 fAttrPerm.reserve(fShapeData.size());
69 for (int i = fShapeData.size() - 1; i >= 0; i--){
70 fAttrPerm.push_back(i);
71 }
72 }
73 std::vector<std::vector<size_t>> inputs = { fShapeData };
74 fShapeOutput = ShapeInference(inputs).front();
76 }
77
78 std::string Generate(std::string OpName){
79 OpName = "op_" + OpName;
80 if (fShapeData.empty() || fShapeOutput.empty()){
81 throw std::runtime_error("TMVA SOFIE Transpose Op called to Generate without being initialized first");
82 }
83 int dim = fShapeData.size();
86 size_t length = inStrides[0]*fShapeData[0]; // total tensor size
87 assert (length == outStrides[0]*fShapeOutput[0]);
88
89 std::stringstream out;
90 // Implement transpose operator using consecutive read inputs.
91 // But
92 // tensorOut[id] = tensorInput[ inStrides[0]*i0 + inStrides[1]*i1 + inStrides[2]*i2 + ...]
93 // now if (j0,j1,j2) are the output indices
94 // j0 = id / outStrides[0]
95 // j1 = (id % outStrides[0])/outStrides[1]
96 // j2 = (id % outStrides[1])/outStrides[2]
97 //......
98 // and we have j_k = i_fAttrPerm[k]
99 // since we are using consecutive writes we should find the inverse of fAttrPerm
100 out << SP << "///------- Transpose operator\n" << std::endl;
101 out << SP << "for (size_t id = 0; id < " << length << " ; id++){\n";
102 out << SP << SP << "tensor_" << fNOutput << "[id] = tensor_" << fNData << "[ ";
103 // compute output j indices
104 std::vector<std::string> i_out(dim);
105 for (int k =0; k < dim; k++){
106 if (k == 0)
107 i_out[k] = "id";
108 else
109 i_out[k] = "(id % " + std::to_string(outStrides[k-1]) + ")";
110 if (k < dim-1)
111 i_out[k] += " / " + std::to_string(outStrides[k]);
112 }
113 // use now them for input tensors
114 // need to invert the fAttrPerm[k]
115 for (int k =0; k < dim; k++){
116 // find value in fAtrrPerm corresponding to k
117 int l = std::find(fAttrPerm.begin(), fAttrPerm.end(), k) - fAttrPerm.begin();
118 assert(l >= 0 && l < dim);
119 out << "( " << i_out[l] << " )";
120 if (k < dim-1) {
121 out << " * " << inStrides[k];
122 out << " + ";
123 }
124 }
125 out << "];\n";
126 out << SP << "}\n";
127 return out.str();
128 }
129
130
131};
132
133}//SOFIE
134}//Experimental
135}//TMVA
136
137
138#endif //TMVA_SOFIE_ROPERATOR_TRANSPOSE
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 data
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:76
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape)
Definition RModel.cxx:156
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:97
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:55
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