Repository navigation
refactor(trainer): make activation checkpointing operator-based #3419
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b8b8b9e
refactor(trainer): make activation checkpointing operator-based
S1ro1 e4dc61b
refactor(models): remove checkpoint-only attention helpers
S1ro1 d58403b
refactor(trainer): clean up selective checkpoint policy
S1ro1 9d91e3a
fix(trainer): enforce varlen ring attention LSE layout
S1ro1 0394e86
fix(trainer): keep full checkpointing cache-free
S1ro1 0a7731b
fix(trainer): remove stale dispatcher import
S1ro1 7cfb50d
refactor(models): colocate sparse MLA operators
S1ro1 e17d90b
refactor(models): simplify ring attention operators
S1ro1 7ab7995
chore(deps): update FlashAttention 4 runtime
S1ro1 1202339
fix(trainer): preserve FSDP selective replay order
S1ro1 3a51c78
fix(trainer): preserve mandatory full-checkpoint ops
S1ro1 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
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
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
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,117 @@ | ||
| """Whole-block activation checkpointing with an operator-based policy.""" | ||
|
|
||
| from collections.abc import Callable | ||
| from functools import partial | ||
|
|
||
| import torch | ||
| from torch import nn | ||
| from torch.distributed.algorithms._checkpoint.checkpoint_wrapper import CheckpointImpl, checkpoint_wrapper | ||
| from torch.utils.checkpoint import ( | ||
| CheckpointPolicy, | ||
| SelectiveCheckpointContext, | ||
| create_selective_checkpoint_contexts, | ||
| ) | ||
|
|
||
| from prime_rl.configs.trainer import ActivationCheckpointConfig | ||
|
|
||
| # Adapted from TorchTitan's whole-block selective activation checkpointing policy. | ||
| # These targets and the CUDA-to-CPU copy rule are correctness requirements. They | ||
| # must remain active when custom targets replace the default selective targets. | ||
| MANDATORY_SAVE_NAMESPACES = frozenset({"deepep"}) | ||
| MANDATORY_SAVE_OPERATIONS = frozenset( | ||
| { | ||
| "aten::topk", | ||
| "prime_rl::record_moe_routing_statistics", | ||
| } | ||
| ) | ||
|
|
||
| DEFAULT_SELECTIVE_SAVE_NAMESPACES = frozenset( | ||
| { | ||
| "_c10d_functional", | ||
| "flash_attn", | ||
| "flash_attn_3", | ||
| "prime_rl_collectives", | ||
| "prime_rl_ring", | ||
| } | ||
| ) | ||
| DEFAULT_SELECTIVE_SAVE_OPERATIONS = frozenset( | ||
| { | ||
| "aten::_efficient_attention_forward", | ||
| "aten::_flash_attention_forward", | ||
| "aten::_scaled_dot_product_attention_math", | ||
| "aten::_scaled_dot_product_cudnn_attention", | ||
| "aten::_scaled_dot_product_efficient_attention", | ||
| "aten::_scaled_dot_product_flash_attention", | ||
| "aten::_scaled_dot_product_flash_attention_for_cpu", | ||
| "aten::_scaled_dot_product_fused_attention_overrideable", | ||
| "aten::_scaled_grouped_mm", | ||
| "aten::_scaled_mm", | ||
| "aten::_scaled_mm_v2", | ||
| "aten::_grouped_mm", | ||
| "aten::addmm", | ||
| "aten::bmm", | ||
| "aten::convolution", | ||
| "aten::linear", | ||
| "aten::mm", | ||
| "prime_rl::fp8_blockwise_mm", | ||
| "prime_rl::grouped_fp8_gemm", | ||
| "prime_rl::sparse_mla", | ||
| } | ||
| ) | ||
| DEFAULT_SELECTIVE_TARGETS = DEFAULT_SELECTIVE_SAVE_NAMESPACES | DEFAULT_SELECTIVE_SAVE_OPERATIONS | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
S1ro1 marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def _mandatory_checkpoint_policy( | ||
| _context: SelectiveCheckpointContext, | ||
| operation: torch._ops.OpOverload | torch._ops.HigherOrderOperator, | ||
| *args, | ||
| **kwargs, | ||
|
S1ro1 marked this conversation as resolved.
Resolved
|
||
| ) -> CheckpointPolicy: | ||
| if operation.namespace in MANDATORY_SAVE_NAMESPACES or operation.name() in MANDATORY_SAVE_OPERATIONS: | ||
| return CheckpointPolicy.MUST_SAVE | ||
|
|
||
| if operation.name() == "aten::_to_copy": | ||
| device = kwargs.get("device") | ||
| if isinstance(device, torch.device) and device.type == "cpu": | ||
| return CheckpointPolicy.MUST_SAVE | ||
|
|
||
| return CheckpointPolicy.PREFER_RECOMPUTE | ||
|
|
||
|
|
||
| def _selective_checkpoint_policy( | ||
| context: SelectiveCheckpointContext, | ||
| operation: torch._ops.OpOverload | torch._ops.HigherOrderOperator, | ||
| *args, | ||
| targets: frozenset[str] = DEFAULT_SELECTIVE_TARGETS, | ||
| **kwargs, | ||
| ) -> CheckpointPolicy: | ||
| runtime_policy = _mandatory_checkpoint_policy(context, operation, *args, **kwargs) | ||
| if runtime_policy is CheckpointPolicy.MUST_SAVE: | ||
| return runtime_policy | ||
| if operation.namespace in targets or operation.name() in targets: | ||
| return CheckpointPolicy.MUST_SAVE | ||
| return CheckpointPolicy.PREFER_RECOMPUTE | ||
|
|
||
|
|
||
| def get_activation_checkpoint_wrapper(config: ActivationCheckpointConfig) -> Callable[[nn.Module], nn.Module]: | ||
| if config.mode == "full": | ||
| return partial(checkpoint_wrapper, checkpoint_impl=CheckpointImpl.NO_REENTRANT) | ||
|
|
||
| targets = DEFAULT_SELECTIVE_TARGETS if config.targets is None else frozenset(config.targets) | ||
| policy = partial(_selective_checkpoint_policy, targets=targets) | ||
|
|
||
| return partial( | ||
| checkpoint_wrapper, | ||
| checkpoint_impl=CheckpointImpl.NO_REENTRANT, | ||
| context_fn=partial(create_selective_checkpoint_contexts, policy), | ||
| ) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "DEFAULT_SELECTIVE_SAVE_NAMESPACES", | ||
| "DEFAULT_SELECTIVE_SAVE_OPERATIONS", | ||
| "DEFAULT_SELECTIVE_TARGETS", | ||
| "MANDATORY_SAVE_NAMESPACES", | ||
| "MANDATORY_SAVE_OPERATIONS", | ||
| "get_activation_checkpoint_wrapper", | ||
| ] | ||
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.
Uh oh!
There was an error while loading. Please reload this page.