Lightning-AI / Lightning-AI/pytorch-lightning
ModelCheckpoint saves multiple checkpoints when trainer is using DDP
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
When trainnig a model with DDP strategy and using ModelCheckpoint, the trainer is not saving only one checkpoint.

### What version are you seeing the problem on?
v2.0
### How to reproduce the bug
```python
pl_model=...
ckpt_path=...
monitor = "val_loss"
ckpt_filename = 'epoch{epoch}-val_loss{val_loss:.3f}'
latest_ckpt_filename = 'latest-epoch{epoch}-val_loss{val_loss:.3f}'
save_on_train_epoch_end = False
checkpoint_callback = ModelCheckpoint(dirpath=ckpt_path,
monitor=monitor,
mode="min",
filename=ckpt_filename,
auto_insert_metric_name=False,
save_top_k = 5,
save_weights_only=False,
save_on_train_epoch_end=save_on_train_epoch_end
)
latest_checkpoint_callback = ModelCheckpoint(
monitor='epoch',
mode="max",
dirpath=ckpt_path,
filename=latest_ckpt_filename,
auto_insert_metric_name=False,
save_top_k = 1,
save_weights_only=False,
save_on_train_epoch_end=save_on_train_epoch_end,
every_n_epochs=1
)
callbacks=[checkpoint_callback,latest_checkpoint_callback]
trainer = pl.Trainer( callbacks = callbacks,
strategy = "auto",
)
trainer.fit(pl_model)
```
### Error messages and logs
```
# Error messages and logs here please
```
There is no error message
### Environment
Current environment
```
#- Lightning Component : Trainer and ModelCheckpoint
#- PyTorch Lightning Version : 2.0.9
#- PyTorch Version : 2.0.0
#- Python version : 3.10
#- CUDA/cuDNN version: NCCL version 2.16.2+cuda11.8
#- GPU models and configuration : AWS ml.p3.8xlarge : 4 NVIDIA Tesla V100 GPUs, NVLink
#- How you installed Lightning : pip
```
### More info
_No response_
cc @carmocca @awaelchli @justusschock
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 with the ModelCheckpoint callbacks and Trainer configured with the DDP strategy in the reproduction snippet. Run the example under Lightning 2.0.9 with two checkpoint callbacks and inspect the files written during validation. Done means DDP produces only the intended checkpoints rather than multiple copies, with coverage for the reported configuration.
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
- Mostly clear
- Newbie friendliness
- 35/100