Lightning-AI / Lightning-AI/pytorch-lightning

Address FSDP + manual optimization

Open
#19,685 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug strategy: fsdp ver: 2.2.x
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Bug description

In manual optimization, the user can call self.backward() anywhere in training_step(). There are no limitations for this in single-device execution, but for distributed strategies there are challenges associated with that.

In DDP, we solve that problem by disabling a backward hook before calling the actual backward:
https://github.com/Lightning-AI/pytorch-lightning/blob/6cfc590716cbf52e09033ae11ebee10864ef7589/src/lightning/pytorch/strategies/ddp.py#L316-L317

However, such a mechanism doesn't exist for FSDP, and calling backward during "forward" is not supported in sharded models. We should investigate whether it is ok to do this from the root fsdp model or not, and discuss possible workarounds if there are issues.

What version are you seeing the problem on?

master

How to reproduce the bug

No response

Error messages and logs
# Error messages and logs here please
Environment
Current environment
#- Lightning Component (e.g. Trainer, LightningModule, LightningApp, LightningWork, LightningFlow):
#- PyTorch Lightning Version (e.g., 1.5.0):
#- Lightning App Version (e.g., 0.5.2):
#- PyTorch Version (e.g., 2.0):
#- Python version (e.g., 3.9):
#- OS (e.g., Linux):
#- CUDA/cuDNN version:
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source):
#- Running environment of LightningApp (e.g. local, cloud):
More info

No response

cc @awaelchli @carmocca

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the manual-optimization path around training_step() and compare the backward-hook handling in src/lightning/pytorch/strategies/ddp.py at the linked lines. Investigate whether invoking backward from the root FSDP model is supported, and identify viable workarounds for sharded models. The issue has no reproduction or test location, so done is not precisely defined.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.