Lightning-AI / Lightning-AI/pytorch-lightning

ModelCheckpoint saves multiple checkpoints when trainer is using DDP

Open
#18,590 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug callback: model checkpoint strategy: ddp ver: 2.0.x
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.
![image](https://github.com/Lightning-AI/lightning/assets/47348327/2a484770-a99e-4f0e-a628-76c267506587)

### 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.