Skip to content

Commit f150953

Browse files
awaelchlicarmoccajustusschock
authored
Error messages for unsupported Trainer attributes (#15059)
Co-authored-by: Carlos Mocholí <[email protected]> Co-authored-by: Justus Schock <[email protected]>
1 parent c5cc2b0 commit f150953

File tree

5 files changed

+297
-0
lines changed

5 files changed

+297
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1+
# Copyright The PyTorch Lightning 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+
import pytorch_lightning._graveyard.trainer
116
import pytorch_lightning._graveyard.training_type # noqa: F401
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Copyright The PyTorch Lightning 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 typing import Any, Optional
16+
17+
from pytorch_lightning import Trainer
18+
19+
20+
def _gpus(_: Trainer) -> None:
21+
# Remove in v2.0.0
22+
raise AttributeError(
23+
"`Trainer.gpus` was deprecated in v1.6 and is no longer accessible as of v1.8."
24+
" Please use `Trainer.num_devices` or `Trainer.device_ids` to get device information instead."
25+
)
26+
27+
28+
def _root_gpu(_: Trainer) -> None:
29+
# Remove in v2.0.0
30+
raise AttributeError(
31+
"`Trainer.root_gpu` was deprecated in v1.6 and is no longer accessible as of v1.8."
32+
" Please use `Trainer.strategy.root_device.index` instead."
33+
)
34+
35+
36+
def _tpu_cores(_: Trainer) -> None:
37+
# Remove in v2.0.0
38+
raise AttributeError(
39+
"`Trainer.tpu_cores` was deprecated in v1.6 and is no longer accessible as of v1.8."
40+
" Please use `Trainer.num_devices` instead."
41+
)
42+
43+
44+
def _ipus(_: Trainer) -> None:
45+
# Remove in v2.0.0
46+
raise AttributeError(
47+
"`Trainer.ipus` was deprecated in v1.6 and is no longer accessible as of v1.8."
48+
" Please use `Trainer.num_devices` instead."
49+
)
50+
51+
52+
def _num_gpus(_: Trainer) -> None:
53+
# Remove in v2.0.0
54+
raise AttributeError(
55+
"`Trainer.num_gpus` was deprecated in v1.6 and is no longer accessible as of v1.8."
56+
" Please use `Trainer.num_devices` instead."
57+
)
58+
59+
60+
def _devices(_: Trainer) -> None:
61+
# Remove in v2.0.0
62+
raise AttributeError(
63+
"`Trainer.devices` was deprecated in v1.6 and is no longer accessible as of v1.8."
64+
" Please use `Trainer.num_devices` or `Trainer.device_ids` to get device information instead."
65+
)
66+
67+
68+
def _use_amp(_: Trainer) -> None:
69+
# Remove in v2.0.0
70+
raise AttributeError(
71+
"`Trainer.use_amp` was deprecated in v1.6 and is no longer accessible as of v1.8."
72+
" Please use `Trainer.amp_backend`.",
73+
)
74+
75+
76+
def _weights_save_path(_: Trainer) -> None:
77+
# Remove in v2.0.0
78+
raise AttributeError("`Trainer.weights_save_path` was deprecated in v1.6 and is no longer accessible as of v1.8.")
79+
80+
81+
def _lightning_optimizers(_: Trainer) -> None:
82+
# Remove in v2.0.0
83+
raise AttributeError(
84+
"`Trainer.lightning_optimizers` was deprecated in v1.6 and is no longer accessible as of v1.8."
85+
)
86+
87+
88+
def _should_rank_save_checkpoint(_: Trainer) -> None:
89+
# Remove in v2.0.0
90+
raise AttributeError(
91+
"`Trainer.should_rank_save_checkpoint` was deprecated in v1.6 and is no longer accessible as of v1.8.",
92+
)
93+
94+
95+
def _validated_ckpt_path(_: Trainer) -> None:
96+
# Remove in v2.0.0
97+
raise AttributeError(
98+
"The `Trainer.validated_ckpt_path` was deprecated in v1.6 and is no longer accessible as of v1.8."
99+
" Please use `Trainer.ckpt_path` instead."
100+
)
101+
102+
103+
def _validated_ckpt_path_setter(_: Trainer, __: Optional[str]) -> None:
104+
# Remove in v2.0.0
105+
raise AttributeError(
106+
"The `Trainer.validated_ckpt_path` was deprecated in v1.6 and is no longer accessible as of v1.8."
107+
" Please use `Trainer.ckpt_path` instead."
108+
)
109+
110+
111+
def _tested_ckpt_path(_: Trainer) -> None:
112+
# Remove in v2.0.0
113+
raise AttributeError(
114+
"The `Trainer.tested_ckpt_path` was deprecated in v1.6 and is no longer accessible as of v1.8."
115+
" Please use `Trainer.ckpt_path` instead."
116+
)
117+
118+
119+
def _tested_ckpt_path_setter(_: Trainer, __: Optional[str]) -> None:
120+
# Remove in v2.0.0
121+
raise AttributeError(
122+
"The `Trainer.tested_ckpt_path` was deprecated in v1.6 and is no longer accessible as of v1.8."
123+
" Please use `Trainer.ckpt_path` instead."
124+
)
125+
126+
127+
def _predicted_ckpt_path(_: Trainer) -> None:
128+
# Remove in v2.0.0
129+
raise AttributeError(
130+
"The `Trainer.predicted_ckpt_path` was deprecated in v1.6 and is no longer accessible as of v1.8."
131+
" Please use `Trainer.ckpt_path` instead."
132+
)
133+
134+
135+
def _predicted_ckpt_path_setter(_: Trainer, __: Optional[str]) -> None:
136+
# Remove in v2.0.0
137+
raise AttributeError(
138+
"The `Trainer.predicted_ckpt_path` was deprecated in v1.6 and is no longer accessible as of v1.8."
139+
" Please use `Trainer.ckpt_path` instead."
140+
)
141+
142+
143+
def _verbose_evaluate(_: Trainer) -> None:
144+
# Remove in v2.0.0
145+
raise AttributeError(
146+
"The `Trainer.verbose_evaluate` was deprecated in v1.6 and is no longer accessible as of v1.8."
147+
" Please use `trainer.{validate,test}_loop.verbose` instead.",
148+
)
149+
150+
151+
def _verbose_evaluate_setter(_: Trainer, __: bool) -> None:
152+
# Remove in v2.0.0
153+
raise AttributeError(
154+
"The `Trainer.verbose_evaluate` was deprecated in v1.6 and is no longer accessible as of v1.8."
155+
" Please use `trainer.{validate,test}_loop.verbose` instead.",
156+
)
157+
158+
159+
def _run_stage(_: Trainer) -> None:
160+
# Remove in v2.0.0
161+
raise NotImplementedError(
162+
"`Trainer.run_stage` was deprecated in v1.6 and is no longer supported as of v1.8."
163+
" Please use `Trainer.{fit,validate,test,predict}` instead."
164+
)
165+
166+
167+
def _call_hook(_: Trainer, *__: Any, **___: Any) -> Any:
168+
# Remove in v2.0.0
169+
raise NotImplementedError("`Trainer.call_hook` was deprecated in v1.6 and is no longer supported as of v1.8.")
170+
171+
172+
# Properties/Attributes
173+
Trainer.gpus = property(_gpus)
174+
Trainer.root_gpu = property(_root_gpu)
175+
Trainer.tpu_cores = property(_tpu_cores)
176+
Trainer.ipus = property(_ipus)
177+
Trainer.num_gpus = property(_num_gpus)
178+
Trainer.devices = property(_devices)
179+
Trainer.use_amp = property(_use_amp)
180+
Trainer.weights_save_path = property(_weights_save_path)
181+
Trainer.lightning_optimizers = property(_lightning_optimizers)
182+
Trainer.should_rank_save_checkpoint = property(_should_rank_save_checkpoint)
183+
Trainer.validated_ckpt_path = property(fget=_validated_ckpt_path, fset=_validated_ckpt_path_setter)
184+
Trainer.tested_ckpt_path = property(fget=_tested_ckpt_path, fset=_tested_ckpt_path_setter)
185+
Trainer.predicted_ckpt_path = property(fget=_predicted_ckpt_path, fset=_predicted_ckpt_path_setter)
186+
Trainer.verbose_evaluate = property(fget=_verbose_evaluate, fset=_verbose_evaluate_setter)
187+
188+
189+
# Methods
190+
Trainer.run_stage = _run_stage
191+
Trainer.call_hook = _call_hook

src/pytorch_lightning/_graveyard/training_type.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright The PyTorch Lightning 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+
115
import sys
216
from typing import Any
317

tests/tests_pytorch/graveyard/__init__.py

Whitespace-only changes.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright The PyTorch Lightning 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+
import pytest
16+
17+
from pytorch_lightning import Trainer
18+
19+
20+
@pytest.mark.parametrize(
21+
"attribute",
22+
[
23+
"gpus",
24+
"num_gpus",
25+
"root_gpu",
26+
"devices",
27+
"tpu_cores",
28+
"ipus",
29+
"use_amp",
30+
"weights_save_path",
31+
"lightning_optimizers",
32+
"should_rank_save_checkpoint",
33+
"validated_ckpt_path",
34+
"tested_ckpt_path",
35+
"predicted_ckpt_path",
36+
"verbose_evaluate",
37+
],
38+
)
39+
def test_v2_0_0_unsupported_getters(attribute):
40+
trainer = Trainer()
41+
with pytest.raises(
42+
AttributeError, match=f"`Trainer.{attribute}` was deprecated in v1.6 and is no longer accessible as of v1.8."
43+
):
44+
getattr(trainer, attribute)
45+
46+
47+
@pytest.mark.parametrize(
48+
"attribute",
49+
[
50+
"validated_ckpt_path",
51+
"tested_ckpt_path",
52+
"predicted_ckpt_path",
53+
"verbose_evaluate",
54+
],
55+
)
56+
def test_v2_0_0_unsupported_setters(attribute):
57+
trainer = Trainer()
58+
with pytest.raises(
59+
AttributeError, match=f"`Trainer.{attribute}` was deprecated in v1.6 and is no longer accessible as of v1.8."
60+
):
61+
setattr(trainer, attribute, None)
62+
63+
64+
def test_v2_0_0_unsupported_run_stage():
65+
trainer = Trainer()
66+
with pytest.raises(
67+
NotImplementedError, match="`Trainer.run_stage` was deprecated in v1.6 and is no longer supported as of v1.8."
68+
):
69+
trainer.run_stage()
70+
71+
72+
def test_v2_0_0_unsupported_call_hook():
73+
trainer = Trainer()
74+
with pytest.raises(
75+
NotImplementedError, match="`Trainer.call_hook` was deprecated in v1.6 and is no longer supported as of v1.8."
76+
):
77+
trainer.call_hook("test_hook")

0 commit comments

Comments
 (0)