1#ifndef TMVA_SOFIE_ROPERATOR_LAYERNORMALIZATION
2#define TMVA_SOFIE_ROPERATOR_LAYERNORMALIZATION
12namespace Experimental {
83 if (!model.CheckIfTensorAlreadyExist(
fNX)) {
84 throw std::runtime_error(
"TMVA::SOFIE - LayerNormalization - Tensor " +
fNX +
" not found.");
138 for (
size_t i = 0; i <
fSize; i++) {
150 for (
size_t i = 0; i <
fSize; i++) {
156 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNX),
fShapeY);
157 if (model.Verbose()){
163 model.AddNeededStdLib(
"cmath");
167 model.AddNeededHelperFunction(
"UnidirectionalBroadcast");
172 std::stringstream out;
174 out <<
SP <<
"// Broadcasting the bias of LayerNormalization op\n";
176 out <<
SP <<
SP <<
"float* data = UTILITY::UnidirectionalBroadcast(tensor_";
179 out <<
SP <<
"delete[] data;\n";
189 throw std::runtime_error(
"TMVA::SOFIE LayerNormalization operator " +
opName +
190 " called to generate without being initialized first.");
193 std::stringstream out;
195 out <<
"//---- Layer Normalization operator " <<
opName <<
"\n";
200 for (
size_t i = 0; i <
fSize; i++) {
205 std::string
inputIndex =
"axis_0 * " + strides[0].GetVal();
206 for (
size_t i = 1; i <
fSize; i++) {
212 for (
size_t i = 0; i <
fSize; i++) {
223 for (
size_t i = 0; i <
fSize; i++) {
226 biasIndex +=
"axis_" + std::to_string(i);
234 for (
size_t i = 1; i <
fAxis; i++) {
241 out <<
SP <<
"// Compute the mean\n";
244 for (
size_t i = 0; i <
fAxis; i++) {
245 std::string
iIdx =
"axis_" + std::to_string(i);
247 <<
"; " <<
iIdx <<
"++) {\n";
249 out <<
SP <<
SP <<
fType <<
" mean = 0.;\n";
252 std::string
jIdx =
"axis_" + std::to_string(
j);
254 <<
"; " <<
jIdx <<
"++) {\n";
258 out <<
SP <<
SP <<
"}\n";
263 out <<
SP <<
"// Compute the inverse Standard Deviation\n";
266 out <<
SP <<
SP <<
fType <<
" sum = 0.;\n";
269 std::string
jIdx =
"axis_" + std::to_string(
j);
271 <<
"; " <<
jIdx <<
"++){\n";
273 out <<
SP <<
SP <<
SP <<
"float tmp = tensor_" <<
fNX <<
"[" <<
inputIndex <<
"] - mean;\n";
274 out <<
SP <<
SP <<
SP <<
"sum += tmp*tmp;\n";
276 out <<
SP <<
SP <<
"}\n";
278 out <<
SP <<
SP <<
fType <<
" invStdDev = 1 / std::sqrt(";
290 out <<
SP <<
"// Y = Scale o InvStdDev (X - Mean)\n";
293 std::string
jIdx =
"axis_" + std::to_string(
j);
308 out <<
SP <<
SP <<
"}\n";
311 for (
size_t i = 0; i <
fAxis; i++) {
318 std::vector<std::string>
GetBlasRoutines()
override {
return { std::string(
"Axpy") }; }
320 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
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::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 ConvertDimShapeToString(const std::vector< Dim > &shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
create variable transformations