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
14// implement Softmax and LogSoftmax
16
17private:
18 bool fLogSoftmax; // for the logsoftmax case
19 bool fUseVDT = false;
20 int64_t fAttrAxis;
21
22 std::string fNX;
23 std::string fNY;
24 std::vector<Dim> fShape;
25
26 std::string fType;
27
28public:
30 ROperator_Softmax(int64_t attr_axis, std::string nameX, std::string nameY, bool logSoftmax = false)
32 fAttrAxis(attr_axis), fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
33
34 {
37 }
38
39 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override { return input; }
40
41 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
42 auto ret = input; // suggest copy to compiler
43 return ret;
44 }
45
46 void Initialize(RModel& model) override {
47 if (model.CheckIfTensorAlreadyExist(fNX) ==
48 false) { // input must be a graph input, or already initialized intermediate tensor
49 throw std::runtime_error("TMVA SOFIE Softmax Op Input Tensor is not found in model");
50 }
54 if (model.Verbose()) {
55 std::cout << "Softmax -> " << fNY << " " << ConvertDimShapeToString(fShape) << std::endl;
56 }
57 fUseVDT = model.UseVDT();
58 if (fUseVDT) {
59 model.AddNeededCustomHeader("vdt/exp.h");
60 if (fLogSoftmax)
61 model.AddNeededCustomHeader("vdt/log.h");
62 }
63 }
64
65 std::string Generate(std::string OpName) override {
66 OpName = "op_" + OpName;
67 if (fShape.empty()) {
68 throw std::runtime_error("TMVA SOFIE Operator Softmax called to Generate without being initialized first");
69 }
70 std::stringstream out;
71 size_t size = fShape.size();
73 size_t axis = fAttrAxis < 0 ? size + fAttrAxis : fAttrAxis;
74
75 std::string expFunction = (fUseVDT) ? "vdt::fast_expf" : "std::exp";
76 std::string logFunction = (fUseVDT) ? "vdt::fast_logf" : "std::log";
77
78 // Check if this is the special case where memory is contiguous.
79 if (axis == size - 1) {
80 std::string axis_size = fShape[axis].GetVal();
81 std::string num_rows;
83 num_rows = std::to_string(std::stoul(length_str) / std::stoul(axis_size));
84 } else {
85 num_rows = "(" + length_str + ") / (" + axis_size + ")";
86 }
87
88 out << "\n" << SP << "//------ SOFTMAX - " << size << " " << length_str << " " << axis << "\n";
89 out << SP << "for (int i = 0; i < " << num_rows << "; ++i) {\n";
90 out << SP << SP << "size_t offset = i * " << axis_size << ";\n";
91 out << SP << SP << fType << " const * x_ptr = &tensor_" << fNX << "[offset];\n";
92 out << SP << SP << fType << " * y_ptr = &tensor_" << fNY << "[offset];\n";
93
94 out << SP << SP << fType << " vmax = x_ptr[0];\n";
95 out << SP << SP << "for (int j = 1; j < " << axis_size << "; ++j) {\n";
96 out << SP << SP << SP << "if (x_ptr[j] > vmax) vmax = x_ptr[j];\n";
97 out << SP << SP << "}\n";
98
99 out << SP << SP << fType << " sum = 0.0;\n";
100 out << SP << SP << "for (int j = 0; j < " << axis_size << "; ++j) {\n";
101 out << SP << SP << SP << "y_ptr[j] = " << expFunction << "(x_ptr[j] - vmax);\n";
102 out << SP << SP << SP << "sum += y_ptr[j];\n";
103 out << SP << SP << "}\n";
104
105 out << SP << SP << fType << " inv_sum = 1.0f / sum;\n";
106 out << SP << SP << "for (int j = 0; j < " << axis_size << "; ++j) {\n";
107 out << SP << SP << SP << "y_ptr[j] *= inv_sum;\n";
108 if (fLogSoftmax)
109 out << SP << SP << SP << "y_ptr[j] = " << logFunction << "(y_ptr[j]);\n";
110 out << SP << SP << "}\n";
111 out << SP << "}\n";
112
113 } else {
115 size_t k = 0;
116 std::vector<std::string> l(size);
117 for (size_t i = 0; i < size; i++) {
118 if (i != axis) {
119 for (size_t j = 0; j < k; j++) out << SP;
120 l[i] = std::string("i") + std::to_string(i);
121 out << "for (int " << l[i] << " = 0; " << l[i] << " < " << fShape[i] << "; " << l[i] << "++) {\n";
122 k++;
123 }
124 }
125 for (size_t j = 0; j < size-1; j++) out << SP;
126 out << fType << " sum = 0.;\n";
127 for (size_t j = 0; j < size-1; j++) out << SP;
128 out << "size_t index = ";
129 bool first = true;
130 for (size_t i = 0; i < size; i++) {
131 if (i == axis) continue;
132 if (!first) out << " + ";
133 if (stride[i].GetVal() != "1")
134 out << stride[i] << "*";
135 out << l[i];
136 first = false;
137 }
138 out << ";\n";
139 // find maximum looping along reduced axis
140 for (size_t j = 0; j < size-1; j++) out << SP;
141 out << fType << " vmax = tensor_" << fNX << "[index];\n";
142 for (size_t j = 0; j < size-1; j++) out << SP;
143 out << "for (int i = 1; i < " << fShape[axis] << "; i++) {\n";
144 for (size_t j = 0; j < size; j++) out << SP;
145 out << fType << " x = tensor_" << fNX << "[index + i";
146 if (stride[axis].GetVal() != "1") out << "*(" << stride[axis] << ")";
147 out << "];\n";
148 for (size_t j = 0; j < size; j++) out << SP;
149 out << "if (x > vmax) vmax = x;\n";
150 for (size_t j = 0; j < size-1; j++) out << SP;
151 out << "}\n";
152 // compute softmax
153 for (size_t j = 0; j < size-1; j++) out << SP;
154 out << "for (int i = 0; i < " << fShape[axis] << "; i++) {\n";
155 for (size_t j = 0; j < size; j++) out << SP;
156 out << "size_t id = index + i";
157 if (stride[axis].GetVal() != "1") out << "*(" << stride[axis] << ")";
158 out << ";\n";
159 for (size_t j = 0; j < size; j++) out << SP;
160 out << "tensor_" << fNY << "[id] = " << expFunction << "(tensor_" << fNX << "[id] - vmax);\n";
161 for (size_t j = 0; j < size; j++) out << SP;
162 out << "sum += tensor_" << fNY << "[id];\n";
163 for (size_t j = 0; j < size-1; j++) out << SP;
164 out << "}\n";
165 // normalize
166 for (size_t j = 0; j < size-1; j++) out << SP;
167 out << "for (int i = 0; i < " << fShape[axis] << "; i++) {\n";
168 for (size_t j = 0; j < size; j++) out << SP;
169 out << "size_t id = index + i";
170 if (stride[axis].GetVal() != "1") out << "*(" << stride[axis] << ");\n";
171 for (size_t j = 0; j < size; j++) out << SP;
172 out << "tensor_" << fNY << "[id] /= sum;\n";
173 if (fLogSoftmax) {
174 for (size_t j = 0; j < size; j++) out << SP;
175 out << "tensor_" << fNY << "[id] = " << logFunction << "(tensor_" << fNY << "[id]);\n";
176 }
177 for (size_t j = 0; j < size-1; j++) out << SP;
178 out << "}\n";
179 //end loops
180 for (int i = static_cast<int>(k) - 1; i >= 0; i--) {
181 for (int j = 0; j < i; j++) out << SP;
182 out << "}\n";
183 }
184 }
185 return out.str();
186 }
187 std::vector<std::string> GetStdLibs() override { return { std::string("cmath") }; }
188};
189
190} // namespace SOFIE
191} // namespace Experimental
192} // namespace TMVA
193
194#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.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
void AddNeededCustomHeader(std::string filename)
std::vector< Dim > GetDimTensorShape(const std::string &name) const
Definition RModel.cxx:87
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:284
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:144
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:112
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::string Generate(std::string OpName) override
std::vector< std::string > GetStdLibs() override
ROperator_Softmax(int64_t attr_axis, std::string nameX, std::string nameY, bool logSoftmax=false)
std::vector< std::string_view > fInputTensorNames
Definition ROperator.hxx:50
const std::string SP
space used to correctly indent the generated C++ code
Definition ROperator.hxx:45
std::vector< std::string_view > fOutputTensorNames
Definition ROperator.hxx:51
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
bool IsInteger(const std::string &s)
create variable transformations
TLine l
Definition textangle.C:4