1#ifndef TMVA_SOFIE_ROperator_BasicBinary
2#define TMVA_SOFIE_ROperator_BasicBinary
12namespace Experimental {
17template <
typename T, EBasicBinaryOperator Op1>
22 static const std::string
Name() {
return "Add"; }
23 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return t1 +
" + " +
t2; }
29 static const std::string
Name() {
return "Sub"; }
30 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return t1 +
" - " +
t2; }
36 static const std::string
Name() {
return "Mul"; }
37 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return t1 +
" * " +
t2; }
43 static const std::string
Name() {
return "Div"; }
44 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return t1 +
" / " +
t2; }
50 static const std::string
Name() {
return "Pow"; }
51 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return "std::pow(" +
t1 +
"," +
t2 +
")"; }
56 static const std::string
Name() {
return "Mod"; }
57 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return "(" +
t1 +
" % " +
t2 +
")"; }
62 static const std::string
Name() {
return "FMod"; }
63 static std::string
Op(
const std::string &
t1,
const std::string
t2) {
return "std::fmod(" +
t1 +
"," +
t2 +
")"; }
67template <
typename T, EBasicBinaryOperator Op>
101 auto ret = std::vector<std::vector<size_t>>(1,
input[0]);
108 if (!model.CheckIfTensorAlreadyExist(
fNA)) {
109 throw std::runtime_error(std::string(
"TMVA SOFIE Binary Op Input Tensor ") +
fNA +
"is not found in model");
111 if (!model.CheckIfTensorAlreadyExist(
fNB)) {
112 throw std::runtime_error(std::string(
"TMVA SOFIE Binary Op Input Tensor ") +
fNB +
"is not found in model");
115 if (model.IsDynamicTensor(
fNA)) {
122 if (model.IsDynamicTensor(
fNB)) {
130 std::cout << BinaryOperatorTrait<T, Op>::Name() <<
" : input " <<
fNA <<
" is dynamic "
145 if (model.IsConstantTensor(
fNA) && model.IsConstantTensor(
fNB)) {
154 auto data = model.GetInitializedTensorData(
fNA);
157 std::default_delete<T[]>());
171 auto data = model.GetInitializedTensorData(
fNB);
178 std::default_delete<T[]>());
196 auto dataA =
static_cast<T *
>(model.GetInitializedTensorData(
nameA).get());
197 auto dataB =
static_cast<T *
>(model.GetInitializedTensorData(
nameB).get());
199 for (
size_t i = 0; i <
dataY.size(); i++) {
204 model.SetNotWritableInitializedTensor(
nameA);
205 model.SetNotWritableInitializedTensor(
nameB);
207 if (model.Verbose()) {
212 }
else if (((model.IsShapeTensor(
fNA) && model.IsShapeTensor(
fNB)) ||
213 (model.IsShapeTensor(
fNA) && model.IsInitializedTensor(
fNB)) ||
214 (model.IsShapeTensor(
fNB) && model.IsInitializedTensor(
fNA)))
219 if (model.IsShapeTensor(
fNA))
221 if (model.IsShapeTensor(
fNB))
230 auto data =
static_cast<int64_t *
>(model.GetInitializedTensorData(
name).get());
232 for (
size_t i = 0; i <
lengthY; i++) {
233 if (!shape.empty() &&
lengthY == shape[0])
239 if (model.IsInitializedTensor(
fNA)) {
241 }
else if (model.IsInitializedTensor(
fNB)) {
247 for (
size_t i = 0; i <
lengthY; i++) {
253 dimValY[i] =
Dim{res,
static_cast<size_t>(-1)};
257 if (model.Verbose()) {
267 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNA),
fShapeY);
268 if (model.Verbose()) {
287 auto inputNames = model.GetInputTensorNames();
289 for (
auto &
i_s : model.GetDimTensorShape(
input)) {
290 if (
i_s.isParam &&
i_s.param ==
p)
296 for (
size_t i = 0; i <
fDimShapeY.size(); i++) {
298 if (s.isParam && s.param.find(
"std::max") != std::string::npos) {
316 if (model.Verbose()) {
325 std::stringstream out;
337 std::stringstream out;
352 for (
size_t i = 0; i <
fDimShapeY.size(); i++) {
357 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast B->A in operator "
364 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast A->B in operator "
370 <<
" != 1 || " <<
fDimShapeB[i] <<
" != 1))\n";
371 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast shapes in operator " <<
opName
387 for (
size_t i = 0; i <
fDimShapeA.size(); ++i) {
396 for (
int j = 0;
j < 3;
j++)
403 for (
size_t i = 0; i <
fDimShapeB.size(); ++i) {
412 for (
int j = 0;
j < 3;
j++)
420 for (
size_t i = 0; i <
fDimShapeY.size(); ++i) {
424 out <<
"for (size_t idx_" << i <<
" = 0; idx_" << i <<
" < " <<
fDimShapeY[i]
425 <<
"; ++idx_" << i <<
"){\n";
433 for (
int j = 0;
j < 3;
j++)
436 for (
int j = 0;
j <
nloop + 1;
j++) out <<
SP;
442 for (
int i =
nloop; i > 0; i--) {
443 for (
int j = 0;
j < i;
j++) out <<
SP;
452 return {std::string(
"cmath")};
463 throw std::runtime_error(
464 (
"TMVA::SOFIE - Unsupported - Operator BasicBinary does not yet support input type " +
layerDType).c_str());
467 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Add>>(
nameA,
nameB,
nameY);
469 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Sub>>(
nameA,
nameB,
nameY);
471 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Mul>>(
nameA,
nameB,
nameY);
473 throw std::runtime_error(
474 (
"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()