File indexing completed on 2025-09-16 09:08:53
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 fInputTensorNames = { fNX };
0041 fOutputTensorNames = { fNY };
0042 }
0043
0044 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
0045 return input;
0046 }
0047
0048 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
0049 auto ret = input;
0050 return ret;
0051 }
0052
0053 void Initialize(RModel& model) override {
0054 if (model.CheckIfTensorAlreadyExist(fNX) == false){
0055 throw std::runtime_error("TMVA SOFIE Leaky Relu Op Input Tensor is not found in model");
0056 }
0057 fShape = model.GetTensorShape(fNX);
0058 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
0059 }
0060
0061
0062 std::string Generate(std::string OpName) override {
0063 OpName = "op_" + OpName;
0064 if (fShape.empty()) {
0065 throw std::runtime_error("TMVA SOFIE Operator Leaky Relu called to Generate without being initialized first");
0066 }
0067 std::stringstream out;
0068 size_t length = ConvertShapeToLength(fShape);
0069
0070 out << SP << "constexpr float " << OpName << "_alpha = " << std::setprecision(std::numeric_limits<float>::max_digits10) << falpha << ";\n";
0071
0072 out << "\n//------ LEAKY RELU\n";
0073 out << SP << "for (int id = 0; id < " << length << " ; id++){\n";
0074 out << SP << SP << "tensor_" << fNY << "[id] = ((tensor_" << fNX << "[id] >= 0 )? tensor_" << fNX << "[id] : "<< OpName << "_alpha * tensor_"<< fNX<<"[id]);\n";
0075 out << SP << "}\n";
0076 return out.str();
0077 }
0078
0079 };
0080
0081 }
0082 }
0083 }
0084
0085
0086 #endif