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>
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 {
33 }
34
35 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) { return input; }
36
37 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input)
38 {
39 auto ret = input; // suggest copy to compiler
40 return ret;
41 }
42
43 void Initialize(RModel& model) override {
44 if (model.CheckIfTensorAlreadyExist(fNX) ==
45 false) { // input must be a graph input, or already initialized intermediate tensor
46 throw std::runtime_error("TMVA SOFIE Softmax Op Input Tensor is not found in model");
47 }
48 fShape = model.GetTensorShape(fNX);
51 if (model.Verbose()) {
52 std::cout << "Softmax -> " << fNY << " " << ConvertShapeToString(fShape) << std::endl;
53 }
54 }
55
56 std::string Generate(std::string OpName)
57 {
58 OpName = "op_" + OpName;
59 if (fShape.empty()) {
60 throw std::runtime_error("TMVA SOFIE Operator Softmax called to Generate without being initialized first");
61 }
62 std::stringstream out;
63 size_t size = fShape.size();
65 size_t axis = fAttrAxis < 0 ? size + fAttrAxis : fAttrAxis;
66 out << "\n" << SP << "//------ SOFTMAX - " << size << " " << length << " " << axis << "\n";
67 // use safe numerically implementation by subtracting max of tensor
68 if (size == 1) {
69 out << SP << fType << " vmax = tensor_" << fNX << "[0];\n";
70 out << SP << "for (size_t i = 1; i < " << length << " ; i++){\n";
71 out << SP << SP << "if (tensor_" << fNX << "[i] > vmax) vmax = tensor_" << fNX << "[i];\n";
72 out << SP << "}\n";
73 out << SP << fType << " sum = 0.0;\n";
74 out << SP << "for (size_t i = 0; i < " << length << " ; i++){\n";
75 out << SP << SP << "tensor_" << fNY << "[i] = std::exp(tensor_" << fNX << "[i] - vmax);\n";
76 out << SP << SP << "sum += tensor_" << fNY << "[i];\n";
77 out << SP << "}\n";
78 out << SP << "for (size_t i = 0; i < " << length << " ; i++){\n";
79 out << SP << SP << "tensor_" << fNY << "[i] /= sum;\n";
80 out << SP << "}\n";
81 } else {
82 size_t batch = fShape[0];
83 size_t channel = fShape[1];
84 size_t width = (size > 2) ? fShape[size - 1] : 1;
85 size_t height = (size > 3) ? fShape[size - 2] : 1;
86 size_t depth = (size > 4) ? fShape[size - 3] : 1;
87 size_t hStride = width;
88 size_t dStride = height * width;
89 size_t cStride = depth * dStride;
90 size_t bStride = channel * cStride;
91
92 size_t N = 0; // Size of the axis
93 size_t iStride = 0;
94 if (axis == 0) {
95 N = batch;
97 } else if (axis == 1) {
98 N = channel;
100 } else if (axis == size - 1) {
101 N = width;
102 iStride = 1;
103 } else if (size > 3 && axis == size - 2) {
104 N = height;
106 } else if (size == 5 && axis == size - 3) {
107 N = depth;
109 } else {
110 throw
111 std::runtime_error("TMVA::SOFIE - Softmax operator along the axis "
112 + std::to_string(fAttrAxis) + " with " + std::to_string(size)
113 + "d input tensor not supported.");
114 }
115
116 bool notBatch = axis != 0;
117 bool notChannel = axis != 1;
118 bool notDepth = (size == 5 && axis != 2);
119 bool notHeight = (size == 5 && axis != 3) || (size == 4 && axis != 2);
120 bool notWidth = (size == 5 && axis != 4) || (size == 4 && axis != 3) || (size == 3 && axis != 2);
121
122 if (notBatch) {
123 out << SP << "for (size_t n = 0; n < " << batch << " ; n++){\n";
124 }
125 if (notChannel) {
126 out << SP << SP << "for (size_t c = 0; c < " << channel << " ; c++){\n";
127 }
128 if (notDepth) {
129 out << SP << SP << "for (size_t d = 0; d < " << depth << " ; d++){\n";
130 }
131 if (notHeight) {
132 out << SP << SP << "for (size_t h = 0; h < " << height << " ; h++){\n";
133 }
134 if (notWidth) {
135 out << SP << SP << "for (size_t w = 0; w < " << width << " ; w++){\n";
136 }
137 out << SP << SP << SP << fType << " sum = 0.;\n";
138 out << SP << SP << SP << "size_t index = 0";
139 if (notBatch) {
140 out << " + n * " << bStride;
141 }
142 if (notChannel) {
143 out << "+ c * " << cStride;
144 }
145 if (notDepth) {
146 out << " + d * " << dStride;
147 }
148 if (notHeight) {
149 out << " + h * " << hStride;
150 }
151 if (notWidth) {
152 out << " + w";
153 }
154 out << ";\n";
155 // apply softmax along the axis - find first maximum value for numerical stability
156 if (N == 0)
157 throw std::runtime_error("TMVA::SOFIE - Softmax operator is along axis with zero elements");
158 out << SP << SP << SP << fType << " vmax = tensor_" << fNX << "[index];\n";
159 out << SP << SP << SP << "for (size_t i = 1; i < " << N << "; i++) {\n";
160 out << SP << SP << SP << SP << "if (tensor_" << fNX << "[index + i*" << iStride << "] > vmax)\n";
161 out << SP << SP << SP << SP << SP << "vmax = tensor_" << fNX << "[index + i*" << iStride << "];\n";
162 out << SP << SP << SP << "}\n";
163 out << SP << SP << SP << "for (size_t i = 0; i < " << N << "; i++) {\n";
164 out << SP << SP << SP << SP << "tensor_" << fNY << "[index + i*" << iStride << "] = std::exp(tensor_" << fNX
165 << "[index + i*" << iStride << "] - vmax);\n";
166 out << SP << SP << SP << SP << "sum += tensor_" << fNY << "[index + i*" << iStride << "];\n";
167 out << SP << SP << SP << "}\n";
168 out << SP << SP << SP << "for (size_t i = 0; i < " << N << "; i++) {\n";
169 out << SP << SP << SP << SP << "tensor_" << fNY << "[index + i*" << iStride << "] /= sum;\n";
170 out << SP << SP << SP << "}\n";
171 if (notWidth) {
172 out << SP << SP << "}\n"; // end w
173 }
174 if (notHeight) {
175 out << SP << SP << "}\n"; // end h
176 }
177 if (notDepth) {
178 out << SP << SP << "}\n"; // end d
179 }
180 if (notChannel) {
181 out << SP << SP << "}\n"; // end c
182 }
183 if (notBatch) {
184 out << SP << "}\n"; // end n
185 }
186 }
187 return out.str();
188 }
189};
190
191} // namespace SOFIE
192} // namespace Experimental
193} // namespace TMVA
194
195#endif // TMVA_SOFIE_ROPERATOR_Softmax
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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:227
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)
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:46
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:42
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:47
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