Handle non-contiguous inputs in CPU blockwise quantize/dequantize#1996
Merged
matthewdouglas merged 1 commit intoJul 9, 2026
Conversation
The CPU quantize_blockwise / dequantize_blockwise kernels pass raw pointers (get_ptr) to the native library, which assumes contiguous memory. Non-contiguous inputs (e.g. strided slices) were read in physical order and produced silently incorrect results. Add `A = A.contiguous()` at kernel entry, matching the CUDA backend fix (bitsandbytes-foundation#1859). Enable the previously CPU-skipped TestNonContiguousInputs blockwise regression tests, which now pass (18 cases: fp16/bf16/fp32 x blocksize 64/128/256). Fixes bitsandbytes-foundation#1995 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
f056fd2 to
317a6d5
Compare
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
matthewdouglas
approved these changes
Jul 9, 2026
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.
Summary
Fixes #1995. The CPU
quantize_blockwise/dequantize_blockwisekernels pass raw pointers (get_ptr) to the native library, which assumes contiguous memory. Non-contiguous inputs (e.g. strided slices) were read in physical order and produced silently incorrect results — the same bug class as #1342 / #1690, which #1859 fixed for CUDA only.Per the maintainer's guidance on #1995, this applies the same approach as the CUDA backend: convert the input with
A = A.contiguous()at kernel entry.Changes
bitsandbytes/backends/cpu/ops.py: addA = A.contiguous()at the start of the CPUquantize_blockwiseanddequantize_blockwisekernels (in the latter, beforetorch.empty_likeso the output is contiguous as well).tests/test_ops.py: enable the previously CPU-skippedTestNonContiguousInputsblockwise tests.Verification
Run locally on the CPU backend (with the compiled native library):
fp16/bf16/fp32× blocksize64/128/256); these were previously silently incorrect..contiguous()is a no-op there):289 passed, 0 failed.Minimal repro before/after:
🤖 Generated with Claude Code