Skip to content

[CUDA] Fixing quantization uint8 packing bug for NF4 and FP4 #1721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Mhmd-Hisham
Copy link
Contributor

This pull request addresses a bug in the quantization logic for FP4 and NF4 during 8-bit packing in the kQuantizeBlockwise CUDA kernel.

The expected behaviour is to pack two quantized 4-bit values into a single unsigned char (uint8) variable to reduce the overall memory footprint. The issue in the current implementation is that when packing values, the intermediate variable used was not getting cleared between iterations.

Since we quantize values per block, the error in larger block sizes would blow up, rendering the algorithm ineffective when block sizes are larger than 512.

List of changes:

  • Fixed uint8 packing bug for NF4 and FP4.
  • Modified the corresponding unit tests to account for larger block sizes.
  • Improved FP4 mean absolute error by ~73% and relative error by ~96% compared to the baseline for block sizes >= 1024.
  • Improved NF4 mean absolute error by ~88% and relative error by ~98% compared to the baseline for block sizes >= 1024.

The following graph highlights the accuracy improvements:

quantization_mean_error_fixed_vs_baseline

@Mhmd-Hisham Mhmd-Hisham marked this pull request as draft August 1, 2025 23:39
@Mhmd-Hisham Mhmd-Hisham marked this pull request as ready for review August 1, 2025 23:45
@Mhmd-Hisham Mhmd-Hisham force-pushed the quantization-packing-bug-fix branch from c2d7ec7 to 639f8c0 Compare August 2, 2025 00:15
@matthewdouglas matthewdouglas added this to the v0.47.0 milestone Aug 6, 2025
@matthewdouglas matthewdouglas added the Bug Something isn't working label Aug 6, 2025
@matthewdouglas matthewdouglas self-assigned this Aug 6, 2025
Copy link

github-actions bot commented Aug 6, 2025

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
Copy link
Member

Hi @Mhmd-Hisham,

Thank you for finding this, contributing this fix, and for your detailed explanation. Much appreciated!

@matthewdouglas matthewdouglas merged commit 19fe95a into bitsandbytes-foundation:main Aug 6, 2025
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants