Cast - version 21#

This page documents version 21 of operator Cast. See Cast for the latest version (since version 28).

  • Domain: ai.onnx

  • Since version: 21

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. The onnx-light native runtime additionally supports complex64 and complex128 as an extension to ONNX Cast. Real-to-complex casts set the imaginary component to zero. Complex-to-real casts discard it; complex precision casts preserve both components. Complex casts involving strings, float8 or packed types are unsupported. The standard ONNX schemas and full checker do not accept this extension.

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

Attributes

  • saturate (int): The parameter defines how the conversion behaves if an input value is out of range of the destination type. It only applies for float 8 conversion (float8e4m3fn, float8e4m3fnuz, float8e5m2, float8e5m2fnuz). It is true by default. All cases are fully described in two tables inserted in the operator description.

  • to (int): The data type to which the elements of the input tensor are cast. Strictly must be one of the types from DataType enum in TensorProto.

Type Constraints

  • T1: Constrain input types. Complex types are an onnx-light runtime extension. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), 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. Complex types are an onnx-light runtime extension. Allowed types: tensor(bfloat16), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), 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_cast_no_saturate_FLOAT16_to_FLOAT8E4M3FN

Node:
  Cast(input) -> (output)
  Attributes:
    to = 17
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fn
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, nan, 0, nan, nan],
     [nan, nan, -0, 0, nan]]

test_cast_no_saturate_FLOAT16_to_FLOAT8E4M3FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 18
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fnuz
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, nan, 0, nan, nan],
     [nan, nan, 0, 0, nan]]

test_cast_no_saturate_FLOAT16_to_FLOAT8E5M2

Node:
  Cast(input) -> (output)
  Attributes:
    to = 19
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, inf, 0, nan, inf],
     [inf, -inf, -0, 0, -inf]]

test_cast_no_saturate_FLOAT16_to_FLOAT8E5M2FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 20
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2fnuz
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, nan, 0, nan, nan],
     [nan, nan, 0, 0, nan]]

test_cast_no_saturate_FLOAT_to_FLOAT8E4M3FN

Node:
  Cast(input) -> (output)
  Attributes:
    to = 17
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fn
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, nan, 0, nan, nan],
     [nan, nan, -0, 0, nan]]

test_cast_no_saturate_FLOAT_to_FLOAT8E4M3FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 18
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fnuz
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, nan, 0, nan, nan],
     [nan, nan, 0, 0, nan]]

test_cast_no_saturate_FLOAT_to_FLOAT8E5M2

Node:
  Cast(input) -> (output)
  Attributes:
    to = 19
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, inf, 0, nan, inf],
     [inf, -inf, -0, 0, -inf]]

test_cast_no_saturate_FLOAT_to_FLOAT8E5M2FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 20
    saturate = 0
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2fnuz
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, nan, 0, nan, nan],
     [nan, nan, 0, 0, nan]]

test_cc_cast_FLOAT16_to_FLOAT8E4M3FN

Node:
  Cast(input) -> (output)
  Attributes:
    to = 17
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fn
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 448, 0, nan, 448],
     [448, -448, -0, 0, -448]]

test_cc_cast_FLOAT16_to_FLOAT8E4M3FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 18
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fnuz
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 240, 0, nan, 240],
     [240, -240, 0, 0, -240]]

test_cc_cast_FLOAT16_to_FLOAT8E5M2

Node:
  Cast(input) -> (output)
  Attributes:
    to = 19
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, -0, 0, -57344]]

test_cc_cast_FLOAT16_to_FLOAT8E5M2FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 20
Inputs:
  input: shape=(3, 5), dtype=float16
    [[ 4.790e-01,  4.802e-01,  4.998e-01,  8.193e-01,  4.702e-01],
     [ 7.231e-01,        inf,  1.192e-07,        nan,        inf],
     [       inf,       -inf, -1.192e-07,  1.192e-07,       -inf]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2fnuz
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, 0, 0, -57344]]

test_cc_cast_FLOAT16_to_INT4

Node:
  Cast(input) -> (output)
  Attributes:
    to = 22
Inputs:
  input: shape=(5, 5), dtype=float16
    [[-9., -8., -7., -6., -5.],
     [-4., -3., -2., -1.,  0.],
     [ 1.,  2.,  3.,  4.,  5.],
     [ 6.,  7.,  8.,  9., 10.],
     [11., 12., 13., 14., 15.]]

Outputs:
  output: shape=(5, 5), dtype=int4
    [[7, -8, -7, -6, -5],
     [-4, -3, -2, -1, 0],
     [1, 2, 3, 4, 5],
     [6, 7, -8, -7, -6],
     [-5, -4, -3, -2, -1]]

test_cc_cast_FLOAT16_to_UINT4

Node:
  Cast(input) -> (output)
  Attributes:
    to = 21
Inputs:
  input: shape=(5, 5), dtype=float16
    [[-9., -8., -7., -6., -5.],
     [-4., -3., -2., -1.,  0.],
     [ 1.,  2.,  3.,  4.,  5.],
     [ 6.,  7.,  8.,  9., 10.],
     [11., 12., 13., 14., 15.]]

Outputs:
  output: shape=(5, 5), dtype=uint4
    [[7, 8, 9, 10, 11],
     [12, 13, 14, 15, 0],
     [1, 2, 3, 4, 5],
     [6, 7, 8, 9, 10],
     [11, 12, 13, 14, 15]]

test_cc_cast_FLOAT8E4M3FNUZ_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(3, 5), dtype=float8_e4m3fnuz
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 240, 0, nan, 240],
     [240, -240, 0, 0, -240]]

Outputs:
  output: shape=(3, 5), dtype=float32
    [[   0.46875,    0.46875,    0.5    ,    0.8125 ,    0.46875],
     [   0.75   ,  240.     ,    0.     ,        nan,  240.     ],
     [ 240.     , -240.     ,    0.     ,    0.     , -240.     ]]

test_cc_cast_FLOAT8E4M3FNUZ_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(3, 5), dtype=float8_e4m3fnuz
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 240, 0, nan, 240],
     [240, -240, 0, 0, -240]]

Outputs:
  output: shape=(3, 5), dtype=float16
    [[   0.4688,    0.4688,    0.5   ,    0.8125,    0.4688],
     [   0.75  ,  240.    ,    0.    ,       nan,  240.    ],
     [ 240.    , -240.    ,    0.    ,    0.    , -240.    ]]

test_cc_cast_FLOAT8E4M3FN_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(3, 5), dtype=float8_e4m3fn
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 448, 0, nan, 448],
     [448, -448, -0, 0, -448]]

Outputs:
  output: shape=(3, 5), dtype=float32
    [[   0.46875,    0.46875,    0.5    ,    0.8125 ,    0.46875],
     [   0.75   ,  448.     ,    0.     ,        nan,  448.     ],
     [ 448.     , -448.     ,   -0.     ,    0.     , -448.     ]]

test_cc_cast_FLOAT8E4M3FN_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(3, 5), dtype=float8_e4m3fn
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 448, 0, nan, 448],
     [448, -448, -0, 0, -448]]

Outputs:
  output: shape=(3, 5), dtype=float16
    [[   0.4688,    0.4688,    0.5   ,    0.8125,    0.4688],
     [   0.75  ,  448.    ,    0.    ,       nan,  448.    ],
     [ 448.    , -448.    ,   -0.    ,    0.    , -448.    ]]

test_cc_cast_FLOAT8E5M2FNUZ_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(3, 5), dtype=float8_e5m2fnuz
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, 0, 0, -57344]]

Outputs:
  output: shape=(3, 5), dtype=float32
    [[ 5.0000e-01,  5.0000e-01,  5.0000e-01,  8.7500e-01,  5.0000e-01],
     [ 7.5000e-01,  5.7344e+04,  0.0000e+00,         nan,  5.7344e+04],
     [ 5.7344e+04, -5.7344e+04,  0.0000e+00,  0.0000e+00, -5.7344e+04]]

test_cc_cast_FLOAT8E5M2FNUZ_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(3, 5), dtype=float8_e5m2fnuz
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, 0, 0, -57344]]

Outputs:
  output: shape=(3, 5), dtype=float16
    [[ 5.000e-01,  5.000e-01,  5.000e-01,  8.750e-01,  5.000e-01],
     [ 7.500e-01,  5.734e+04,  0.000e+00,        nan,  5.734e+04],
     [ 5.734e+04, -5.734e+04,  0.000e+00,  0.000e+00, -5.734e+04]]

test_cc_cast_FLOAT8E5M2_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(3, 5), dtype=float8_e5m2
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, -0, 0, -57344]]

Outputs:
  output: shape=(3, 5), dtype=float32
    [[ 5.0000e-01,  5.0000e-01,  5.0000e-01,  8.7500e-01,  5.0000e-01],
     [ 7.5000e-01,  5.7344e+04,  0.0000e+00,         nan,  5.7344e+04],
     [ 5.7344e+04, -5.7344e+04, -0.0000e+00,  0.0000e+00, -5.7344e+04]]

test_cc_cast_FLOAT8E5M2_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(3, 5), dtype=float8_e5m2
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, -0, 0, -57344]]

Outputs:
  output: shape=(3, 5), dtype=float16
    [[ 5.000e-01,  5.000e-01,  5.000e-01,  8.750e-01,  5.000e-01],
     [ 7.500e-01,  5.734e+04,  0.000e+00,        nan,  5.734e+04],
     [ 5.734e+04, -5.734e+04, -0.000e+00,  0.000e+00, -5.734e+04]]

test_cc_cast_FLOAT_to_FLOAT8E4M3FN

Node:
  Cast(input) -> (output)
  Attributes:
    to = 17
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fn
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 448, 0, nan, 448],
     [448, -448, -0, 0, -448]]

test_cc_cast_FLOAT_to_FLOAT8E4M3FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 18
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e4m3fnuz
    [[0.46875, 0.46875, 0.5, 0.8125, 0.46875],
     [0.75, 240, 0, nan, 240],
     [240, -240, 0, 0, -240]]

test_cc_cast_FLOAT_to_FLOAT8E5M2

Node:
  Cast(input) -> (output)
  Attributes:
    to = 19
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, -0, 0, -57344]]

test_cc_cast_FLOAT_to_FLOAT8E5M2FNUZ

Node:
  Cast(input) -> (output)
  Attributes:
    to = 20
Inputs:
  input: shape=(3, 5), dtype=float32
    [[ 4.7892547e-01,  4.8033667e-01,  4.9968487e-01,  8.1910545e-01,
       4.7031248e-01],
     [ 7.2290379e-01,  1.0000000e+06,  1.0000000e-07,            nan,
                 inf],
     [           inf,           -inf, -1.0000000e-07,  1.0000000e-07,
      -1.0000000e+06]]

Outputs:
  output: shape=(3, 5), dtype=float8_e5m2fnuz
    [[0.5, 0.5, 0.5, 0.875, 0.5],
     [0.75, 57344, 0, nan, 57344],
     [57344, -57344, 0, 0, -57344]]

test_cc_cast_FLOAT_to_INT4

Node:
  Cast(input) -> (output)
  Attributes:
    to = 22
Inputs:
  input: shape=(5, 5), dtype=float32
    [[-9., -8., -7., -6., -5.],
     [-4., -3., -2., -1.,  0.],
     [ 1.,  2.,  3.,  4.,  5.],
     [ 6.,  7.,  8.,  9., 10.],
     [11., 12., 13., 14., 15.]]

Outputs:
  output: shape=(5, 5), dtype=int4
    [[7, -8, -7, -6, -5],
     [-4, -3, -2, -1, 0],
     [1, 2, 3, 4, 5],
     [6, 7, -8, -7, -6],
     [-5, -4, -3, -2, -1]]

test_cc_cast_FLOAT_to_UINT4

Node:
  Cast(input) -> (output)
  Attributes:
    to = 21
Inputs:
  input: shape=(5, 5), dtype=float32
    [[-9., -8., -7., -6., -5.],
     [-4., -3., -2., -1.,  0.],
     [ 1.,  2.,  3.,  4.,  5.],
     [ 6.,  7.,  8.,  9., 10.],
     [11., 12., 13., 14., 15.]]

Outputs:
  output: shape=(5, 5), dtype=uint4
    [[7, 8, 9, 10, 11],
     [12, 13, 14, 15, 0],
     [1, 2, 3, 4, 5],
     [6, 7, 8, 9, 10],
     [11, 12, 13, 14, 15]]

test_cc_cast_INT4_to_FLOAT

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

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

test_cc_cast_INT4_to_FLOAT16

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

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

test_cc_cast_INT4_to_INT8

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

Outputs:
  output: shape=(5, 5), dtype=int8
    [[ 7, -8, -7, -6, -5],
     [-4, -3, -2, -1,  0],
     [ 1,  2,  3,  4,  5],
     [ 6,  7, -8, -7, -6],
     [-5, -4, -3, -2, -1]]

test_cc_cast_UINT4_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(5, 5), dtype=uint4
    [[7, 8, 9, 10, 11],
     [12, 13, 14, 15, 0],
     [1, 2, 3, 4, 5],
     [6, 7, 8, 9, 10],
     [11, 12, 13, 14, 15]]

Outputs:
  output: shape=(5, 5), dtype=float32
    [[ 7.,  8.,  9., 10., 11.],
     [12., 13., 14., 15.,  0.],
     [ 1.,  2.,  3.,  4.,  5.],
     [ 6.,  7.,  8.,  9., 10.],
     [11., 12., 13., 14., 15.]]

test_cc_cast_UINT4_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(5, 5), dtype=uint4
    [[7, 8, 9, 10, 11],
     [12, 13, 14, 15, 0],
     [1, 2, 3, 4, 5],
     [6, 7, 8, 9, 10],
     [11, 12, 13, 14, 15]]

Outputs:
  output: shape=(5, 5), dtype=float16
    [[ 7.,  8.,  9., 10., 11.],
     [12., 13., 14., 15.,  0.],
     [ 1.,  2.,  3.,  4.,  5.],
     [ 6.,  7.,  8.,  9., 10.],
     [11., 12., 13., 14., 15.]]

test_cc_cast_UINT4_to_UINT8

Node:
  Cast(input) -> (output)
  Attributes:
    to = 2
Inputs:
  input: shape=(5, 5), dtype=uint4
    [[7, 8, 9, 10, 11],
     [12, 13, 14, 15, 0],
     [1, 2, 3, 4, 5],
     [6, 7, 8, 9, 10],
     [11, 12, 13, 14, 15]]

Outputs:
  output: shape=(5, 5), dtype=uint8
    [[ 7,  8,  9, 10, 11],
     [12, 13, 14, 15,  0],
     [ 1,  2,  3,  4,  5],
     [ 6,  7,  8,  9, 10],
     [11, 12, 13, 14, 15]]

test_cc_cast_e8m0_DOUBLE_boundary

Node:
  Cast(input) -> (output)
  Attributes:
    to = 24
Inputs:
  input: shape=(3,), dtype=float64
    [1.17549435e-38, 1.17549435e-38, 1.00000000e+39]

Outputs:
  output: shape=(3,), dtype=float8_e8m0fnu
    [1.17549e-38, 2.35099e-38, 1.70141e+38]

test_cc_cast_e8m0_FLOAT16_to_FLOAT8E8M0

Node:
  Cast(input) -> (output)
  Attributes:
    to = 24
Inputs:
  input: shape=(2, 4), dtype=float16
    [[0.   , 0.124, 0.25 , 0.5  ],
     [1.1  , 2.   , 4.   , 8.   ]]

Outputs:
  output: shape=(2, 4), dtype=float8_e8m0fnu
    [[5.87747e-39, 0.125, 0.25, 0.5],
     [2, 2, 4, 8]]

test_cc_cast_e8m0_FLOAT8E8M0_to_FLOAT

Node:
  Cast(input) -> (output)
  Attributes:
    to = 1
Inputs:
  input: shape=(2, 4), dtype=float8_e8m0fnu
    [[5.87747e-39, 0.125, 0.25, 0.5],
     [2, 2, 4, 8]]

Outputs:
  output: shape=(2, 4), dtype=float32
    [[5.877472e-39, 1.250000e-01, 2.500000e-01, 5.000000e-01],
     [2.000000e+00, 2.000000e+00, 4.000000e+00, 8.000000e+00]]

test_cc_cast_e8m0_FLOAT8E8M0_to_FLOAT16

Node:
  Cast(input) -> (output)
  Attributes:
    to = 10
Inputs:
  input: shape=(2, 4), dtype=float8_e8m0fnu
    [[5.87747e-39, 0.125, 0.25, 0.5],
     [2, 2, 4, 8]]

Outputs:
  output: shape=(2, 4), dtype=float16
    [[0.   , 0.125, 0.25 , 0.5  ],
     [2.   , 2.   , 4.   , 8.   ]]

test_cc_cast_e8m0_FLOAT_subnormal

Node:
  Cast(input) -> (output)
  Attributes:
    to = 24
Inputs:
  input: shape=(5,), dtype=float32
    [5.8774718e-39, 7.3468397e-40, 5.8774732e-39, 1.1754944e-38, 2.5521178e+38]

Outputs:
  output: shape=(5,), dtype=float8_e8m0fnu
    [5.87747e-39, 5.87747e-39, 1.17549e-38, 1.17549e-38, 1.70141e+38]

test_cc_cast_e8m0_FLOAT_to_FLOAT8E8M0

Node:
  Cast(input) -> (output)
  Attributes:
    to = 24
Inputs:
  input: shape=(2, 4), dtype=float32
    [[0.   , 0.124, 0.25 , 0.5  ],
     [1.1  , 2.   , 4.   , 8.   ]]

Outputs:
  output: shape=(2, 4), dtype=float8_e8m0fnu
    [[5.87747e-39, 0.125, 0.25, 0.5],
     [2, 2, 4, 8]]

Differences with previous version (19)#

SchemaDiff: Cast (domain 'ai.onnx')

  • old version: 19

  • new version: 21

  • breaking: no

Type constraints:

  • changed ‘T1’: added types: [‘tensor(int4)’, ‘tensor(uint4)’]

  • changed ‘T2’: added types: [‘tensor(int4)’, ‘tensor(uint4)’]