1#ifndef TMVA_SOFIE_ROperator_BasicBinary
2#define TMVA_SOFIE_ROperator_BasicBinary
11namespace Experimental {
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 +
")"; }
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 +
")"; }
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");
114 if (model.IsDynamicTensor(
fNA)) {
121 if (model.IsDynamicTensor(
fNB)) {
129 std::cout << BinaryOperatorTrait<T, Op>::Name() <<
" : input " <<
fNA <<
" is dynamic "
144 if (model.IsConstantTensor(
fNA) && model.IsConstantTensor(
fNB)) {
153 auto data = model.GetInitializedTensorData(
fNA);
156 std::default_delete<T[]>());
170 auto data = model.GetInitializedTensorData(
fNB);
177 std::default_delete<T[]>());
195 auto dataA =
static_cast<T *
>(model.GetInitializedTensorData(
nameA).get());
196 auto dataB =
static_cast<T *
>(model.GetInitializedTensorData(
nameB).get());
198 for (
size_t i = 0; i <
dataY.size(); i++) {
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.IsInitializedTensor(
fNB)) ||
213 (model.IsShapeTensor(
fNB) && model.IsInitializedTensor(
fNA)))
218 if (model.IsShapeTensor(
fNA))
220 if (model.IsShapeTensor(
fNB))
229 auto data =
static_cast<int64_t *
>(model.GetInitializedTensorData(
name).get());
231 for (
size_t i = 0; i <
lengthY; i++) {
232 if (!shape.empty() &&
lengthY == shape[0])
238 if (model.IsInitializedTensor(
fNA)) {
240 }
else if (model.IsInitializedTensor(
fNB)) {
246 for (
size_t i = 0; i <
lengthY; i++) {
252 dimValY[i] =
Dim{res,
static_cast<size_t>(-1)};
256 if (model.Verbose()) {
266 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNA),
fShapeY);
267 if (model.Verbose()) {
286 auto inputNames = model.GetInputTensorNames();
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) {
315 if (model.Verbose()) {
324 std::stringstream out;
336 std::stringstream out;
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 "
363 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast A->B in operator "
369 <<
" != 1 || " <<
fDimShapeB[i] <<
" != 1))\n";
370 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast shapes in operator " <<
opName
386 for (
size_t i = 0; i <
fDimShapeA.size(); ++i) {
395 for (
int j = 0;
j < 3;
j++)
402 for (
size_t i = 0; i <
fDimShapeB.size(); ++i) {
411 for (
int j = 0;
j < 3;
j++)
419 for (
size_t i = 0; i <
fDimShapeY.size(); ++i) {
423 out <<
"for (size_t idx_" << i <<
" = 0; idx_" << i <<
" < " <<
fDimShapeY[i]
424 <<
"; ++idx_" << i <<
"){\n";
432 for (
int j = 0;
j < 3;
j++)
435 for (
int j = 0;
j <
nloop + 1;
j++) out <<
SP;
441 for (
int i =
nloop; i > 0; i--) {
442 for (
int j = 0;
j < i;
j++) out <<
SP;
451 return {std::string(
"cmath")};
462 throw std::runtime_error(
463 (
"TMVA::SOFIE - Unsupported - Operator BasicBinary does not yet support input type " +
layerDType).c_str());
466 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Add>>(
nameA,
nameB,
nameY);
468 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Sub>>(
nameA,
nameB,
nameY);
470 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Mul>>(
nameA,
nameB,
nameY);
472 throw std::runtime_error(
473 (
"TMVA::SOFIE - Unsupported - Operator BasicBinary does not yet support layer type " +
layerType).c_str());
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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::unique_ptr< ROperator > createBasicBinary(std::string layerDType, std::string layerType, std::string nameA, std::string nameB, std::string nameY)
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)
ETensorType ConvertStringToType(std::string type)
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()