Skip to content

Conversation

@vraspar
Copy link

@vraspar vraspar commented Nov 10, 2025

Adds INT2 (2-bit signed) and UINT2 (2-bit unsigned) data types following ONNX spec PR #7446. These complement existing INT4/UINT4 support with similar packed representation.

Changes

Enums (_enums.py)

  • Added INT2=25, UINT2=26 enum values
  • Added ml_dtypes.int2/uint2 mappings and type properties (bitwidth=2, signed/unsigned)
  • Added short names "i2"/"u2"

Packing (_type_casting.py)

  • Implemented pack_2bitx4() / unpack_2bitx4() for 4-values-per-byte packing
  • Handles non-multiple-of-4 sizes with padding

Core (_core.py)

  • Updated PackedTensor to support 2-bit types (was 4-bit only)
  • Added INT2/UINT2 to non-numpy-native types list
  • Updated byte representation to pack 2-bit types

Serialization (serde.py)

  • Added 2-bit unpacking from raw_data
  • Added INT2/UINT2 to int32_data serialization paths

Torch adapters (tensor_adapters.py)

  • Added torch.int2/uint2 dtype mappings for future compatibility
  • PyTorch doesn't yet support creating tensors with these types

Example

import numpy as np
import ml_dtypes
import onnx_ir as ir

# Create and serialize INT2 tensor
array = np.array([-2, -1, 0, 1], dtype=ml_dtypes.int2)
tensor = ir.Tensor(array)
proto = ir.serde.to_proto(tensor)

# 4 elements packed into 1 byte
assert len(proto.raw_data) == 1

# Round-trip works
restored = ir.serde.from_proto(proto)
assert np.array_equal(restored.numpy(), array)
Original prompt

Helpe me create PR for this issue: #250


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@codecov
Copy link

codecov bot commented Nov 11, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
941 1 940 0
View the top 1 failed test(s) by shortest run time
src.onnx_ir._core_test.PackedTensorTest::test_initialize_raises_when_dtype_not_4bit
Stack Traces | 0.003s run time
src\onnx_ir\_core_test.py:2939: in test_initialize_raises_when_dtype_not_4bit
    self.assertIn("PackedTensor only supports INT4, UINT4, FLOAT4E2M1", str(cm.exception))
E   AssertionError: 'PackedTensor only supports INT4, UINT4, FLOAT4E2M1' not found in 'PackedTensor only supports INT2, UINT2, INT4, UINT4, FLOAT4E2M1, but got FLOAT'

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

This function is used for serializing the tensor to bytes. It handles the
special cases for 4-bit data types and endianness.
special cases for 2-bit and 4-bit data types and endianness.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized we don’t need to handle endianess because these are subtbyte types. So endianess is irrelevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants