|
| 1 | +# Copyright The Lightning AI team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +from lightning.pytorch import Trainer |
| 18 | +from lightning.pytorch.callbacks import ModelSummary, ProgressBar, RichModelSummary, RichProgressBar, TQDMProgressBar |
| 19 | + |
| 20 | + |
| 21 | +class TestRichIntegration: |
| 22 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 23 | + def test_no_rich_defaults_tqdm_and_model_summary(self, tmp_path): |
| 24 | + trainer = Trainer(default_root_dir=tmp_path, logger=False, enable_checkpointing=False) |
| 25 | + assert any(isinstance(cb, TQDMProgressBar) for cb in trainer.callbacks) |
| 26 | + assert any(isinstance(cb, ModelSummary) for cb in trainer.callbacks) |
| 27 | + assert not any(isinstance(cb, RichProgressBar) for cb in trainer.callbacks) |
| 28 | + assert not any(isinstance(cb, RichModelSummary) for cb in trainer.callbacks) |
| 29 | + |
| 30 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 31 | + def test_no_rich_respects_user_provided_tqdm_progress_bar(self, tmp_path): |
| 32 | + user_progress_bar = TQDMProgressBar() |
| 33 | + trainer = Trainer( |
| 34 | + default_root_dir=tmp_path, callbacks=[user_progress_bar], logger=False, enable_checkpointing=False |
| 35 | + ) |
| 36 | + assert user_progress_bar in trainer.callbacks |
| 37 | + assert sum(isinstance(cb, ProgressBar) for cb in trainer.callbacks) == 1 |
| 38 | + |
| 39 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 40 | + def test_no_rich_respects_user_provided_rich_progress_bar(self, tmp_path): |
| 41 | + # If user explicitly provides RichProgressBar, it should be used, |
| 42 | + # even if _RICH_AVAILABLE is False (simulating our connector logic). |
| 43 | + # RequirementCache would normally prevent RichProgressBar instantiation if rich is truly not installed. |
| 44 | + user_progress_bar = RichProgressBar() |
| 45 | + trainer = Trainer( |
| 46 | + default_root_dir=tmp_path, callbacks=[user_progress_bar], logger=False, enable_checkpointing=False |
| 47 | + ) |
| 48 | + assert user_progress_bar in trainer.callbacks |
| 49 | + assert sum(isinstance(cb, ProgressBar) for cb in trainer.callbacks) == 1 |
| 50 | + assert isinstance(trainer.progress_bar_callback, RichProgressBar) |
| 51 | + |
| 52 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 53 | + def test_no_rich_respects_user_provided_model_summary(self, tmp_path): |
| 54 | + user_model_summary = ModelSummary() |
| 55 | + trainer = Trainer( |
| 56 | + default_root_dir=tmp_path, callbacks=[user_model_summary], logger=False, enable_checkpointing=False |
| 57 | + ) |
| 58 | + assert user_model_summary in trainer.callbacks |
| 59 | + assert sum(isinstance(cb, ModelSummary) for cb in trainer.callbacks) == 1 |
| 60 | + # Check that the specific instance is the one from the trainer's list of ModelSummary callbacks |
| 61 | + assert trainer.callbacks[trainer.callbacks.index(user_model_summary)] == user_model_summary |
| 62 | + |
| 63 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 64 | + def test_no_rich_respects_user_provided_rich_model_summary(self, tmp_path): |
| 65 | + user_model_summary = RichModelSummary() |
| 66 | + trainer = Trainer( |
| 67 | + default_root_dir=tmp_path, callbacks=[user_model_summary], logger=False, enable_checkpointing=False |
| 68 | + ) |
| 69 | + assert user_model_summary in trainer.callbacks |
| 70 | + assert sum(isinstance(cb, ModelSummary) for cb in trainer.callbacks) == 1 |
| 71 | + # Check that the specific instance is the one from the trainer's list of ModelSummary callbacks |
| 72 | + model_summary_callbacks = [cb for cb in trainer.callbacks if isinstance(cb, ModelSummary)] |
| 73 | + assert user_model_summary in model_summary_callbacks |
| 74 | + assert isinstance(model_summary_callbacks[0], RichModelSummary) |
| 75 | + |
| 76 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", True) |
| 77 | + def test_rich_available_defaults_rich_progress_and_summary(self, tmp_path): |
| 78 | + trainer = Trainer(default_root_dir=tmp_path, logger=False, enable_checkpointing=False) |
| 79 | + assert any(isinstance(cb, RichProgressBar) for cb in trainer.callbacks) |
| 80 | + assert any(isinstance(cb, RichModelSummary) for cb in trainer.callbacks) |
| 81 | + assert not any(isinstance(cb, TQDMProgressBar) for cb in trainer.callbacks) |
| 82 | + # Ensure the only ModelSummary is the RichModelSummary |
| 83 | + model_summaries = [cb for cb in trainer.callbacks if isinstance(cb, ModelSummary)] |
| 84 | + assert len(model_summaries) == 1 |
| 85 | + assert isinstance(model_summaries[0], RichModelSummary) |
| 86 | + |
| 87 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", True) |
| 88 | + def test_rich_available_respects_user_tqdm_progress_bar(self, tmp_path): |
| 89 | + user_progress_bar = TQDMProgressBar() |
| 90 | + trainer = Trainer( |
| 91 | + default_root_dir=tmp_path, callbacks=[user_progress_bar], logger=False, enable_checkpointing=False |
| 92 | + ) |
| 93 | + assert user_progress_bar in trainer.callbacks |
| 94 | + assert sum(isinstance(cb, ProgressBar) for cb in trainer.callbacks) == 1 |
| 95 | + assert isinstance(trainer.progress_bar_callback, TQDMProgressBar) |
| 96 | + |
| 97 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", True) |
| 98 | + def test_rich_available_respects_user_model_summary(self, tmp_path): |
| 99 | + user_model_summary = ModelSummary() # Non-rich |
| 100 | + trainer = Trainer( |
| 101 | + default_root_dir=tmp_path, callbacks=[user_model_summary], logger=False, enable_checkpointing=False |
| 102 | + ) |
| 103 | + assert user_model_summary in trainer.callbacks |
| 104 | + model_summaries = [cb for cb in trainer.callbacks if isinstance(cb, ModelSummary)] |
| 105 | + assert len(model_summaries) == 1 |
| 106 | + assert isinstance(model_summaries[0], ModelSummary) |
| 107 | + assert not isinstance(model_summaries[0], RichModelSummary) |
| 108 | + |
| 109 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 110 | + def test_progress_bar_disabled_no_rich(self, tmp_path): |
| 111 | + trainer = Trainer( |
| 112 | + default_root_dir=tmp_path, enable_progress_bar=False, logger=False, enable_checkpointing=False |
| 113 | + ) |
| 114 | + assert not any(isinstance(cb, ProgressBar) for cb in trainer.callbacks) |
| 115 | + |
| 116 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", True) |
| 117 | + def test_progress_bar_disabled_with_rich(self, tmp_path): |
| 118 | + trainer = Trainer( |
| 119 | + default_root_dir=tmp_path, enable_progress_bar=False, logger=False, enable_checkpointing=False |
| 120 | + ) |
| 121 | + assert not any(isinstance(cb, ProgressBar) for cb in trainer.callbacks) |
| 122 | + |
| 123 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", False) |
| 124 | + def test_model_summary_disabled_no_rich(self, tmp_path): |
| 125 | + trainer = Trainer( |
| 126 | + default_root_dir=tmp_path, enable_model_summary=False, logger=False, enable_checkpointing=False |
| 127 | + ) |
| 128 | + assert not any(isinstance(cb, ModelSummary) for cb in trainer.callbacks) |
| 129 | + |
| 130 | + @patch("lightning.pytorch.trainer.connectors.callback_connector._RICH_AVAILABLE", True) |
| 131 | + def test_model_summary_disabled_with_rich(self, tmp_path): |
| 132 | + trainer = Trainer( |
| 133 | + default_root_dir=tmp_path, enable_model_summary=False, logger=False, enable_checkpointing=False |
| 134 | + ) |
| 135 | + assert not any(isinstance(cb, ModelSummary) for cb in trainer.callbacks) |
0 commit comments