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;
119 if (!model.CheckIfTensorAlreadyExist(
name))
120 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op Input Tensor ") +
name +
"is not found in model");
125 auto shape = model.GetTensorShape(
name);
130 for (
size_t j = 0;
j < shape.size();
j++) {
131 if (
j >= labels.length()) {
132 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op Input Tensor has invalid label or shape ") + labels +
" " +
ConvertShapeToString(shape));
141 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op : output label ") + std::string(&
l) +
" is not present in inputs");
178 if (model.Verbose()) {
179 std::cout <<
"Einsum op ";
180 for (i = 0; i <
fNInputs.size(); i++) {
181 if (i > 0) std::cout <<
", ";
190 std::stringstream out;
201 throw std::runtime_error(
"TMVA SOFIE Einsum Op called to Generate without being initialized first");
205 auto tensorIndex = [](
const std::vector<size_t> &
stride,
const std::string & labels) {
206 std::stringstream
strst;
207 int dims = labels.length();
209 if (dims == 0)
return std::string(
"0");
211 for (
int i = 0; i < dims-1; i++) {
212 strst <<
stride[i] <<
"*" << std::string{labels[i]} <<
" + ";
214 strst << std::string{labels[dims-1]};
218 std::stringstream out;
219 out <<
SP <<
"\n//-------- Einsum \n";
229 for (
int i = 0; i <
outDims; i++) {
230 for (
int j = 0;
j < i;
j++) out <<
SP;
232 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fShapeY[i] <<
"; " <<
l <<
"++) {\n";
240 for (
int i = 0; i <
inDims; i++) {
243 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fSumDims[i] <<
"; " <<
l <<
"++) {\n";
248 for (
size_t k = 0; k <
fNInputs.size(); k++) {
259 for (
int j = 0;
j < i;
j++) out <<
SP;
266 out <<
SP <<
"// implementing Einsum using MatMul \n";
278 out <<
SP <<
"int " <<
opName <<
"_m = " <<
m <<
";\n";
279 out <<
SP <<
"int " <<
opName <<
"_n = " <<
n <<
";\n";
280 out <<
SP <<
"int " <<
opName <<
"_k = " << k <<
";\n";
281 out <<
SP <<
"float " <<
opName <<
"_alpha = 1.0;\n";
282 out <<
SP <<
"float " <<
opName <<
"_beta = 0.0;\n";
283 out <<
SP <<
"int " <<
opName <<
"_lda = " << ((
fGemmType[0] ==
't') ?
m : k) <<
";\n";
284 out <<
SP <<
"int " <<
opName <<
"_ldb = " << ((
fGemmType[1] ==
't') ? k :
n) <<
";\n";
291 for (
int j = 0;
j < i;
j++) out <<
SP;
293 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fShapeY[i] <<
"; " <<
l <<
"++) {\n";
295 auto tensorOffset = [](
const std::vector<size_t> &
stride,
const std::string & labels) {
296 std::stringstream
strst;
297 int dims = labels.length()-2;
299 if (dims == 0)
return std::string(
"0");
301 for (
int i = 0; i < dims; i++) {
302 strst <<
stride[i] <<
"*" << std::string{labels[i]};
303 if (i < dims-1)
strst <<
" + ";
311 <<
"], &" <<
opName <<
"_ldb, "
317 for (
int j = 0;
j < i;
j++) out <<
SP;
328 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 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
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
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< 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(const std::vector< size_t > &shape)
create variable transformations