-
Notifications
You must be signed in to change notification settings - Fork 216
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
Support power of 2 scaling factors in float8 training and use e4m3 everywhere #1670
Merged
+145
−21
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f2433b1
support power of 2 scaling factors in float8 training
danielvegamyhre a9fe17e
fix linter issues
danielvegamyhre 896bd8f
power of 2 scale in amax_to_scale
danielvegamyhre c70ad60
add docstring to tensor_to_scale
danielvegamyhre 34cc033
rename round_scales_to_power_of_2
danielvegamyhre ab93e18
use bitshifting for power of 2 rounding
danielvegamyhre 56132a3
add tests for round to power of 2
danielvegamyhre 4169927
add unit tests for rounding scale down to nearest power of 2
danielvegamyhre c434498
rename to test_float8_utils.py
danielvegamyhre 40166e1
convert to fp32 before rounding scale down to power of 2; update unit…
danielvegamyhre c6bcac8
run tests on gpu
danielvegamyhre 77d004e
test nan
danielvegamyhre 533e027
skip torch versions < 2.5
danielvegamyhre 69dbadb
explicitly use float32
danielvegamyhre fa552c6
add todo for truncation issue
danielvegamyhre 21e8061
e4m3 on all casts for fp8 rowwise
danielvegamyhre 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 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 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 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,65 @@ | ||
import unittest | ||
|
||
import pytest | ||
import torch | ||
|
||
from torchao.float8.float8_utils import _round_scale_down_to_power_of_2 | ||
from torchao.utils import TORCH_VERSION_AT_LEAST_2_5 | ||
|
||
if not TORCH_VERSION_AT_LEAST_2_5: | ||
pytest.skip("Unsupported PyTorch version", allow_module_level=True) | ||
|
||
|
||
# source for notable single-precision cases: | ||
# https://en.wikipedia.org/wiki/Single-precision_floating-point_format | ||
@unittest.skipIf(not torch.cuda.is_available(), "CUDA not available") | ||
@pytest.mark.parametrize( | ||
"test_case", | ||
[ | ||
# ("test_case_name", input, expected result) | ||
("one", 1.0, 1.0), | ||
("inf", float("inf"), float("inf")), | ||
("nan", float("nan"), float("nan")), | ||
("smallest positive subnormal number", 2**-126 * 2**-23, 2**-126 * 2**-23), | ||
("largest normal number", 2**127 * (2 - 2**-23), float("inf")), | ||
("smallest positive normal number", 2**-126, 2**-126), | ||
("largest number less than one", 1.0 - 2**-24, 0.5), | ||
("smallest number larger than one", 1.0 + 2**-23, 1.0), | ||
# TODO(danielvegamyhre): debug why creating a tensor with largest | ||
# subnormal value in CI env for pytorch 2.5.1 truncates the value to 0. | ||
# ("largest subnormal number", [2**-126 * (1 - 2**-23), 1.1754943508222875e-38]), | ||
], | ||
) | ||
def test_round_scale_down_to_power_of_2_valid_inputs( | ||
test_case: dict, | ||
): | ||
test_case_name, input, expected_result = test_case | ||
input_tensor, expected_tensor = ( | ||
torch.tensor(input, dtype=torch.float32).cuda(), | ||
torch.tensor(expected_result, dtype=torch.float32).cuda(), | ||
) | ||
result = _round_scale_down_to_power_of_2(input_tensor) | ||
|
||
assert ( | ||
torch.equal(result, expected_tensor) | ||
or (result.isnan() and expected_tensor.isnan()) | ||
), f"test: {test_case_name}, input: {input_tensor}, expected {expected_tensor}, but got {result}" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"invalid_dtype", | ||
[ | ||
torch.bfloat16, | ||
torch.float16, | ||
torch.float64, | ||
torch.int8, | ||
torch.uint8, | ||
torch.int32, | ||
torch.uint32, | ||
torch.int64, | ||
], | ||
) | ||
def test_non_float32_input(invalid_dtype: torch.dtype): | ||
non_float32_tensor = torch.tensor([3.0], dtype=invalid_dtype) | ||
with pytest.raises(AssertionError, match="scale must be float32 tensor"): | ||
_round_scale_down_to_power_of_2(non_float32_tensor) |
This file contains 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 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 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 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.
if we're using bit shifting, IMO it would be good to
it's ok as is, but numerical correctness is IMO a good place to be super explicit and eliminate potential confusion
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.
Makes sense, done. I also did another round of torchtitan benchmarks with the final implementation:
Float8 row wise without power of 2:
Float8 row wise with power of 2:
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.
this is ready for another look when you have time - CI error on H100s is unrelated:
I think it may be caused by using a legacy container image without certain IMEX env var set? NVIDIA/nvidia-container-toolkit#797
anyway, i'll try retriggering CI and also in the meantime i'll take a look at the triton kernels compile generates for the exp2(floor(log2(x)) approach and see if i can tell why it's slow
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.
i just looked into why exp2(floor(log2(x))) is slow actually and it's actually an easy fix. when we do the rounding on this line, the scale is still in fp64:
ao/torchao/float8/float8_utils.py
Line 49 in 1d75c8f
if we convert to fp32 before doing the rounding, instead of at the end when we return (
ao/torchao/float8/float8_utils.py
Line 50 in 1d75c8f
Maybe simply because with double the bit-width these rounding ops are slower (we can only achieve 50% max TFLOPs in FP64 as FP32 on H100)? I'm surprised the effect is pronounced enough to cause a 8% regression in overall TPS when rounding fp64 scales, though. I haven't looked into the generated triton kernels yet, prioritizing shipping this first.
Benchmark data:
When scale is still float64 when rounding:
When scale is converted to fp32 before rounding: