Skip to content

Commit

Permalink
Deprecate pl/utilities/apply_func (#14516)
Browse files Browse the repository at this point in the history
  • Loading branch information
awaelchli committed Sep 5, 2022
1 parent 9fea2ed commit 7f148b2
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/pytorch_lightning/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
82 changes: 82 additions & 0 deletions src/pytorch_lightning/utilities/apply_func.py
Original file line number Diff line number Diff line change
@@ -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__()
37 changes: 37 additions & 0 deletions tests/tests_pytorch/deprecated_api/test_remove_1-10.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 7f148b2

Please sign in to comment.