1#ifndef TMVA_SOFIE_ROPERATOR_GEMM
2#define TMVA_SOFIE_ROPERATOR_GEMM
17namespace Experimental{
54 static_assert(std::is_same_v<T, float>,
55 "TMVA::SOFIE - Unsupported type parsing a Gemm operator");
77 if (
input.size() > 3)
throw std::runtime_error(
"TMVA SOFIE Gemm Op Shape Inference only need 2 or 3 input tensor");
82 throw std::runtime_error(
"TMVA SOFIE Gemm Op Shape Inference only accept input tensor with >=2 dimensions");
86 std::vector<std::vector<U>>
ret;
88 if (
input.size() == 3){
109 for (
size_t i = 0; i <
input[0].size()-2; i++) {
113 if (
valueB.GetVal() ==
"1")
115 else if (
valueA.GetVal() ==
"1")
118 throw std::runtime_error(
"TMVA SOFIE Gemm Op - invalid input shapes " +
valueA.GetVal() +
" and "
144 throw std::runtime_error(
"TMVA SOFIE Gemm Op Input Tensor " +
fNA +
" or " +
fNB +
" is not found in model");
148 throw std::runtime_error(
"TMVA SOFIE Gemm Op Input Tensor" +
fNC +
" is not found in model");
191 std::vector<size_t>
shapeY;
203 throw std::runtime_error(
"TMVA SOFIE Gemm Op Input Tensor" +
fNC +
" is dynamic and is not supported");
217 throw std::runtime_error(
"TMVA SOFIE Gemm Op: dynamic tensors not supported without a session");
221 if (
fType ==
"float") {
222 std::shared_ptr<void>
new_data_ptr(UTILITY::UnidirectionalBroadcast<float>(
224 std::default_delete<
float[]>());
262 std::cout <<
"Gemm (or MatMul) " <<
" ---> " <<
fNY <<
" shape ";
274 std::stringstream out;
282 out <<
"//--- broadcast bias tensor " <<
fNC <<
"for Gemm op\n";
284 out <<
" float * data = TMVA::Experimental::SOFIE::UTILITY::UnidirectionalBroadcast<float>(tensor_"
287 out <<
SP <<
SP <<
"std::copy(data, data + " <<
length <<
", tensor_" <<
fNC2 <<
");\n";
288 out <<
SP <<
SP <<
"delete [] data;\n";
298 throw std::runtime_error(
"TMVA SOFIE Gemm Op called to Generate without being initialized first");
300 std::stringstream out;
301 out <<
"\n//--------- Gemm\n";
302 out <<
SP <<
"char " <<
opName <<
"_transA = " << (
fAttrTransA ?
"\'t\'" :
"\'n\'") <<
";\n";
303 out <<
SP <<
"char " <<
opName <<
"_transB = " << (
fAttrTransB ?
"\'t\'" :
"\'n\'") <<
";\n";
309 throw std::runtime_error(
"TMVA SOFIE Gemm(MatMul) has invalid shape for inputs or output");
317 for (int64_t i = 0; i <
dimY-2; i++) {
323 out <<
SP <<
"int " <<
opName <<
"_m = " <<
m <<
";\n";
324 out <<
SP <<
"int " <<
opName <<
"_n = " <<
n <<
";\n";
325 out <<
SP <<
"int " <<
opName <<
"_k = " << k <<
";\n";
326 out <<
SP <<
"float " <<
opName <<
"_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10) <<
fAttrAlpha <<
";\n";
327 out <<
SP <<
"float " <<
opName <<
"_beta = " << std::setprecision(std::numeric_limits<float>::max_digits10) <<
fAttrBeta <<
";\n";
338 throw std::runtime_error(
"TMVA SOFIE Gemm Op " +
opName +
" Bias tensor has not correct size "
349 throw std::runtime_error(
"TMVA SOFIE Gemm Op " +
opName +
" Bias tensor is not present but beta value in Gemm is not zero");
357 out <<
SP <<
"size_t " <<
opName <<
"_yoffset = 0;\n";
358 out <<
SP <<
"for (int i = 0; i < " <<
lengthExtra <<
"; i++){\n";
363 out <<
SP <<
"std::copy(" <<
"tensor_" <<
fNC2 <<
", " <<
"tensor_" <<
fNC2 <<
" + " <<
lengthGemm <<
", "
370 if (
fType ==
"float"){
374 <<
", &" <<
opName <<
"_ldb, " <<
"tensor_" <<
fNA <<
", &" <<
opName <<
"_lda, &" <<
opName <<
"_beta, "
377 out <<
", &" <<
opName <<
"_n);\n";
381 out <<
SP <<
SP <<
"tensor_" <<
fNY <<
"[id] = ((tensor_" <<
fNY <<
"[id] > 0 )? tensor_" <<
fNY <<
"[id] : 0);\n";
394 std::vector<std::string>
GetBlasRoutines() {
return { std::string(
"Gemm"), std::string(
"Gemv") }; }
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 length
const_iterator begin() const
const_iterator end() const
void AddNeededStdLib(std::string libname)
const ETensorType & GetTensorType(std::string name)
bool IsDynamicTensor(const std::string &name) const
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
std::vector< Dim > GetDynamicTensorShape(std::string name)
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
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)
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::string Generate(std::string opName)
std::vector< std::vector< U > > DoShapeInference(const std::vector< std::vector< U > > &input)
std::vector< Dim > fShapeY
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< Dim > fShapeA
std::string GenerateInitCode()
std::vector< Dim > fShapeB
std::vector< size_t > fShapeC
std::vector< std::string > GetBlasRoutines()
void Initialize(RModel &model) override
std::vector< std::vector< Dim > > DynamicShapeInference(const std::vector< std::vector< Dim > > &input)
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input)
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
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::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t >, std::vector< size_t >)
std::vector< Dim > ConvertShapeToDim(std::vector< size_t > shape)
Convert shape from integer format to dynamic one (based on Dim)
std::string ConvertDynamicShapeToLength(std::vector< Dim > shape)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
std::vector< size_t > ConvertShapeToInt(std::vector< Dim > shape)
Convert shape based on Dim to integer format.
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations