BitCast#

  • Domain: ai.onnx

  • Since version: 26

Reinterprets the binary representation of a tensor as a different data type, specified by the ‘to’ attribute. Unlike Cast, BitCast preserves the exact bit pattern without any value conversion.

The target data type must have the same bit-width as the input data type. The output tensor has the same shape as the input tensor. All types except string are supported. Implementations must treat the underlying bytes as little endian.

Inputs

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

Outputs

  • output (T2): Output tensor with the same shape as the input.

Attributes

  • to (int): The data type to which the input tensor is bitwise reinterpreted. Must be one of the non-string types from DataType enum in TensorProto. The target type must have the same bit-width as the input type.

Type Constraints

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

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

Examples#

test_bitcast_2d_float32_to_int32

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 6
Inputs:
  x: shape=(2, 3), dtype=float32
    [[1., 2., 3.],
     [4., 5., 6.]]

Outputs:
  y: shape=(2, 3), dtype=int32
    [[1065353216, 1073741824, 1077936128],
     [1082130432, 1084227584, 1086324736]]

test_bitcast_bool_to_uint8

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 2
Inputs:
  x: shape=(4,), dtype=bool
    [ True, False,  True, False]

Outputs:
  y: shape=(4,), dtype=uint8
    [1, 0, 1, 0]

test_bitcast_float32_to_int32

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 6
Inputs:
  x: shape=(3,), dtype=float32
    [ 1.  , -2.5 ,  3.75]

Outputs:
  y: shape=(3,), dtype=int32
    [ 1065353216, -1071644672,  1081081856]

test_bitcast_float64_to_int64

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 7
Inputs:
  x: shape=(3,), dtype=float64
    [ 1.  , -2.5 ,  3.75]

Outputs:
  y: shape=(3,), dtype=int64
    [ 4607182418800017408, -4610560118520545280,  4615626668101337088]

test_bitcast_int32_to_float32

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 1
Inputs:
  x: shape=(3,), dtype=int32
    [ 1065353216, -1071644672,  1081081856]

Outputs:
  y: shape=(3,), dtype=float32
    [ 1.  , -2.5 ,  3.75]

test_bitcast_int64_to_float64

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 11
Inputs:
  x: shape=(3,), dtype=int64
    [ 4607182418800017408, -4611686018427387904,  4614256656552045184]

Outputs:
  y: shape=(3,), dtype=float64
    [ 1.        , -2.        ,  3.14159265]

test_bitcast_int8_to_uint8

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 2
Inputs:
  x: shape=(4,), dtype=int8
    [  -1, -128,  127,    0]

Outputs:
  y: shape=(4,), dtype=uint8
    [255, 128, 127,   0]

test_bitcast_scalar_float32_to_int32

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 6
Inputs:
  x: shape=(), dtype=float32
    1.

Outputs:
  y: shape=(), dtype=int32
    1065353216

test_bitcast_uint32_to_int32

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 6
Inputs:
  x: shape=(3,), dtype=uint32
    [4294967295, 2147483648, 2147483647]

Outputs:
  y: shape=(3,), dtype=int32
    [         -1, -2147483648,  2147483647]

test_cc_bitcast_double_to_int64

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 7
Inputs:
  x: shape=(2, 2), dtype=float64
    [[ 0.  ,  1.  ],
     [-1.  ,  3.14]]

Outputs:
  y: shape=(2, 2), dtype=int64
    [[                   0,  4607182418800017408],
     [-4616189618054758400,  4614253070214989087]]

test_cc_bitcast_float_to_int32

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 6
Inputs:
  x: shape=(3,), dtype=float32
    [ 0.,  1., -1.]

Outputs:
  y: shape=(3,), dtype=int32
    [          0,  1065353216, -1082130432]

test_cc_bitcast_int32_to_float

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 1
Inputs:
  x: shape=(3,), dtype=int32
    [          0,  1065353216, -1082130432]

Outputs:
  y: shape=(3,), dtype=float32
    [ 0.,  1., -1.]

test_cc_bitcast_uint16_to_int16

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 5
Inputs:
  x: shape=(4,), dtype=uint16
    [    0, 15360, 16384, 48128]

Outputs:
  y: shape=(4,), dtype=int16
    [     0,  15360,  16384, -17408]

test_cc_bitcast_uint8_to_int8

Node:
  BitCast(x) -> (y)
  Attributes:
    to = 3
Inputs:
  x: shape=(3, 4), dtype=uint8
    [[ 76, 180, 137,  31],
     [ 33, 128, 195,  17],
     [ 98, 158, 141,  62]]

Outputs:
  y: shape=(3, 4), dtype=int8
    [[  76,  -76, -119,   31],
     [  33, -128,  -61,   17],
     [  98,  -98, -115,   62]]