def test_lr_monitor_update_callback_metrics(tmp_path):
    """Test that the `LearningRateMonitor` callback updates trainer.callback_metrics."""

    class TestModel(BoringModel):
        def configure_optimizers(self):
            optimizer = torch.optim.SGD(self.layer.parameters(), lr=0.1)
            lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.5)
            return [optimizer], [lr_scheduler]

    monitor_key = "lr-SGD"
    stop_threshold = 0.02
    expected_stop_epoch = 3

    lr_monitor = LearningRateMonitor()
    lr_es = EarlyStopping(
        monitor=monitor_key, mode="min", stopping_threshold=stop_threshold, check_on_train_epoch_end=True
    )
    trainer = Trainer(
        default_root_dir=tmp_path,
        callbacks=[lr_monitor, lr_es],
        max_epochs=5,
        limit_val_batches=0,
        limit_train_batches=2,
        logger=CSVLogger(tmp_path),
    )
    model = TestModel()
    trainer.fit(model)

    assert monitor_key in trainer.callback_metrics
    assert lr_monitor.lrs[monitor_key] == [0.1, 0.05, 0.025, 0.0125]
    assert min(lr_monitor.lrs[monitor_key][:expected_stop_epoch]) > stop_threshold
    assert len(lr_monitor.lrs[monitor_key][expected_stop_epoch:]) == 1
    assert min(lr_monitor.lrs[monitor_key][expected_stop_epoch:]) < stop_threshold
    assert trainer.current_epoch - 1 == expected_stop_epoch
    assert lr_es.stopped_epoch == expected_stop_epoch
Test that the `LearningRateMonitor` callback updates trainer.callback_metrics.
tests/tests_pytorch/callbacks/test_lr_monitor.py