From 7f148b2c47813987f41b5b2b8a934acea3c1547d Mon Sep 17 00:00:00 2001 From: awaelchli Date: Sat, 3 Sep 2022 15:03:39 +0200 Subject: [PATCH] Deprecate pl/utilities/apply_func (#14516) --- src/pytorch_lightning/CHANGELOG.md | 4 + src/pytorch_lightning/utilities/apply_func.py | 82 +++++++++++++++++++ .../deprecated_api/test_remove_1-10.py | 37 +++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/pytorch_lightning/utilities/apply_func.py diff --git a/src/pytorch_lightning/CHANGELOG.md b/src/pytorch_lightning/CHANGELOG.md index 08197e442f367..f517d56fb0465 100644 --- a/src/pytorch_lightning/CHANGELOG.md +++ b/src/pytorch_lightning/CHANGELOG.md @@ -83,6 +83,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). +- Deprecated the functions in `pytorch_lightning.utilities.apply_func` in favor of `lightning_lite.utilities.apply_func` ([#14516](https://github.com/Lightning-AI/lightning/pull/14516)) + + + ### Removed - Removed the deprecated `Trainer.training_type_plugin` property in favor of `Trainer.strategy` ([#14011](https://github.com/Lightning-AI/lightning/pull/14011)) diff --git a/src/pytorch_lightning/utilities/apply_func.py b/src/pytorch_lightning/utilities/apply_func.py new file mode 100644 index 0000000000000..bc516d3fb3d6f --- /dev/null +++ b/src/pytorch_lightning/utilities/apply_func.py @@ -0,0 +1,82 @@ +# Copyright The PyTorch Lightning 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. +"""Utilities used for collections.""" + +from typing import Any + +from lightning_lite.utilities.apply_func import apply_to_collection as new_apply_to_collection +from lightning_lite.utilities.apply_func import apply_to_collections as new_apply_to_collections +from lightning_lite.utilities.apply_func import convert_to_tensors as new_convert_to_tensors +from lightning_lite.utilities.apply_func import from_numpy as new_from_numpy +from lightning_lite.utilities.apply_func import move_data_to_device as new_move_data_to_device +from lightning_lite.utilities.apply_func import to_dtype_tensor as new_to_dtype_tensor +from lightning_lite.utilities.apply_func import TransferableDataType as NewTransferableDataType +from pytorch_lightning.utilities import rank_zero_deprecation + + +def apply_to_collection(*args: Any, **kwargs: Any) -> Any: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.apply_to_collection` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.apply_to_collection` instead." + ) + return new_apply_to_collection(*args, **kwargs) + + +def apply_to_collections(*args: Any, **kwargs: Any) -> Any: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.apply_to_collections` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.apply_to_collections` instead." + ) + return new_apply_to_collections(*args, **kwargs) + + +def convert_to_tensors(*args: Any, **kwargs: Any) -> Any: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.convert_to_tensors` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.convert_to_tensors` instead." + ) + return new_convert_to_tensors(*args, **kwargs) + + +def from_numpy(*args: Any, **kwargs: Any) -> Any: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.from_numpy` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.from_numpy` instead." + ) + return new_from_numpy(*args, **kwargs) + + +def move_data_to_device(*args: Any, **kwargs: Any) -> Any: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.move_data_to_device` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.move_data_to_device` instead." + ) + return new_move_data_to_device(*args, **kwargs) + + +def to_dtype_tensor(*args: Any, **kwargs: Any) -> Any: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.to_dtype_tensor` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.to_dtype_tensor` instead." + ) + return new_to_dtype_tensor(*args, **kwargs) + + +class TransferableDataType(NewTransferableDataType): + def __init__(self) -> None: + rank_zero_deprecation( + "`pytorch_lightning.utilities.apply_func.TransferableDataType` has been deprecated in v1.8.0 and will be" + " removed in v1.10.0. Please use `lightning_lite.utilities.apply_func.TransferableDataType` instead." + ) + super().__init__() diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-10.py b/tests/tests_pytorch/deprecated_api/test_remove_1-10.py index ffb6abfcc9e12..2193085255fb9 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-10.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-10.py @@ -14,7 +14,9 @@ """Test deprecated functionality which will be removed in v1.10.0.""" from unittest import mock +import numpy import pytest +import torch from pytorch_lightning import Trainer from pytorch_lightning.core.mixins.device_dtype_mixin import DeviceDtypeModuleMixin @@ -26,6 +28,15 @@ from pytorch_lightning.strategies.deepspeed import LightningDeepSpeedModule from pytorch_lightning.strategies.ipu import LightningIPUModule from pytorch_lightning.strategies.utils import on_colab_kaggle +from pytorch_lightning.utilities.apply_func import ( + apply_to_collection, + apply_to_collections, + convert_to_tensors, + from_numpy, + move_data_to_device, + to_dtype_tensor, + TransferableDataType, +) from pytorch_lightning.utilities.cloud_io import atomic_save, get_filesystem, load from pytorch_lightning.utilities.xla_device import inner_f, pl_multi_process, XLADeviceUtils from tests_pytorch.helpers.runif import RunIf @@ -110,3 +121,29 @@ def test_v1_10_deprecated_cloud_io_utilities(tmpdir): with pytest.deprecated_call(match="cloud_io.load` has been deprecated in v1.8.0"): load(str(tmpdir / "atomic_save.ckpt")) + + +def test_v1_10_deprecated_apply_func_utilities(): + with pytest.deprecated_call(match="apply_func.apply_to_collection` has been deprecated in v1.8.0"): + apply_to_collection([], dtype=object, function=(lambda x: x)) + + with pytest.deprecated_call(match="apply_func.apply_to_collections` has been deprecated in v1.8.0"): + apply_to_collections([], [], dtype=object, function=(lambda x, y: x)) + + with pytest.deprecated_call(match="apply_func.convert_to_tensors` has been deprecated in v1.8.0"): + convert_to_tensors([], torch.device("cpu")) + + with pytest.deprecated_call(match="apply_func.from_numpy` has been deprecated in v1.8.0"): + from_numpy(numpy.zeros(2), torch.device("cpu")) + + with pytest.deprecated_call(match="apply_func.move_data_to_device` has been deprecated in v1.8.0"): + move_data_to_device(torch.tensor(2), torch.device("cpu")) + + with pytest.deprecated_call(match="apply_func.to_dtype_tensor` has been deprecated in v1.8.0"): + to_dtype_tensor(torch.tensor(2), dtype=torch.float32, device=torch.device("cpu")) + + class MyModule(TransferableDataType): + pass + + with pytest.deprecated_call(match="apply_func.TransferableDataType` has been deprecated in v1.8.0"): + MyModule()