File indexing completed on 2025-01-18 10:11:06
0001 #ifndef TMVA_SOFIE_ROPERATOR_EyeLike
0002 #define TMVA_SOFIE_ROPERATOR_EyeLike
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 class ROperator_EyeLike final : public ROperator
0015 {
0016
0017 private:
0018
0019 int fdtype = static_cast<int>(ETensorType::FLOAT);
0020 int fk = 0;
0021 std::string fNX;
0022 std::string fNY;
0023 std::vector<size_t> fShape;
0024
0025 public:
0026 ROperator_EyeLike(){}
0027 ROperator_EyeLike(int dtype, int k, std::string nameX, std::string nameY):
0028 fdtype(dtype), fk(k), fNX(UTILITY::Clean_name(nameX)), fNY(UTILITY::Clean_name(nameY)){}
0029
0030 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input){
0031 return input;
0032 }
0033
0034 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input){
0035 auto ret = input;
0036 return ret;
0037 }
0038
0039 void Initialize(RModel& model){
0040 if (model.CheckIfTensorAlreadyExist(fNX) == false){
0041 throw std::runtime_error("TMVA SOFIE EyeLike Op Input Tensor is not found in model");
0042 }
0043 fShape = model.GetTensorShape(fNX);
0044 if (fShape.size() != 2)
0045 throw std::runtime_error("TMVA SOFIE EyeLike Op Input Tensor is not of rank 2");
0046
0047 if(fdtype){
0048 ETensorType extractedType = static_cast<ETensorType>(fdtype);
0049 model.AddIntermediateTensor(fNY, extractedType, fShape);
0050 }
0051 else{
0052 model.AddIntermediateTensor(fNY, model.GetTensorType(fNX), fShape);
0053 }
0054 }
0055
0056
0057 std::string Generate(std::string OpName){
0058 OpName = "op_" + OpName;
0059 if (fShape.empty()){
0060 throw std::runtime_error("TMVA SOFIE Operator EyeLike called to Generate without being initialized first");
0061 }
0062 auto length = ConvertShapeToLength(fShape);
0063 auto stride = TMVA::Experimental::SOFIE::UTILITY::ComputeStrideFromShape(fShape);
0064 std::stringstream out;
0065 out << SP << "///--------EyeLike operator\n" << std::endl;
0066
0067 out << SP << "(void) tensor_" << fNX << ";\n";
0068
0069 out << SP << "fTensor_" << fNY << ".assign(" << length << ", 0);\n";
0070 out << SP << "for (int i = 0; i < " << fShape[0] << "; i++) {\n";
0071 out << SP << SP << "int j = i +" << fk << ";\n";
0072 out << SP << SP << "if (j >= 0 && j < " << fShape[1] << ")\n";
0073 out << SP << SP << SP << "tensor_" << fNY << "[i * " << fShape[1] << "+ j] = 1;\n";
0074 out << SP << "}\n";
0075 return out.str();
0076 }
0077
0078 };
0079
0080 }
0081 }
0082 }
0083
0084
0085 #endif