Skip to content

Commit 7e9cea4

Browse files
authored
Fix rich progress bar crashing on empty val dataloader sanity checking (#21108)
* fix sanity check breaking on len=0 val dataloader * add testing * changelog
1 parent a08b64e commit 7e9cea4

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/lightning/pytorch/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
3939

4040
- Fixed misalignment column while using rich model summary in `DeepSpeedstrategy` ([#21100](https://github.com/Lightning-AI/pytorch-lightning/pull/21100))
4141

42+
43+
- Fixed `RichProgressBar` crashing when sanity checking using val dataloader with 0 len ([#21108](https://github.com/Lightning-AI/pytorch-lightning/pull/21108))
44+
4245
---
4346

4447
## [2.5.3] - 2025-08-13

src/lightning/pytorch/callbacks/progress/rich_progress.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,7 @@ def on_sanity_check_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningM
390390

391391
@override
392392
def on_sanity_check_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
393-
if self.progress is not None:
394-
assert self.val_sanity_progress_bar_id is not None
393+
if self.progress is not None and self.val_sanity_progress_bar_id is not None:
395394
self.progress.update(self.val_sanity_progress_bar_id, advance=0, visible=False)
396395
self.refresh()
397396

tests/tests_pytorch/callbacks/progress/test_rich_progress_bar.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,31 @@ def test_rich_progress_bar_metrics_theme_update(*_):
577577
theme = RichProgressBar(theme=RichProgressBarTheme(metrics_format=".3e", metrics_text_delimiter="\n")).theme
578578
assert theme.metrics_format == ".3e"
579579
assert theme.metrics_text_delimiter == "\n"
580+
581+
582+
@RunIf(rich=True)
583+
def test_rich_progress_bar_empty_val_dataloader_model(tmp_path):
584+
"""Test that RichProgressBar doesn't crash with empty val_dataloader list from model."""
585+
586+
class EmptyListModel(BoringModel):
587+
def train_dataloader(self):
588+
return DataLoader(RandomDataset(32, 64), batch_size=2)
589+
590+
def val_dataloader(self):
591+
return []
592+
593+
model = EmptyListModel()
594+
progress_bar = RichProgressBar()
595+
596+
trainer = Trainer(
597+
default_root_dir=tmp_path,
598+
max_epochs=1,
599+
num_sanity_val_steps=1,
600+
callbacks=[progress_bar],
601+
limit_train_batches=2,
602+
enable_checkpointing=False,
603+
logger=False,
604+
)
605+
606+
# This should not raise an AssertionError
607+
trainer.fit(model)

0 commit comments

Comments
 (0)