2020-10-13 11:18:07 +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.
|
2021-06-01 12:09:20 +00:00
|
|
|
import pytest
|
2020-05-19 15:05:07 +00:00
|
|
|
import torch
|
2023-02-01 20:34:38 +00:00
|
|
|
from lightning.fabric.utilities.apply_func import convert_tensors_to_scalars, move_data_to_device
|
ruff: replace isort with ruff +TPU (#17684)
* ruff: replace isort with ruff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing & imports
* lines in warning test
* docs
* fix enum import
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing
* import
* fix lines
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* type ClusterEnvironment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-26 15:54:55 +00:00
|
|
|
from torch import Tensor
|
2022-01-05 22:29:03 +00:00
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@pytest.mark.parametrize("should_return", [False, True])
|
2021-07-19 08:47:14 +00:00
|
|
|
def test_wrongly_implemented_transferable_data_type(should_return):
|
|
|
|
class TensorObject:
|
2022-12-24 06:44:27 +00:00
|
|
|
def __init__(self, tensor: Tensor, should_return: bool = True):
|
2021-07-19 08:47:14 +00:00
|
|
|
self.tensor = tensor
|
|
|
|
self.should_return = should_return
|
|
|
|
|
|
|
|
def to(self, device):
|
|
|
|
self.tensor.to(device)
|
|
|
|
# simulate a user forgets to return self
|
|
|
|
if self.should_return:
|
|
|
|
return self
|
2023-05-05 09:34:40 +00:00
|
|
|
return None
|
2021-07-19 08:47:14 +00:00
|
|
|
|
|
|
|
tensor = torch.tensor(0.1)
|
|
|
|
obj = TensorObject(tensor, should_return)
|
|
|
|
assert obj == move_data_to_device(obj, torch.device("cpu"))
|
2023-01-10 16:11:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_convert_tensors_to_scalars():
|
|
|
|
assert convert_tensors_to_scalars("string") == "string"
|
|
|
|
assert convert_tensors_to_scalars(1) == 1
|
|
|
|
assert convert_tensors_to_scalars(True) is True
|
|
|
|
assert convert_tensors_to_scalars({"scalar": 1.0}) == {"scalar": 1.0}
|
|
|
|
|
|
|
|
result = convert_tensors_to_scalars({"tensor": torch.tensor(2.0)})
|
|
|
|
# note: `==` comparison as above is not sufficient, since `torch.tensor(x) == x` evaluates to truth
|
2023-05-04 15:50:39 +00:00
|
|
|
assert not isinstance(result["tensor"], Tensor)
|
|
|
|
assert result["tensor"] == 2.0
|
2023-01-10 16:11:47 +00:00
|
|
|
|
2023-01-10 22:02:55 +00:00
|
|
|
data = {"tensor": torch.tensor([2.0])}
|
|
|
|
result = convert_tensors_to_scalars(data)
|
2023-05-04 15:50:39 +00:00
|
|
|
assert not isinstance(result["tensor"], Tensor)
|
|
|
|
assert result["tensor"] == 2.0
|
|
|
|
assert isinstance(data["tensor"], Tensor)
|
|
|
|
assert data["tensor"] == 2.0
|
2023-01-10 16:11:47 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="does not contain a single element"):
|
|
|
|
convert_tensors_to_scalars({"tensor": torch.tensor([1, 2, 3])})
|