File indexing completed on 2025-01-30 10:23:00
0001 #ifndef TMVA_SOFIE_ROPERATOR_LeakyRelu
0002 #define TMVA_SOFIE_ROPERATOR_LeakyRelu
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_LeakyRelu final : public ROperator
0016 {
0017
0018 private:
0019
0020
0021 float falpha=0.01;
0022 std::string fNX;
0023 std::string fNY;
0024 std::vector<size_t> fShape;
0025 std::string fType;
0026
0027 public:
0028 ROperator_LeakyRelu(){}
0029 ROperator_LeakyRelu(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
0037 std::runtime_error("TMVA SOFIE Encountered unsupported type parsing a Leaky Relu operator");
0038 }
0039 }
0040
0041 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
0042 return input;
0043 }
0044
0045 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
0046 auto ret = input;
0047 return ret;
0048 }
0049
0050 void Initialize(RModel& model){
0051 if (model.CheckIfTensorAlreadyExist(fNX) == false){
0052 throw std::runtime_error("TMVA SOFIE Leaky Relu 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
0059 std::string Generate(std::string OpName){
0060 OpName = "op_" + OpName;
0061 if (fShape.empty()) {
0062 throw std::runtime_error("TMVA SOFIE Operator Leaky Relu 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) << falpha << ";\n";
0068
0069 out << "\n//------ LEAKY RELU\n";
0070 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
0071 out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX << "[id] : "<< OpName << "_alpha * tensor_"<< fNX<<"[id]);\n";
0072 out << SP << "}\n";
0073 return out.str();
0074 }
0075
0076 };
0077
0078 }
0079 }
0080 }
0081
0082
0083 #endif