1#ifndef TMVA_SOFIE_ROPERATOR_BatchNormalization
2#define TMVA_SOFIE_ROPERATOR_BatchNormalization
13namespace Experimental{
48 std::string nameX, std::string nameScale, std::string nameB,
49 std::string nameMean, std::string nameVar, std::string nameY):
51 fNX(UTILITY::Clean_name(nameX)),
fNScale(UTILITY::Clean_name(nameScale)),
52 fNB(UTILITY::Clean_name(nameB)),
fNMean(UTILITY::Clean_name(nameMean)),
53 fNVar(UTILITY::Clean_name(nameVar)),
fNY(UTILITY::Clean_name(nameY))
55 if(std::is_same<T, float>::value){
60 std::runtime_error(
"TMVA SOFIE Encountered unsupported type parsing a BatchNormalization operator");
71 if (
input.size() != 5 ) {
73 std::runtime_error(
"TMVA SOFIE BatchNormalization Op Shape inference need 5 input tensors");
75 for(
size_t i = 0; i <
input.size(); i++) {
78 std::runtime_error(
"TMVA SOFIE BatchNormalization Op Shape inference only accept tensor with 4 dimensions");
89 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNX +
" fnx is not found in model");
93 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNScale +
" fns is not found in model");
97 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNB +
" fnb is not found in model");
101 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNMean +
" fnm is not found in model");
105 std::runtime_error(
"TMVA SOFIE BatchNormalization op Input Tensor " +
fNVar +
" fnv is not found in model");
133 if (
fType ==
"float") {
134 float *original_bias =
static_cast<float *
>(original_B.get());
135 float *original_scale =
static_cast<float *
>(original_S.get());
136 float *original_mean =
static_cast<float *
>(original_M.get());
137 float *original_var =
static_cast<float *
>(original_V.get());
138 float *new_bias =
new float[
n];
139 float *new_scale =
new float[
n];
140 float *new_mean =
new float[
n];
141 float *new_var =
new float[
n];
142 size_t bs = 0, ch = 0,
h = 0,
w = 0;
143 for (ch = 0; ch < channels; ch++) {
155 for (bs = 1; bs < batchSize; bs++) {
156 std::copy(new_bias, new_bias + Batchoffset, new_bias + (bs * Batchoffset));
157 std::copy(new_scale, new_scale + Batchoffset, new_scale + (bs * Batchoffset));
158 std::copy(new_mean, new_mean + Batchoffset, new_mean + (bs * Batchoffset));
159 std::copy(new_var, new_var + Batchoffset, new_var + (bs * Batchoffset));
162 for (
size_t i = 0; i <
n; i++) {
163 new_var[i] = 1. / sqrt(new_var[i] +
fepsilon);
164 new_scale[i] *= new_var[i];
166 std::vector<size_t> new_bias_shape = {batchSize, channels,
height,
width};
167 std::shared_ptr<void> new_bias_ptr(new_bias, std::default_delete<
float[]>());
168 std::shared_ptr<void> new_scale_ptr(new_scale, std::default_delete<
float[]>());
169 std::shared_ptr<void> new_mean_ptr(new_mean, std::default_delete<
float[]>());
170 std::shared_ptr<void> new_var_ptr(new_var, std::default_delete<
float[]>());
184 OpName =
"op_" + OpName;
186 throw std::runtime_error(
"TMVA SOFIE Batch Normalization called to Generate without being initialized first");
189 std::stringstream out;
198 out <<
SP <<
"constexpr int " << OpName <<
"_N =" << batchSize * channels *
height *
width <<
";\n";
199 out <<
SP <<
"constexpr int "<<OpName<<
"_incx = 1;\n";
200 out <<
SP <<
"constexpr int "<<OpName<<
"_incy = 1;\n";
201 out <<
SP <<
"BLAS::scopy_(&" << OpName <<
"_N, " <<
"tensor_" <<
fNX <<
", &" << OpName <<
"_incx," <<
"tensor_" <<
fNY <<
", &" << OpName <<
"_incy);\n\n";
204 out <<
SP <<
"float "<<OpName<<
"_alpha = -1;\n";
205 out <<
SP <<
"BLAS::saxpy_(&" << OpName <<
"_N, &" << OpName <<
"_alpha, " <<
"tensor_" <<
fNMean <<
", &" << OpName <<
"_incx,"
206 <<
"tensor_" <<
fNY <<
", &" << OpName <<
"_incy);\n\n ";
209 out <<
SP <<
"for (size_t i = 0; i < " <<
n <<
"; i++) {\n";
211 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[i] *= tensor_" <<
fNScale <<
"[i]; \n";
215 out <<
SP <<OpName<<
"_alpha = 1;\n";
216 out <<
SP <<
"BLAS::saxpy_(&" << OpName <<
"_N, &" << OpName <<
"_alpha, " <<
"tensor_" <<
fNB <<
", &" << OpName <<
"_incx, "
217 <<
"tensor_" <<
fNY <<
", &" << OpName <<
"_incy);\n\n";
222 std::vector<std::string>
GetBlasRoutines() {
return { std::string(
"Copy"), std::string(
"Axpy") }; }
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 height
const ETensorType & GetTensorType(std::string name)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const std::vector< size_t > & GetTensorShape(std::string name)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
std::vector< size_t > fShapeScale
ROperator_BatchNormalization()=delete
std::vector< std::string > GetBlasRoutines()
std::vector< size_t > fShapeY
std::string Generate(std::string OpName)
void Initialize(RModel &model)
std::vector< size_t > fShapeX
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< size_t > fShapeB
std::vector< size_t > fShapeMean
std::size_t ftraining_mode
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
std::vector< size_t > fShapeVar
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)
const std::string SP
space used to correctly indent the generated C++ code
std::string ConvertShapeToString(std::vector< size_t > shape)
create variable transformations