Lightning-AI / Lightning-AI/pytorch-lightning

Loading from a checkpoint does not work properly in distributed training

Open
#19,738 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug checkpointing distributed
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Bug description

I train my model on multiple GPUs and save it with the `checkpoint callback` and `save_hyperparameters()`.
I get a directory which looks like this, so this part seems to work flawlessly:
```
/epoch=5
--/checkpoint
----mp_rank_00_model_states.pt
----zero_pp_rank_0_mp_rank_00_optim_states.pt
----zero_pp_rank_1_mp_rank_00_optim_states.pt
...
```

When I try to load the checkpoint with `MyModel.load_from_checkpoint()` on any of these files I only get errors. The tutorials only point me to some apparently outdated examples with a .ckpt file, which does not exist in these log directories.

Loading from the model directory does not help either.

When I load mp_rank_00_model_states.pt I get:

```
File "/.../projects/classifier_lightning/venv/lib/python3.10/site-packages/lightning/pytorch/core/saving.py", line 180, in _load_state
keys = obj.load_state_dict(checkpoint["state_dict"], strict=strict)
KeyError: 'state_dict'
```

When i load the other other files I get a lightning version error, even though it runs on the same environment.

So - how do I load my model from a checkpoint?

### What version are you seeing the problem on?

v2.2

### 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 @justusschock @lantiga

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 MyModel.load_from_checkpoint() and the checkpoint-loading path in lightning/pytorch/core/saving.py, especially the _load_state failure at line 180. Compare the checkpoint callback output files under checkpoint with the expected .ckpt structure, and determine the documented or supported loading workflow for distributed-training checkpoints.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.