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
10namespace TMVA{
11namespace Experimental{
12namespace SOFIE{
13
14
15
16
17template <typename T>
18class ROperator_Transpose final : public ROperator
19{
20
21private:
22 std::vector<int_t> fAttrPerm;
23
24 std::string fNData;
25 std::string fNOutput;
26 std::vector<size_t> fShapeData;
27 std::vector<size_t> fShapeOutput;
28
29public:
30
32 ROperator_Transpose(std::vector<int_t> attr_perm, std::string nameData, std::string nameOutput):
33 fAttrPerm(attr_perm), fNData(UTILITY::Clean_name(nameData)), fNOutput(UTILITY::Clean_name(nameOutput)) {
34 }
35
36 ROperator_Transpose(std::string nameData, std::string nameOutput):
37 fNData(UTILITY::Clean_name(nameData)), fNOutput(UTILITY::Clean_name(nameOutput)) {
38 }
39
40 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
41 return input;
42 }
43
44 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
45 if (input.size() > 1) throw std::runtime_error("TMVA SOFIE Tranpose Op Shape Inference only need 1 input tensor");
46 auto& data = input[0];
47 std::vector<size_t> output_shape(fAttrPerm.size());
48 for (size_t i = 0; i < fAttrPerm.size(); i++){
49 output_shape[fAttrPerm[i]] = data[i];
50 }
51 std::vector<std::vector<size_t>> ret;
52 ret.push_back(output_shape);
53 return ret;
54 }
55
56
57 void Initialize(RModel& model){
58 if (model.CheckIfTensorAlreadyExist(fNData) == false){ //input must be a graph input, or already initialized intermediate tensor
59 throw std::runtime_error("TMVA SOFIE Tranpose Op Input Tensor is not found in model");
60 }
62 if (fAttrPerm.empty()){
63 for (int i = fShapeData.size() - 1; i >= 0; i--){
64 fAttrPerm.push_back(i);
65 }
66 }
67
68 std::vector<size_t> output_shape(fAttrPerm.size());
69 for (size_t i = 0; i < fAttrPerm.size(); i++){
70 output_shape[fAttrPerm[i]] = fShapeData[i];
71 }
72
73 model.AddIntermediateTensor(fNOutput, model.GetTensorType(fNData), output_shape);
74 fShapeOutput = output_shape;
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();
83 int length=1;
84 std::vector<int> sizeofindex(dim);
85 for (int i = dim - 1; i>=0; i--){
86 sizeofindex[i] = length;
87 length *= fShapeData[i];
88 }
89 std::vector<int> index_goto(dim);
90 for (int i = 0; i < dim; i++){
91 index_goto[fAttrPerm[i]] = i;
92 }
93 std::vector<int> new_sizeofindex(dim);
94 int t = 1;
95 for (int i = dim - 1; i>=0; i--){
96 new_sizeofindex[i] = t;
97 t *= fShapeOutput[i];
98 }
99
100 std::stringstream out;
101 out << SP << "///------- Transpose operator\n" << std::endl;
102 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
103 out << SP << SP << "tensor_" << fNOutput << "[";
104 for (int i =0; i < dim; i++){
105 out << "id / " << sizeofindex[i] << " % " << fShapeData[i] << " * " << new_sizeofindex[index_goto[i]];
106 if (i != dim - 1) out << " + ";
107 }
108 out << "] = " << "tensor_" << fNData << "[id];\n";
109 out << SP << "}\n";
110 return out.str();
111 }
112
113
114};
115
116}//SOFIE
117}//Experimental
118}//TMVA
119
120
121#endif //TMVA_SOFIE_ROPERATOR_TRANSPOSE
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:70
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape)
Definition RModel.cxx:136
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:91
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:49
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:39
create variable transformations