Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROperator_Softmax.hxx
Go to the documentation of this file.
1#ifndef TMVA_SOFIE_ROPERATOR_Softmax
2#define TMVA_SOFIE_ROPERATOR_Softmax
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>
15class ROperator_Softmax final : public ROperator {
16
17private:
18 int64_t fAttrAxis;
19
20 std::string fNX;
21 std::string fNY;
22 std::vector<size_t> fShape;
23
24 std::string fType;
25
26public:
28 ROperator_Softmax(int64_t attr_axis, std::string nameX, std::string nameY)
29 : fAttrAxis(attr_axis), fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
30 {
31 }
32
33 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) { return input; }
34
35 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input)
36 {
37 auto ret = input; // suggest copy to compiler
38 return ret;
39 }
40
41 void Initialize(RModel &model)
42 {
43 if (model.CheckIfTensorAlreadyExist(fNX) ==
44 false) { // input must be a graph input, or already initialized intermediate tensor
45 throw std::runtime_error("TMVA SOFIE Softmax Op Input Tensor is not found in model");
46 }
47 fShape = model.GetTensorShape(fNX);
50 }
51
52 std::string Generate(std::string OpName)
53 {
54 OpName = "op_" + OpName;
55 if (fShape.empty()) {
56 throw std::runtime_error("TMVA SOFIE Operator Softmax called to Generate without being initialized first");
57 }
58 std::stringstream out;
59 size_t size = fShape.size();
61 size_t axis = fAttrAxis < 0 ? size + fAttrAxis : fAttrAxis;
62 out << "\n" << SP << "//------ SOFTMAX\n";
63 if (size == 1) {
64 out << SP << fType << " sum = 0.0;\n";
65 out << SP << "for (size_t i = 0; i < " << length << " ; i++){\n";
66 out << SP << SP << "tensor_" << fNY << "[i] = std::exp(tensor_" << fNX << "[i]);\n";
67 out << SP << SP << "sum += tensor_" << fNY << "[i];\n";
68 out << SP << "}\n";
69 out << SP << "for (size_t i = 0; i < " << length << " ; i++){\n";
70 out << SP << SP << "tensor_" << fNY << "[i] /= sum;\n";
71 out << SP << "}\n";
72 } else {
73 size_t batch = fShape[0];
74 size_t channel = fShape[1];
75 size_t width = (size > 2) ? fShape[size - 1] : 1;
76 size_t height = (size > 3) ? fShape[size - 2] : 1;
77 size_t depth = (size > 4) ? fShape[size - 3] : 1;
78 size_t hStride = width;
79 size_t dStride = height * width;
80 size_t cStride = depth * dStride;
81 size_t bStride = channel * cStride;
82
83 size_t N = 0; // Size of the axis
84 size_t iStride = 0;
85 if (axis == 0) {
86 N = batch;
87 iStride = bStride;
88 } else if (axis == 1) {
89 N = channel;
90 iStride = cStride;
91 } else if (axis == size - 1) {
92 N = width;
93 iStride = 1;
94 } else if (size > 3 && axis == size - 2) {
95 N = height;
96 iStride = hStride;
97 } else if (size == 5 && axis == size - 3) {
98 N = depth;
99 iStride = dStride;
100 } else {
101 throw
102 std::runtime_error("TMVA::SOFIE - Softmax operator along the axis "
103 + std::to_string(fAttrAxis) + " with " + std::to_string(size)
104 + "d input tensor not supported.");
105 }
106
107 bool notBatch = axis != 0;
108 bool notChannel = axis != 1;
109 bool notDepth = (size == 5 && axis != 2);
110 bool notHeight = (size == 5 && axis != 3) || (size == 4 && axis != 2);
111 bool notWidth = (size == 5 && axis != 4) || (size == 4 && axis != 3) || (size == 3 && axis != 2);
112
113 if (notBatch) {
114 out << SP << "for (size_t n = 0; n < " << batch << " ; n++){\n";
115 }
116 if (notChannel) {
117 out << SP << SP << "for (size_t c = 0; c < " << channel << " ; c++){\n";
118 }
119 if (notDepth) {
120 out << SP << SP << "for (size_t d = 0; d < " << depth << " ; d++){\n";
121 }
122 if (notHeight) {
123 out << SP << SP << "for (size_t h = 0; h < " << height << " ; h++){\n";
124 }
125 if (notWidth) {
126 out << SP << SP << "for (size_t w = 0; w < " << width << " ; w++){\n";
127 }
128 out << SP << SP << SP << fType << " sum = 0.;\n";
129 out << SP << SP << SP << "size_t index = 0";
130 if (notBatch) {
131 out << "+ n * " << bStride;
132 }
133 if (notChannel) {
134 out << "+ c * " << cStride;
135 }
136 if (notDepth) {
137 out << "+ d * " << dStride;
138 }
139 if (notHeight) {
140 out << "+ h * " << hStride;
141 }
142 if (notWidth) {
143 out << " + w";
144 }
145 out << ";\n";
146 // apply softmax along the axis
147 out << SP << SP << SP << "for (size_t i = 0; i < " << N << "; i++) {\n";
148 out << SP << SP << SP << SP << "tensor_" << fNY << "[index + i*" << iStride << "] = std::exp(tensor_" << fNX
149 << "[index + i*" << iStride << "]);\n";
150 out << SP << SP << SP << SP << "sum += tensor_" << fNY << "[index + i*" << iStride << "];\n";
151 out << SP << SP << SP << "}\n";
152 out << SP << SP << SP << "for (size_t i = 0; i < " << N << "; i++) {\n";
153 out << SP << SP << SP << SP << "tensor_" << fNY << "[index + i*" << iStride << "] /= sum;\n";
154 out << SP << SP << SP << "}\n";
155 if (notWidth) {
156 out << SP << SP << "}\n"; // end w
157 }
158 if (notHeight) {
159 out << SP << SP << "}\n"; // end h
160 }
161 if (notDepth) {
162 out << SP << SP << "}\n"; // end d
163 }
164 if (notChannel) {
165 out << SP << SP << "}\n"; // end c
166 }
167 if (notBatch) {
168 out << SP << "}\n"; // end n
169 }
170 }
171 return out.str();
172 }
173};
174
175} // namespace SOFIE
176} // namespace Experimental
177} // namespace TMVA
178
179#endif // TMVA_SOFIE_ROPERATOR_Softmax
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define N
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
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
const ETensorType & GetTensorType(std::string name)
Definition RModel.cxx:91
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:187
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:116
const std::vector< size_t > & GetTensorShape(std::string name)
Definition RModel.cxx:56
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
ROperator_Softmax(int64_t attr_axis, std::string nameX, std::string nameY)
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:41
std::string ConvertTypeToString(ETensorType type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations