Skip to content

Bitpacking default pack and unpack dimensions do not round-trip multidimensional tensors #4815

Open

Description

@aswanth-07

Bug

The optimized pack() and unpack() helpers have incompatible default dimensions: pack(..., dim=-1) packs the last dimension, while unpack(..., dim=0) expands the first dimension. As a result, the documented default round trip only works for one-dimensional tensors.

Reproduction

import torch
from torchao.prototype.dtypes.uintx.bitpacking import pack, unpack

x = torch.randint(0, 16, (8, 16), dtype=torch.uint8)
packed = pack(x, 4)
restored = unpack(packed, 4)

print(x.shape, restored.shape)

On current main (36027d82d), this prints:

torch.Size([8, 16]) torch.Size([16, 8])

The CPU reference pair (pack_cpu / unpack_cpu) already uses dim=-1 consistently, and TorchAO's internal UIntxTensor callers pass an explicit dimension.

Proposed fix

Change unpack()'s default to dim=-1 so the optimized pair has the same default contract as the CPU pair, and add a multidimensional round-trip regression covering bit widths 1 through 7. I have this small fix ready.

Activity

  1. Pratyush439 commented on Oct 11, 2026

    @Pratyush439

    Hi! I reproduced this issue on CPU using TorchAO's main branch (commit c54ce72).
    When calling unpack(pack(x, 4), 4) on a tensor of shape (8, 16), the resulting tensor has shape (16, 8), and the round-trip equality check fails.
    Explicitly specifying dim=-1 in both calls fixes the issue.
    I noticed you mentioned having a fix ready. Are you planning to submit a PR? If not, I'd be happy to work on the fix and add regression tests.
    Thank you!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions