-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
set_epoch
not getting called for prediction dataloaders (#16785)
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright The Lightning AI team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from unittest import mock | ||
from unittest.mock import call | ||
|
||
from pytorch_lightning import Trainer | ||
from pytorch_lightning.demos.boring_classes import BoringModel | ||
|
||
|
||
def test_prediction_loop_batch_sampler_set_epoch_called(tmp_path): | ||
"""Tests that set_epoch is called on the dataloader's batch sampler (if any) during prediction.""" | ||
model = BoringModel() | ||
trainer = Trainer( | ||
default_root_dir=tmp_path, | ||
limit_predict_batches=1, | ||
enable_model_summary=False, | ||
enable_checkpointing=False, | ||
logger=False, | ||
) | ||
trainer.fit_loop.epoch_progress.current.processed = 2 | ||
|
||
with mock.patch("pytorch_lightning.overrides.distributed.IndexBatchSamplerWrapper.set_epoch") as set_epoch_mock: | ||
trainer.predict(model) | ||
assert set_epoch_mock.mock_calls == [call(2)] |