Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:06

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    /* Attributes*/
0021    float falpha= 1.0; //default value
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       if(std::is_same<T, float>::value){
0033          fType = "float";
0034       }
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){
0041       return input;
0042    }
0043 
0044    std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
0045       auto ret = input; //suggest copy to compiler
0046       return ret;
0047    }
0048 
0049    void Initialize(RModel& model){
0050       if (model.CheckIfTensorAlreadyExist(fNX) == false){   //input must be a graph input, or already initialized intermediate tensor
0051          throw std::runtime_error("TMVA SOFIE Elu Op Input Tensor is not found in model");
0052       }
0053       fShape = model.GetTensorShape(fNX);
0054       model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
0055    }
0056 
0057 
0058    std::string Generate(std::string OpName){
0059       OpName = "op_" + OpName;
0060       if (fShape.empty()) {
0061          throw std::runtime_error("TMVA SOFIE Operator Elu called to Generate without being initialized first");
0062       }
0063       std::stringstream out;
0064       size_t length = ConvertShapeToLength(fShape);
0065 
0066       out << SP << "float " << OpName << "_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10) << falpha << ";\n";
0067 
0068       out << "\n//------ ELU \n";
0069       out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
0070       out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX << "[id] : "<< OpName << "_alpha * std::exp(tensor_"<< fNX<<"[id]) - 1);\n";
0071       out << SP << "}\n";
0072       return out.str();
0073    }
0074 
0075 };
0076 
0077 }//SOFIE
0078 }//Experimental
0079 }//TMVA
0080 
0081 
0082 #endif //TMVA_SOFIE_ROPERATOR_Elu