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 out << "///------- Softmax " << opName << " ---> " // << fNY << " "
72 << ConvertDimShapeToString(fShape) << "\n" << std::endl;
73 size_t size = fShape.size();
75 size_t axis = fAttrAxis < 0 ? size + fAttrAxis : fAttrAxis;
76
77 std::string expFunction = (fUseVDT) ? "vdt::fast_expf" : "std::exp";
78 std::string logFunction = (fUseVDT) ? "vdt::fast_logf" : "std::log";
79
80 // Check if this is the special case where memory is contiguous.
81 if (axis == size - 1) {
82 std::string axis_size = fShape[axis].GetVal();
83 std::string num_rows;
85 num_rows = std::to_string(std::stoul(length_str) / std::stoul(axis_size));
86 } else {
87 num_rows = "(" + length_str + ") / (" + axis_size + ")";
88 }
89
90 out << SP << "//----- softmax axis is last one - " << axis << "\n";
91 out << SP << "for (int i = 0; i < " << num_rows << "; ++i) {\n";
92 out << SP << SP << "size_t offset = i * " << axis_size << ";\n";
93 out << SP << SP << fType << " const * x_ptr = &tensor_" << fNX << "[offset];\n";
94 out << SP << SP << fType << " * y_ptr = &tensor_" << fNY << "[offset];\n";
95
96 out << SP << SP << fType << " vmax = x_ptr[0];\n";
97 out << SP << SP << "for (int j = 1; j < " << axis_size << "; ++j) {\n";
98 out << SP << SP << SP << "if (x_ptr[j] > vmax) vmax = x_ptr[j];\n";
99 out << SP << SP << "}\n";
100
101 out << SP << SP << fType << " sum = 0.0;\n";
102 out << SP << SP << "for (int j = 0; j < " << axis_size << "; ++j) {\n";
103 out << SP << SP << SP << "y_ptr[j] = " << expFunction << "(x_ptr[j] - vmax);\n";
104 out << SP << SP << SP << "sum += y_ptr[j];\n";
105 out << SP << SP << "}\n";
106
107 out << SP << SP << fType << " inv_sum = 1.0f / sum;\n";
108 out << SP << SP << "for (int j = 0; j < " << axis_size << "; ++j) {\n";
109 out << SP << SP << SP << "y_ptr[j] *= inv_sum;\n";
110 if (fLogSoftmax)
111 out << SP << SP << SP << "y_ptr[j] = " << logFunction << "(y_ptr[j]);\n";
112 out << SP << SP << "}\n";
113 out << SP << "}\n";
114
115 } else {
116 // generic case for any axis
118 size_t k = 0;
119 std::vector<std::string> l(size);
120 for (size_t i = 0; i < size; i++) {
121 if (i != axis) {
122 for (size_t j = 0; j < k; j++) out << SP;
123 l[i] = std::string("i") + std::to_string(i);
124 out << SP << "for (int " << l[i] << " = 0; " << l[i] << " < " << fShape[i] << "; " << l[i] << "++) {\n";
125 k++;
126 }
127 }
128 for (size_t j = 0; j < size-1; j++) out << SP;
129 out << fType << " sum = 0.;\n";
130 for (size_t j = 0; j < size-1; j++) out << SP;
131 out << "size_t index = ";
132 bool first = true;
133 for (size_t i = 0; i < size; i++) {
134 if (i == axis) continue;
135 if (!first) out << " + ";
136 if (stride[i].GetVal() != "1")
137 out << stride[i] << "*";
138 out << l[i];
139 first = false;
140 }
141 out << ";\n";
142 // find maximum looping along reduced axis
143 for (size_t j = 0; j < size-1; j++) out << SP;
144 out << fType << " vmax = tensor_" << fNX << "[index];\n";
145 for (size_t j = 0; j < size-1; j++) out << SP;
146 out << "for (int i = 1; i < " << fShape[axis] << "; i++) {\n";
147 for (size_t j = 0; j < size; j++) out << SP;
148 out << fType << " x = tensor_" << fNX << "[index + i";
149 if (stride[axis].GetVal() != "1") out << "*(" << stride[axis] << ")";
150 out << "];\n";
151 for (size_t j = 0; j < size; j++) out << SP;
152 out << "if (x > vmax) vmax = x;\n";
153 for (size_t j = 0; j < size-1; j++) out << SP;
154 out << "}\n";
155 // compute softmax
156 for (size_t j = 0; j < size-1; j++) out << SP;
157 out << "for (int i = 0; i < " << fShape[axis] << "; i++) {\n";
158 for (size_t j = 0; j < size; j++) out << SP;
159 out << "size_t id = index + i";
160 if (stride[axis].GetVal() != "1") out << "*(" << stride[axis] << ")";
161 out << ";\n";
162 for (size_t j = 0; j < size; j++) out << SP;
163 out << "tensor_" << fNY << "[id] = " << expFunction << "(tensor_" << fNX << "[id] - vmax);\n";
164 for (size_t j = 0; j < size; j++) out << SP;
165 out << "sum += tensor_" << fNY << "[id];\n";
166 for (size_t j = 0; j < size-1; j++) out << SP;
167 out << "}\n";
168 // normalize
169 for (size_t j = 0; j < size-1; j++) out << SP;
170 out << "for (int i = 0; i < " << fShape[axis] << "; i++) {\n";
171 for (size_t j = 0; j < size; j++) out << SP;
172 out << "size_t id = index + i";
173 if (stride[axis].GetVal() != "1") out << "*(" << stride[axis] << ")";
174 out << ";\n";
175 for (size_t j = 0; j < size; j++) out << SP;
176 out << "tensor_" << fNY << "[id] /= sum;\n";
177 if (fLogSoftmax) {
178 for (size_t j = 0; j < size; j++) out << SP;
179 out << "tensor_" << fNY << "[id] = " << logFunction << "(tensor_" << fNY << "[id]);\n";
180 }
181 for (size_t j = 0; j < size-1; j++) out << SP;
182 out << "}\n";
183 //end loops
184 for (int i = static_cast<int>(k) - 1; i >= 0; i--) {
185 for (int j = 0; j < i; j++) out << SP;
186 out << "}\n";
187 }
188 }
189 return out.str();
190 }
191 std::vector<std::string> GetStdLibs() override { return { std::string("cmath") }; }
192};
193
194} // namespace SOFIE
195} // namespace Experimental
196} // namespace TMVA
197
198#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:100
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
Definition RModel.cxx:301
bool CheckIfTensorAlreadyExist(std::string tensor_name)
Definition RModel.cxx:157
ETensorType GetTensorType(std::string name) const
Definition RModel.cxx:125
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::vector< std::string > GetStdLibs() override
std::string Generate(std::string opName) 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