Skip to content

Commit 68be7d1

Browse files
committed
intx: pass dim through cat dispatch
The aten.cat branch of SubbyteTensor.__torch_dispatch__ reads dim via fill_defaults, then calls the op without it, so every cat runs on dim 0. Two (2, 4) tensors with dim=1 give shape (4, 4) rather than (2, 8). IntxTensor and UintxTensor share the branch, so both are affected.
1 parent 698f11a commit 68be7d1

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

coreai_torch/_compression/_intx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def __torch_dispatch__(cls, func, _types, args, kwargs=None):
380380
if func is torch.ops.aten.cat.default:
381381
self, dim = fill_defaults(args, 2, [0])
382382
unpacked = [x.unpack_func(x.elem, x.tensor_shape, x.nbits) for x in self]
383-
return cls.from_unpacked(func(unpacked), self[0].nbits)
383+
return cls.from_unpacked(func(unpacked, dim), self[0].nbits)
384384
if func is torch.ops.aten.min.default:
385385
(self,) = args
386386
unpacked = self.unpack_func(self.elem, self.tensor_shape, self.nbits)

tests/compression/test_intx.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,40 @@ def test_intx_scalar(nbits: int) -> None:
8585
assert intx_tensor.elem.numel() == 1
8686

8787

88+
@pytest.mark.parametrize(
89+
"dim",
90+
[0, 1, -1],
91+
)
92+
def test_uintx_cat(dim: int) -> None:
93+
"""Test uintx tensor concatenation along a given dimension."""
94+
unpacked = [
95+
torch.tensor([[3, 0, 14, 1], [0, 7, 15, 2]], dtype=torch.uint8),
96+
torch.tensor([[1, 4, 5, 6], [2, 3, 6, 1]], dtype=torch.uint8),
97+
]
98+
uintx_tensors = [UintxTensor.from_unpacked(x, 4) for x in unpacked]
99+
concatenated = torch.cat(uintx_tensors, dim=dim)
100+
expected = torch.cat(unpacked, dim=dim)
101+
assert concatenated.shape == expected.shape
102+
assert torch.equal(concatenated.to(torch.uint8), expected)
103+
104+
105+
@pytest.mark.parametrize(
106+
"dim",
107+
[0, 1, -1],
108+
)
109+
def test_intx_cat(dim: int) -> None:
110+
"""Test intx tensor concatenation along a given dimension."""
111+
unpacked = [
112+
torch.tensor([[3, 0, -2, 1], [0, 7, -8, 2]], dtype=torch.int8),
113+
torch.tensor([[-1, 4, 5, -6], [2, -3, 6, 1]], dtype=torch.int8),
114+
]
115+
intx_tensors = [IntxTensor.from_unpacked(x, 4) for x in unpacked]
116+
concatenated = torch.cat(intx_tensors, dim=dim)
117+
expected = torch.cat(unpacked, dim=dim)
118+
assert concatenated.shape == expected.shape
119+
assert torch.equal(concatenated.to(torch.int8), expected)
120+
121+
88122
def test_uint4_packed_value() -> None:
89123
"""Test uint4 packed values."""
90124
unpacked = torch.tensor([3, 0, 2, 0, 9, 14, 15], dtype=torch.uint8)

0 commit comments

Comments
 (0)