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