2022-10-12 12:22:01 +00:00
|
|
|
# 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.
|
2023-02-10 15:31:21 +00:00
|
|
|
from unittest import mock
|
|
|
|
from unittest.mock import Mock
|
2022-10-12 12:22:01 +00:00
|
|
|
|
2023-02-10 15:31:21 +00:00
|
|
|
import pytest
|
2022-10-12 12:22:01 +00:00
|
|
|
import torch
|
2023-03-29 19:43:28 +00:00
|
|
|
from lightning.fabric.utilities.imports import _TORCH_EQUAL_2_0
|
2023-02-02 10:06:45 +00:00
|
|
|
from lightning.pytorch import Trainer
|
|
|
|
from lightning.pytorch.demos.boring_classes import BoringModel
|
2023-02-10 15:31:21 +00:00
|
|
|
from lightning.pytorch.loops import _Loop
|
|
|
|
from lightning.pytorch.loops.utilities import _no_grad_context
|
2022-10-12 12:22:01 +00:00
|
|
|
|
|
|
|
|
2023-05-05 06:25:15 +00:00
|
|
|
@pytest.mark.parametrize("trainer_fn", ["validate", "test", "predict"])
|
2023-02-10 15:31:21 +00:00
|
|
|
def test_eval_inference_mode(tmp_path, trainer_fn):
|
2022-10-12 12:22:01 +00:00
|
|
|
class BoringModelNoGrad(BoringModel):
|
2023-02-10 15:31:21 +00:00
|
|
|
def assert_not_enabled(self):
|
2022-10-12 12:22:01 +00:00
|
|
|
assert not torch.is_grad_enabled()
|
|
|
|
assert not torch.is_inference_mode_enabled()
|
2023-02-10 15:31:21 +00:00
|
|
|
|
|
|
|
on_test_start = assert_not_enabled
|
|
|
|
on_validation_start = assert_not_enabled
|
|
|
|
on_predict_start = assert_not_enabled
|
2022-10-12 12:22:01 +00:00
|
|
|
|
|
|
|
class BoringModelForInferenceMode(BoringModel):
|
2023-02-10 15:31:21 +00:00
|
|
|
def assert_enabled(self):
|
2022-10-12 12:22:01 +00:00
|
|
|
assert not torch.is_grad_enabled()
|
|
|
|
assert torch.is_inference_mode_enabled()
|
|
|
|
|
2023-02-10 15:31:21 +00:00
|
|
|
on_test_start = assert_enabled
|
|
|
|
on_validation_start = assert_enabled
|
|
|
|
on_predict_start = assert_enabled
|
|
|
|
|
|
|
|
trainer = Trainer(default_root_dir=tmp_path, logger=False, inference_mode=False, fast_dev_run=True)
|
|
|
|
getattr(trainer, trainer_fn)(BoringModelNoGrad())
|
2022-10-12 12:22:01 +00:00
|
|
|
trainer = Trainer(logger=False, inference_mode=True, fast_dev_run=True)
|
2023-02-10 15:31:21 +00:00
|
|
|
getattr(trainer, trainer_fn)(BoringModelForInferenceMode())
|
|
|
|
|
|
|
|
|
|
|
|
def test_no_grad_context():
|
2023-02-16 02:40:34 +00:00
|
|
|
trainer = Mock()
|
|
|
|
|
2023-02-10 15:31:21 +00:00
|
|
|
class Foo:
|
|
|
|
@_no_grad_context
|
2024-02-15 18:39:17 +00:00
|
|
|
def run(self): ...
|
2023-02-10 15:31:21 +00:00
|
|
|
|
|
|
|
f = Foo()
|
|
|
|
with pytest.raises(TypeError, match="Foo` needs to be a Loop"):
|
|
|
|
f.run()
|
|
|
|
|
|
|
|
class Foo(_Loop):
|
|
|
|
@_no_grad_context
|
2024-02-15 18:39:17 +00:00
|
|
|
def run(self): ...
|
2023-02-10 15:31:21 +00:00
|
|
|
|
2023-02-16 02:40:34 +00:00
|
|
|
f = Foo(trainer)
|
2023-02-10 15:31:21 +00:00
|
|
|
with pytest.raises(TypeError, match="Foo.inference_mode` needs to be defined"):
|
|
|
|
f.run()
|
|
|
|
|
|
|
|
class Foo(_Loop):
|
|
|
|
def __init__(self):
|
2023-02-16 02:40:34 +00:00
|
|
|
super().__init__(trainer)
|
2023-02-10 15:31:21 +00:00
|
|
|
self.inference_mode = False
|
|
|
|
|
|
|
|
@_no_grad_context
|
2024-02-15 18:39:17 +00:00
|
|
|
def run(self): ...
|
2023-02-10 15:31:21 +00:00
|
|
|
|
|
|
|
f = Foo()
|
|
|
|
with mock.patch("torch.no_grad") as no_grad_mock:
|
|
|
|
f.run()
|
|
|
|
no_grad_mock.assert_called_once_with()
|
|
|
|
f.inference_mode = True
|
|
|
|
with mock.patch("torch.inference_mode") as inference_mode_mock:
|
|
|
|
f.run()
|
2023-03-29 19:43:28 +00:00
|
|
|
if not _TORCH_EQUAL_2_0:
|
|
|
|
inference_mode_mock.assert_called_once_with()
|