File indexing completed on 2026-09-20 09:26:23
0001 #ifndef TMVA_SOFIE_ROPERATOR_Elu
0002 #define TMVA_SOFIE_ROPERATOR_Elu
0003
0004 #include "TMVA/SOFIE_common.hxx"
0005 #include "TMVA/ROperator.hxx"
0006 #include "TMVA/RModel.hxx"
0007
0008 #include <sstream>
0009
0010 namespace TMVA {
0011 namespace Experimental {
0012 namespace SOFIE {
0013
0014 template <typename T>
0015 class ROperator_Elu final : public ROperator {
0016
0017 private:
0018
0019 float falpha = 1.0;
0020 std::string fNX;
0021 std::string fNY;
0022 std::vector<size_t> fShape;
0023 std::string fType;
0024
0025 public:
0026 ROperator_Elu() {}
0027 ROperator_Elu(float alpha, std::string nameX, std::string nameY)
0028 : falpha(alpha), fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
0029 {
0030 fInputTensorNames = {fNX};
0031 fOutputTensorNames = {fNY};
0032
0033 if (std::is_same<T, float>::value) {
0034 fType = "float";
0035 } else {
0036 throw std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Elu operator");
0037 }
0038 }
0039
0040 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override { return input; }
0041
0042 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override
0043 {
0044 auto ret = input;
0045 return ret;
0046 }
0047
0048 void Initialize(RModel &model) override
0049 {
0050 if (model.CheckIfTensorAlreadyExist(fNX) ==
0051 false) {
0052 throw std::runtime_error("TMVA SOFIE Elu Op Input Tensor is not found in model");
0053 }
0054 fShape = model.GetTensorShape(fNX);
0055 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
0056 }
0057
0058 std::string Generate(std::string OpName) override
0059 {
0060 OpName = "op_" + OpName;
0061 if (fShape.empty()) {
0062 throw std::runtime_error("TMVA SOFIE Operator Elu called to Generate without being initialized first");
0063 }
0064 std::stringstream out;
0065 size_t length = ConvertShapeToLength(fShape);
0066
0067 out << SP << "float " << OpName << "_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10)
0068 << falpha << ";\n";
0069
0070 out << "\n//------ ELU \n";
0071 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
0072 out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX
0073 << "[id] : " << OpName << "_alpha * (std::exp(tensor_" << fNX << "[id]) - 1));\n";
0074 out << SP << "}\n";
0075 return out.str();
0076 }
0077 };
0078
0079 }
0080 }
0081 }
0082
0083 #endif