Skip to content

Conversation

@vraspar
Copy link
Contributor

@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.

Copilot AI and others added 4 commits November 10, 2025 21:32
Co-authored-by: vraspar <51386888+vraspar@users.noreply.github.com>
…them yet

Co-authored-by: vraspar <51386888+vraspar@users.noreply.github.com>
Co-authored-by: vraspar <51386888+vraspar@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Nov 11, 2025

Codecov Report

❌ Patch coverage is 73.21429% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.30%. Comparing base (17754ff) to head (83fc971).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/onnx_ir/tensor_adapters.py 40.00% 1 Missing and 5 partials ⚠️
src/onnx_ir/_core.py 69.23% 2 Missing and 2 partials ⚠️
src/onnx_ir/_enums.py 33.33% 4 Missing ⚠️
src/onnx_ir/serde.py 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #251      +/-   ##
==========================================
- Coverage   77.33%   77.30%   -0.04%     
==========================================
  Files          40       40              
  Lines        5042     5097      +55     
  Branches     1009     1027      +18     
==========================================
+ Hits         3899     3940      +41     
- Misses        853      860       +7     
- Partials      290      297       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vraspar vraspar changed the title Copilot/fix issue 250 handling Adds INT2 (2-bit signed) and UINT2 (2-bit unsigned) data type Nov 27, 2025
@justinchuby
Copy link
Member

@vraspar could you check the test errors? Thanks

@vraspar vraspar marked this pull request as ready for review December 15, 2025 23:35
@vraspar vraspar requested review from a team and titaiwangms as code owners December 15, 2025 23:35
@vraspar vraspar requested a review from justinchuby December 15, 2025 23:38
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
@justinchuby justinchuby enabled auto-merge (squash) December 16, 2025 21:05
@justinchuby justinchuby merged commit 7283e95 into onnx:main Dec 16, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants