1#ifndef TMVA_SOFIE_ROPERATOR_Softmax
2#define TMVA_SOFIE_ROPERATOR_Softmax
11namespace Experimental {
46 throw std::runtime_error(
"TMVA SOFIE Softmax Op Input Tensor is not found in model");
60 throw std::runtime_error(
"TMVA SOFIE Operator Softmax called to Generate without being initialized first");
62 std::stringstream out;
66 out <<
"\n" <<
SP <<
"//------ SOFTMAX - " <<
size <<
" " <<
length <<
" " << axis <<
"\n";
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";
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";
78 out <<
SP <<
"for (size_t i = 0; i < " <<
length <<
" ; i++){\n";
79 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] /= sum;\n";
83 size_t channel =
fShape[1];
97 }
else if (axis == 1) {
100 }
else if (axis ==
size - 1) {
103 }
else if (
size > 3 && axis ==
size - 2) {
106 }
else if (
size == 5 && axis ==
size - 3) {
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.");
120 bool notWidth = (
size == 5 && axis != 4) || (
size == 4 && axis != 3) || (
size == 3 && axis != 2);
123 out <<
SP <<
"for (size_t n = 0; n < " <<
batch <<
" ; n++){\n";
126 out <<
SP <<
SP <<
"for (size_t c = 0; c < " << channel <<
" ; c++){\n";
129 out <<
SP <<
SP <<
"for (size_t d = 0; d < " <<
depth <<
" ; d++){\n";
132 out <<
SP <<
SP <<
"for (size_t h = 0; h < " <<
height <<
" ; h++){\n";
135 out <<
SP <<
SP <<
"for (size_t w = 0; w < " <<
width <<
" ; w++){\n";
138 out <<
SP <<
SP <<
SP <<
"size_t index = 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";
172 out <<
SP <<
SP <<
"}\n";
175 out <<
SP <<
SP <<
"}\n";
178 out <<
SP <<
SP <<
"}\n";
181 out <<
SP <<
SP <<
"}\n";
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.
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)
void Initialize(RModel &model) override
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)
ROperator_Softmax(int64_t attr_axis, std::string nameX, std::string nameY)
std::string Generate(std::string OpName)
std::vector< std::string_view > fInputTensorNames
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
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