-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathROperator_BasicUnary.hxx
144 lines (119 loc) · 4.26 KB
/
ROperator_BasicUnary.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef TMVA_EXPERIMENTAL_SOFIE_ROPERATOR_BASIC_UNARY
#define TMVA_EXPERIMENTAL_SOFIE_ROPERATOR_BASIC_UNARY
#include <TMVA/ROperator.hxx>
#include <TMVA/RModel.hxx>
#include <TMVA/SOFIE_common.hxx>
namespace TMVA {
namespace Experimental {
namespace SOFIE {
enum class EBasicUnaryOperator {
kReciprocal,
kSqrt,
kNeg,
kExp,
kLog,
kSin,
kCos,
kAbs,
kRound
};
template <typename T, EBasicUnaryOperator Op>
struct UnaryOpTraits {
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kReciprocal> {
static std::string Name() { return "Reciprocal"; }
static std::string Op(const std::string &X) { return "1/" + X; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kSqrt> {
static std::string Name() { return "Sqrt"; }
static std::string Op(const std::string &X) { return "std::sqrt(" + X + ")"; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kNeg> {
static std::string Name() { return "Neg"; }
static std::string Op(const std::string &X) { return "-" + X; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kExp> {
static std::string Name() { return "Exp"; }
static std::string Op(const std::string &X) { return "std::exp(" + X + ")"; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kLog> {
static std::string Name() { return "Log"; }
static std::string Op(const std::string &X) { return "std::log(" + X + ")"; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kSin> {
static std::string Name() { return "Sin"; }
static std::string Op(const std::string &X) { return "std::sin(" + X + ")"; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kCos> {
static std::string Name() { return "Cos"; }
static std::string Op(const std::string &X) { return "std::cos(" + X + ")"; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kAbs> {
static std::string Name() { return "Abs"; }
static std::string Op(const std::string &X) { return "std::abs(" + X + ")"; }
};
template <typename T>
struct UnaryOpTraits<T, EBasicUnaryOperator::kRound> {
static std::string Name() { return "Round"; }
static std::string Op(const std::string &X)
{
return "(std::fabs(" + X + "- std::trunc(" + X + ")) == 0.5) ? std::trunc(" + X + ") : std::round(" + X + ");";
}
};
template <typename T, EBasicUnaryOperator Op>
class ROperator_BasicUnary final : public ROperator {
private:
std::string fNX;
std::string fNY;
std::vector<size_t> fShapeX;
std::vector<size_t> fShapeY;
public:
ROperator_BasicUnary() {}
ROperator_BasicUnary(std::string nameX, std::string nameY)
: fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
{
fInputTensorNames = { fNX };
fOutputTensorNames = { fNY };
}
std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override { return input; }
std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override { return input; }
void Initialize(RModel& model) override {
if (!model.CheckIfTensorAlreadyExist(fNX)) {
throw std::runtime_error("TMVA::SOFIE - Tensor " + fNX + " not found.");
}
fShapeX = model.GetTensorShape(fNX);
fShapeY = ShapeInference({fShapeX})[0];
model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeY);
}
std::string Generate(std::string OpName) override
{
OpName = "op_" + OpName;
std::stringstream out;
out << SP << "\n//---- Operator" << UnaryOpTraits<T, Op>::Name() << " " << OpName << "\n";
size_t length = ConvertShapeToLength(fShapeX);
out << SP << "for (size_t i = 0; i < " << length << "; i++) {\n";
out << SP << SP << "tensor_" << fNY << "[i] = " << UnaryOpTraits<T, Op>::Op("tensor_" + fNX + "[i]") << ";\n";
out << SP << "}\n";
return out.str();
}
std::vector<std::string> GetStdLibs() override {
if (Op == EBasicUnaryOperator::kSqrt || Op == EBasicUnaryOperator::kExp || Op == EBasicUnaryOperator::kLog ||
Op == EBasicUnaryOperator::kRound) {
return { std::string("cmath") };
} else {
return {};
}
}
};
} // namespace SOFIE
} // namespace Experimental
} // namespace TMVA
#endif