1#ifndef TMVA_SOFIE_ROPERATOR_INSTANCENORMALIZATION
2#define TMVA_SOFIE_ROPERATOR_INSTANCENORMALIZATION
12namespace Experimental {
60 throw std::runtime_error(
"TMVA::SOFIE - InstanceNormalization - Tensor " +
name +
" not found.");
66 throw std::runtime_error(
"TMVA::SOFIE - InstanceNormalization - Input tensor " +
fNInput +
" has rank " +
67 std::to_string(
fShape.size()) +
" but at least rank 3 (N, C, D1, ...) is required.");
74 if (shape.size() != 1 || shape[0] !=
channels) {
75 throw std::runtime_error(
"TMVA::SOFIE - InstanceNormalization - Tensor " +
name +
" has invalid shape " +
88 throw std::runtime_error(
"TMVA::SOFIE - InstanceNormalization called to generate without being initialized.");
91 const size_t batchSize =
fShape[0];
94 for (
size_t i = 2; i <
fShape.size(); ++i)
97 std::stringstream out;
98 out <<
"\n" <<
SP <<
"//---- InstanceNormalization " <<
fNOutput <<
"\n";
99 out <<
SP <<
"for (size_t n = 0; n < " << batchSize <<
"; n++) {\n";
100 out <<
SP <<
SP <<
"for (size_t c = 0; c < " <<
channels <<
"; c++) {\n";
106 out <<
SP <<
SP <<
SP <<
"for (size_t i = 0; i < " <<
spatialSize <<
"; i++) {\n";
107 out <<
SP <<
SP <<
SP <<
SP <<
"mean += tensor_" <<
fNInput <<
"[offset + i];\n";
108 out <<
SP <<
SP <<
SP <<
"}\n";
113 out <<
SP <<
SP <<
SP <<
"for (size_t i = 0; i < " <<
spatialSize <<
"; i++) {\n";
114 out <<
SP <<
SP <<
SP <<
SP <<
fType <<
" tmp = tensor_" <<
fNInput <<
"[offset + i] - mean;\n";
115 out <<
SP <<
SP <<
SP <<
SP <<
"sum += tmp * tmp;\n";
116 out <<
SP <<
SP <<
SP <<
"}\n";
118 << std::to_string(
fEpsilon) <<
");\n";
123 out <<
SP <<
SP <<
SP <<
"for (size_t i = 0; i < " <<
spatialSize <<
"; i++) {\n";
125 <<
"[offset + i] - mean) * invStdDev + bias;\n";
126 out <<
SP <<
SP <<
SP <<
"}\n";
128 out <<
SP <<
SP <<
"}\n";
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
std::vector< size_t > GetTensorShape(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
ETensorType GetTensorType(std::string name) const
Implementation of the ONNX InstanceNormalization operator.
void Initialize(RModel &model) override
ROperator_InstanceNormalization()
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > inputShapes) override
ROperator_InstanceNormalization(float epsilon, std::string nameInput, std::string nameScale, std::string nameBias, std::string nameOutput)
std::string Generate(std::string) override
std::vector< size_t > fShape
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::string ConvertTypeToString(ETensorType type)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations