-
Notifications
You must be signed in to change notification settings - Fork 46
Feat: Activation Checkpointing #154
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
Chamberlain0w0
wants to merge
3
commits into
master
Choose a base branch
from
feat/activation_checkpointing
base: master
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.
+957
−30
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -8,10 +8,13 @@ class GradMode { | |
| // Whether to enable Autograd (enabled by default) | ||
| static bool IsEnabled() { return grad_enabled_; } | ||
| static void SetEnabled(bool enabled) { grad_enabled_ = enabled; } | ||
| static bool PropagateRequiresGrad() { return propagate_requires_grad_; } | ||
| static void SetPropagateRequiresGrad(bool enabled) { propagate_requires_grad_ = enabled; } | ||
|
|
||
| private: | ||
| // grad mode should be thread_local | ||
| static thread_local bool grad_enabled_; | ||
| static thread_local bool propagate_requires_grad_; | ||
| }; | ||
|
|
||
| // RAII: Disable grad (align with torch.no_grad) | ||
|
|
@@ -34,4 +37,19 @@ class EnableGradGuard { | |
| bool prev_; | ||
| }; | ||
|
|
||
| // RAII: Propagate requires_grad metadata while graph construction is disabled. | ||
| // Used by non-reentrant checkpoint recomputation so downstream SetupContext | ||
| // calls see the same needs_input_grad_ pattern as the original forward, | ||
| // without wiring the recompute graph into the engine. | ||
| class PropagateRequiresGradGuard { | ||
|
Contributor
Author
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. 本质上是需要一个 2*2=4 种情况的精细控制(with_grad/no_grad x 带引用计数建图/仅传递 requires_grad)。 也可以不单独实现一个 guard,而是给原先的 GradGuard 重载一个带参数 (比如 bool record_context = false) 的构造方式,让 GradGuard 本身能够覆盖这四种情况。 |
||
| public: | ||
| PropagateRequiresGradGuard() : prev_(GradMode::PropagateRequiresGrad()) { | ||
| GradMode::SetPropagateRequiresGrad(true); | ||
| } | ||
| ~PropagateRequiresGradGuard() { GradMode::SetPropagateRequiresGrad(prev_); } | ||
|
|
||
| private: | ||
| bool prev_; | ||
| }; | ||
|
|
||
| } // namespace infini_train::autograd | ||
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,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <functional> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace infini_train { | ||
| class Tensor; | ||
| } // namespace infini_train | ||
|
|
||
| namespace infini_train::utils::checkpoint { | ||
|
|
||
| using CheckpointForwardFn | ||
| = std::function<std::vector<std::shared_ptr<Tensor>>(const std::vector<std::shared_ptr<Tensor>> &)>; | ||
|
|
||
| std::vector<std::shared_ptr<Tensor>> Checkpoint(const CheckpointForwardFn &forward_fn, | ||
| const std::vector<std::shared_ptr<Tensor>> &inputs, | ||
| bool use_reentrant = false, bool preserve_rng_state = false, | ||
| bool determinism_check = true, bool early_stop = true); | ||
|
|
||
| } // namespace infini_train::utils::checkpoint |
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.