forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 75
[release/2.8] Add mx fp4 support #2472
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
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -882,6 +882,8 @@ def compute_error(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | |||||||||||
|
|
||||||||||||
| # largest power of 2 representable in `torch.float8_e4m3fn` | ||||||||||||
| F8E4M3_LARGEST_POW2 = 8 | ||||||||||||
| # largest power of 2 representable in `torch.float4_e2m1fn_x2` | ||||||||||||
| FP4E2M1FN_LARGEST_POW2 = 1.0 | ||||||||||||
| # max value of `torch.float8_e4m3fn` (448) | ||||||||||||
| F8E4M3_MAX_VAL = torch.finfo(torch.float8_e4m3fn).max | ||||||||||||
| # exponent bias of `torch.float8_e8m0fnu` | ||||||||||||
|
|
@@ -890,14 +892,20 @@ def compute_error(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: | |||||||||||
| FP4_EBITS, FP4_MBITS = 2, 1 | ||||||||||||
| FP4_MAX_VAL = 6.0 | ||||||||||||
|
|
||||||||||||
| def data_to_mx_scale(x, block_size): | ||||||||||||
| def data_to_mx_scale(x, block_size, recipe): | ||||||||||||
| # simple implementation of https://www.opencompute.org/documents/ocp-microscaling-formats-mx-v1-0-spec-final-pdf | ||||||||||||
| # section 6.3, not all edge cases (such as NaN) are handled/tested | ||||||||||||
| if recipe == "mxfp8": | ||||||||||||
| largest_pow2 = F8E4M3_LARGEST_POW2 | ||||||||||||
| elif recipe == "mxfp4": | ||||||||||||
| largest_pow2 = FP4E2M1FN_LARGEST_POW2 | ||||||||||||
| else: | ||||||||||||
| raise ValueError(f"data_to_mx_scale(): Unsupported mx recipe: {recipe}") | ||||||||||||
| orig_shape = x.shape | ||||||||||||
| x = x.reshape(-1, block_size) | ||||||||||||
| max_abs = torch.amax(torch.abs(x), 1) | ||||||||||||
| largest_p2_lt_max_abs = torch.floor(torch.log2(max_abs)) | ||||||||||||
| scale_e8m0_unbiased = largest_p2_lt_max_abs - F8E4M3_LARGEST_POW2 | ||||||||||||
| scale_e8m0_unbiased = largest_p2_lt_max_abs - largest_pow2 | ||||||||||||
| scale_e8m0_unbiased = torch.clamp(scale_e8m0_unbiased, -1 * F8E8M0_EXP_BIAS, F8E8M0_EXP_BIAS) | ||||||||||||
| scale_e8m0_biased = scale_e8m0_unbiased + F8E8M0_EXP_BIAS | ||||||||||||
| scale_e8m0_biased = scale_e8m0_biased.to(torch.uint8) | ||||||||||||
|
|
@@ -1446,20 +1454,21 @@ def test_pack_uint4(self): | |||||||||||
| (127, 96, 1024), | ||||||||||||
| (1025, 128, 96) | ||||||||||||
| ], name_fn=lambda mkn: f"{mkn[0]}_{mkn[1]}_{mkn[2]}") | ||||||||||||
| @parametrize("recipe", ["mxfp8", "nvfp4"]) | ||||||||||||
| def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, recipe) -> None: | ||||||||||||
| if recipe == "nvfp4" and fast_accum: | ||||||||||||
| return unittest.skip("fast_accum not supported in nvfp4 cublas gemm, skipping") | ||||||||||||
| @parametrize("recipe", ["mxfp8", "mxfp4" if torch.version.hip else "nvfp4"]) | ||||||||||||
| def test_blockwise_mxfp8_nvfp4_mxfp4_numerics(self, test_case_name, fast_accum, mkn, recipe) -> None: | ||||||||||||
| if (recipe == "nvfp4" or recipe == "mxfp4") and fast_accum: | ||||||||||||
| raise unittest.SkipTest("fast_accum not supported in nvfp4/mxfp4 cublas gemm, skipping") | ||||||||||||
|
|
||||||||||||
| device = "cuda" | ||||||||||||
| M, K, N = mkn | ||||||||||||
| if torch.version.hip: | ||||||||||||
| if not (M % 32 == 0 and K % 32 == 0 and N % 32 == 0): | ||||||||||||
| raise unittest.SkipTest("Matrix dimensions must be multiples of 32 on ROCm, skipping") | ||||||||||||
|
|
||||||||||||
| if recipe == "nvfp4" and K % 32 != 0: | ||||||||||||
| return unittest.skip("K must be divisible by 32 for nvfp4 cublas gemm, skipping") | ||||||||||||
| if (recipe == "nvfp4" or recipe == "mxfp4") and K % 32 != 0: | ||||||||||||
| raise unittest.SkipTest("K must be divisible by 32 for nvfp4/mxfp4 cublas gemm, skipping") | ||||||||||||
|
|
||||||||||||
| fp4_scaling_dtype = torch.float8_e8m0fnu if torch.version.hip else torch.float8_e4m3fn | ||||||||||||
| BLOCK_SIZE = 16 if recipe == "nvfp4" else 32 | ||||||||||||
| require_exact_match = True | ||||||||||||
| approx_match_sqnr_target = 22.0 | ||||||||||||
|
|
@@ -1475,11 +1484,11 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| B = B_ref.to(torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
|
|
||||||||||||
| elif test_case_name == "a_ones_b_ones": | ||||||||||||
| A_ref = torch.ones(M, K, device=device, dtype=torch.bfloat16) | ||||||||||||
|
|
@@ -1490,11 +1499,11 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| B = B_ref.to(torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
|
|
||||||||||||
| elif test_case_name == "a_ones_modified_b_ones": | ||||||||||||
| A_ref = torch.ones(M, K, device=device, dtype=torch.bfloat16) | ||||||||||||
|
|
@@ -1506,11 +1515,11 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| B = B_ref.to(torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
|
|
||||||||||||
| elif test_case_name == "a_ones_b_ones_modified": | ||||||||||||
| A_ref = torch.ones(M, K, device=device, dtype=torch.bfloat16) | ||||||||||||
|
|
@@ -1522,11 +1531,11 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| B = B_ref.to(torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
|
|
||||||||||||
| elif test_case_name == "a_scale_modified_b_ones": | ||||||||||||
| A_ref = torch.ones(M, K, device=device, dtype=torch.bfloat16) | ||||||||||||
|
|
@@ -1540,11 +1549,11 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| A_ref[1][0:BLOCK_SIZE] = 4 | ||||||||||||
| A[1][0:BLOCK_SIZE] = 2 | ||||||||||||
| A_scale[1][0] = 2 | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| A_ref[1][0:BLOCK_SIZE] = 4 | ||||||||||||
| A.view(torch.uint8)[1][0:(BLOCK_SIZE // 2)] = 0b01000100 | ||||||||||||
| A_scale[1][0] = 2 | ||||||||||||
|
|
@@ -1561,11 +1570,11 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| B_ref[1][0:BLOCK_SIZE] = 4 | ||||||||||||
| B[1][0:BLOCK_SIZE] = 2 | ||||||||||||
| B_scale[1][0] = 2 | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_ref[1][0:BLOCK_SIZE] = 4 | ||||||||||||
| B.view(torch.uint8)[1][0:(BLOCK_SIZE // 2)] = 0b01000100 | ||||||||||||
| B_scale[1][0] = 2 | ||||||||||||
|
|
@@ -1585,7 +1594,7 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| B = B_ref.to(torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e8m0fnu) | ||||||||||||
| else: # nvfp4 | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| # scales all-ones, element data random while being exactly representable in float4_e2m1fn_x2 | ||||||||||||
| # generate integers in [0, 16] and cast to bfloat16 | ||||||||||||
| A_ref = _floatx_unpacked_to_f32( | ||||||||||||
|
|
@@ -1600,8 +1609,8 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
| ).bfloat16() | ||||||||||||
| A = _bfloat16_to_float4_e2m1fn_x2(A_ref) | ||||||||||||
| B = _bfloat16_to_float4_e2m1fn_x2(B_ref) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=torch.float8_e4m3fn) | ||||||||||||
| A_scale = torch.full((M, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
| B_scale = torch.full((N, ceil_div(K, BLOCK_SIZE)), 1.0, device=device, dtype=fp4_scaling_dtype) | ||||||||||||
|
|
||||||||||||
| elif test_case_name == "data_random_scales_from_data": | ||||||||||||
| if not K % BLOCK_SIZE == 0: | ||||||||||||
|
|
@@ -1613,17 +1622,18 @@ def test_blockwise_mxfp8_nvfp4_numerics(self, test_case_name, fast_accum, mkn, r | |||||||||||
|
|
||||||||||||
| if recipe == "mxfp8": | ||||||||||||
| # Calculate scales based on the inputs | ||||||||||||
| A_scale = data_to_mx_scale(A_ref, BLOCK_SIZE) | ||||||||||||
| B_scale = data_to_mx_scale(B_ref, BLOCK_SIZE) | ||||||||||||
| A_scale = data_to_mx_scale(A_ref, BLOCK_SIZE, recipe) | ||||||||||||
| B_scale = data_to_mx_scale(B_ref, BLOCK_SIZE, recipe) | ||||||||||||
| max_val = F8E4M3_MAX_VAL | ||||||||||||
| min_val = -1 * max_val | ||||||||||||
| A = (A_ref.reshape(-1, BLOCK_SIZE) / A_scale.reshape(M * ceil_div(K, BLOCK_SIZE), 1).float()).reshape(M, K) | ||||||||||||
| A = A.clamp(min=min_val, max=max_val).to(torch.float8_e4m3fn) | ||||||||||||
| B = (B_ref.reshape(-1, BLOCK_SIZE) / B_scale.reshape(N * ceil_div(K, BLOCK_SIZE), 1).float()).reshape(N, K) | ||||||||||||
| B = B.clamp(min=min_val, max=max_val).to(torch.float8_e4m3fn) | ||||||||||||
| else: # nvfp4 | ||||||||||||
| A_scale = data_to_nvfp4_scale(A_ref, BLOCK_SIZE) | ||||||||||||
| B_scale = data_to_nvfp4_scale(B_ref, BLOCK_SIZE) | ||||||||||||
| else: # nvfp4 # mxfp4 | ||||||||||||
| scale_func = data_to_mx_scale if recipe == "mxfp4" else data_to_nvfp4_scale | ||||||||||||
| A_scale = scale_func(A_ref, BLOCK_SIZE, recipe if recipe == "mxfp4" else None) | ||||||||||||
| B_scale = scale_func(B_ref, BLOCK_SIZE, recipe if recipe == "mxfp4" else None) | ||||||||||||
|
Comment on lines
+1635
to
+1636
|
||||||||||||
| A_scale = scale_func(A_ref, BLOCK_SIZE, recipe if recipe == "mxfp4" else None) | |
| B_scale = scale_func(B_ref, BLOCK_SIZE, recipe if recipe == "mxfp4" else None) | |
| scale_recipe_arg = recipe if recipe == "mxfp4" else None | |
| A_scale = scale_func(A_ref, BLOCK_SIZE, scale_recipe_arg) | |
| B_scale = scale_func(B_ref, BLOCK_SIZE, scale_recipe_arg) |
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.
[nitpick] The conditional expression
recipe if recipe == "mxfp4" else Noneis confusing. Consider extracting this logic into a clearer variable assignment or using a more explicit approach to pass the correct arguments to each scaling function.