1#ifndef TMVA_SOFIE_ROperator_Einsum
2#define TMVA_SOFIE_ROperator_Einsum
42 ROperator_Einsum(
const std::string & equation,
const std::vector<std::string> & namesX,
const std::string & nameY):
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; });
59 std::string eq (input_equation);
61 eq.erase(std::remove(eq.begin(), eq.end(),
' '), eq.end());
64 size_t pos = eq.find(
target);
65 if (pos == std::string::npos) {
66 std::cout <<
"'->' not found in the equation." << std::endl;
70 std::string inputStr = eq.substr(0, pos);
72 std::string outputStr = eq.substr(pos +
target.length());
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;
98 if (!checkLabel(label))
return false;
100 if (!checkLabel(outputStr)) {
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]);
129 std::map<char, int> labelsMap;
131 if (!model.CheckIfTensorAlreadyExist(
name))
132 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op Input Tensor ") +
name +
"is not found in model");
137 auto shape = model.GetTensorShape(
name);
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));
146 labelsMap[labels[j]] = shape[j];
152 if (labelsMap.count(
l) == 0)
153 throw std::runtime_error(std::string(
"TMVA SOFIE Einsum Op : output label ") + std::string(&
l) +
" is not present in inputs");
160 for (
auto &
l : labelsMap) {
190 if (model.Verbose()) {
191 std::cout <<
"Einsum op ";
192 for (i = 0; i <
fNInputs.size(); i++) {
193 if (i > 0) std::cout <<
", ";
202 std::stringstream out;
206 std::string
Generate(std::string opName)
override {
210 opName =
"op_" + opName;
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");
222 assert (dims == (
int) stride.size());
223 for (
int i = 0; i < dims-1; i++) {
224 strst << stride[i] <<
"*" << std::string{labels[i]} <<
" + ";
226 strst << std::string{labels[dims-1]};
230 std::stringstream out;
231 out <<
SP <<
"\n//-------- Einsum \n";
240 assert(inDims ==
int(
fSumDims.size()));
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";
247 std::string outputIndex = tensorIndex(outputStride,
fOutputLabels);
249 for (
int j = 0; j < outDims; j++) out <<
SP;
250 out <<
"tensor_" <<
fNY <<
"[" << outputIndex <<
"] = 0;\n";
252 for (
int i = 0; i < inDims; i++) {
253 for (
int j = 0; j < outDims + i; j++) out <<
SP;
255 out <<
"for (int " <<
l <<
" = 0; " <<
l <<
" < " <<
fSumDims[i] <<
"; " <<
l <<
"++) {\n";
257 for (
int j = 0; j < outDims+inDims; j++) out <<
SP;
259 out <<
"tensor_" <<
fNY <<
"[" << outputIndex <<
"] +=\n";
260 for (
size_t k = 0; k <
fNInputs.size(); k++) {
262 std::string inputIndex = tensorIndex(inputStride,
fInputLabels[k]);
263 for (
int j = 0; j < outDims+inDims; j++) out <<
SP;
264 out <<
SP <<
"tensor_" <<
fNInputs[k] <<
"[" << inputIndex <<
"]";
270 for (
int i = outDims+inDims-1; i >= 0; i--) {
271 for (
int j = 0; j < i; j++) out <<
SP;
278 out <<
SP <<
"// implementing Einsum using MatMul \n";
280 out <<
SP <<
"char " << opName <<
"_transA = '" <<
fGemmType[0] <<
"';\n";
281 out <<
SP <<
"char " << opName <<
"_transB = '" <<
fGemmType[1] <<
"';\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";
301 int stackDims =
fShapeY.size()-2;
302 for (
int i = 0; i < stackDims; i++) {
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");
312 assert (dims +2 == (
int) stride.size());
313 for (
int i = 0; i < dims; i++) {
314 strst << stride[i] <<
"*" << std::string{labels[i]};
315 if (i < dims-1) strst <<
" + ";
320 out <<
SP <<
"BLAS::sgemm_(&" << opName <<
"_transB, &" << opName <<
"_transA, &" << opName
321 <<
"_n, &" << opName <<
"_m, &" << opName <<
"_k, &" << opName <<
"_alpha, "
323 <<
"], &" << opName <<
"_ldb, "
324 <<
"&tensor_" <<
fNInputs[0] <<
"[" << tensorOffset(inputStrideA,
fInputLabels[0] ) <<
"], &" << opName <<
"_lda, &" << opName <<
"_beta, "
325 <<
"&tensor_" <<
fNY <<
"[" << tensorOffset(outputStride,
fOutputLabels) <<
"], &" << opName <<
"_n);\n";
328 for (
int i = stackDims-1; i >= 0; i--) {
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
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
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(const std::vector< size_t > &shape)
create variable transformations