1#ifndef TMVA_SOFIE_ROPERATOR_LAYERNORMALIZATION
2#define TMVA_SOFIE_ROPERATOR_LAYERNORMALIZATION
11namespace Experimental {
82 throw std::runtime_error(
"TMVA::SOFIE - Tensor " +
fNX +
" not found.");
145 std::stringstream out;
147 out <<
SP <<
"// Broadcasting the bias of LayerNormalization op\n";
149 out <<
SP <<
SP <<
"float* data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<float>(tensor_";
152 out <<
SP <<
"delete[] data;\n";
162 throw std::runtime_error(
"TMVA::SOFIE LayerNormalization operator " +
OpName +
163 " called to generate without being initialized first.");
166 throw std::runtime_error(
"TMVA::SOFIE LayerNormalization operator not "
167 "implemented for input tensor of size > 5.");
170 std::stringstream out;
172 out <<
"//---- Layer Normalization operator " <<
OpName <<
"\n";
175 out <<
SP <<
"std::vector<size_t> " <<
OpName <<
"_InputShape ({";
176 for (
size_t i = 0; i <
fSize; i++) {
186 std::string
InputIndex =
"axis_0 * " + strides[0].GetVal();
187 for (
size_t i = 1; i <
fSize; i++) {
188 InputIndex +=
" + axis_" + std::to_string(i) +
" * " + strides[i].GetVal();
193 for (
size_t i = 1; i <
fAxis; i++) {
205 out <<
SP <<
"for (size_t i = 0; i < " <<
fLength <<
"; i++) {\n";
206 out <<
SP <<
SP <<
"tensor_" <<
fNCastedX <<
"[i] = " <<
"static_cast<float>(tensor_" <<
fNX;
211 out <<
SP <<
"// Compute the mean\n";
213 for (
size_t i = 0; i <
fAxis; i++) {
214 std::string
iIdx =
"axis_" + std::to_string(i);
216 out <<
"[" << i <<
"]; " <<
iIdx <<
"++) {\n";
218 out <<
SP <<
SP <<
fType <<
" sum = 0.;\n";
221 std::string
jIdx =
"axis_" + std::to_string(
j);
223 out <<
"[" <<
j <<
"]; " <<
jIdx <<
"++) {\n";
227 out <<
SP <<
SP <<
"}\n";
235 out <<
SP <<
"// Compute the inverse Standard Deviation\n";
237 for (
size_t i = 0; i <
fAxis; i++) {
238 std::string
iIdx =
"axis_" + std::to_string(i);
240 out <<
"[" << i <<
"]; " <<
iIdx <<
"++){\n";
243 out <<
SP <<
SP <<
fType <<
" sum = 0.;\n";
246 std::string
jIdx =
"axis_" + std::to_string(
j);
248 out <<
"[" <<
j <<
"]; " <<
jIdx <<
"++){\n";
250 out <<
SP <<
SP <<
SP <<
"sum += std::pow(tensor_" <<
fNX <<
"[" <<
InputIndex <<
"] - tensor_";
253 out <<
SP <<
SP <<
"}\n";
257 for (
size_t i = 0; i <
fAxis; i++) {
262 out <<
"// NormalizedX = InvStdDev * (CastedX - Mean)\n";
263 for (
size_t i = 0; i <
fAxis; i++) {
264 std::string
iIdx =
"axis_" + std::to_string(i);
266 out <<
"[" << i <<
"]; " <<
iIdx <<
"++){\n";
269 std::string
jIdx =
"axis_" + std::to_string(
j);
271 out <<
"[" <<
j <<
"]; " <<
jIdx <<
"++){\n";
277 out <<
SP <<
SP <<
"}\n";
282 out <<
"// Y = Scale o NormalizedX";
283 for (
size_t i = 0; i <
fAxis; i++) {
284 std::string
iIdx =
"axis_" + std::to_string(i);
286 out <<
"[" << i <<
"]; " <<
iIdx <<
"++){\n";
289 std::string
jIdx =
"axis_" + std::to_string(
j);
291 out <<
"[" <<
j <<
"]; " <<
jIdx <<
"++){\n";
297 out <<
SP <<
SP <<
"}\n";
303 out <<
SP <<
"// Y = Scale o InvStdDev (X - Mean)\n";
304 for (
size_t i = 0; i <
fAxis; i++) {
305 std::string
iIdx =
"axis_" + std::to_string(i);
307 out <<
"[" << i <<
"]; " <<
iIdx <<
"++){\n";
310 std::string
jIdx =
"axis_" + std::to_string(
j);
312 out <<
"[" <<
j <<
"]; " <<
jIdx <<
"++){\n";
319 out <<
SP <<
SP <<
"}\n";
328 out <<
SP <<
"// Add the bias to Y\n";
330 out <<
SP <<
"float " <<
OpName <<
"_alpha = 1.;\n";
331 out <<
SP <<
"int " <<
OpName <<
"_inc = 1;\n";
332 out <<
SP <<
"BLAS::saxpy_(&" <<
OpName <<
"_n, &" <<
OpName <<
"_alpha, " <<
Bias <<
", &";
333 out <<
OpName <<
"_inc, " <<
"tensor_" <<
fNY <<
", &" <<
OpName <<
"_inc);\n";
339 std::vector<std::string>
GetBlasRoutines()
override {
return { std::string(
"Axpy") }; }
341 std::vector<std::string>
GetStdLibs()
override {
return { std::string(
"cmath") }; }
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 Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
void AddNeededStdLib(std::string libname)
const ETensorType & GetTensorType(std::string name)
bool IsDynamicTensor(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
std::vector< Dim > GetDynamicTensorShape(std::string name)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const std::vector< size_t > & GetTensorShape(std::string name)
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::string fNormalizedLength
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::vector< size_t > fShapeB
std::string Generate(std::string OpName) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::string fNBroadcastedB
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
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 ConvertDynamicShapeToLength(std::vector< Dim > shape)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
ETensorType ConvertStringToType(std::string type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations