File indexing completed on 2025-09-17 09:14:53
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
0018 private:
0019
0020
0021 float falpha= 1.0;
0022 std::string fNX;
0023 std::string fNY;
0024 std::vector<size_t> fShape;
0025 std::string fType;
0026
0027 public:
0028 ROperator_Elu(){}
0029 ROperator_Elu(float alpha,std::string nameX, std::string nameY):
0030 falpha(alpha),fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY))
0031 {
0032 fInputTensorNames = { fNX };
0033 fOutputTensorNames = { fNY };
0034
0035 if(std::is_same<T, float>::value){
0036 fType = "float";
0037 }
0038 else{
0039 throw std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Elu operator");
0040 }
0041 }
0042
0043 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
0044 return input;
0045 }
0046
0047 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
0048 auto ret = input;
0049 return ret;
0050 }
0051
0052 void Initialize(RModel& model) override {
0053 if (model.CheckIfTensorAlreadyExist(fNX) == false){
0054 throw std::runtime_error("TMVA SOFIE Elu Op Input Tensor is not found in model");
0055 }
0056 fShape = model.GetTensorShape(fNX);
0057 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
0058 }
0059
0060
0061 std::string Generate(std::string OpName) override {
0062 OpName = "op_" + OpName;
0063 if (fShape.empty()) {
0064 throw std::runtime_error("TMVA SOFIE Operator Elu called to Generate without being initialized first");
0065 }
0066 std::stringstream out;
0067 size_t length = ConvertShapeToLength(fShape);
0068
0069 out << SP << "float " << OpName << "_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10) << falpha << ";\n";
0070
0071 out << "\n//------ ELU \n";
0072 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
0073 out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX << "[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
0084
0085 #endif