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 +
")"; }
52 static T
Func(T
t1, T 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 +
")"; }
64 static T
Func(T
t1, T t2) {
return std::fmod(
t1, t2); }
67template <
typename T, EBasicBinaryOperator Op>
88 :
fNA(UTILITY::Clean_name(nameA)),
fNB(UTILITY::Clean_name(nameB)),
fNY(UTILITY::Clean_name(nameY))
97 if (!model.CheckIfTensorAlreadyExist(
fNA)) {
98 throw std::runtime_error(std::string(
"TMVA SOFIE Binary Op Input Tensor ") +
fNA +
"is not found in model");
100 if (!model.CheckIfTensorAlreadyExist(
fNB)) {
101 throw std::runtime_error(std::string(
"TMVA SOFIE Binary Op Input Tensor ") +
fNB +
"is not found in model");
103 int dynamicInputs = 0;
104 if (model.IsDynamicTensor(
fNA)) {
111 if (model.IsDynamicTensor(
fNB)) {
118 if (dynamicInputs & 1 && model.Verbose())
119 std::cout << BinaryOperatorTrait<T, Op>::Name() <<
" : input " <<
fNA <<
" is dynamic "
121 if (dynamicInputs & 2 && model.Verbose())
129 if (dynamicInputs == 0) {
134 if (model.IsConstantTensor(
fNA) && model.IsConstantTensor(
fNB)) {
143 auto data = model.GetInitializedTensorData(
fNA);
144 std::shared_ptr<void> broadcastedData(
146 std::default_delete<T[]>());
150 static_cast<T *
>(broadcastedData.get()))
160 auto data = model.GetInitializedTensorData(
fNB);
165 std::shared_ptr<void> broadcastedData(
167 std::default_delete<T[]>());
172 static_cast<T *
>(broadcastedData.get()))
185 auto dataA =
static_cast<T *
>(model.GetInitializedTensorData(nameA).get());
186 auto dataB =
static_cast<T *
>(model.GetInitializedTensorData(nameB).get());
187 std::vector<T> dataY(lengthY);
188 for (
size_t i = 0; i < dataY.size(); i++) {
191 model.AddConstantTensor<T>(
fNY,
fShapeY, dataY.data());
193 model.SetNotWritableInitializedTensor(nameA);
194 model.SetNotWritableInitializedTensor(nameB);
196 if (model.Verbose()) {
201 }
else if (((model.IsShapeTensor(
fNA) && model.IsShapeTensor(
fNB)) ||
202 (model.IsShapeTensor(
fNA) && model.IsInitializedTensor(
fNB)) ||
203 (model.IsShapeTensor(
fNB) && model.IsInitializedTensor(
fNA)))
206 std::vector<Dim> dimValA;
207 std::vector<Dim> dimValB;
208 if (model.IsShapeTensor(
fNA))
209 dimValA = model.GetShapeTensorValues(
fNA);
210 if (model.IsShapeTensor(
fNB))
211 dimValB = model.GetShapeTensorValues(
fNB);
214 if (dimValA.size() == 1) dimValA = std::vector<Dim>(
fShapeY[0], dimValA[0]);
215 if (dimValB.size() == 1) dimValB = std::vector<Dim>(
fShapeY[0], dimValB[0]);
218 auto convertDataToDim = [&](
const std::string &
name,
const std::vector<size_t> & shape, std::vector<Dim> & dimValues) {
219 auto data =
static_cast<int64_t *
>(model.GetInitializedTensorData(
name).get());
220 dimValues.resize(lengthY);
221 for (
size_t i = 0; i < lengthY; i++) {
222 if (!shape.empty() && lengthY == shape[0])
223 dimValues[i] =
Dim{
static_cast<size_t>(
data[i])};
225 dimValues[i] =
Dim{
static_cast<size_t>(
data[0])};
228 if (model.IsInitializedTensor(
fNA)) {
230 }
else if (model.IsInitializedTensor(
fNB)) {
235 std::vector<Dim> dimValY(lengthY);
236 for (
size_t i = 0; i < lengthY; i++) {
237 if (!dimValA[i].isParam && !dimValB[i].isParam) {
242 dimValY[i] =
Dim{res,
static_cast<size_t>(-1)};
245 model.AddShapeTensor(
fNY,dimValY,
fShapeY.empty());
246 if (model.Verbose()) {
256 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNA),
fShapeY);
257 if (model.Verbose()) {
275 auto IsInputDimParam = [&](
const std::string &
p) {
276 auto inputNames = model.GetInputTensorNames();
277 for (
auto &
input : inputNames) {
278 for (
auto &i_s : model.GetDimTensorShape(
input)) {
279 if (i_s.isParam && i_s.param ==
p)
285 for (
size_t i = 0; i <
fDimShapeY.size(); i++) {
287 if (s.isParam && s.param.find(
"std::max") != std::string::npos) {
294 }
else if (IsInputDimParam(
fDimShapeB[i].param)) {
305 if (model.Verbose()) {
314 std::stringstream out;
324 opName =
"op_" + opName;
326 std::stringstream out;
338 out <<
SP <<
"if (" << lengthA <<
"!=" << lengthB <<
") {\n";
341 for (
size_t i = 0; i <
fDimShapeY.size(); i++) {
346 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast B->A in operator "
347 << opName <<
"\");\n";
353 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast A->B in operator "
354 << opName <<
"\");\n";
359 <<
" != 1 || " <<
fDimShapeB[i] <<
" != 1))\n";
360 out <<
SP <<
SP <<
SP <<
"throw std::runtime_error(\"SOFIE - Cannot broadcast shapes in operator " << opName
371 std::string compute_idx_A, compute_idx_B, compute_idx_Y;
376 for (
size_t i = 0; i <
fDimShapeA.size(); ++i) {
380 if (stridesA[i].GetVal() !=
"1")
381 compute_idx_A +=
" * " + stridesA[i].GetVal();
382 compute_idx_A +=
" + ";
385 for (
int j = 0; j < 3; j++)
386 compute_idx_A.pop_back();
392 for (
size_t i = 0; i <
fDimShapeB.size(); ++i) {
396 if (stridesB[i].GetVal() !=
"1")
397 compute_idx_B +=
" * " + stridesB[i].GetVal();
398 compute_idx_B +=
" + ";
401 for (
int j = 0; j < 3; j++)
402 compute_idx_B.pop_back();
409 for (
size_t i = 0; i <
fDimShapeY.size(); ++i) {
412 for (
int j = 0; j < nloop; j++) out <<
SP;
413 out <<
"for (size_t idx_" << i <<
" = 0; idx_" << i <<
" < " <<
fDimShapeY[i]
414 <<
"; ++idx_" << i <<
"){\n";
415 compute_idx_Y +=
"idx_" + std::to_string(i);
416 if (stridesY[i].GetVal() !=
"1")
417 compute_idx_Y +=
" * " + stridesY[i].GetVal();
418 compute_idx_Y +=
" + ";
422 for (
int j = 0; j < 3; j++)
423 compute_idx_Y.pop_back();
425 for (
int j = 0; j < nloop + 1; j++) out <<
SP;
426 out <<
"tensor_" <<
fNY <<
"[" << compute_idx_Y <<
"] = "
428 "tensor_" +
fNB +
"[" + compute_idx_B +
"]")
431 for (
int i = nloop; i > 0; i--) {
432 for (
int j = 0; j < i; j++) out <<
SP;
441 return {std::string(
"cmath")};
448inline std::unique_ptr<ROperator>
createBasicBinary(std::string layerDType, std::string layerType, std::string nameA,
449 std::string nameB, std::string nameY)
452 throw std::runtime_error(
453 (
"TMVA::SOFIE - Unsupported - Operator BasicBinary does not yet support input type " + layerDType).c_str());
455 if (layerType ==
"Add")
456 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Add>>(nameA, nameB, nameY);
457 if (layerType ==
"Subtract")
458 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Sub>>(nameA, nameB, nameY);
459 if (layerType ==
"Multiply")
460 return std::make_unique<ROperator_BasicBinary<float, EBasicBinaryOperator::Mul>>(nameA, nameB, nameY);
462 throw std::runtime_error(
463 (
"TMVA::SOFIE - Unsupported - Operator BasicBinary does not yet support layer type " + layerType).c_str());
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< size_t > fShapeY
std::string fNBroadcastedA
std::vector< Dim > fDimShapeA
std::string fNBroadcastedB
void Initialize(RModel &model) 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()