Lightning-AI / Lightning-AI/pytorch-lightning

MLFlowLogger saves copies of ModelCheckpoints

Open
#17,904 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Bug description

I am trying to achieve the following behavior:
1. `ModelCheckpoint` callbacks save model checkpoint files to a certain location
2. `MLFlowLogger` (with `log_model=True`) only references the saved checkpoints

The problem is that no matter what I do, `MLFlowLogger` tries to save copies of the checkpoints in a new location.

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

v2.0

### How to reproduce the bug

```python
from lightning.pytorch import Trainer
from lightning.pytorch.callbacks import (
RichProgressBar, EarlyStopping, ModelCheckpoint
)

def get_lightning_mlflow_logger(experiment_name: str, _artifact_location: str) -> pl_loggers.MLFlowLogger:
return pl_loggers.MLFlowLogger(
experiment_name=experiment_name,
run_name=datetime.now().isoformat(),
tracking_uri=os.path.join(EXPERIMENT_LOGS_DIR, './mlruns'),
log_model=True,
artifact_location=_artifact_location
)

def _configure_callbacks():
early_stopping = EarlyStopping(
monitor="val_loss",
mode='min',
patience=10,
stopping_threshold=0.05,
divergence_threshold=5.0
)

checkpoint_callback = ModelCheckpoint(
save_top_k=2,
save_last=True,
monitor="val_loss",
mode="min",
verbose=True
)
checkpoint_callback.CHECKPOINT_JOIN_CHAR = '_'

return (
[
early_stopping,
checkpoint_callback,
RichProgressBar()
],
checkpoint_callback.dirpath
)

def cli_main():
model = MNISTClassifier()
data_module = MNISTDataModule()

callbacks, checkpoints_dirpath = _configure_callbacks()
print(f'ModelCheckpoint Callback dirpath: {checkpoints_dirpath}')
mlflow_logger = get_lightning_mlflow_logger(EXPERIMENT_NAME, checkpoints_dirpath)
trainer = Trainer(
callbacks=callbacks,
logger=mlflow_logger,
max_epochs=5
)
print(f'ModelCheckpoint Callback dirpath: {checkpoints_dirpath}')

trainer.fit(model, datamodule=data_module)
trainer.test(model=model, datamodule=data_module)

if __name__ == "__main__":
cli_main()
```

### Error messages and logs

The above code saves model checkpoints in the `tracking_uri` location of the `MLFlowLogger` even though checkpoints already exist in the directory from which I ran the script (which is where the `ModelCheckpoint` callbacks are saving it by default.

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

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 MLFlowLogger and ModelCheckpoint entry points and reproduce the provided v2.0 example with log_model=True. The issue is resolved when MLFlowLogger references the checkpoints already written by ModelCheckpoint instead of saving duplicate copies in the tracking location.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.