File indexing completed on 2025-01-18 10:11:08
0001 #ifndef TMVA_SOFIE_ROPERATOR_RANGE
0002 #define TMVA_SOFIE_ROPERATOR_RANGE
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_Range final : public ROperator
0016 {
0017 private:
0018
0019 std::string fNStart;
0020 std::string fNLimit;
0021 std::string fNDelta;
0022 std::string fNOutput;
0023 std::vector<Dim> fShape;
0024 std::string fType;
0025
0026 public:
0027 ROperator_Range(){}
0028
0029 ROperator_Range(std::string start, std::string limit, std::string delta, std::string nameOutput):
0030 fNStart(start), fNLimit(limit), fNDelta(delta),
0031 fNOutput(UTILITY::Clean_name(nameOutput)) {
0032 if (std::is_same<T, float>::value) {
0033 fType = "float";
0034 } else if (std::is_same<T, int64_t>::value) {
0035 fType = "int64_t";
0036 }
0037 static_assert( (std::is_same_v<T, float> || std::is_same_v<T, int64_t>),
0038 "TMVA::SOFIE - Unsupported type by Range operator");
0039 }
0040
0041 std::vector<ETensorType> TypeInference(std::vector<ETensorType> input) override {
0042 return input;
0043 }
0044
0045 std::vector<std::vector<size_t>> ShapeInference(std::vector<std::vector<size_t>> input) override {
0046 auto ret = input;
0047 return ret;
0048 }
0049
0050 void Initialize(RModel& model) override {
0051
0052 if (!model.CheckIfTensorAlreadyExist(fNStart)) {
0053 throw
0054 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNStart + "is not found in model");
0055 }
0056 if (!model.CheckIfTensorAlreadyExist(fNLimit)) {
0057 throw
0058 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNLimit + "is not found in model");
0059 }
0060 if (!model.CheckIfTensorAlreadyExist(fNDelta)) {
0061 throw
0062 std::runtime_error("TMVA SOFIE Range Op Input Tensor " + fNDelta + "is not found in model");
0063 }
0064 ETensorType type = ConvertStringToType(fType);
0065 fShape = {Dim{"range_size"}};
0066 model.AddDynamicTensor(fNOutput, type, fShape);
0067 }
0068
0069 std::string Generate(std::string OpName) override {
0070 OpName = "op_" + OpName;
0071 if (fShape.empty()) {
0072 throw std::runtime_error("TMVA SOFIE Range operator called to Generate without being initialized first");
0073 }
0074 std::stringstream out;
0075 out << "\n//------ Range\n";
0076 std::string sizeName = fShape[0].param;
0077 out << SP << "size_t " << sizeName << " = static_cast<size_t>(std::max(std::ceil((static_cast<float>(*tensor_" << fNLimit << ") - static_cast<float>(*tensor_" << fNStart << ")) / static_cast<float>(*tensor_" << fNDelta << ")), 0.0f));\n";
0078 out << SP << "if (" << sizeName << " > " << "fTensor_" << fNOutput << ".size() ){\n";
0079 out << SP << SP << "fTensor_" << fNOutput << ".resize(" << sizeName << ");\n";
0080
0081 out << SP << SP << "tensor_" << fNOutput << " = fTensor_" << fNOutput << ".data();\n";
0082 out << SP << "}\n";
0083 out << SP << "for (size_t i = 0; i < " << sizeName << "; i++) {\n";
0084 out << SP << SP << "fTensor_" << fNOutput << "[i] = *tensor_" << fNStart << " + i * (*tensor_" << fNDelta << ");\n";
0085 out << SP << "}\n";
0086 return out.str();
0087 }
0088 };
0089
0090 }
0091 }
0092 }
0093
0094 #endif