1#ifndef TMVA_SOFIE_ROperator_BasicBinary
2#define TMVA_SOFIE_ROperator_BasicBinary
16template <
typename T, EBasicBinaryOperator Op1>
21 static const std::string
Name() {
return "Add"; }
22 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return t1 +
" + " + t2; }
28 static const std::string
Name() {
return "Sub"; }
29 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return t1 +
" - " + t2; }
35 static const std::string
Name() {
return "Mul"; }
36 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return t1 +
" * " + t2; }
42 static const std::string
Name() {
return "Div"; }
43 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return t1 +
" / " + t2; }
49 static const std::string
Name() {
return "Pow"; }
50 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return "std::pow(" +
t1 +
"," + t2 +
")"; }
51 static T
Func(T
t1, T t2) {
return std::pow(
t1, t2); }
55 static const std::string
Name() {
return "Mod"; }
56 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return "(" +
t1 +
" % " + t2 +
")"; }
61 static const std::string
Name() {
return "FMod"; }
62 static std::string
Op(
const std::string &
t1,
const std::string t2) {
return "std::fmod(" +
t1 +
"," + t2 +
")"; }
63 static T
Func(T
t1, T t2) {
return std::fmod(
t1, t2); }
66template <
typename T, EBasicBinaryOperator Op>
100 auto ret = std::vector<std::vector<size_t>>(1,
input[0]);
107 if (!model.CheckIfTensorAlreadyExist(
fNA)) {
108 throw std::runtime_error(std::string(
"TMVA SOFIE Binary Op Input Tensor ") +
fNA +
"is not found in model");
110 if (!model.CheckIfTensorAlreadyExist(
fNB)) {
111 throw std::runtime_error(std::string(
"TMVA SOFIE Binary Op Input Tensor ") +
fNB +
"is not found in model");
113 int dynamicInputs = 0;
114 if (model.IsDynamicTensor(
fNA)) {
121 if (model.IsDynamicTensor(
fNB)) {
128 if (dynamicInputs & 1 && model.Verbose())
129 std::cout << BinaryOperatorTrait<T, Op>::Name() <<
" : input " <<
fNA <<
" is dynamic "
131 if (dynamicInputs & 2 && model.Verbose())
132 std::cout << BinaryOperatorTrait<T, Op>::Name() <<
" : input " <<
fNB <<
" is dynamic "
139 if (dynamicInputs == 0) {
144 if (model.IsConstantTensor(
fNA) && model.IsConstantTensor(
fNB)) {
153 auto data = model.GetInitializedTensorData(
fNA);
154 std::shared_ptr<void> broadcastedData(
156 std::default_delete<T[]>());
160 static_cast<T *
>(broadcastedData.get()))
170 auto data = model.GetInitializedTensorData(
fNB);
175 std::shared_ptr<void> broadcastedData(
177 std::default_delete<T[]>());
182 static_cast<T *
>(broadcastedData.get()))
195 auto dataA =
static_cast<T *
>(model.GetInitializedTensorData(nameA).get());
196 auto dataB =
static_cast<T *
>(model.GetInitializedTensorData(nameB).get());
197 std::vector<T> dataY(lengthY);
198 for (
size_t i = 0; i < dataY.size(); i++) {
201 model.AddConstantTensor<T>(
fNY,
fShapeY, dataY.data());
203 model.SetNotWritableInitializedTensor(nameA);
204 model.SetNotWritableInitializedTensor(nameB);
206 if (model.Verbose()) {
211 }
else if (((model.IsShapeTensor(
fNA) && model.IsShapeTensor(
fNB)) ||
212 (model.IsShapeTensor(
fNA) && model.IsConstantTensor(
fNB)) ||
213 (model.IsShapeTensor(
fNB) && model.IsConstantTensor(
fNA)))
216 std::vector<Dim> dimValA;
217 std::vector<Dim> dimValB;
218 if (model.IsShapeTensor(
fNA))
219 dimValA = model.GetShapeTensorValues(
fNA);
220 if (model.IsShapeTensor(
fNB))
221 dimValB = model.GetShapeTensorValues(
fNB);
224 if (dimValA.size() == 1) dimValA = std::vector<Dim>(
fShapeY[0], dimValA[0]);
225 if (dimValB.size() == 1) dimValB = std::vector<Dim>(
fShapeY[0], dimValB[0]);
228 auto convertDataToDim = [&](
const std::string &
name,
const std::vector<size_t> & shape, std::vector<Dim> & dimValues) {
229 auto data =
static_cast<int64_t *
>(model.GetInitializedTensorData(
name).get());
230 dimValues.resize(lengthY);
231 for (
size_t i = 0; i < lengthY; i++) {
232 if (!shape.empty() && lengthY == shape[0])
233 dimValues[i] =
Dim{
static_cast<size_t>(
data[i])};
235 dimValues[i] =
Dim{
static_cast<size_t>(
data[0])};
238 if (model.IsConstantTensor(
fNA)) {
240 }
else if (model.IsConstantTensor(
fNB)) {
245 std::vector<Dim> dimValY(lengthY);
246 for (
size_t i = 0; i < lengthY; i++) {
247 if (!dimValA[i].isParam && !dimValB[i].isParam) {
252 dimValY[i] =
Dim{res,
static_cast<size_t>(-1)};
255 model.AddShapeTensor(
fNY,dimValY,
fShapeY.empty());
256 if (model.Verbose()) {
266 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNA),
fShapeY);
267 if (model.Verbose()) {
285 auto IsInputDimParam = [&](
const std::string &p) {
286 auto inputNames = model.GetInputTensorNames();
287 for (
auto &
input : inputNames) {
288 for (
auto &i_s : model.GetDimTensorShape(
input)) {
289 if (i_s.isParam && i_s.param == p)
295 for (
size_t i = 0; i <
fDimShapeY.size(); i++) {
297 if (s.isParam && s.param.find(
"std::max") != std::string::npos) {
304 }
else if (IsInputDimParam(
fDimShapeB[i].param)) {
315 if (model.Verbose()) {
324 std::stringstream out;
334 opName =
"op_" + opName;
336 std::stringstream out;
348 out <<
SP <<
"if (" << lengthA <<
"!=" << lengthB <<
") {\n";
351 for (
size_t i = 0; i <
fDimShapeY.size(); i++) {
356 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast B->A in operator "
357 << opName <<
"\");\n";
363 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast A->B in operator "
364 << opName <<
"\");\n";
369 <<
" != 1 || " <<
fDimShapeB[i] <<
" != 1))\n";
370 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast shapes in operator " << opName
381 std::string compute_idx_A, compute_idx_B, compute_idx_Y;
386 for (
size_t i = 0; i <
fDimShapeA.size(); ++i) {
390 if (stridesA[i].GetVal() !=
"1")
391 compute_idx_A +=
" * " + stridesA[i].GetVal();
392 compute_idx_A +=
" + ";
395 for (
int j = 0; j < 3; j++)
396 compute_idx_A.pop_back();
402 for (
size_t i = 0; i <
fDimShapeB.size(); ++i) {
406 if (stridesB[i].GetVal() !=
"1")
407 compute_idx_B +=
" * " + stridesB[i].GetVal();
408 compute_idx_B +=
" + ";
411 for (
int j = 0; j < 3; j++)
412 compute_idx_B.pop_back();
419 for (
size_t i = 0; i <
fDimShapeY.size(); ++i) {
422 for (
int j = 0; j < nloop; j++) out <<
SP;
423 out <<
"for (size_t idx_" << i <<
" = 0; idx_" << i <<
" < " <<
fDimShapeY[i]
424 <<
"; ++idx_" << i <<
"){\n";
425 compute_idx_Y +=
"idx_" + std::to_string(i);
426 if (stridesY[i].GetVal() !=
"1")
427 compute_idx_Y +=
" * " + stridesY[i].GetVal();
428 compute_idx_Y +=
" + ";
432 for (
int j = 0; j < 3; j++)
433 compute_idx_Y.pop_back();
435 for (
int j = 0; j < nloop + 1; j++) out <<
SP;
436 out <<
"tensor_" <<
fNY <<
"[" << compute_idx_Y <<
"] = "
438 "tensor_" +
fNB +
"[" + compute_idx_B +
"]")
441 for (
int i = nloop; i > 0; i--) {
442 for (
int j = 0; j < i; j++) out <<
SP;
451 return {std::string(
"cmath")};
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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
std::string Generate(std::string opName) override
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input) override
std::vector< size_t > fShapeY
std::string fNBroadcastedA
std::vector< Dim > fDimShapeA
std::string fNBroadcastedB
void Initialize(RModel &model) override
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > input) override
std::vector< size_t > fShapeB
std::string GenerateInitCode() override
std::vector< Dim > fDimShapeY
std::vector< std::string > GetStdLibs() override
std::vector< Dim > fDimShapeB
ROperator_BasicBinary(std::string nameA, std::string nameB, std::string nameY)
std::vector< size_t > fShapeA
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::vector< size_t > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
T * UnidirectionalBroadcast(const T *data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape)
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 ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim).
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
create variable transformations
static std::string Op(const std::string &t1, const std::string t2)
static const std::string Name()
static T Func(T t1, T t2)
static T Func(T t1, T t2)
static std::string Op(const std::string &t1, const std::string t2)
static const std::string Name()
static std::string Op(const std::string &t1, const std::string t2)
static const std::string Name()
static T Func(T t1, T t2)
static std::string Op(const std::string &t1, const std::string t2)
static T Func(T t1, T t2)
static const std::string Name()
static std::string Op(const std::string &t1, const std::string t2)
static const std::string Name()
static T Func(T t1, T t2)
static const std::string Name()
static std::string Op(const std::string &t1, const std::string t2)
static T Func(T t1, T t2)
static std::string Op(const std::string &t1, const std::string t2)
static T Func(T t1, T t2)
static const std::string Name()