Skip to content

Fix 0-element tensor handling in blockwise and 4-bit quantization - #2163

Closed
archidoge0 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
archidoge0:fix/empty-tensor-quantize
Closed

archidoge0 wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
archidoge0:fix/empty-tensor-quantize

Conversation

@archidoge0

Copy link
Copy Markdown

Fixes #2162

Summary

  • Guard n <= 0 in csrc/ops.cu (quantizeBlockwise and dequantizeBlockwise, matching csrc/cpu_ops.cpp) and skip the native CUDA kernel call when numel() == 0 in bitsandbytes/backends/cuda/ops.py after validating dtype and blocksize, preventing a <<<0, ...>>> launch and C++ exit(1) on empty parameter shards.
  • Guard _absmax.numel() > 0 when computing offset for compress_statistics=True in bitsandbytes/functional.py::quantize_4bit so state.offset is 0.0 instead of NaN on 0-element inputs.
  • Use out.reshape(shape) in bitsandbytes/backends/default/ops.py::_dequantize_4bit_compute so zero-sized trailing dimensions (e.g. (4, 0)) do not fail reshape(-1, 0).

Testing

  • Added empty-tensor roundtrip and torch.library.opcheck tests in tests/test_ops.py (test_quantize_dequantize_blockwise_empty and test_quantize_dequantize_4bit_empty).
  • Verified pytest tests/test_ops.py -k BlockwiseQuantOps on NVIDIA L4 (CUDA 13.0, PyTorch 2.14.1+cu130) across CPU and CUDA.

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.

quantize_4bit / quantize_blockwise: C++ exit(1) on 0-element CUDA tensor and NaN offset with compress_statistics=True

1 participant