Fix ModelPruning iterative reuse RuntimeError on non-leaf tensors#21717
Open
Priyanshu-byte-coder wants to merge 3 commits into
Open
Conversation
When ModelPruning with use_lottery_ticket_hypothesis=True is reused across multiple trainer.fit() calls, setup() is called again each run. After the first pass, _copy_param sets dst.data = src.data, making the parameters non-leaf tensors. The subsequent deepcopy(module) then raises: RuntimeError: Only Tensors created explicitly by the user (graph leaves) support the deepcopy protocol at the moment Fix by adding _deepcopy_for_pruning(), a helper that temporarily replaces non-leaf parameters with detached clones before deepcopy and restores the originals afterward. Also explicitly set _original_layers to None before re-populating in setup() so the previous run's tensor references are released before new copies are allocated. Fixes Lightning-AI#8542
The actual non-leaf tensor after pruning is module.weight stored in module.__dict__ by the forward pre-hook (weight_orig * weight_mask), not in module._parameters. Patching _parameters had two problems: 1. Did not fix the RuntimeError (wrong dict being patched) 2. Caused mypy errors: assigning Tensor to Parameter | None Switch to iterating module.__dict__ instead, which correctly captures the hook-written non-leaf weight attribute.
3de4112 to
03ed64a
Compare
for more information, see https://pre-commit.ci
Author
|
Pinging for review — all CI checks are green (triage, pre-commit, GitGuardian, ReadTheDocs all pass). @justusschock @tchaton could one of you take a look when you get a chance? The fix is in |
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.
What does this PR fix?
Fixes #8542.
When
ModelPruning(use_lottery_ticket_hypothesis=True)is reused across multipletrainer.fit()calls (iterative pruning), the second and subsequent calls tosetup()raise:Root cause: After the first training pass,
apply_lottery_ticket_hypothesis()calls_copy_param()which doesdst.data = src.data.to(dst.device). This in-place data assignment makes the model parameters non-leaf tensors. Whensetup()is called again on the nexttrainer.fit(),deepcopy(module)fails.Changes
src/lightning/pytorch/callbacks/pruning.py_deepcopy_for_pruning(module)static helper: temporarily replaces non-leaf parameters withdetach().clone()copies, deepcopies, then restores originalssetup()to use_deepcopy_for_pruninginstead of baredeepcopy(module)setup(), setself._original_layers = Nonebefore re-populating to release previous-run tensor referencestests/tests_pytorch/callbacks/test_pruning.pytest_iterative_pruning_no_runtime_error: runs 3 consecutivetrainer.fit()calls with the same pruning callback and verifies noRuntimeErroris raisedReproduction
📚 Documentation preview 📚: https://pytorch-lightning--21717.org.readthedocs.build/en/21717/