Repository navigation
Add bf16 optimizer state support via step pre-hook - #2732
Conversation
tianyu-l
left a comment
There was a problem hiding this comment.
Sounds OK to me, had some comments on where to place code. Thanks!
|
@tianyu-l Pushed patch with changes to take care of review comments, please let me know if any other changes are required. |
tianyu-l
left a comment
There was a problem hiding this comment.
lgtm, had one more nit comment
Let us know if it's ready to merge.
janeyx99
left a comment
There was a problem hiding this comment.
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.
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
1a3a206 to
d68c1ad
Compare
|
@tianyu-l This PR is now ready. Please help review. Results shared are with latest nightly torch build. Added snapshots from torch traces showing performance gain only for optimizer.step calls, average wall duration of call reduced by ~25%. |
|
@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 |
a2054a5 to
1f07d19
Compare
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
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
|
@tianyu-l Responded to your review comments. Need some guidance from you on OptimizerBackwardContainer change. Thanks. |
|
need to rebase |
1d3f52a to
7af71b9
Compare
|
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. |
|
lgtm, let me know when it's ready to be tested against CI. Thanks a lot! |
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
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.
2cd6620 to
235c2d5
Compare
|
|
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
|
@tianyu-l This PR is now ready to be tested against CI. I have rebased it to latest. Thanks. |
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
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
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
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



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).