Skip to content

Commit

Permalink
Merge branch 'release/1.2-dev' into feature/on_eval_test_end
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitgr7 committed Jan 13, 2021
2 parents 79d2ee4 + 7449ce2 commit f3d7a4e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
27 changes: 26 additions & 1 deletion pytorch_lightning/trainer/deprecated_api.py
Expand Up @@ -11,14 +11,15 @@
# 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 pytorch_lightning.trainer.states import RunningStage
from pytorch_lightning.utilities import DistributedType, DeviceType, rank_zero_warn


class DeprecatedDistDeviceAttributes:

_distrib_type: DistributedType
_device_type: DeviceType
_running_stage: RunningStage
num_gpus: int

@property
Expand Down Expand Up @@ -129,3 +130,27 @@ def use_single_gpu(self, val: bool) -> None:
)
if val:
self._device_type = DeviceType.GPU

@property
def training(self) -> bool:
# todo: consider rename as `is_training`
return self._running_stage == RunningStage.TRAINING

@training.setter
def training(self, val: bool) -> None:
if val:
self._running_stage = RunningStage.TRAINING
else:
self._running_stage = None

@property
def testing(self) -> bool:
# todo: consider rename as `is_testing`
return self._running_stage == RunningStage.TESTING

@testing.setter
def testing(self, val: bool) -> None:
if val:
self._running_stage = RunningStage.TESTING
else:
self._running_stage = None
17 changes: 15 additions & 2 deletions pytorch_lightning/trainer/states.py
Expand Up @@ -23,10 +23,10 @@ class TrainerState(LightningEnum):
""" State which is set in the :class:`~pytorch_lightning.trainer.trainer.Trainer`
to indicate what is currently or was executed.
>>> # you can math the type with string
>>> # you can compare the type with a string
>>> TrainerState.RUNNING == 'RUNNING'
True
>>> # which is case sensitive
>>> # which is case insensitive
>>> TrainerState.FINISHED == 'finished'
True
"""
Expand All @@ -36,6 +36,19 @@ class TrainerState(LightningEnum):
INTERRUPTED = 'INTERRUPTED'


class RunningStage(LightningEnum):
"""Type of train phase.
>>> # you can match the Enum with string
>>> RunningStage.TRAINING == 'train'
True
"""
TRAINING = 'train'
EVALUATING = 'eval'
TESTING = 'test'
TUNING = 'tune'


def trainer_state(*, entering: Optional[TrainerState] = None, exiting: Optional[TrainerState] = None) -> Callable:
""" Decorator for :class:`~pytorch_lightning.trainer.trainer.Trainer` methods
which changes state to `entering` before the function execution and `exiting`
Expand Down
1 change: 1 addition & 0 deletions pytorch_lightning/trainer/trainer.py
Expand Up @@ -294,6 +294,7 @@ def __init__(
super().__init__()
self._device_type = DeviceType.CPU
self._distrib_type = None
self._running_stage = None

# init connectors
self.dev_debugger = InternalDebugger(self)
Expand Down
18 changes: 17 additions & 1 deletion tests/deprecated_api/test_remove_1-4.py
Expand Up @@ -37,7 +37,7 @@ def test_v1_4_0_deprecated_imports():
from pytorch_lightning.utilities.xla_device_utils import XLADeviceUtils # noqa: F811 F401


def test_v1_4_0_deprecated_trainer_attributes():
def test_v1_4_0_deprecated_trainer_device_distrib():
"""Test that Trainer attributes works fine."""
trainer = Trainer()
trainer._distrib_type = None
Expand Down Expand Up @@ -84,6 +84,22 @@ def test_v1_4_0_deprecated_trainer_attributes():
assert trainer.use_horovod


def test_v1_4_0_deprecated_trainer_phase():
"""Test that Trainer attributes works fine."""
trainer = Trainer()

assert not trainer.training
assert not trainer.testing

trainer.training = True
assert trainer.training
assert not trainer.testing

trainer.testing = True
assert not trainer.training
assert trainer.testing


def test_v1_4_0_deprecated_metrics():
from pytorch_lightning.metrics.functional.classification import stat_scores_multiple_classes
with pytest.deprecated_call(match='will be removed in v1.4'):
Expand Down

0 comments on commit f3d7a4e

Please sign in to comment.