Cast - version 23#

This page documents version 23 of operator Cast. See Cast for the latest version (since version 25).

  • Domain: ai.onnx

  • Since version: 23

The operator casts the elements of a given input tensor to a data type specified by the ‘to’ argument and returns an output tensor of the same size in the converted type. The ‘to’ argument must be one of the data types specified in the ‘DataType’ enum field in the TensorProto message.

Inputs

  • input (T1): Input tensor to be cast.

Outputs

  • output (T2): Output tensor with the same shape as input with type specified by the ‘to’ argument

Type Constraints

  • T1: Constrain input types. Casting from complex is not supported. Allowed types: tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8).

  • T2: Constrain output types. Casting to complex is not supported. Allowed types: tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float4e2m1), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int4), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint4), tensor(uint64), tensor(uint8).

Examples#

test_cc_cast_FLOAT16_to_FLOAT4E2M1

Node:
  Cast(input) -> (output)
  Attributes:
    to = 23
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.80e-01,  2.50e-01,  1.05e+00, -3.50e+00, -8.00e+00],
     [ 9.00e+00,       inf,  1.19e-07,       nan,       inf],
     [      inf,      -inf, -4.00e+00,  1.00e-02, -0.00e+00]]

Outputs:
  output: shape=(3, 5), dtype=float4_e2m1fn
    [[0.5, 0, 1, -4, -6],
     [6, 6, 0, 0, 6],
     [6, -6, -4, 0, -0]]

test_cc_cast_FLOAT4E2M1_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(3, 5), dtype=float4_e2m1fn
    [[0.5, 0, 1, -4, -6],
     [6, 6, 0, 0, 6],
     [6, -6, -4, 0, -0]]

Outputs:
  output: shape=(3, 5), dtype=float32
    [[ 0.5,  0. ,  1. , -4. , -6. ],
     [ 6. ,  6. ,  0. ,  0. ,  6. ],
     [ 6. , -6. , -4. ,  0. , -0. ]]

test_cc_cast_FLOAT4E2M1_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(3, 5), dtype=float4_e2m1fn
    [[0.5, 0, 1, -4, -6],
     [6, 6, 0, 0, 6],
     [6, -6, -4, 0, -0]]

Outputs:
  output: shape=(3, 5), dtype=float16
    [[ 0.5,  0. ,  1. , -4. , -6. ],
     [ 6. ,  6. ,  0. ,  0. ,  6. ],
     [ 6. , -6. , -4. ,  0. , -0. ]]

test_cc_cast_FLOAT_to_FLOAT4E2M1

Node:
  Cast(input) -> (output)
  Attributes:
    to = 23
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.80e-01,  2.50e-01,  1.05e+00, -3.50e+00, -8.00e+00],
     [ 9.00e+00,  1.00e+06,  1.00e-07,       nan,       inf],
     [      inf,      -inf, -4.00e+00,  1.00e-02, -0.00e+00]]

Outputs:
  output: shape=(3, 5), dtype=float4_e2m1fn
    [[0.5, 0, 1, -4, -6],
     [6, 6, 0, 0, 6],
     [6, -6, -4, 0, -0]]

test_cc_shape_inference_tiny_llm

Node:
  Cast(attention_mask) -> (mask_float)
  Attributes:
    to = 1

test_cc_shape_inference_tiny_llm_inlined

Node:
  Cast(attention_mask) -> (mask_float)
  Attributes:
    to = 1

Differences with previous version (21)#

SchemaDiff: Cast (domain 'ai.onnx')

  • old version: 21

  • new version: 23

  • breaking: no

Type constraints:

  • changed ‘T1’: added types: [‘tensor(float4e2m1)’]

  • changed ‘T2’: added types: [‘tensor(float4e2m1)’]