1#ifndef TMVA_SOFIE_ROperator_Einsum
2#define TMVA_SOFIE_ROperator_Einsum
12namespace Experimental{
45 for (
size_t i = 0; i <
namesX.size(); i++)
50 throw std::runtime_error(
"TMVA SOFIE Einsum Op: Error parsing the equation " +
equation);
54 [](
const std::string& s) -> std::string_view { return s; });
65 if (pos == std::string::npos) {
66 std::cout <<
"'->' not found in the equation." << std::endl;
78 while ((
pos1 =
inputStr.find(
',', start)) != std::string::npos) {
79 std::string labels =
inputStr.substr(start,
pos1 - start);
87 auto checkLabel = [](
const std::string & label) {
88 for (
char c : label) {
89 if (!std::isalnum(
c)) {
90 std::cout <<
"Wrong tensor label " << label << std::endl;
101 std::cout <<
"invalid output label" << std::endl;
107 std::cout <<
"Invalid number of input labels found " <<
fInputLabels.size() <<
" for #inputs = " <<
fNInputs.size() << std::endl;
122 auto ret = std::vector<std::vector<size_t>>(1,
input[0]);
132 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op Input Tensor ") +
name +
"is not found in model");
142 for (
size_t j = 0;
j < shape.size();
j++) {
143 if (
j >= labels.length()) {
144 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op Input Tensor has invalid label or shape ") + labels +
" " +
ConvertShapeToString(shape));
153 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op : output label ") + std::string(&
l) +
" is not present in inputs");
191 std::cout <<
"Einsum op ";
192 for (i = 0; i <
fNInputs.size(); i++) {
193 if (i > 0) std::cout <<
", ";
202 std::stringstream out;
213 throw std::runtime_error(
"TMVA SOFIE Einsum Op called to Generate without being initialized first");
217 auto tensorIndex = [](
const std::vector<size_t> &
stride,
const std::string & labels) {
218 std::stringstream
strst;
219 int dims = labels.length();
221 if (
dims == 0)
return std::string(
"0");
223 for (
int i = 0; i <
dims-1; i++) {
224 strst <<
stride[i] <<
"*" << std::string{labels[i]} <<
" + ";
230 std::stringstream out;
231 out <<
SP <<
"\n//-------- Einsum \n";
241 for (
int i = 0; i <
outDims; i++) {
242 for (
int j = 0;
j < i;
j++) out <<
SP;
244 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fShapeY[i] <<
"; " <<
l <<
"++) {\n";
252 for (
int i = 0; i <
inDims; i++) {
255 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fSumDims[i] <<
"; " <<
l <<
"++) {\n";
260 for (
size_t k = 0; k <
fNInputs.size(); k++) {
271 for (
int j = 0;
j < i;
j++) out <<
SP;
278 out <<
SP <<
"// implementing Einsum using MatMul \n";
290 out <<
SP <<
"int " <<
opName <<
"_m = " <<
m <<
";\n";
291 out <<
SP <<
"int " <<
opName <<
"_n = " <<
n <<
";\n";
292 out <<
SP <<
"int " <<
opName <<
"_k = " << k <<
";\n";
293 out <<
SP <<
"float " <<
opName <<
"_alpha = 1.0;\n";
294 out <<
SP <<
"float " <<
opName <<
"_beta = 0.0;\n";
295 out <<
SP <<
"int " <<
opName <<
"_lda = " << ((
fGemmType[0] ==
't') ?
m : k) <<
";\n";
296 out <<
SP <<
"int " <<
opName <<
"_ldb = " << ((
fGemmType[1] ==
't') ? k :
n) <<
";\n";
303 for (
int j = 0;
j < i;
j++) out <<
SP;
305 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fShapeY[i] <<
"; " <<
l <<
"++) {\n";
307 auto tensorOffset = [](
const std::vector<size_t> &
stride,
const std::string & labels) {
308 std::stringstream
strst;
309 int dims = labels.length()-2;
311 if (
dims == 0)
return std::string(
"0");
313 for (
int i = 0; i <
dims; i++) {
314 strst <<
stride[i] <<
"*" << std::string{labels[i]};
323 <<
"], &" <<
opName <<
"_ldb, "
329 for (
int j = 0;
j < i;
j++) out <<
SP;
340 return { std::string(
"Gemm") };
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 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 AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
const ETensorType & GetTensorType(std::string name) const
const std::vector< size_t > & GetTensorShape(std::string name) const
std::vector< size_t > fShapeY
std::vector< std::vector< size_t > > fShapeInputs
bool ParseEquation(const std::string &input_equation)
std::string Generate(std::string opName) override
std::string fOutputLabels
std::string GenerateInitCode() override
std::vector< std::string > fInputLabels
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
ROperator_Einsum(const std::string &equation, const std::vector< std::string > &namesX, const std::string &nameY)
std::vector< std::string > GetBlasRoutines() override
void Initialize(RModel &model) override
std::vector< int > fSumDims
std::vector< std::string > fNInputs
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< std::string_view > fInputTensorNames
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
const std::string SP
space used to correctly indent the generated C++ code
std::vector< std::string_view > fOutputTensorNames
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertShapeToString(std::vector< size_t > shape)
create variable transformations