Skip to content

FabricModule: wrap forward methods instead of monkeypatch-based redirect #20711

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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tonyf
Copy link

@tonyf tonyf commented Apr 13, 2025

What does this PR do?

When running with torch.compile and ModelParallelStrategy the monkeypatch method of redirecting forward methods through the FabricModule's custom forward method raises an exception if the marked method does not have the same signature as the original forward method.

Instead of monkeypatching forward to the method being called, this PR wraps the function to ensure the right strategy contexts are provided.

This could be cleaned up further by just wrapping the functions in mark_forward_method but that would require touching _LIGHTNING_MODULE_STEP_METHODS = ("training_step", "validation_step", "test_step", "predict_step").

Fixes #20710

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • [x ] Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--20711.org.readthedocs.build/en/20711/

@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Apr 13, 2025
@Borda
Copy link
Member

Borda commented Apr 14, 2025

seems the falling stand-alone test is a false alarm :)
but the others seems real...

Copy link

codecov bot commented Apr 14, 2025

Codecov Report

Attention: Patch coverage is 8.33333% with 11 lines in your changes missing coverage. Please review.

Project coverage is 79%. Comparing base (4281b58) to head (0798ef7).
Report is 1 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (4281b58) and HEAD (0798ef7). Click for more details.

HEAD has 1086 uploads less than BASE
Flag BASE (4281b58) HEAD (0798ef7)
cpu 270 27
python 30 3
lightning_fabric 63 2
pytest 139 2
python3.10 60 6
python3.12.7 90 9
lightning 151 15
python3.11 60 6
python3.12 30 3
gpu 4 2
pytorch2.1 30 6
pytorch_lightning 60 12
pytest-full 135 27
pytorch2.2.2 15 3
pytorch2.5 15 3
pytorch2.3 15 3
pytorch2.4.1 15 3
pytorch2.5.1 15 3
pytorch2.6 15 3
pytorch2.7 15 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #20711     +/-   ##
=========================================
- Coverage      87%      79%     -8%     
=========================================
  Files         268      268             
  Lines       23449    23449             
=========================================
- Hits        20389    18472   -1917     
- Misses       3060     4977   +1917     

@tonyf
Copy link
Author

tonyf commented Apr 14, 2025

Ah! looks like I need to update the redirection tests. Will try to get around to it soon. Currently using this fix in prod successfully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fabric lightning.fabric.Fabric
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mark_forward_method does not work with ModelParallelStrategy
2 participants