Repository navigation
Conversation
Author
|
cc @LaurentMazare @ivarflakstad @EricLBuehler — |
`QTensor::new` only ran `check_shape`, which verifies the last dimension is divisible by the block size but never checks that the storage actually holds as many blocks as the shape requires. A mismatched pair such as a one-block Q4_0 storage with shape `(64,)` (which needs two blocks) is accepted, then `dequantize` sizes its output from the shape and indexes the storage past its end: an out-of-bounds panic for the legacy quants, and a silently truncated/zero-padded tensor for the k-quants. `QTensor::new`/`QStorage` are public and `GgmlDType::cpu_zeros` hands out storage, so this is reachable through the public API. Compare the storage block count against `shape.elem_count() / block_size` and return an error on mismatch. The in-tree constructors (quantize and the GGUF/GGML loaders) already size storage from the shape, so they are unaffected. Adds a regression test.
v-code01
force-pushed
the
qtensor-validate-storage-shape
branch
from
July 10, 2026 18:40
569f243 to
0082709
Compare
astorise
added a commit
to astorise/candle
that referenced
this pull request
Jul 21, 2026
…t in QTensor::new
4 tasks done
astorise
added a commit
to astorise/candle
that referenced
this pull request
Jul 21, 2026
Upstream huggingface#3716: validate storage/shape block count in QTensor::new
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
QTensor::newruns onlycheck_shape, which verifies the last dimension is divisible by the block size but never checks that the storage actually holds as many blocks as the shape requires:So a mismatched pair — e.g. a one-block
Q4_0storage (32 elements) with shape(64,)(which needs two blocks) — is accepted, and thendequantizesizes its output from the shape and reads the storage past its end:QTensor::newandQStorageare public andGgmlDType::cpu_zeroshands out storage, so this is reachable through the public API:Fix
Compare the storage block count against
shape.elem_count() / block_sizeand return an error on mismatch:The in-tree constructors (
QTensor::quantizeand the GGUF/GGML loaders) already size storage from the shape, so they're unaffected.Tests
Adds
qtensor_new_rejects_storage_shape_mismatch. Existingquantized_tests(39),gguf_tests(10) stay green;cargo fmt/clippyclean.cc @ivarflakstad @EricLBuehler