1#ifndef TMVA_SOFIE_ROPERATOR_CONV
2#define TMVA_SOFIE_ROPERATOR_CONV
16namespace Experimental {
56 std::vector<size_t> strides, std::string
nameX, std::string
nameW,
63 if(std::is_same<T, float>::value) {
67 std::runtime_error(
"TMVA SOFIE Encountered unsupported type parsing a Conv operator");
75 std::vector<size_t> strides, std::string
nameX, std::string
nameW,
81 if(std::is_same<T, float>::value) {
85 std::runtime_error(
"TMVA SOFIE Encountered unsupported type parsing a Conv operator");
102 throw std::runtime_error(
"TMVA SOFIE Conv Op Shape inference - invalid input ");
104 if (weight.size() -2 !=
fDim) {
105 throw std::runtime_error(
"TMVA SOFIE Conv Op Shape inference - invalid weights ");
108 throw std::runtime_error(
"TMVA SOFIE Conv - param shapes not supported without group attr");
111 throw std::runtime_error(
"TMVA SOFIE Conv - param shapes not supported without kernel attr");
124 size_t i1 = (
fDim > 1) ? ((
fDim > 2) ? 3 : 2) : 1;
125 size_t i2 = (
fDim > 2) ? 4 : 3;
165 std::runtime_error(
"TMVA SOFIE Conv Op invalid fAutopad");
196 std::string
outStr =
"(" +
inputDim.param +
"+" + std::to_string(
v) +
")";
197 return Dim{
outStr,
static_cast<size_t>(-1)};
201 std::string
outStr =
"((" +
inputDim.param +
"+" + std::to_string(
v) +
")/"
202 + std::to_string(
stride) +
"1)";
203 return Dim{
outStr,
static_cast<size_t>(-1)};
206 std::runtime_error(
"TMVA SOFIE Conv Op - invalid values");
240 std::runtime_error(
"TMVA SOFIE Conv op Input Tensor " +
fNX +
" is not found in model");
246 std::runtime_error(
"TMVA SOFIE Conv Op input data tensor" +
fNX +
" is not of 3,4 or 5 dimensions");
251 std::runtime_error(
"TMVA SOFIE Conv op Input weight Tensor " +
fNW +
" is not found in model");
256 throw std::runtime_error(
"TMVA SOFIE Conv Op input weight tensor" +
fNW +
" is not of 3,4 or 5 dimensions");
263 std::runtime_error(
"TMVA SOFIE Conv op Input Tensor " +
fNB +
" is not found in model");
268 std::runtime_error(
"TMVA SOFIE Conv op : invalid shape for Bias tensor (is not 1D)");
276 throw std::runtime_error(
"TMVA SOFIE Conv op: Bias Tensor has empty shape");
280 throw std::runtime_error(
"TMVA SOFIE Conv op: Bias Tensor has wrong shape: " +
282 if (
fType !=
"float")
283 throw std::runtime_error(
"TMVA SOFIE Conv op: Broadcasting for non-float type tensors is not supported");
288 std::vector<size_t> shape(
fDim + 1, 1);
293 std::default_delete<
float[]>());
312 for (
size_t i = 1; i <
fDim; i++) {
334 std::stringstream out;
338 std::vector<size_t> shape(
fDim + 1, 1);
342 out <<
"//--- broadcast bias tensor " <<
fNB <<
"for Conv op if needed \n";
350 out <<
SP <<
SP <<
"float * data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<float>(tensor_"
352 out <<
SP <<
SP <<
"fTensor_" <<
fNB <<
".resize(" <<
length <<
");\n";
353 out <<
SP <<
SP <<
"std::copy(data, data + " <<
length <<
", fTensor_" <<
fNB <<
".begin());\n";
354 out <<
SP <<
SP <<
"tensor_" <<
fNB <<
" = fTensor_" <<
fNB <<
".data();\n";
355 out <<
SP <<
SP <<
"delete[] data;\n";
366 std::runtime_error(
"TMVA SOFIE Conv Op called to Generate without being initialized first");
369 std::stringstream out;
387 out <<
"\n//---- operator Conv " <<
OpName <<
"\n";
393 size_t id = (
fDim > 2) ?
fDim-3 : 2;
407 out <<
SP <<
"for (std::size_t oc = 0; oc < " <<
fShapeW[0] <<
"; oc++) {\n";
408 out <<
SP <<
SP <<
"for (std::size_t ic = 0; ic < " <<
fShapeW[1] <<
"; ic++) {\n";
410 out <<
SP <<
SP <<
SP <<
"for (std::size_t kd = 0; kd < " << kDepth <<
"; kd++) {\n";
412 out <<
SP <<
SP <<
SP <<
"for (std::size_t kh = 0; kh < " <<
kHeight <<
"; kh++) {\n";
413 out <<
SP <<
SP <<
SP <<
SP <<
"for (std::size_t kw = 0; kw < " <<
kWidth <<
"; kw++) {\n";
415 out <<
SP <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fNX <<
"_f[oc * "
424 out <<
SP <<
SP <<
SP <<
SP <<
"}\n";
427 out <<
SP <<
SP <<
"}\n";
431 out <<
SP <<
"char " <<
OpName <<
"_transA = 'N';\n";
432 out <<
SP <<
"char " <<
OpName <<
"_transB = 'N';\n";
438 out <<
SP <<
"float " <<
OpName <<
"_alpha = 1.0;\n";
439 out <<
SP <<
"float " <<
OpName <<
"_beta = 0.0;\n";
443 out <<
SP <<
"for (size_t n = 0; n < " <<
bsize <<
"; n++) {\n";
454 std::cout <<
"TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding "
463 std::cout <<
"TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding " << std::endl;
470 std::cout <<
"TMVA SOFIE Operator Conv: asymmetric padding not supported. Assume an average padding " << std::endl;
483 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::Im2col<float>(tensor_" <<
fNX
496 out <<
"," <<
"tensor_" <<
fNX <<
"_xcol);\n\n ";
499 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::Im2col_3d<float>(tensor_" <<
fNX
509 <<
"tensor_" <<
fNX <<
"_xcol);\n\n ";
512 out <<
SP <<
SP <<
"BLAS::sgemm_(&" <<
OpName <<
"_transA, &" <<
OpName <<
"_transB, &" <<
OpName <<
"_m, &"
516 <<
" + out_offset, &" <<
OpName <<
"_m);\n";
522 out <<
SP <<
SP <<
"for (size_t g = 0; g < " <<
fAttrGroup <<
"; g++) {\n";
529 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::Im2col<float>(tensor_" <<
fNX
542 out <<
", tensor_" <<
fNX <<
"_xcol);\n\n ";
545 out <<
SP <<
SP <<
"TMVA::Experimental::SOFIE::UTILITY::Im2col_3d<float>(tensor_" <<
fNX
561 out <<
SP <<
SP <<
SP <<
"size_t offset_f = g * "
564 out <<
SP <<
SP <<
"BLAS::sgemm_(&" <<
OpName <<
"_transA, &" <<
OpName <<
"_transB, &" <<
OpName <<
"_m, &"
567 out <<
SP <<
SP <<
SP <<
"tensor_" <<
fNX <<
"_f + offset_f, &" <<
OpName <<
"_k, &" <<
OpName <<
"_beta, tensor_" <<
fNY
569 <<
", &" <<
OpName <<
"_m);\n";
571 out <<
SP <<
SP <<
"}\n";
576 out <<
SP <<
"float " <<
OpName <<
"_gamma = 1.0;\n";
577 out <<
SP <<
"int " <<
OpName <<
"_incx = 1;\n";
578 out <<
SP <<
"int " <<
OpName <<
"_incy = 1;\n";
580 out <<
SP <<
"BLAS::saxpy_(&" <<
OpName <<
"_size, &" <<
OpName <<
"_gamma, tensor_" <<
fNB <<
", &"
581 <<
OpName <<
"_incx, tensor_" <<
fNY <<
" + out_offset, &" <<
OpName <<
"_incy);\n";
591 std::vector<std::string>
GetBlasRoutines()
override {
return { std::string(
"Gemm"), std::string(
"Axpy") }; }
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 length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
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)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
ETensorType GetTensorType(std::string name) const
void UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
std::vector< size_t > fAttrDilations
std::string Generate(std::string OpName) override
std::vector< Dim > fShapeY
std::vector< size_t > fShapeW
ROperator_Conv(std::string autopad, std::vector< size_t > dilations, size_t group, std::vector< size_t > kernelShape, std::vector< size_t > pads, std::vector< size_t > strides, std::string nameX, std::string nameW, std::string nameB, std::string nameY)
std::vector< std::string > GetBlasRoutines() override
Returns the blas routines needed to compile the generated code.
std::vector< Dim > fShapeX
void Initialize(RModel &model) override
std::string GenerateInitCode() override
std::vector< size_t > fAttrStrides
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< size_t > fShapeB
std::vector< size_t > fAttrPads
ROperator_Conv(std::string autopad, std::vector< size_t > dilations, size_t group, std::vector< size_t > kernelShape, std::vector< size_t > pads, std::vector< size_t > strides, std::string nameX, std::string nameW, std::string nameY)
std::vector< Dim > DoShapeInference(const std::vector< Dim > &input, const std::vector< size_t > &weight)
std::vector< size_t > fAttrKernelShape
std::vector< std::string_view > fInputTensorNames
const std::string SP
space used to correctly indent the generated C++ code
bool fUseSession
flag to identify if using the session class
std::vector< std::string_view > fOutputTensorNames
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
ETensorType ConvertStringToType(std::string type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations