fix(train): check trainable layers separately during checkpointing - #10142
Open
MrCapricornLiu wants to merge 1 commit into
Open
MrCapricornLiu wants to merge 1 commit into
MrCapricornLiu wants to merge 1 commit into
Conversation
Signed-off-by: Chenghao Liu <chliu@stu.pku.edu.cn>
Contributor
Author
|
The lint job failed while importing YAPF, with |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR type
PR information
Dynamic gradient checkpointing shares one
requires_gradcache across every wrapped layer. If the first layer is frozen, later trainable layers also seeFalse. With reentrant checkpointing and inputs that do not require gradients, those layers receive no parameter gradients. This affects partially frozen towers, such as a vision tower with a frozen prefix. The cache also becomes stale when a layer is unfrozen later.Check the current layer's trainable parameters when its input needs to be made differentiable. This removes the shared cache while preserving the existing checkpointing behavior.
Experiment results
python -m unittest discover -s tests -p test_dynamic_checkpointing.py -vBoth tests pass on CPU and a single CUDA device. They use the public dynamic checkpointing helper with a small ten-layer tower and compare outputs and every parameter gradient against an eager copy. Coverage includes a frozen prefix, an all-trainable control, non-reentrant checkpointing, and unfreezing a layer after the first forward. On the original code, 12 parameter-gradient checks fail across the two tests.
pre-commit run --all-filespassed. Tested with PyTorch 2.12.0 and Transformers 5.3.0; no pretrained vision model or distributed training run was used.