-
Notifications
You must be signed in to change notification settings - Fork 345
Support MX4 E3M0 format and add stochastic rounding #477
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
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
47f7bc1
refactor custom fp cast
gau-nernst da17611
add dequant
gau-nernst 3345740
small formating
gau-nernst 2690b92
compile with fullgraph=True
gau-nernst 8aa0146
add fullgraph=true
gau-nernst be77632
undo
gau-nernst 95f4582
add another version
gau-nernst dcd5a05
fast path for mbits=1
gau-nernst f61ff05
Merge branch 'pytorch:main' into custom_fpx
gau-nernst 4ad065f
Merge branch 'pytorch:main' into custom_fpx
gau-nernst bd64efc
add back docstring
gau-nernst ea3efa0
add e3m0 support
NicoleMayer fcce64e
add stochastic rounding support for MX6 and MX4
NicoleMayer 5326dce
add unit test for e3m0 and stochastic rounding
NicoleMayer dfdd8db
fix the subnormal part for stochastic rounding
NicoleMayer 45520d2
delete DTYPE_FP4 and use DTYPE_FP4_E2M1/DTYPE_FP4_E3M0 separately
NicoleMayer 67255c3
update RoundingMode API
NicoleMayer 9a8575e
fix the bug for subnormal part
NicoleMayer 65ed552
add rounding before calculating the largest power of 2 for scaling fa…
NicoleMayer 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
|
||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import pytest | ||
|
||
import torch | ||
from torchao.prototype.mx_formats.constants import ( | ||
F32_MIN_NORMAL, | ||
F4_E3M0_EXP_BIAS, | ||
F4_E3M0_MAX, | ||
F4_E3M0_MIN_NORMAL, | ||
) | ||
from torchao.prototype.mx_formats.custom_cast import ( | ||
EBITS_F4_E3M0, | ||
f32_to_f4_e3m0_unpacked, | ||
MBITS_F4_E3M0, | ||
) | ||
from torchao.prototype.mx_formats.mx_tensor import MXTensor, to_mx | ||
|
||
|
||
torch.manual_seed(2) | ||
|
||
|
||
@pytest.mark.parametrize("hp_dtype", [torch.float32]) | ||
@pytest.mark.parametrize("device", ["cuda", "cpu"]) | ||
@pytest.mark.parametrize("sign", [1, -1]) | ||
@pytest.mark.parametrize("use_stochastic_rounding", [False, True]) | ||
def test_overflow_cast(hp_dtype, device, sign, use_stochastic_rounding): | ||
data_min = sign * F4_E3M0_MAX | ||
data_max = sign * F4_E3M0_MAX * F4_E3M0_MAX | ||
data = ( | ||
torch.rand(1024, 1024, dtype=hp_dtype, device=device) * (data_max - data_min) | ||
+ data_min | ||
) | ||
|
||
data_lp = f32_to_f4_e3m0_unpacked(data, use_stochastic_rounding) | ||
if sign == 1: | ||
target_lp = torch.full_like(data, 2**EBITS_F4_E3M0 - 1, dtype=torch.uint8) | ||
else: | ||
target_lp = torch.full_like( | ||
data, 2 ** (EBITS_F4_E3M0 + 1) - 1, dtype=torch.uint8 | ||
) | ||
|
||
torch.testing.assert_close( | ||
data_lp, | ||
target_lp, | ||
atol=0, | ||
rtol=0, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("hp_dtype", [torch.float32]) | ||
@pytest.mark.parametrize("device", ["cuda", "cpu"]) | ||
def test_underflow_cast(hp_dtype, device): | ||
data_min = -F4_E3M0_MIN_NORMAL | ||
data_max = F4_E3M0_MIN_NORMAL | ||
data = ( | ||
torch.rand(1024, 1024, dtype=hp_dtype, device=device) * (data_max - data_min) | ||
+ data_min | ||
) | ||
|
||
data_lp = f32_to_f4_e3m0_unpacked(data, use_stochastic_rounding=False) | ||
target_lp = torch.where((data >= 0) & (data <= F4_E3M0_MIN_NORMAL / 2), 0, 1).to( | ||
torch.uint8 | ||
) | ||
target_lp = torch.where( | ||
data < -F4_E3M0_MIN_NORMAL / 2, 1 + 2**EBITS_F4_E3M0, data_lp | ||
) | ||
|
||
torch.testing.assert_close( | ||
data_lp, | ||
target_lp, | ||
atol=0, | ||
rtol=0, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("hp_dtype", [torch.float32]) | ||
@pytest.mark.parametrize("device", ["cuda", "cpu"]) | ||
def test_underflow_cast_use_stochastic_rounding(hp_dtype, device): | ||
data_min = -F4_E3M0_MIN_NORMAL | ||
data_max = F4_E3M0_MIN_NORMAL | ||
data = ( | ||
torch.rand(1024, 1024, dtype=hp_dtype, device=device) * (data_max - data_min) | ||
+ data_min | ||
) | ||
|
||
data_lp = f32_to_f4_e3m0_unpacked(data, use_stochastic_rounding=True) | ||
target_lp = torch.where((data >= 0) & (data <= F4_E3M0_MIN_NORMAL / 2), 0, 1).to( | ||
torch.uint8 | ||
) | ||
target_lp = torch.where( | ||
data < -F4_E3M0_MIN_NORMAL / 2, 1 + 2**EBITS_F4_E3M0, data_lp | ||
) | ||
|
||
|
||
torch.testing.assert_close( | ||
data_lp, | ||
target_lp, | ||
atol=1, | ||
rtol=0, | ||
) | ||
|
||
zeros_in_data_lp = (data_lp == 0).sum().item() | ||
zeros_in_target_lp = (target_lp == 0).sum().item() | ||
|
||
assert ( | ||
zeros_in_data_lp >= zeros_in_target_lp | ||
), f"stochastic rounding should have more non-zero values {zeros_in_data_lp} >= {zeros_in_target_lp}" | ||
|
||
|
||
@pytest.mark.parametrize("exp_range", list(range(-2, 4))) | ||
@pytest.mark.parametrize("hp_dtype", [torch.float32]) | ||
@pytest.mark.parametrize("device", ["cuda", "cpu"]) | ||
@pytest.mark.parametrize("sign", [1, -1]) | ||
@pytest.mark.parametrize("use_stochastic_rounding", [False, True]) | ||
def test_normal_cast(exp_range, hp_dtype, device, sign, use_stochastic_rounding): | ||
if sign == 1: | ||
data_min = pow(2, exp_range) | ||
data_max = pow(2, exp_range + 1) | ||
else: | ||
data_min = - pow(2, exp_range + 1) | ||
data_max = - pow(2, exp_range) | ||
|
||
data = ( | ||
torch.rand(1024, 1024, dtype=hp_dtype, device=device) * (data_max - data_min) | ||
+ data_min | ||
) | ||
|
||
data_lp = f32_to_f4_e3m0_unpacked(data, use_stochastic_rounding).to(torch.float32) | ||
if sign == 1: | ||
data_lp = torch.pow(2, data_lp - F4_E3M0_EXP_BIAS) | ||
else: | ||
data_lp = -torch.pow(2, data_lp - F4_E3M0_EXP_BIAS - 8) | ||
|
||
torch.testing.assert_close( | ||
data_lp, | ||
data, | ||
atol=data_max - data_min, | ||
rtol=0, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("data_range", [1, 0.75, 0.5, 0.25, 0.125]) | ||
@pytest.mark.parametrize("hp_dtype", [torch.float32, torch.bfloat16]) | ||
@pytest.mark.parametrize("device", ["cuda", "cpu"]) | ||
@pytest.mark.parametrize("block_size", [32]) | ||
@pytest.mark.parametrize("use_stochastic_rounding", [False, True]) | ||
def test_mx_qdq(data_range, hp_dtype, block_size, device, use_stochastic_rounding): | ||
data_min = -data_range | ||
data_max = data_range | ||
data = ( | ||
torch.rand(1024, 1024, dtype=hp_dtype, device=device) * (data_max - data_min) | ||
+ data_min | ||
) | ||
scale_e8m0_biased, data_lp = to_mx( | ||
data, "fp4_e3m0", block_size, use_stochastic_rounding | ||
) | ||
mx_args = MXTensor(scale_e8m0_biased, data_lp, "fp4_e3m0", block_size, data.dtype) | ||
data_qdq = mx_args.to_dtype(mx_args._orig_dtype) | ||
|
||
scale_e8m0_unbiased = scale_e8m0_biased - 127 | ||
scale_fp = torch.pow( | ||
torch.full(scale_e8m0_unbiased.size(), 2.0, device=data.device), | ||
scale_e8m0_unbiased, | ||
) | ||
scale_fp = torch.clamp(scale_fp, min=F32_MIN_NORMAL) | ||
|
||
data_lp = data.reshape(-1, block_size) / scale_fp.unsqueeze(1) | ||
data_lp = data_lp.reshape(data.shape) | ||
|
||
# exclude overflow values whose error is unbounded | ||
saturate_mask = data_lp >= F4_E3M0_MAX | ||
data_qdq = torch.where(saturate_mask, data, data_qdq) | ||
|
||
# the largest error equals to max_scale_value * max_exp_range | ||
max_scale_value = torch.max(scale_fp) | ||
largest_error = max_scale_value * (2**4 - 2**3) | ||
|
||
torch.testing.assert_close( | ||
data_qdq, | ||
data, | ||
atol=largest_error, | ||
rtol=0, | ||
) |
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
Oops, something went wrong.
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.
can we add these tests to test/prototype/mx_formats/test_custom_cast.py to keep the testing of MX numerics in one place?