// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "range_schema_defs.h" #include "core/framework/tensorprotoutils.h" #include "core/graph/constants.h" #include "core/graph/op.h" #include #include #include namespace onnxruntime { namespace contrib { using ::ONNX_NAMESPACE::InferenceContext; using ::ONNX_NAMESPACE::OpSchema; using ::ONNX_NAMESPACE::OPTIONAL_VALUE; using ::ONNX_NAMESPACE::TensorProto; using ::ONNX_NAMESPACE::TensorProto_DataType; using ::ONNX_NAMESPACE::TensorShapeProto; // This Doc based on LSTM_ver7, and modification static const char* Range_ver1_doc = R"DOC( Creates a sequence of numbers that begins at `start` and extends by increments of `delta` up to but not including `limit`. )DOC"; template static T get_data(const TensorProto*) { fail_shape_inference("Unsupported non-raw-data data type!"); } template <> int32_t get_data(const TensorProto* shapeInitializer) { if (shapeInitializer->int32_data_size() > 0) return shapeInitializer->int32_data(0); fail_shape_inference("Can not get shape initializer data!"); } template <> int64_t get_data(const TensorProto* shapeInitializer) { if (shapeInitializer->int64_data_size() > 0) return shapeInitializer->int64_data(0); fail_shape_inference("Can not get shape initializer data!"); } template <> int16_t get_data(const TensorProto* shapeInitializer) { // ONNX packs INT16 initializer values into the int32_data repeated field. if (shapeInitializer->int32_data_size() > 0) return static_cast(shapeInitializer->int32_data(0)); fail_shape_inference("Can not get shape initializer data!"); } template <> float get_data(const TensorProto* shapeInitializer) { if (shapeInitializer->float_data_size() > 0) return shapeInitializer->float_data(0); fail_shape_inference("Can not get shape initializer data!"); } template <> double get_data(const TensorProto* shapeInitializer) { if (shapeInitializer->double_data_size() > 0) return shapeInitializer->double_data(0); fail_shape_inference("Can not get shape initializer data!"); } template static T GetFirstElement(const TensorProto* shapeInitializer) { if (shapeInitializer == nullptr) return T{1}; if (utils::HasRawData(*shapeInitializer)) { const std::string& bytes = shapeInitializer->raw_data(); if (bytes.size() < sizeof(T)) { fail_shape_inference("Range: raw_data size is smaller than the element size for the given data type."); } // std::string data is only char-aligned, so copy the bytes into a properly aligned value. T value; std::memcpy(&value, bytes.data(), sizeof(T)); return value; } return get_data(shapeInitializer); } template static int64_t CalcRangeDim(const TensorProto* startShapeInitializer, const TensorProto* limitShapeInitializer, const TensorProto* deltaShapeInitializer) { auto start = static_cast(GetFirstElement(startShapeInitializer)); auto limit = static_cast(GetFirstElement(limitShapeInitializer)); auto delta = static_cast(GetFirstElement(deltaShapeInitializer)); if (delta == 0) { fail_shape_inference("delta in Range operator can not be zero!"); } // Mirror the CPU kernel (core/providers/cpu/generator/range.cc ComputeRange) exactly so the // two paths stay byte-consistent: clamp empty or backward ranges to 0 and promote the // operands to double before the subtraction. Keep this expression identical to the kernel. double count = ceil((static_cast(limit) - static_cast(start)) / static_cast(delta)); if (!std::isfinite(count)) { fail_shape_inference("Range: the computed number of elements is not a finite value."); } // Empty or backward ranges clamp to 0; handle the non-positive case before the cast so a // large-magnitude negative count can never reach (and overflow) the int64 conversion below. if (count <= 0) { return 0; } // static_cast(INT64_MAX) rounds up to 2^63 (9223372036854775808.0), which is not // representable as int64_t, so reject any count at or above that boundary before the cast. if (count >= 9223372036854775808.0) { fail_shape_inference("Range: the computed number of elements exceeds the supported range."); } return static_cast(count); } static int64_t CalcResultDim(const TensorProto* startShapeInitializer, const TensorProto* limitShapeInitializer, const TensorProto* deltaShapeInitializer, int dtype) { int64_t dim = -1LL; if (dtype == TensorProto::FLOAT) { dim = CalcRangeDim(startShapeInitializer, limitShapeInitializer, deltaShapeInitializer); } else if (dtype == TensorProto::INT32) { dim = CalcRangeDim(startShapeInitializer, limitShapeInitializer, deltaShapeInitializer); } else if (dtype == TensorProto::INT64) { dim = CalcRangeDim(startShapeInitializer, limitShapeInitializer, deltaShapeInitializer); } else if (dtype == TensorProto::INT16) { dim = CalcRangeDim(startShapeInitializer, limitShapeInitializer, deltaShapeInitializer); } else if (dtype == TensorProto::DOUBLE) { dim = CalcRangeDim(startShapeInitializer, limitShapeInitializer, deltaShapeInitializer); } else { fail_shape_inference("Unsupported type:", dtype); } return dim; } OpSchema& RegisterRangeOpSchema(OpSchema&& op_schema) { return op_schema .SetDomain(kMSDomain) .SinceVersion(1) .TypeConstraint( "T", {"tensor(float)", "tensor(double)", "tensor(int16)", "tensor(int32)", "tensor(int64)"}, "Constrain input and output types.") .Input( 0, "start", "Tensor(scalar, or dims=[1]). First entry in the range.", "T") .Input( 1, "limit", "Tensor(scalar, or dims=[1]). Upper limit of sequence, exclusive.", "T") .Input( 2, "delta", "Tensor(scalar, or dims=[1]). Number that increments start. Defaults to 1.", "T", OpSchema::Optional) .Output( 0, "Y", "1-D Tensor of the range.", "T") .SetDoc(Range_ver1_doc) .TypeAndShapeInferenceFunction([](InferenceContext& ctx) { propagateElemTypeFromInputToOutput(ctx, 0, 0); TensorShapeProto::Dimension dim; bool enoughShapeInfo = ctx.getInputData(0) != nullptr && ctx.getInputData(1) != nullptr && (ctx.getNumInputs() == 2 || ctx.getInputData(2) != nullptr); if (enoughShapeInfo) { const TensorProto* startShapeInitializer = ctx.getInputData(0); const TensorProto* limitShapeInitializer = ctx.getInputData(1); const TensorProto* deltaShapeInitializer = (ctx.getNumInputs() > 2) ? ctx.getInputData(2) : nullptr; const auto& startTensorType = ctx.getInputType(0)->tensor_type(); int dtype = startTensorType.elem_type(); int64_t n = CalcResultDim(startShapeInitializer, limitShapeInitializer, deltaShapeInitializer, dtype); dim.set_dim_value(n); } // else unknown 1-D tensor updateOutputShape(ctx, 0, {dim}); }); } } // namespace contrib } // namespace onnxruntime