BitCast¶
BitCast - 26¶
Version¶
name: BitCast (GitHub)
domain:
mainsince_version:
26function:
Falsesupport_level:
SupportType.COMMONshape inference:
True
This version of the operator has been available since version 26.
Summary¶
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.
Attributes¶
to - INT (required) :
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.
Inputs¶
input (heterogeneous) - T1:
Input tensor to be bitcast.
Outputs¶
output (heterogeneous) - T2:
Output tensor with the same shape as the input.
Type Constraints¶
T1 in (
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)):Constrain input types. Bitcasting from string is not supported.
T2 in (
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)):Constrain output types. Bitcasting to string is not supported.
Examples¶
_bitcast_float32_to_int32¶
import numpy as np
import onnx
"""Test bitcasting from float32 to int32 (same size)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.INT32,
)
x = np.array([1.0, -2.5, 3.75], dtype=np.float32)
y = x.view(np.int32)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_float32_to_int32")
_bitcast_int32_to_float32¶
import numpy as np
import onnx
"""Test bitcasting from int32 to float32 (same size)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.FLOAT,
)
x = np.array([1065353216, -1071644672, 1081081856], dtype=np.int32)
y = x.view(np.float32)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_int32_to_float32")
_bitcast_float64_to_int64¶
import numpy as np
import onnx
"""Test bitcasting from float64 to int64 (same size)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.INT64,
)
x = np.array([1.0, -2.5, 3.75], dtype=np.float64)
y = x.view(np.int64)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_float64_to_int64")
_bitcast_int64_to_float64¶
import numpy as np
import onnx
"""Test bitcasting from int64 to float64 (same size)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.DOUBLE,
)
x = np.array(
[4607182418800017408, -4611686018427387904, 4614256656552045184],
dtype=np.int64,
)
y = x.view(np.float64)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_int64_to_float64")
_bitcast_uint32_to_int32¶
import numpy as np
import onnx
"""Test bitcasting from uint32 to int32 (same size, different signedness)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.INT32,
)
x = np.array([4294967295, 2147483648, 2147483647], dtype=np.uint32)
y = x.view(np.int32)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_uint32_to_int32")
_bitcast_2d_float32_to_int32¶
import numpy as np
import onnx
"""Test bitcasting 2D array from float32 to int32."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.INT32,
)
x = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32)
y = x.view(np.int32)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_2d_float32_to_int32")
_bitcast_int8_to_uint8¶
import numpy as np
import onnx
"""Test bitcasting from int8 to uint8 (same size, different signedness)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.UINT8,
)
x = np.array([-1, -128, 127, 0], dtype=np.int8)
y = x.view(np.uint8)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_int8_to_uint8")
_bitcast_scalar_float32_to_int32¶
import numpy as np
import onnx
"""Test bitcasting scalar from float32 to int32."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.INT32,
)
x = np.array(1.0, dtype=np.float32)
y = x.view(np.int32)
expect(
node, inputs=[x], outputs=[y], name="test_bitcast_scalar_float32_to_int32"
)
_bitcast_uint16_to_int16¶
import numpy as np
import onnx
"""Test bitcasting from uint16 to int16 (same size, different signedness)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.INT16,
)
x = np.array([1, 32768, 65535], dtype=np.uint16)
y = x.view(np.int16)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_uint16_to_int16")
_bitcast_bool_to_uint8¶
import numpy as np
import onnx
"""Test bitcasting from bool to uint8 (same size)."""
node = onnx.helper.make_node(
"BitCast",
inputs=["x"],
outputs=["y"],
to=onnx.TensorProto.UINT8,
)
x = np.array([True, False, True, False], dtype=np.bool_)
y = x.view(np.uint8)
expect(node, inputs=[x], outputs=[y], name="test_bitcast_bool_to_uint8")