-
Notifications
You must be signed in to change notification settings - Fork 584
[Quantization] Add per-expert global scaling factor for fp4 batched quantize #1835
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| DTYPES = [torch.float16, torch.bfloat16] | ||
| # The batch dimension doesn't need to be multiple of 128 | ||
| SHAPES = [(128, 64), (256, 128), (120, 64), (200, 256)] | ||
| BATCH_SHAPES = [(2, 128, 64), (3, 256, 128), (1, 120, 64)] | ||
| BATCH_SHAPES = [(1, 256, 128), (2, 128, 64), (3, 256, 128), (1, 120, 64)] | ||
| SEEDS = [42] | ||
| CUDA_DEVICES = ["cuda:0"] | ||
|
|
||
|
|
@@ -334,7 +334,7 @@ def test_nvfp4_batched_quantize( | |
|
|
||
| b, m, n = batch_shape | ||
| x = torch.randn(batch_shape, dtype=dtype) | ||
| tensor_amax = torch.abs(x).max().to(torch.float32) | ||
| tensor_amax = torch.abs(x).amax(dim=(1, 2)).to(torch.float32) | ||
| global_scale = FLOAT8_E4M3_MAX * FLOAT4_E2M1_MAX / tensor_amax | ||
| mask = None | ||
| # Test the batched quantization | ||
|
|
@@ -357,7 +357,7 @@ def test_nvfp4_batched_quantize( | |
|
|
||
| # Compare with single tensor quantization for each batch | ||
| for i in range(b): | ||
| single_out, single_scale = fp4_quantize(x[i], global_scale, 16, False, True) | ||
| single_out, single_scale = fp4_quantize(x[i], global_scale[i], 16, False, True) | ||
| if use_mask: | ||
| torch.testing.assert_close( | ||
| out[i][: mask[i]], single_out[: mask[i]], rtol=1e-5, atol=1e-5 | ||
|
|
@@ -414,7 +414,7 @@ def test_silu_and_mul_nvfp4_batched_quantize( | |
| for i in range(b): | ||
| x_silu_mul = silu_and_mul(x[i]) | ||
| single_out, single_scale = fp4_quantize( | ||
| x_silu_mul, global_scale, 16, False, True | ||
| x_silu_mul, global_scale[i], 16, False, True | ||
| ) | ||
|
Comment on lines
416
to
418
Contributor
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. Similar to the other test, this change to use Specifically, the lines should be: scale_ans = unswizzle_sf(out_scale[i].reshape(single_scale.shape), m, n)
ref_out_scale_expert = unswizzle_sf(ref_out_scale[i].reshape(single_scale.shape), m, n) |
||
| torch.testing.assert_close( | ||
| out[i][: mask[i]], single_out[: mask[i]], rtol=1e-5, atol=1e-5 | ||
|
|
||
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.
While this change to use per-batch
global_scale[i]is correct, it reveals a latent bug in the test logic for theuse_mask=Truecase on line 366. Theout_scale[i]tensor is 1D, but theunswizzle_sffunction expects a 2D tensor. This will likely cause the test to fail. You should reshapeout_scale[i]using the shape ofsingle_scalebefore passing it tounswizzle_sf.Specifically, line 366 should be: