Lightning-AI / Lightning-AI/pytorch-lightning
Model parameters size is zero when using fabric.sharded_model() context with deepspeed zero-3 strategy
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 31.4k
- Forks
- 3.8k
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 6
Description
### Bug description
Hi,
I use fabric with deepspeed zero-3 strategy to shard model among 2 gpus, and get ```Model params = 0.0 M``` of model size when using with fabric.sharded_model() context.
```Python
import lightning as L
fabric = L.Fabric(accelerator="cuda", strategy='deepspeed_stage_3', precision='bf16-mixed')
fabric.launch()
with fabric.sharded_model():
net = mymodel()
num_params = sum([param.nelement() for param in net.parameters()])
fabric.print('Model params = %2.1f M' % (num_params / 1000**2))
```
Without the fabric.sharded_model() context, I get the correct model size as ```Model params = 13.6 M```.
How to solve this issue? Thanks.
### What version are you seeing the problem on?
v2.0
### 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 @carmocca @justusschock @awaelchli
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the fabric.sharded_model() entry point and the deepspeed_stage_3 strategy, then reproduce the provided two-GPU example and compare parameter counts inside and outside the context. Done means the sharded-model path reports the correct nonzero model size, with a regression test covering the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- distributed-systems, machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100