Lightning-AI / Lightning-AI/pytorch-lightning
Introduce `Logger.experiment_dir`
@tshu-w is already working on this.
Since Oct 31, 2022.
- Dominant language
- Python
- Stars
- 31.4k
- Forks
- 3.8k
- Avg merge
- 6d 7h
- Merged PRs (30d)
- 6
Description
## 🚀 Feature
Introduce a property/method `experiment_dir` on the abstract logger interface. All loggers must implement this and return a path to where their files are being saved.
### Motivation
The Trainer currently determines where checkpoints are saved like so:
```py
os.path.join(logger.save_dir, str(logger.name), logger.version, "checkpoints")
```
As you can see, it assumes that there is a rigid folder structure `save_dir/name/version`. Every logger has to follow this structure.
However, at least one logger (wandb) struggles with this, because it has an additional concept of "project" that does not fit well within this layout (https://github.com/Lightning-AI/lightning/issues/14054). Furthermore, the "name" may change AFTER the files were written, so there is a disconnect between the folder structure and what is represented "online" in the wandb ui.
One observation is that the trainer actually never cares about any of the subfolders. All it needs is the full path. This is why I believe, if we let the logger decide how to assemble the path, we will avoid many issues and disagreements with 3rd party experiment management.
We are struggling with making all loggers agree to a standardized layout, so maybe we shouldn't try to do that, at least not in every aspect of how the loggers work (https://github.com/Lightning-AI/lightning/issues/12028#issuecomment-1088325894).
### Pitch
Introduce
```py
def experiment_dir(self) -> str:
"""Returns the (relative) path to where logs and artifacts get saved for the current version."""
```
This should probably be a relative path, so that the Trainer can prepend a root dir if needed (default cwd).
An implementation of a logger could then look like this:
```py
def experiment_dir(self):
return os.path.join(self.save_dir, self.name, self.version)
```
And the checkpointing in the Trainer would save files to
```py
os.path.join(logger.experiment_dir(), "checkpoints")
```
for example.
### Implications
1. After introducing experiment dir, we could consider deprecating `save_dir` and `log_dir` or remove it from the base interface.
2. The code here in `Trainer.log_dir` would probably get simplified: https://github.com/Lightning-AI/lightning/blob/fe9e5d55bf7991ba36b76d6adae9075b93dfcaa0/src/pytorch_lightning/trainer/trainer.py#L2208-L2219
3. The code in the checkpoint callback would get simplified too: https://github.com/Lightning-AI/lightning/blob/fe9e5d55bf7991ba36b76d6adae9075b93dfcaa0/src/pytorch_lightning/callbacks/model_checkpoint.py#L595-L608
4. It would immediately address these issues, as the LightningCLI can write to the experiment dir without ambiguity: #14162, #12748
### Alternatives
Struggle
### Additional context
Discussions in:
https://github.com/Lightning-AI/lightning/issues/14054
Current blockers:
#12177
______________________________________________________________________
#### If you enjoy Lightning, check out our other projects! ⚡
- [**Metrics**](https://github.com/Lightning-AI/metrics): Machine learning metrics for distributed, scalable PyTorch applications.
- [**Lite**](https://pytorch-lightning.readthedocs.io/en/latest/starter/lightning_lite.html): enables pure PyTorch users to scale their existing code on any kind of device while retaining full control over their own loops and optimization logic.
- [**Flash**](https://github.com/Lightning-AI/lightning-flash): The fastest way to get a Lightning baseline! A collection of tasks for fast prototyping, baselining, fine-tuning, and solving problems with deep learning.
- [**Bolts**](https://github.com/Lightning-AI/lightning-bolts): Pretrained SOTA Deep Learning models, callbacks, and more for research and production with PyTorch Lightning and PyTorch.
- [**Lightning Transformers**](https://github.com/Lightning-AI/lightning-transformers): Flexible interface for high-performance research using SOTA Transformers leveraging PyTorch Lightning, Transformers, and Hydra.
cc @borda @tchaton @justusschock @awaelchli @edward-io @ananthsub @rohitgr7 @kamil-kaczmarek @Raalsky @Blaizzy
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.
Assessment
This issue has not been assessed yet.