1#ifndef TMVA_SOFIE_ROPERATOR_LAYERNORMALIZATION
2#define TMVA_SOFIE_ROPERATOR_LAYERNORMALIZATION
12namespace Experimental {
58 const std::string &nameScale,
const std::string &nameB,
const std::string &nameY,
59 const std::string &nameMean,
const std::string &nameInvStdDev)
61 fNScale(UTILITY::Clean_name(nameScale)),
fNB(UTILITY::Clean_name(nameB)),
62 fNY(UTILITY::Clean_name(nameY)),
fNMean(UTILITY::Clean_name(nameMean)),
fNInvStdDev(UTILITY::Clean_name(nameInvStdDev))
79 if (!model.CheckIfTensorAlreadyExist(
fNX)) {
80 throw std::runtime_error(
"TMVA::SOFIE - LayerNormalization - Tensor " +
fNX +
" not found.");
82 bool isDynamic = model.IsDynamicTensor(
fNX);
90 const int rank =
static_cast<int>(
fSize);
91 if (fAttrAxis < -rank || fAttrAxis >= rank)
92 throw std::runtime_error(
"TMVA::SOFIE - LayerNormalization - axis " + std::to_string(
fAttrAxis) +
93 " is out of range for input of rank " + std::to_string(
fSize));
134 if (dimScale <
fSize) {
135 for (
size_t i = 0; i <
fSize-dimScale; i++)
139 for (
size_t i = 0; i <
fSize; i++) {
148 for (
size_t i = 0; i <
fSize-dimB; i++)
151 for (
size_t i = 0; i <
fSize; i++) {
157 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNX),
fShapeY);
158 if (model.Verbose()){
164 model.AddNeededStdLib(
"cmath");
168 model.AddNeededHelperFunction(
"UnidirectionalBroadcast");
173 std::stringstream out;
175 out <<
SP <<
"// Broadcasting the bias of LayerNormalization op\n";
177 out <<
SP <<
SP <<
"float* data = UTILITY::UnidirectionalBroadcast(tensor_";
180 out <<
SP <<
"delete[] data;\n";
188 opName =
"op_" + opName;
190 throw std::runtime_error(
"TMVA::SOFIE LayerNormalization operator " + opName +
191 " called to generate without being initialized first.");
194 std::stringstream out;
196 out <<
"//---- Layer Normalization operator " << opName <<
"\n";
199 std::vector<std::string> inputShape(
fSize);
201 for (
size_t i = 0; i <
fSize; i++) {
202 inputShape[i] =
fShapeX[i].GetVal();
206 std::string inputIndex =
"axis_0 * " + strides[0].GetVal();
207 for (
size_t i = 1; i <
fSize; i++) {
208 inputIndex +=
" + axis_" + std::to_string(i);
209 if (i <
fSize-1) inputIndex +=
" * " + strides[i].GetVal();
212 std::string scaleIndex;
213 for (
size_t i = 0; i <
fSize; i++) {
215 if (!scaleIndex.empty()) scaleIndex +=
" + ";
216 scaleIndex +=
"axis_" + std::to_string(i);
217 if ( scaleStrides[i].dim != 1) scaleIndex +=
" * " + scaleStrides[i].GetVal();
220 if (scaleIndex.empty()) scaleIndex =
"0";
223 std::string biasIndex;
226 for (
size_t i = 0; i <
fSize; i++) {
228 if (!biasIndex.empty()) biasIndex +=
" + ";
229 biasIndex +=
"axis_" + std::to_string(i);
230 if ( biasStrides[i].dim != 1) biasIndex +=
" * " + biasStrides[i].GetVal();
234 if (biasIndex.empty()) biasIndex =
"0";
238 std::string axesIndex =
"0";
241 axesIndex =
"axis_0 * " + axesStrides[0].GetVal();
242 for (
size_t i = 1; i <
fAxis; i++) {
243 axesIndex +=
" + axis_" + std::to_string(i) +
" * " + axesStrides[i].GetVal();
254 out <<
SP <<
"// Compute the mean\n";
257 for (
size_t i = 0; i <
fAxis; i++) {
258 std::string iIdx =
"axis_" + std::to_string(i);
259 out <<
SP <<
"for (size_t " << iIdx <<
" = 0; " << iIdx <<
" < " << inputShape[i]
260 <<
"; " << iIdx <<
"++) {\n";
262 out <<
SP <<
SP <<
fType <<
" mean = 0.;\n";
265 std::string jIdx =
"axis_" + std::to_string(j);
266 out <<
SP <<
SP <<
"for (size_t " << jIdx <<
" = 0; " << jIdx <<
" < " << inputShape[j]
267 <<
"; " << jIdx <<
"++) {\n";
269 out <<
SP <<
SP <<
SP <<
"mean += tensor_" <<
fNX <<
"[" << inputIndex <<
"];\n";
271 out <<
SP <<
SP <<
"}\n";
276 out <<
SP <<
"// Compute the inverse Standard Deviation\n";
279 out <<
SP <<
SP <<
fType <<
" sum = 0.;\n";
282 std::string jIdx =
"axis_" + std::to_string(j);
283 out <<
SP <<
SP <<
"for (size_t " << jIdx <<
" = 0; " << jIdx <<
" < " << inputShape[j]
284 <<
"; " << jIdx <<
"++){\n";
286 out <<
SP <<
SP <<
SP <<
"float tmp = tensor_" <<
fNX <<
"[" << inputIndex <<
"] - mean;\n";
287 out <<
SP <<
SP <<
SP <<
"sum += tmp*tmp;\n";
289 out <<
SP <<
SP <<
"}\n";
291 out <<
SP <<
SP <<
fType <<
" invStdDev = 1 / std::sqrt(";
297 out <<
SP <<
SP <<
"tensor_" <<
fNMean <<
"[" << axesIndex <<
"] = mean;\n";
299 out <<
SP <<
SP <<
"tensor_" <<
fNInvStdDev <<
"[" << axesIndex <<
"] = invStdDev;\n";
303 out <<
SP <<
"// Y = Scale o InvStdDev (X - Mean)\n";
306 std::string jIdx =
"axis_" + std::to_string(j);
307 out <<
SP <<
SP <<
"for (size_t " << jIdx <<
" = 0; " << jIdx <<
" < " << inputShape[j] <<
"; " << jIdx
310 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[" << inputIndex <<
"] = tensor_" <<
fNScale;
311 out <<
"[" << scaleIndex <<
"] * invStdDev * (tensor_" <<
fNX <<
"[" << inputIndex <<
"] - mean)";
316 out <<
" + tensor_" <<
fNB <<
"[" << biasIndex <<
"]";
321 out <<
SP <<
SP <<
"}\n";
324 for (
size_t i = 0; i <
fAxis; i++) {
332 std::vector<std::string>
GetBlasRoutines()
override {
return { std::string(
"Axpy") }; }
334 std::vector<std::string>
GetStdLibs()
override {
return { std::string(
"cmath") }; }
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 Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
std::vector< std::string > GetBlasRoutines() override
std::vector< Dim > fShapeX
ROperator_LayerNormalization()
std::vector< Dim > fShapeInvStdDev
ROperator_LayerNormalization(int axis, float epsilon, size_t stashType, const std::string &nameX, const std::string &nameScale, const std::string &nameB, const std::string &nameY, const std::string &nameMean, const std::string &nameInvStdDev)
std::vector< Dim > fShapeB
std::string fNormalizedLength
std::string Generate(std::string opName) override
std::vector< std::string > GetStdLibs() override
std::string GenerateInitCode() override
std::vector< Dim > fNormalizedShape
std::vector< Dim > fShapeScale
void Initialize(RModel &model) override
std::vector< Dim > fShapeY
std::string fNBroadcastedB
std::vector< Dim > fShapeMean
std::string fNNormalizedX
std::vector< Dim > fAxesShape
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::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)
create variable transformations