File indexing completed on 2026-10-31 09:46:09
0001 #ifndef TMVA_EXPERIMENTAL_SOFIE_ROPERATOR_BASIC_UNARY
0002 #define TMVA_EXPERIMENTAL_SOFIE_ROPERATOR_BASIC_UNARY
0003
0004 #include <TMVA/ROperator.hxx>
0005 #include <TMVA/RModel.hxx>
0006 #include <TMVA/SOFIE_common.hxx>
0007
0008 namespace TMVA {
0009 namespace Experimental {
0010 namespace SOFIE {
0011
0012 enum class EBasicUnaryOperator { kReciprocal, kSqrt , kNeg, kExp, kLog, kSin, kCos, kAbs, kSoftplus, kAtan, kFloor };
0013
0014 template <typename T, EBasicUnaryOperator Op>
0015 struct UnaryOpTraits {
0016 };
0017
0018 template <typename T>
0019 struct UnaryOpTraits<T, EBasicUnaryOperator::kReciprocal> {
0020 static std::string Name() { return "Reciprocal"; }
0021 static std::string Op(const std::string &X) { return "1/" + X; }
0022 };
0023
0024 template <typename T>
0025 struct UnaryOpTraits<T, EBasicUnaryOperator::kSqrt> {
0026 static std::string Name() { return "Sqrt"; }
0027 static std::string Op(const std::string &X) { return "std::sqrt(" + X + ")"; }
0028 };
0029
0030 template <typename T>
0031 struct UnaryOpTraits<T, EBasicUnaryOperator::kNeg> {
0032 static std::string Name() { return "Neg"; }
0033 static std::string Op(const std::string &X) { return "-" + X; }
0034 };
0035
0036 template <typename T>
0037 struct UnaryOpTraits<T, EBasicUnaryOperator::kExp> {
0038 static std::string Name() { return "Exp"; }
0039 static std::string Op(const std::string &X) { return "std::exp(" + X + ")"; }
0040 };
0041
0042 template <typename T>
0043 struct UnaryOpTraits<T, EBasicUnaryOperator::kLog> {
0044 static std::string Name() { return "Log"; }
0045 static std::string Op(const std::string &X) { return "std::log(" + X + ")"; }
0046 };
0047
0048 template <typename T>
0049 struct UnaryOpTraits<T, EBasicUnaryOperator::kSin> {
0050 static std::string Name() { return "Sin"; }
0051 static std::string Op(const std::string &X) { return "std::sin(" + X + ")"; }
0052 };
0053
0054 template <typename T>
0055 struct UnaryOpTraits<T, EBasicUnaryOperator::kCos> {
0056 static std::string Name() { return "Cos"; }
0057 static std::string Op(const std::string &X) { return "std::cos(" + X + ")"; }
0058 };
0059
0060 template <typename T>
0061 struct UnaryOpTraits<T, EBasicUnaryOperator::kAbs> {
0062 static std::string Name() { return "Abs"; }
0063 static std::string Op(const std::string &X) { return "std::abs(" + X + ")"; }
0064 };
0065
0066 template <typename T>
0067 struct UnaryOpTraits<T, EBasicUnaryOperator::kSoftplus> {
0068 static std::string Name() { return "Softplus"; }
0069 static std::string Op(const std::string &X) { return "std::log(std::exp(" + X + ") + 1)"; }
0070 };
0071
0072 template <typename T>
0073 struct UnaryOpTraits<T, EBasicUnaryOperator::kAtan> {
0074 static std::string Name() { return "Atan"; }
0075 static std::string Op(const std::string &X) { return "std::atan(" + X + ")"; }
0076 };
0077
0078 template <typename T>
0079 struct UnaryOpTraits<T, EBasicUnaryOperator::kFloor> {
0080 static std::string Name() { return "Floor"; }
0081 static std::string Op(const std::string &X) { return "std::floor(" + X + ")"; }
0082 };
0083
0084 template <typename T, EBasicUnaryOperator Op>
0085 class ROperator_BasicUnary final : public ROperator {
0086 private:
0087 std::string fNX;
0088 std::string fNY;
0089
0090 std::vector<Dim> fShapeX;
0091 std::vector<Dim> fShapeY;
0092
0093 public:
0094 ROperator_BasicUnary() {}
0095
0096 ROperator_BasicUnary(std::string nameX, std::string nameY)
0097 : fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
0098 {
0099 fInputTensorNames = { fNX };
0100 fOutputTensorNames = { fNY };
0101 }
0102
0103 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override { return input; }
0104
0105 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override { return input; }
0106
0107 void Initialize(RModel& model) override {
0108 if (!model.CheckIfTensorAlreadyExist(fNX)) {
0109 throw std::runtime_error("TMVA::SOFIE - Tensor " + fNX + " not found.");
0110 }
0111 fShapeX = model.GetDimTensorShape(fNX);
0112 fShapeY = fShapeX;
0113 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShapeY);
0114
0115 model.AddNeededStdLib("cmath");
0116 }
0117
0118 std::string Generate(std::string OpName) override
0119 {
0120 OpName = "op_" + OpName;
0121 std::stringstream out;
0122
0123 out << SP << "\n//---- Operator" << UnaryOpTraits<T, Op>::Name() << " " << OpName << "\n";
0124 auto length = ConvertDimShapeToLength(fShapeX);
0125 out << SP << "for (size_t i = 0; i < " << length << "; i++) {\n";
0126 out << SP << SP << "tensor_" << fNY << "[i] = " << UnaryOpTraits<T, Op>::Op("tensor_" + fNX + "[i]") << ";\n";
0127 out << SP << "}\n";
0128 return out.str();
0129 }
0130
0131 std::vector<std::string> GetStdLibs() override {
0132 return { std::string("cmath") };
0133 }
0134 };
0135
0136 }
0137 }
0138 }
0139
0140 #endif