1#ifndef TMVA_SOFIE_ROPERATOR_BatchNormalization
2#define TMVA_SOFIE_ROPERATOR_BatchNormalization
13namespace Experimental{
57 if(std::is_same<T, float>::value){
62 std::runtime_error(
"TMVA SOFIE Encountered unsupported type parsing a BatchNormalization operator");
73 if (
input.size() != 5 ) {
75 std::runtime_error(
"TMVA SOFIE BatchNormalization Op Shape inference need 5 input tensors");
77 for(
size_t i = 0; i <
input.size(); i++) {
80 std::runtime_error(
"TMVA SOFIE BatchNormalization Op Shape inference only accept tensor with 4 dimensions");
91 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNX +
" fnx is not found in model");
95 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNScale +
" fns is not found in model");
99 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNB +
" fnb is not found in model");
103 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNMean +
" fnm is not found in model");
107 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNVar +
" fnv is not found in model");
125 throw std::runtime_error(
"TMVA SOFIE BatchNormalization 'scale' tensor must be 1D (per-channel).");
129 if (
fType ==
"float") {
134 for (
size_t i = 0; i <
channels; i++) {
147 throw std::runtime_error(
"TMVA SOFIE Batch Normalization called to Generate without being initialized first");
150 std::stringstream out;
152 auto batchSize =
fShapeX[0].GetVal();
163 out <<
SP <<
" size_t i = 0;\n";
164 out <<
SP <<
" for (size_t n = 0; n < " << batchSize <<
"; ++n) {\n";
165 out <<
SP <<
" for (size_t c = 0; c < " <<
channels <<
"; ++c) {\n";
166 out <<
SP <<
" const float mean_val = tensor_" <<
fNMean <<
"[c];\n";
167 out <<
SP <<
" const float fused_scale_val = tensor_" <<
fNFusedScale <<
"[c];\n";
168 out <<
SP <<
" const float bias_val = tensor_" <<
fNB <<
"[c];\n";
169 out <<
SP <<
" for (size_t sp = 0; sp < " <<
spatial_dim <<
"; ++sp) {\n";
170 out <<
SP <<
" float val = (tensor_" <<
fNX <<
"[i] - mean_val) * fused_scale_val + bias_val;\n";
173 out <<
SP <<
" tensor_" <<
fNY <<
"[i] = (val > 0.0f) ? val : 0.0f;\n";
175 out <<
SP <<
" tensor_" <<
fNY <<
"[i] = val;\n";
177 out <<
SP <<
" i++;\n";
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
const_iterator begin() const
std::vector< size_t > GetTensorShape(const std::string &name) const
std::vector< Dim > GetDimTensorShape(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
void AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
ETensorType GetTensorType(std::string name) const
std::vector< std::string > GetBlasRoutines() override
void Initialize(RModel &model) override
ROperator_BatchNormalization()=delete
std::vector< Dim > fShapeX
std::vector< Dim > fShapeY
std::size_t ftraining_mode
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::string Generate(std::string OpName) override
ROperator_BatchNormalization(float epsilon, float momentum, std::size_t training_mode, std::string nameX, std::string nameScale, std::string nameB, std::string nameMean, std::string nameVar, std::string nameY, EActivationType activation=EActivationType::UNDEFINED)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
EActivationType fActivation
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 ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations