Skip to content

[release/2.13] Fix Unpickler::rebuildTensor storage size check for quantized tensors (#199497) - #4112

Draft
sstamenk wants to merge 1 commit into
release/2.13from
sstamenk/release-2.13-unpickler-quantized-storage-check
Draft

sstamenk wants to merge 1 commit into
release/2.13from
sstamenk/release-2.13-unpickler-quantized-storage-check

Conversation

@sstamenk

@sstamenk sstamenk commented Dec 1, 2026

Copy link
Copy Markdown
Member

Linked issue or supporting maintainer

Cherry-pick of pytorch#199497 (by @Flamefire), which is not merged upstream yet. This draft exists for early testing; it should be re-picked from the merged upstream commit before it is marked ready.

BC-breaking?

No

Summary (human written only)

Agent details (AI-generated)

Drafted with an AI assistant (Claude); to be reviewed by the author.

Problem. torch.jit.load rejects valid quint4x2 / quint2x4 tensors. Those dtypes pack several values per byte, but the storage bounds check added to Unpickler::rebuildTensor by pytorch#183381 counts one byte per element:

TestQuantizeEagerPTQStatic::test_quantized_embedding_bag
RuntimeError: Tensor: sizes 10 12, strides 12 1, storage offset 0 and itemsize 1 require a storage of at least 120 bytes, but storage only has 60 bytes

It is not specific to a platform or to ROCm: it reproduces on Linux CPU with the 2.14 and 2.15 ROCm nightly wheels by saving and loading a scripted torch.ao.nn.quantized.EmbeddingBag with a quint4x2 weight.

Change. The upstream PR at d4048dcf3986 (13 commits) squashed into one commit, original author kept.

Adaptation for release/2.13. The C++ changes applied except for one include-block conflict in unpickler.cpp: the upstream context line #include <c10/util/FbcodeMaps.h> does not exist on this branch, so only the new #include <c10/util/ArrayRef.h> was kept. In test_quantized_tensor.py the new test_jit_serialization_buffer_size conflicted with the neighbouring test_jit_serialization, whose body differs from main; the existing test is left as it is on this branch and the new test is added after it.

Testing. Not built and the tests were not run. The changed C++ files are byte-identical to the release/2.14 pick, which passes g++ -std=c++20 -fsyntax-only against a 2.14 wheel's headers. I have no 2.13 wheel to check this branch directly.

🤖 Generated with Claude Code

…rs (pytorch#199497)

It currently overestimates the required memory when multiple values are packed into a single byte.

E.g. for quint4x2:
> TestQuantizeEagerPTQStatic::test_quantized_embedding_bag - RuntimeError: Tensor: sizes 10 12, strides 12 1, storage offset 0 and itemsize 1 require a storage of at least 120 bytes, but storage only has 60 bytes

This can be reproduced using 
- python test_quantization.py TestQuantizeEagerPTQStatic.test_quantized_embedding
- python test_quantization.py TestQuantizeEagerPTQStatic.test_quantized_embedding_bag
and possibly others.

I'm wondering why this isn't caught by CI at all. Only guard seems AVX2 for FBGEMM

It might make sense to fold this calculation into `computeStorageNbytes` (pass data_type instead of item_size) but that change felt too large and I focused on fixing the immediate bug


(cherry picked from pytorch#199497 at d4048dc, squashed; the PR is not merged upstream yet)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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