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 if (model.Verbose()) {
51 std::cout << "Softmax -> " << fNY << " " << ConvertShapeToString(fShape) << std::endl;
52 }
53 }
54
55 std::string Generate(std::string OpName)
56 {
57 OpName = "op_" + OpName;
58 if (fShape.empty()) {
59 throw std::runtime_error("TMVA SOFIE Operator Softmax called to Generate without being initialized first");
60 }
61 std::stringstream out;
62 size_t size = fShape.size();
64 size_t axis = fAttrAxis < 0 ? size + fAttrAxis : fAttrAxis;
65 out << "\n" << SP << "//------ SOFTMAX - " << size << " " << length << " " << axis << "\n";
66 // use safe numerically implementation by subtracting max of tensor
67 if (size == 1) {
68 out << SP << fType << " vmax = tensor_" << fNX << "[0];\n";
69 out << SP << "for (size_t i = 1; i < " << length << " ; i++){\n";
70 out << SP << SP << "if (tensor_" << fNX << "[i] > vmax) vmax = tensor_" << fNX << "[i];\n";
71 out << SP << "}\n";
72 out << SP << fType << " sum = 0.0;\n";
73 out << SP << "for (size_t i = 0; i < " << length << " ; i++){\n";
74 out << SP << SP << "tensor_" << fNY << "[i] = std::exp(tensor_" << fNX << "[i] - vmax);\n";
75 out << SP << SP << "sum += tensor_" << fNY << "[i];\n";
76 out << SP << "}\n";
77 out << SP << "for (size_t i = 0; i < " << length << " ; i++){\n";
78 out << SP << SP << "tensor_" << fNY << "[i] /= sum;\n";
79 out << SP << "}\n";
80 } else {
81 size_t batch = fShape[0];
82 size_t channel = fShape[1];
83 size_t width = (size > 2) ? fShape[size - 1] : 1;
84 size_t height = (size > 3) ? fShape[size - 2] : 1;
85 size_t depth = (size > 4) ? fShape[size - 3] : 1;
86 size_t hStride = width;
87 size_t dStride = height * width;
88 size_t cStride = depth * dStride;
89 size_t bStride = channel * cStride;
90
91 size_t N = 0; // Size of the axis
92 size_t iStride = 0;
93 if (axis == 0) {
94 N = batch;
95 iStride = bStride;
96 } else if (axis == 1) {
97 N = channel;
98 iStride = cStride;
99 } else if (axis == size - 1) {
100 N = width;
101 iStride = 1;
102 } else if (size > 3 && axis == size - 2) {
103 N = height;
104 iStride = hStride;
105 } else if (size == 5 && axis == size - 3) {
106 N = depth;
107 iStride = dStride;
108 } else {
109 throw
110 std::runtime_error("TMVA::SOFIE - Softmax operator along the axis "
111 + std::to_string(fAttrAxis) + " with " + std::to_string(size)
112 + "d input tensor not supported.");
113 }
114
115 bool notBatch = axis != 0;
116 bool notChannel = axis != 1;
117 bool notDepth = (size == 5 && axis != 2);
118 bool notHeight = (size == 5 && axis != 3) || (size == 4 && axis != 2);
119 bool notWidth = (size == 5 && axis != 4) || (size == 4 && axis != 3) || (size == 3 && axis != 2);
120
121 if (notBatch) {
122 out << SP << "for (size_t n = 0; n < " << batch << " ; n++){\n";
123 }
124 if (notChannel) {
125 out << SP << SP << "for (size_t c = 0; c < " << channel << " ; c++){\n";
126 }
127 if (notDepth) {
128 out << SP << SP << "for (size_t d = 0; d < " << depth << " ; d++){\n";
129 }
130 if (notHeight) {
131 out << SP << SP << "for (size_t h = 0; h < " << height << " ; h++){\n";
132 }
133 if (notWidth) {
134 out << SP << SP << "for (size_t w = 0; w < " << width << " ; w++){\n";
135 }
136 out << SP << SP << SP << fType << " sum = 0.;\n";
137 out << SP << SP << SP << "size_t index = 0";
138 if (notBatch) {
139 out << " + n * " << bStride;
140 }
141 if (notChannel) {
142 out << "+ c * " << cStride;
143 }
144 if (notDepth) {
145 out << " + d * " << dStride;
146 }
147 if (notHeight) {
148 out << " + h * " << hStride;
149 }
150 if (notWidth) {
151 out << " + w";
152 }
153 out << ";\n";
154 // apply softmax along the axis - find first maximum value for numerical stability
155 if (N == 0)
156 throw std::runtime_error("TMVA::SOFIE - Softmax operator is along axis with zero elements");
157 out << SP << SP << SP << fType << " vmax = tensor_" << fNX << "[index];\n";
158 out << SP << SP << SP << "for (size_t i = 1; i < " << N << "; i++) {\n";
159 out << SP << SP << SP << SP << "if (tensor_" << fNX << "[index + i*" << iStride << "] > vmax)\n";
160 out << SP << SP << SP << SP << SP << "vmax = tensor_" << fNX << "[index + i*" << iStride << "];\n";
161 out << SP << SP << SP << "}\n";
162 out << SP << SP << SP << "for (size_t i = 0; i < " << N << "; i++) {\n";
163 out << SP << SP << SP << SP << "tensor_" << fNY << "[index + i*" << iStride << "] = std::exp(tensor_" << fNX
164 << "[index + i*" << iStride << "] - vmax);\n";
165 out << SP << SP << SP << SP << "sum += tensor_" << fNY << "[index + i*" << iStride << "];\n";
166 out << SP << SP << SP << "}\n";
167 out << SP << SP << SP << "for (size_t i = 0; i < " << N << "; i++) {\n";
168 out << SP << SP << SP << SP << "tensor_" << fNY << "[index + i*" << iStride << "] /= sum;\n";
169 out << SP << SP << SP << "}\n";
170 if (notWidth) {
171 out << SP << SP << "}\n"; // end w
172 }
173 if (notHeight) {
174 out << SP << SP << "}\n"; // end h
175 }
176 if (notDepth) {
177 out << SP << SP << "}\n"; // end d
178 }
179 if (notChannel) {
180 out << SP << SP << "}\n"; // end c
181 }
182 if (notBatch) {
183 out << SP << "}\n"; // end n
184 }
185 }
186 return out.str();
187 }
188};
189
190} // namespace SOFIE
191} // namespace Experimental
192} // namespace TMVA
193
194#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:94
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:203
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:122
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 ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations