-
Notifications
You must be signed in to change notification settings - Fork 627
[Common][PyTorch] Fuse scaling and unscaling of bf16 momentums into kernels #2632
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
Open
yaox12
wants to merge
6
commits into
NVIDIA:main
Choose a base branch
from
yaox12:xiny/fused_bf16_unscale
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4eb228a
fused scaling and unscaling of bf16 momentum
yaox12 38388ef
add more comments
yaox12 a608ec8
enable cuda graphs for bf16 momentums
yaox12 8931852
add tests
yaox12 b3b419c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 19ff141
update the check for store_param_remainders and capturable
yaox12 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -407,6 +407,20 @@ def test_bf16_exp_avg_sq(self): | |
| master_atol=2e-3, | ||
| ) | ||
|
|
||
| @pytest.mark.skipif(not is_bf16_available(), reason="bf16 if not supported") | ||
| def test_bf16_exp_avg_and_exp_avg_sq(self): | ||
| self.gen_precision_aware_test( | ||
| use_fp8_params=False, | ||
| param_dtype=torch.bfloat16, | ||
| use_master_weights=True, | ||
| master_weight_dtype=torch.float32, | ||
| grad_dtype=torch.float32, | ||
| exp_avg_dtype=torch.bfloat16, | ||
| exp_avg_sq_dtype=torch.bfloat16, | ||
| master_rtol=2e-3, | ||
| master_atol=2e-3, | ||
| ) | ||
|
Comment on lines
+410
to
+422
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. Test only covers non-capturable mode. Add test for capturable mode with BF16 momentums since PR enables CUDA Graphs for this case. Note: Check that |
||
|
|
||
| @pytest.mark.skipif(not is_bf16_available(), reason="bf16 if not supported") | ||
| @pytest.mark.skipif(not fp8_available, reason=reason_for_no_fp8) | ||
| def test_fp8_exp_avg_sq(self): | ||
|
|
@@ -553,7 +567,7 @@ def forward(self, x): | |
| return y | ||
|
|
||
|
|
||
| class AdamTest: | ||
| class TestAdamTest: | ||
|
|
||
| def setup_method(self, *, seed: int = 0) -> None: | ||
| torch.manual_seed(seed) | ||
|
|
@@ -569,8 +583,8 @@ def setup_method(self, *, seed: int = 0) -> None: | |
| def test_grad_scaler(self): | ||
| params_ = [p for p in self.model_.parameters() if p.requires_grad] | ||
| optimizer_ = te.optimizers.FusedAdam(params_, lr=self.lr, capturable=False) | ||
| scaler = torch.cuda.amp.GradScaler(enabled=True) | ||
| scaler_ = torch.cuda.amp.GradScaler(enabled=True) | ||
| scaler = torch.amp.GradScaler("cuda", enabled=True) | ||
| scaler_ = torch.amp.GradScaler("cuda", enabled=True) | ||
|
|
||
| for i in range(100): | ||
| x = torch.rand([32, 1, 28, 28]).cuda().to(memory_format=torch.channels_last) | ||
|
|
@@ -620,8 +634,8 @@ def test_grad_scaler(self): | |
| def test_grad_scaler_capturable(self): | ||
| params_ = [p for p in self.model_.parameters() if p.requires_grad] | ||
| optimizer_ = te.optimizers.FusedAdam(params_, lr=self.lr, capturable=True) | ||
| scaler = torch.cuda.amp.GradScaler(enabled=True) | ||
| scaler_ = torch.cuda.amp.GradScaler(enabled=True) | ||
| scaler = torch.amp.GradScaler("cuda", enabled=True) | ||
| scaler_ = torch.amp.GradScaler("cuda", enabled=True) | ||
|
|
||
| for i in range(100): | ||
| x = torch.rand([32, 1, 28, 28]).cuda().to(memory_format=torch.channels_last) | ||
|
|
@@ -678,8 +692,8 @@ def test_grad_scaler_capturable_master(self): | |
| optimizer_ = te.optimizers.FusedAdam( | ||
| params_, lr=self.lr, capturable=True, master_weights=master_weights | ||
| ) | ||
| scaler = torch.cuda.amp.GradScaler(enabled=True) | ||
| scaler_ = torch.cuda.amp.GradScaler(enabled=True) | ||
| scaler = torch.amp.GradScaler("cuda", enabled=True) | ||
| scaler_ = torch.amp.GradScaler("cuda", enabled=True) | ||
|
|
||
| for i in range(100): | ||
| x = torch.rand([32, 1, 28, 28]).cuda().to(memory_format=torch.channels_last) | ||
|
|
||
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.
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.
Consider adding a test for capturable mode (CUDA Graphs) with BF16 momentums, since the PR description mentions "Enable CUDA Graphs for BF16 momentums" as a key feature. The current test only covers non-capturable mode.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!