Skip to content

Add bf16 optimizer state support via step pre-hook - #2732

Merged
tianyu-l merged 3 commits into
pytorch:mainfrom
vivekgoe:bf16_optimizer_states
Apr 15, 2026
Merged

tianyu-l merged 3 commits into
pytorch:mainfrom
vivekgoe:bf16_optimizer_states

Conversation

@vivekgoe

Copy link
Copy Markdown
Contributor

Use a step pre-hook to pre-populate Adam/AdamW optimizer states (exp_avg, exp_avg_sq) in bfloat16 before the optimizer's lazy initialization runs. This causes _init_group to skip its own fp32 allocation and the fused CUDA kernel to dispatch to its mixed-precision path (FusedAdamMathFunctorMP), reducing optimizer memory usage while maintaining convergence.
Enabled via --optimizer.bf16_optimizer_states flag (requires fused implementation and Adam/AdamW).

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 27, 2026
@vivekgoe

Copy link
Copy Markdown
Contributor Author

@tianyu-l @janeyx99 Changes to initialize optimizer states in bf16 from TorchTitan such that we can use Pytorch optimizer Fused kernels that support mixed precision optimizer states. Please review, if this looks acceptable then I will go ahead and work on cleaning up corresponding Pytorch PR.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds OK to me, had some comments on where to place code. Thanks!

Comment thread torchtitan/components/optimizer.py Resolved
Comment thread torchtitan/trainer.py Resolved
Comment thread torchtitan/trainer.py Resolved
@vivekgoe

Copy link
Copy Markdown
Contributor Author

@tianyu-l Pushed patch with changes to take care of review comments, please let me know if any other changes are required.
If everything looks ok, we can merge this once pytorch/pytorch#175230 is completed and merged.

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, had one more nit comment
Let us know if it's ready to merge.

Comment thread torchtitan/components/optimizer.py Resolved

@janeyx99 janeyx99 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also looks fine/expected to me--nice job reusing an existing hook! i'm less familiar with the torchtitan portions so tianyu's review is more weighty there.

pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Apr 3, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to #146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: #175230
Approved by: https://github.com/janeyx99
@vivekgoe
vivekgoe force-pushed the bf16_optimizer_states branch from 1a3a206 to d68c1ad Compare April 4, 2026 19:24
@vivekgoe
vivekgoe marked this pull request as ready for review April 6, 2026 06:20
@vivekgoe

vivekgoe commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@tianyu-l This PR is now ready. Please help review. Results shared are with latest nightly torch build.
Some plots from deepseek-v3-16b training to show memory saving from the feature, also shows there is no adverse impact on convergence & e2e performance.
optimizer_states_comparison

Added snapshots from torch traces showing performance gain only for optimizer.step calls, average wall duration of call reduced by ~25%.
FP32:
image
BF16
image

Comment thread docs/bf16_optimizer_states.md Resolved
Comment thread docs/bf16_optimizer_states.md Resolved
@vivekgoe

vivekgoe commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@tianyu-l Updated as per your review comments. Note I had to make a small change to optimizer.states_dtype default configuration to maintain backward compatibility
states_dtype: Literal["float32", "bfloat16"] | None = None
This ensures that someone who was already using training.dtype=bfloat16 does not hit the new assert added to check training.dtype=bfloat16 and optimizer.states_dtype=float32 combination.

@vivekgoe
vivekgoe force-pushed the bf16_optimizer_states branch from a2054a5 to 1f07d19 Compare April 6, 2026 16:02
Comment thread torchtitan/components/optimizer.py Resolved
Comment thread torchtitan/components/optimizer.py Resolved
Comment thread torchtitan/components/optimizer.py Resolved
Comment thread torchtitan/components/optimizer.py Resolved
Comment thread torchtitan/trainer.py Resolved
weifengpy pushed a commit to pytorch/pytorch that referenced this pull request Apr 7, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to #146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: #175230
Approved by: https://github.com/janeyx99
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to pytorch#146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: pytorch#175230
Approved by: https://github.com/janeyx99
@vivekgoe

vivekgoe commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

@tianyu-l Responded to your review comments. Need some guidance from you on OptimizerBackwardContainer change. Thanks.

@tianyu-l

tianyu-l commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

need to rebase

@vivekgoe
vivekgoe force-pushed the bf16_optimizer_states branch from 1d3f52a to 7af71b9 Compare April 9, 2026 06:31
@vivekgoe

vivekgoe commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

I have pushed latest version which aligns to all review comments. But Pytorch patch for mixed precision optimizer states has been reverted due to some failures with ROCm builds, so this one will need to wait until those failures get fixed. I will let you know.
Please review latest version and let me know if any other changes are required. Thanks.

@tianyu-l

tianyu-l commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

lgtm, let me know when it's ready to be tested against CI. Thanks a lot!

pytorch-bot Bot pushed a commit to pytorch/pytorch that referenced this pull request Apr 10, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to #146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: #175230
Approved by: https://github.com/janeyx99
pytorchmergebot pushed a commit to pytorch/pytorch that referenced this pull request Apr 12, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to #146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: #175230
Approved by: https://github.com/atalman
Use a step pre-hook to pre-populate Adam/AdamW optimizer states (exp_avg,
exp_avg_sq) in bfloat16 before the optimizer's lazy initialization runs.
This causes _init_group to skip its own fp32 allocation and the fused CUDA
kernel to dispatch to its mixed-precision path (FusedAdamMathFunctorMP),
reducing optimizer memory usage while maintaining convergence.

Enabled via --optimizer.bf16_optimizer_states flag (requires fused
implementation and Adam/AdamW).

Co-authored-by: Claude <[email protected]>
Made-with: Cursor

Address review: rename config, move validation and hook into optimizer

- Rename `bf16_optimizer_states: bool` to `states_dtype: Literal` to
  match the pattern used by TrainingConfig.dtype
- Move validation into Config.__post_init__ instead of Trainer.__init__
- Move hook registration into OptimizersContainer.__init__ so the
  optimizer is self-contained; trainer.py no longer needs changes

Co-authored-by: Claude <[email protected]>
Made-with: Cursor

Refactor bf16 optimizer state hook registration

- Remove the standalone `register_bf16_optimizer_state_hook` function and integrate its functionality directly into the `OptimizersContainer` class.
- Introduce a private method `_register_bf16_optimizer_state_hook` to handle the registration of a step pre-hook for Adam optimizer states in bfloat16.
- This change enhances encapsulation and simplifies the optimizer's initialization process.

Co-authored-by: Claude <[email protected]>
Made-with: Cursor

Add integration test and documents

Address review comments and add integration test and docs

- Add assertion in OptimizersInBackwardContainer to block states_dtype='bfloat16'
- Add cross-config validation in Trainer.Config to prevent states_dtype='float32'
  when training.dtype='bfloat16'
- Change states_dtype default from 'float32' to None for backward compatibility
- Add GPU integration test (optimizer_bf16_states) in features.py
- Add docs/bf16_optimizer_states.md with usage, background, and limitations
- Add bf16 optimizer states entry to README features list

Made-with: Cursor

Enhance BF16 optimizer states support and update related documentation

- Update optimizer implementation.
- Modify documentation to clarify usage and requirements for BF16 optimizer states.
- Adjust integration tests to reflect changes in optimizer configuration.
- Remove deprecated checks for states_dtype in Trainer and OptimizersInBackwardContainer.
@vivekgoe
vivekgoe force-pushed the bf16_optimizer_states branch from 2cd6620 to 235c2d5 Compare April 15, 2026 04:26
@pytorch-bot

pytorch-bot Bot commented Apr 15, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

Replace zero-argument super().__post_init__() with explicit
OptimizersContainer.Config.__post_init__(self) to work around
the known Python issue where @DataClass(slots=True) breaks the
__class__ closure that implicit super() relies on.

Made-with: Cursor
@vivekgoe

vivekgoe commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor Author

@tianyu-l This PR is now ready to be tested against CI. I have rebased it to latest. Thanks.

@tianyu-l
tianyu-l merged commit 0610235 into pytorch:main Apr 15, 2026
18 of 27 checks passed
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to pytorch#146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: pytorch#175230
Approved by: https://github.com/janeyx99
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to https://github.com/pytorch/pytorch/issues/146542#issuecomment-2836583889 for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here https://github.com/pytorch/torchtitan/pull/2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/175230
Approved by: https://github.com/atalman
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to pytorch#146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: pytorch#175230
Approved by: https://github.com/janeyx99
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Added Mixed precision optimizer Fused kernels for Adam/AdamW. Continuation of POC work done by @janeyx99 [Refer to pytorch#146542 (comment) for more details].
Motivation for adding these kernels is to provide a path to users to initialize optimizer states for Adam/AdamW in low-precision from user code and use these Fused kernels to achieve reduced device memory foot-print. TorchTitan example here pytorch/torchtitan#2732

Co-authored-by: Jane (Yuan) Xu <janeyx99>
Pull Request resolved: pytorch#175230
Approved by: https://github.com/atalman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants