Lightning-AI / Lightning-AI/pytorch-lightning

LearningRateMonitor may not require loggers

Open
#17,159 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

callback: lr monitor feature
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

### Description & Motivation

I was lately been working with lightning and mlflow autolog feature (I couldn't use lightning's mlflow logger since I mlflow context manager does more stuff and allows to log outside loops) and wanted to log metrics from the `LearningRateMonitor`. Unfortunately, since I do not attach any loggers (I do not want default Tensorboard) it is impossible to use this callback due to assert in the code. My question is why can't we use `pl_module.log_dict(...)` from the callback instead of `logger.log_metrics`? With `pl_module.log` you can specify whether you want the metric to land in loggers or not. For me, it is more general. Although I am not sure if proper step value will be used.

### Pitch

I want the `LearningRateMonitor` to use `pl_module.log_dict(...)` in `on_train_batch_start` and `on_train_epoch_start` which will not require the presence of loggers and external tools like `mlflow.pytorch.autolog` will be able to log metrics.

```diff
- for logger in trainer.loggers:
- logger.log_metrics(latest_stat, step=trainer.fit_loop.epoch_loop._batches_that_stepped)
+ pl_module.log_dict(latest_stat, on_step=True, on_epoch=False)
```
A private attribute is necessary in order to correctly log this metric.

```diff
- if not trainer.loggers:
- raise MisconfigurationException(
- "Cannot use `LearningRateMonitor` callback with `Trainer` that has no logger."
- )
```

### Alternatives

_No response_

### Additional context

_No response_

cc @borda

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

Read the LearningRateMonitor paths at on_train_batch_start and on_train_epoch_start, including the logger assertion and current step handling. Check how pl_module.log_dict behaves without an attached logger and whether the metric step remains correct. Done means the callback works without loggers while its metrics remain available to external logging contexts.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.