Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions onnxscript/ir/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
_enums.DataType.FLOAT8E5M2FNUZ,
_enums.DataType.INT4,
_enums.DataType.UINT4,
_enums.DataType.FLOAT4E2M1,
)
)

Expand Down Expand Up @@ -182,7 +183,7 @@
When the dtype is not one of the numpy native dtypes, the value needs need to be:
- ``int8`` or ``uint8`` for int4, with the sign bit extended to 8 bits.
- ``uint8`` for uint4.
- ``uint8`` for uint4 or float4.
- ``uint8`` for 8-bit data types.
- ``uint16`` for bfloat16
Expand Down Expand Up @@ -213,6 +214,11 @@
raise TypeError(
f"The numpy array dtype must be uint8 or or ml_dtypes.uint4 (not {array.dtype}) for IR data type {dtype}."
)
if dtype == _enums.DataType.FLOAT4E2M1:
if array.dtype not in (np.uint8, ml_dtypes.float4_e2m1fn):
raise TypeError(

Check warning on line 219 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L219

Added line #L219 was not covered by tests
f"The numpy array dtype must be uint8 or ml_dtypes.float4_e2m1fn (not {array.dtype}) for IR data type {dtype}."
)
return

try:
Expand Down Expand Up @@ -256,6 +262,8 @@
return array.view(ml_dtypes.int4)
if dtype == _enums.DataType.UINT4:
return array.view(ml_dtypes.uint4)
if dtype == _enums.DataType.FLOAT4E2M1:
return array.view(ml_dtypes.float4_e2m1fn)

Check warning on line 266 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L266

Added line #L266 was not covered by tests
return array


Expand Down Expand Up @@ -431,7 +439,11 @@
"""
# TODO(justinchuby): Support DLPack
array = self.numpy()
if self.dtype in {_enums.DataType.INT4, _enums.DataType.UINT4}:
if self.dtype in {
_enums.DataType.INT4,
_enums.DataType.UINT4,
_enums.DataType.FLOAT4E2M1,
}:
# Pack the array into int4
array = _type_casting.pack_int4(array)
else:
Expand Down Expand Up @@ -609,7 +621,11 @@
)
# Handle the byte order correctly by always using little endian
dt = np.dtype(self.dtype.numpy()).newbyteorder("<")
if self.dtype in {_enums.DataType.INT4, _enums.DataType.UINT4}:
if self.dtype in {
_enums.DataType.INT4,
_enums.DataType.UINT4,
_enums.DataType.FLOAT4E2M1,
}:
# Use uint8 to read in the full byte. Otherwise ml_dtypes.int4 will clip the values
dt = np.dtype(np.uint8).newbyteorder("<")
count = self.size // 2 + self.size % 2
Expand All @@ -622,6 +638,8 @@
self._array = _type_casting.unpack_int4(self._array, shape)
elif self.dtype == _enums.DataType.UINT4:
self._array = _type_casting.unpack_uint4(self._array, shape)
elif self.dtype == _enums.DataType.FLOAT4E2M1:
self._array = _type_casting.unpack_float4_e2m1(self._array, shape)

Check warning on line 642 in onnxscript/ir/_core.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_core.py#L642

Added line #L642 was not covered by tests
else:
self._array = self._array.reshape(shape)

Expand Down
8 changes: 8 additions & 0 deletions onnxscript/ir/_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class DataType(enum.IntEnum):
FLOAT8E5M2FNUZ = 20
UINT4 = 21
INT4 = 22
FLOAT4E2M1 = 23

@classmethod
def from_numpy(cls, dtype: np.dtype) -> DataType:
Expand Down Expand Up @@ -150,5 +151,12 @@ def __str__(self) -> str:
np.dtype(ml_dtypes.uint4): DataType.UINT4,
}

# TODO(after min req for ml_dtypes>=0.5): Move this inside _NP_TYPE_TO_DATA_TYPE
_NP_TYPE_TO_DATA_TYPE.update(
{np.dtype(ml_dtypes.float4_e2m1fn): DataType.FLOAT4E2M1}
if hasattr(ml_dtypes, "float4_e2m1fn")
else {}
)

# ONNX DataType to Numpy dtype.
_DATA_TYPE_TO_NP_TYPE = {v: k for k, v in _NP_TYPE_TO_DATA_TYPE.items()}
2 changes: 2 additions & 0 deletions onnxscript/ir/_enums_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def test_enums_are_the_same_as_spec(self):
self.assertEqual(_enums.DataType.FLOAT8E5M2FNUZ, onnx.TensorProto.FLOAT8E5M2FNUZ)
self.assertEqual(_enums.DataType.UINT4, onnx.TensorProto.UINT4)
self.assertEqual(_enums.DataType.INT4, onnx.TensorProto.INT4)
if hasattr(onnx.TensorProto, "FLOAT4E2M1"):
self.assertEqual(_enums.DataType.FLOAT4E2M1, onnx.TensorProto.FLOAT4E2M1)
self.assertEqual(_enums.DataType.UNDEFINED, onnx.TensorProto.UNDEFINED)

def test_from_numpy_takes_np_dtype_and_returns_data_type(self):
Expand Down
15 changes: 15 additions & 0 deletions onnxscript/ir/_type_casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,18 @@
"""
unpacked = _unpack_uint4_as_uint8(data, dims)
return _extend_int4_sign_bits(unpacked).view(ml_dtypes.int4)


def unpack_float4e2m1(
data: npt.NDArray[np.uint8], dims: Sequence[int]
) -> npt.NDArray[ml_dtypes.float4e2m1]:
"""Convert a packed float4e2m1 array to unpacked float4e2m1 array.
Args:
data: A numpy array.
dims: The dimensions are used to reshape the unpacked buffer.
Returns:
A numpy array of float32 reshaped to dims.
"""
return _unpack_uint4_as_uint8(data, dims).view(ml_dtypes.float4e2m1)

Check warning on line 106 in onnxscript/ir/_type_casting.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/_type_casting.py#L106

Added line #L106 was not covered by tests
3 changes: 3 additions & 0 deletions onnxscript/ir/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@
return _type_casting.unpack_int4(array.astype(np.uint8), self._proto.dims)
elif dtype == _enums.DataType.UINT4:
return _type_casting.unpack_uint4(array.astype(np.uint8), self._proto.dims)
elif dtype == _enums.DataType.FLOAT4E2M1:
return _type_casting.unpack_float4e2m1(array.astype(np.uint8), self._proto.dims)

Check warning on line 327 in onnxscript/ir/serde.py

View check run for this annotation

Codecov / codecov/patch

onnxscript/ir/serde.py#L327

Added line #L327 was not covered by tests
else:
# Otherwise convert to the correct dtype and reshape
# Note we cannot use view() here because the storage dtype may not be the same size as the target
Expand Down Expand Up @@ -369,6 +371,7 @@
_enums.DataType.FLOAT8E5M2FNUZ,
_enums.DataType.INT4,
_enums.DataType.UINT4,
_enums.DataType.FLOAT4E2M1,
}:
# uint4 and int4 values are already packed, even when stored as int32
# so we don't need to pack them again
Expand Down
Loading