|
| 1 | +import re |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from litmodels.integrations.imports import _LIGHTNING_AVAILABLE, _PYTORCHLIGHTNING_AVAILABLE |
| 5 | +from litmodels.integrations.lightning_checkpoint import LitModelCheckpoint |
| 6 | + |
| 7 | +if _LIGHTNING_AVAILABLE: |
| 8 | + from lightning import Trainer |
| 9 | + from lightning.pytorch.demos.boring_classes import BoringModel |
| 10 | +elif _PYTORCHLIGHTNING_AVAILABLE: |
| 11 | + from pytorch_lightning import Trainer |
| 12 | + from pytorch_lightning.demos.boring_classes import BoringModel |
| 13 | + |
| 14 | + |
| 15 | +@mock.patch("litmodels.io.cloud.sdk_upload_model") |
| 16 | +def test_lightning_checkpoint_callback(mock_upload_model, tmp_path): |
| 17 | + mock_upload_model.return_value.name = "org-name/teamspace/model-name" |
| 18 | + |
| 19 | + trainer = Trainer( |
| 20 | + max_epochs=2, |
| 21 | + callbacks=LitModelCheckpoint(model_name="org-name/teamspace/model-name"), |
| 22 | + ) |
| 23 | + trainer.fit(BoringModel()) |
| 24 | + |
| 25 | + # expected_path = model_path % str(tmpdir) if "%" in model_path else model_path |
| 26 | + assert mock_upload_model.call_count == 2 |
| 27 | + assert mock_upload_model.call_args_list == [ |
| 28 | + mock.call(name="org-name/teamspace/model-name", path=mock.ANY, progress_bar=True, cloud_account=None), |
| 29 | + mock.call(name="org-name/teamspace/model-name", path=mock.ANY, progress_bar=True, cloud_account=None), |
| 30 | + ] |
| 31 | + |
| 32 | + # Verify paths match the expected pattern |
| 33 | + for call_args in mock_upload_model.call_args_list: |
| 34 | + path = call_args[1]["path"] |
| 35 | + assert re.match(r".*[/\\]lightning_logs[/\\]version_\d+[/\\]checkpoints[/\\]epoch=\d+-step=\d+\.ckpt$", path) |
0 commit comments