Skip to content

Fix refresh rate for metrics in RichProgressBar #21032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- fix progress bar console clearing for Rich `14.1+` ([#21016](https://github.com/Lightning-AI/pytorch-lightning/pull/21016))


- Fixed metrics in `RichProgressBar` being updated according to user provided `refresh_rate` ([#21032](https://github.com/Lightning-AI/pytorch-lightning/pull/21032))

---

## [2.5.2] - 2025-06-20
Expand Down
20 changes: 17 additions & 3 deletions src/lightning/pytorch/callbacks/progress/rich_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,12 @@ def on_train_batch_end(
# can happen when resuming from a mid-epoch restart
self._initialize_train_progress_bar_id()
self._update(self.train_progress_bar_id, batch_idx + 1)
self._update_metrics(trainer, pl_module)
self._update_metrics(trainer, pl_module, batch_idx + 1)
self.refresh()

@override
def on_train_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
self._update_metrics(trainer, pl_module)
self._update_metrics(trainer, pl_module, total_batches=True)

@override
def on_validation_batch_end(
Expand Down Expand Up @@ -632,7 +632,21 @@ def _reset_progress_bar_ids(self) -> None:
self.test_progress_bar_id = None
self.predict_progress_bar_id = None

def _update_metrics(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
def _update_metrics(
self,
trainer: "pl.Trainer",
pl_module: "pl.LightningModule",
current: Optional[int] = None,
total_batches: bool = False,
) -> None:
if not self.is_enabled or self._metric_component is None:
return

if current is not None and not total_batches:
total = self.total_train_batches
if not self._should_update(current, total):
return

metrics = self.get_metrics(trainer, pl_module)
if self._metric_component:
self._metric_component.update(metrics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def test_rich_progress_bar_with_refresh_rate(tmp_path, refresh_rate, train_batch
with mock.patch.object(
trainer.progress_bar_callback.progress, "update", wraps=trainer.progress_bar_callback.progress.update
) as progress_update:
metrics_update = mock.MagicMock()
trainer.progress_bar_callback._update_metrics = metrics_update

trainer.fit(model)
assert progress_update.call_count == expected_call_count

Expand All @@ -260,6 +263,9 @@ def test_rich_progress_bar_with_refresh_rate(tmp_path, refresh_rate, train_batch
assert fit_val_bar.total == val_batches
assert not fit_val_bar.visible

# one call for each train batch + one at the end of training epoch + one for validation end
assert metrics_update.call_count == train_batches + (1 if train_batches > 0 else 0) + (1 if val_batches > 0 else 0)


@RunIf(rich=True)
@pytest.mark.parametrize("limit_val_batches", [1, 5])
Expand Down
Loading