1#ifndef TMVA_SOFIE_ROPERATOR_GEMM
2#define TMVA_SOFIE_ROPERATOR_GEMM
17namespace Experimental{
56 static_assert(std::is_same_v<T, float>,
57 "TMVA::SOFIE - Unsupported type parsing a Gemm operator");
80 if (
input.size() > 3)
throw std::runtime_error(
"TMVA SOFIE Gemm Op Shape Inference only need 2 or 3 input tensor");
85 throw std::runtime_error(
"TMVA SOFIE Gemm Op Shape Inference only accept input tensor with >=2 dimensions");
90 if (
input.size() == 3){
112 for (
size_t i = 0; i <
input[0].size()-2; i++) {
116 if (
valueB.GetVal() ==
"1")
118 else if (
valueA.GetVal() ==
"1")
121 throw std::runtime_error(
"TMVA SOFIE Gemm Op - invalid input shapes " +
valueA.GetVal() +
" and "
136 throw std::runtime_error(
"TMVA SOFIE Gemm Op - invalid input shapes " +
valueA.GetVal() +
" and "
150 std::vector<std::vector<size_t>>
ret;
165 throw std::runtime_error(
"TMVA SOFIE Gemm Op Input Tensor " +
fNA +
" or " +
fNB +
" is not found in model");
169 throw std::runtime_error(
"TMVA SOFIE Gemm Op Input Tensor " +
fNC +
" is not found in model");
263 std::cout <<
"Gemm (or MatMul) " <<
" ---> " <<
fNY <<
" shape ";
277 throw std::runtime_error(
"TMVA SOFIE Gemm Op called to Generate without being initialized first");
279 std::stringstream out;
292 throw std::runtime_error(
"TMVA SOFIE Gemm(MatMul) has invalid shape for inputs or output");
302 for (int64_t i = 0; i <
dimY-2; i++) {
313 for (int64_t i = 0; i <
dimC-2; i++) {
318 }
else if (
dimC > 0) {
319 for (int64_t i = 0; i <
dimC; i++) {
334 throw std::runtime_error(
"TMVA SOFIE Gemm Op " +
opName +
" Bias tensor " +
fNC +
" has not correct size "
347 throw std::runtime_error(
"TMVA SOFIE Gemm Op " +
opName +
" Bias tensor is not present but beta value in Gemm is not zero");
376 out <<
SP <<
"size_t " <<
opName <<
"_y_offset = 0;\n";
378 out <<
SP <<
"size_t " <<
opName <<
"_A_offset = 0;\n";
380 out <<
SP <<
"size_t " <<
opName <<
"_B_offset = 0;\n";
382 out <<
SP <<
"size_t " <<
opName <<
"_C_offset = 0;\n";
383 out <<
SP <<
"for (size_t i = 0; i < " <<
lengthExtra_Y <<
"; i++){\n";
391 out <<
SP2 <<
"for (size_t j = 0; j < " <<
sY[0] <<
"; j++) { \n";
392 out <<
SP2 <<
SP <<
"size_t y_index = ";
394 out <<
opName <<
"_y_offset + ";
395 if (
sY[1].GetVal() !=
"1")
396 out <<
sY[1] <<
" * j;\n";
400 std::string prefix =
SP2 +
SP +
"TMVA::Experimental::SOFIE::";
402 if (
sC.size() != 2) {
404 }
if (
sC[0].GetVal() ==
"1" &&
sC[1].GetVal() ==
sY[1].GetVal()) {
405 out << prefix <<
"Copy(" <<
target <<
" + y_index, tensor_" <<
fNC <<
", " <<
sY[1] <<
");\n";
406 }
else if (
sC[1].GetVal() ==
"1" &&
sC[0].GetVal() ==
sY[0].GetVal()) {
407 out << prefix <<
"Fill(" <<
target <<
" + y_index, tensor_" <<
fNC <<
"[j], " <<
sY[1] <<
");\n";
408 }
else if (
sC[0].GetVal() ==
"1" &&
sC[1].GetVal() ==
"1") {
410 out << prefix <<
"Fill(" <<
target <<
" + y_index, tensor_" <<
fNC <<
"[0], " <<
sY[1] <<
");\n";
418 if (
fType ==
"float"){
420 out <<
SP2 <<
"TMVA::Experimental::SOFIE::Gemm_Call(" <<
"tensor_" <<
fNY;
425 <<
n <<
", " <<
m <<
", " << k <<
", ";
426 out << std::setprecision(std::numeric_limits<float>::max_digits10) <<
fAttrAlpha <<
", tensor_" <<
fNB;
428 out <<
", tensor_" <<
fNA;
430 out <<
", " << std::setprecision(std::numeric_limits<float>::max_digits10) <<
fAttrBeta <<
",";
433 out <<
"tensor_" <<
fNC;
435 out <<
" + " <<
opName <<
"_C_offset";
458 out <<
SP <<
"//--- applying RELU to output\n";
459 std::string
tnsr =
"tensor_" +
fNY;
461 out <<
SP <<
"TMVA::Experimental::SOFIE::Relu(" <<
tnsr <<
", " <<
tnsr <<
", " <<
reluSize <<
");\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
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 target
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 r
const_iterator begin() const
const_iterator end() const
void AddNeededStdLib(std::string libname)
std::vector< size_t > GetTensorShape(const std::string &name) const
bool IsDynamicTensor(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 AddDynamicTensor(std::string tensor_name, ETensorType type, std::vector< Dim > shape)
bool IsDimInputTensor(const std::string &name) const
std::vector< Dim > GetDynamicTensorShape(const std::string &name) const
ETensorType GetTensorType(std::string name) const
const std::vector< std::string > & GetDimShapeNames() const
ROperator_Gemm(float alpha, float beta, int_t transA, int_t transB, std::string nameA, std::string nameB, std::string nameC, std::string nameY, EActivationType activation=EActivationType::UNDEFINED)
std::vector< Dim > DynamicShapeInference(const std::vector< std::vector< Dim > > &input)
std::vector< Dim > fShapeY
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
ROperator_Gemm(float alpha, float beta, int_t transA, int_t transB, std::string nameA, std::string nameB, std::string nameY, EActivationType activation=EActivationType::UNDEFINED)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< U > DoShapeInference(const std::vector< std::vector< U > > &input)
std::vector< Dim > fShapeA
std::vector< Dim > fShapeB
std::vector< size_t > fShapeC
std::string Generate(std::string opName) override
void Initialize(RModel &model) override
std::vector< Dim > fDimShapeC
EActivationType fActivation
std::vector< std::string > GetBlasRoutines() override
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 > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations