1#ifndef TMVA_SOFIE_ROPERATOR_Softmax
2#define TMVA_SOFIE_ROPERATOR_Softmax
11namespace Experimental {
29 :
fAttrAxis(attr_axis),
fNX(UTILITY::Clean_name(nameX)),
fNY(UTILITY::Clean_name(nameY))
45 throw std::runtime_error(
"TMVA SOFIE Softmax Op Input Tensor is not found in model");
57 OpName =
"op_" + OpName;
59 throw std::runtime_error(
"TMVA SOFIE Operator Softmax called to Generate without being initialized first");
61 std::stringstream out;
65 out <<
"\n" <<
SP <<
"//------ SOFTMAX - " <<
size <<
" " <<
length <<
" " << axis <<
"\n";
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";
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";
77 out <<
SP <<
"for (size_t i = 0; i < " <<
length <<
" ; i++){\n";
78 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] /= sum;\n";
82 size_t channel =
fShape[1];
86 size_t hStride =
width;
88 size_t cStride = depth * dStride;
89 size_t bStride = channel * cStride;
96 }
else if (axis == 1) {
99 }
else if (axis ==
size - 1) {
102 }
else if (
size > 3 && axis ==
size - 2) {
105 }
else if (
size == 5 && axis ==
size - 3) {
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.");
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);
122 out <<
SP <<
"for (size_t n = 0; n < " << batch <<
" ; n++){\n";
125 out <<
SP <<
SP <<
"for (size_t c = 0; c < " << channel <<
" ; c++){\n";
128 out <<
SP <<
SP <<
"for (size_t d = 0; d < " << depth <<
" ; d++){\n";
131 out <<
SP <<
SP <<
"for (size_t h = 0; h < " <<
height <<
" ; h++){\n";
134 out <<
SP <<
SP <<
"for (size_t w = 0; w < " <<
width <<
" ; w++){\n";
137 out <<
SP <<
SP <<
SP <<
"size_t index = 0";
139 out <<
" + n * " << bStride;
142 out <<
"+ c * " << cStride;
145 out <<
" + d * " << dStride;
148 out <<
" + h * " << hStride;
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";
171 out <<
SP <<
SP <<
"}\n";
174 out <<
SP <<
SP <<
"}\n";
177 out <<
SP <<
SP <<
"}\n";
180 out <<
SP <<
SP <<
"}\n";
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 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)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const std::vector< size_t > & GetTensorShape(std::string name)
std::vector< size_t > fShape
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
void Initialize(RModel &model)
ROperator_Softmax(int64_t attr_axis, std::string nameX, std::string nameY)
std::string Generate(std::string OpName)
const std::string SP
space used to correctly indent the generated C++ code
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