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");
54 OpName =
"op_" + OpName;
56 throw std::runtime_error(
"TMVA SOFIE Operator Softmax called to Generate without being initialized first");
58 std::stringstream out;
62 out <<
"\n" <<
SP <<
"//------ SOFTMAX\n";
64 out <<
SP <<
fType <<
" sum = 0.0;\n";
65 out <<
SP <<
"for (size_t i = 0; i < " <<
length <<
" ; i++){\n";
66 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] = std::exp(tensor_" <<
fNX <<
"[i]);\n";
67 out <<
SP <<
SP <<
"sum += tensor_" <<
fNY <<
"[i];\n";
69 out <<
SP <<
"for (size_t i = 0; i < " <<
length <<
" ; i++){\n";
70 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] /= sum;\n";
74 size_t channel =
fShape[1];
78 size_t hStride =
width;
80 size_t cStride = depth * dStride;
81 size_t bStride = channel * cStride;
88 }
else if (axis == 1) {
91 }
else if (axis ==
size - 1) {
94 }
else if (
size > 3 && axis ==
size - 2) {
97 }
else if (
size == 5 && axis ==
size - 3) {
102 std::runtime_error(
"TMVA::SOFIE - Softmax operator along the axis "
103 + std::to_string(
fAttrAxis) +
" with " + std::to_string(
size)
104 +
"d input tensor not supported.");
107 bool notBatch = axis != 0;
108 bool notChannel = axis != 1;
109 bool notDepth = (
size == 5 && axis != 2);
110 bool notHeight = (
size == 5 && axis != 3) || (
size == 4 && axis != 2);
111 bool notWidth = (
size == 5 && axis != 4) || (
size == 4 && axis != 3) || (
size == 3 && axis != 2);
114 out <<
SP <<
"for (size_t n = 0; n < " << batch <<
" ; n++){\n";
117 out <<
SP <<
SP <<
"for (size_t c = 0; c < " << channel <<
" ; c++){\n";
120 out <<
SP <<
SP <<
"for (size_t d = 0; d < " << depth <<
" ; d++){\n";
123 out <<
SP <<
SP <<
"for (size_t h = 0; h < " <<
height <<
" ; h++){\n";
126 out <<
SP <<
SP <<
"for (size_t w = 0; w < " <<
width <<
" ; w++){\n";
129 out <<
SP <<
SP <<
SP <<
"size_t index = 0";
131 out <<
"+ n * " << bStride;
134 out <<
"+ c * " << cStride;
137 out <<
"+ d * " << dStride;
140 out <<
"+ h * " << hStride;
147 out <<
SP <<
SP <<
SP <<
"for (size_t i = 0; i < " <<
N <<
"; i++) {\n";
148 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[index + i*" << iStride <<
"] = std::exp(tensor_" <<
fNX
149 <<
"[index + i*" << iStride <<
"]);\n";
150 out <<
SP <<
SP <<
SP <<
SP <<
"sum += tensor_" <<
fNY <<
"[index + i*" << iStride <<
"];\n";
151 out <<
SP <<
SP <<
SP <<
"}\n";
152 out <<
SP <<
SP <<
SP <<
"for (size_t i = 0; i < " <<
N <<
"; i++) {\n";
153 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[index + i*" << iStride <<
"] /= sum;\n";
154 out <<
SP <<
SP <<
SP <<
"}\n";
156 out <<
SP <<
SP <<
"}\n";
159 out <<
SP <<
SP <<
"}\n";
162 out <<
SP <<
SP <<
"}\n";
165 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 ConvertTypeToString(ETensorType type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations