-
-
Notifications
You must be signed in to change notification settings - Fork 929
Fix 4bit quantization with blocksize = 4096 #1160
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
Merged
Titus-von-Koeller
merged 2 commits into
bitsandbytes-foundation:main
from
matthewdouglas:quant4bit-blocksize4096
Apr 2, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1928,7 +1928,9 @@ def test_bench_dequantization(): | |
|
|
||
|
|
||
| @pytest.mark.parametrize("dtype", [torch.float32, torch.float16, torch.bfloat16], ids=describe_dtype) | ||
| def test_fp4_quant(dtype): | ||
| @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) | ||
| @pytest.mark.parametrize("blocksize", [64, 128, 256, 512, 1024, 2048, 4096]) | ||
| def test_4bit_quant(dtype, quant_type, blocksize): | ||
|
Comment on lines
-1931
to
+1933
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously only fp4 and blocksize=64 was tested here. Expanded these tests, but note the degradation with the larger block size. |
||
| vals = list(product([0, 1], repeat=4)) | ||
|
|
||
| code = {} | ||
|
|
@@ -1953,17 +1955,33 @@ def test_fp4_quant(dtype): | |
| code[idx] = result | ||
|
|
||
| A1 = torch.randn(1024, 1024, device="cuda", dtype=dtype) | ||
| qa, SA = F.quantize_fp4(A1, blocksize=64) | ||
| A2 = F.dequantize_fp4(qa, SA) | ||
| qa, SA = F.quantize_4bit(A1, blocksize=blocksize, quant_type=quant_type) | ||
| A2 = F.dequantize_4bit(qa, SA, blocksize=blocksize, quant_type=quant_type) | ||
|
|
||
| err = (A1 - A2).abs().float() | ||
| relerr = (err / (A1.abs().float() + 1e-8)).mean() | ||
| idx = err > 1.0 | ||
| err = err.mean() | ||
|
|
||
| assert A2.dtype == dtype | ||
| assert err.item() < 0.1 | ||
| assert relerr.item() < 0.28 | ||
|
|
||
| # With larger block sizes, we can expect this to blow up. | ||
| # At blocksize>=1024, don't even bother looking at relerr. | ||
| if blocksize <= 64: | ||
| assert err.item() < 0.1 | ||
| assert relerr.item() < 0.28 | ||
| elif blocksize <= 256: | ||
| assert err.item() < 0.11 | ||
| assert relerr.item() < 0.30 | ||
| elif blocksize <= 512: | ||
| assert err.item() < 0.12 | ||
| assert relerr.item() < 0.31 | ||
| elif quant_type == "fp4": | ||
| # 1024 => 0.48, 2048 => 0.52, 4096 => 0.56 | ||
| assert err.item() < 0.08 + math.log2(blocksize) * 4e-2 | ||
| else: | ||
| # 1024 => 0.8, 2048 => 0.88, 4096 => 0.96 | ||
| assert err.item() < math.log2(blocksize) * 8e-2 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("quant_type", ["fp4", "nf4"]) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhat subtle, but here's where the issue was.