Range#

  • Domain: ai.onnx

  • Since version: 27

Generate a tensor containing a sequence of numbers that begin at start and extends by increments of delta up to limit (exclusive).

The number of elements in the output of range is computed as below:

number_of_elements = max( ceil( (limit - start) / delta ) , 0 )

The pseudocode determining the contents of the output is shown below:

for(int i=0; i<number_of_elements; ++i) {
  output[i] =  start + (i * delta);
}

Example 1

Inputs: start = 3, limit = 9, delta = 3
Output: [3, 6]

Example 2

Inputs: start = 10, limit = 4, delta = -2
Output: [10, 8, 6]

For float16 and bfloat16 inputs, the stash_type attribute controls the precision used for intermediate accumulation. Setting stash_type to 1 (float) causes start, limit, and delta to be cast to 32-bit float before the loop, with the output cast back to the original type. This avoids precision loss for large ranges where successive additions in float16 or bfloat16 would otherwise be inexact (e.g. x + 1 == x for large x).

Inputs

  • start (T): Scalar. First entry for the range of output values.

  • limit (T): Scalar. Exclusive upper limit for the range of output values.

  • delta (T): Scalar. Value to step by.

Outputs

  • output (T): A 1-D tensor with same type as the inputs containing generated range of values.

Attributes

  • stash_type (int): The data type used for intermediate computation when T is float16 or bfloat16. Defaults to 1 (float). Has no effect for other types.

Type Constraints

  • T: Constrain input types to common numeric type tensors. Allowed types: tensor(bfloat16), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64).

Examples#

test_range_bfloat16_type_positive_delta

Node:
  Range(start, limit, delta) -> (output)
Inputs:
  start: shape=(), dtype=bfloat16
    1
  limit: shape=(), dtype=bfloat16
    5
  delta: shape=(), dtype=bfloat16
    2

Outputs:
  output: shape=(2,), dtype=bfloat16
    [1, 3]

test_range_float16_type_positive_delta

Node:
  Range(start, limit, delta) -> (output)
Inputs:
  start: shape=(), dtype=float16
    1.
  limit: shape=(), dtype=float16
    5.
  delta: shape=(), dtype=float16
    2.

Outputs:
  output: shape=(2,), dtype=float16
    [1., 3.]

Differences with previous version (11)#

SchemaDiff: Range (domain 'ai.onnx')

  • old version: 11

  • new version: 27

  • breaking: no

Attributes:

  • added ‘stash_type’: type=INT; required=False; default=1

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’, ‘tensor(float16)’]

Documentation:

  • line similarity: 0.91 (+6/-0 lines)

--- Range v11
+++ Range v27
@@ -29,3 +29,9 @@
 Inputs: start = 10, limit = 4, delta = -2
 Output: [10, 8, 6]
 ```
+
+For `float16` and `bfloat16` inputs, the `stash_type` attribute controls the precision used for
+intermediate accumulation. Setting `stash_type` to `1` (float) causes `start`, `limit`, and
+`delta` to be cast to 32-bit float before the loop, with the output cast back to the original
+type. This avoids precision loss for large ranges where successive additions in float16 or
+bfloat16 would otherwise be inexact (e.g. `x + 1 == x` for large `x`).

Version History#