2020-10-13 11:18:07 +00:00
|
|
|
# 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.
|
2020-11-01 00:04:25 +00:00
|
|
|
import time
|
|
|
|
|
2020-10-06 17:54:37 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-12-21 23:23:33 +00:00
|
|
|
import pytorch_lightning.utilities.xla_device as xla_utils
|
2020-12-14 14:49:05 +00:00
|
|
|
from pytorch_lightning.utilities import _XLA_AVAILABLE, _TPU_AVAILABLE
|
2020-10-06 17:54:37 +00:00
|
|
|
from tests.base.develop_utils import pl_multi_process_test
|
|
|
|
|
|
|
|
|
2020-12-21 08:53:09 +00:00
|
|
|
# lets hope that in or env we have installed XLA only for TPU devices, otherwise,
|
|
|
|
# it is testing in the cycle "if I am true test that I am true :D"
|
2020-12-14 14:49:05 +00:00
|
|
|
@pytest.mark.skipif(_XLA_AVAILABLE, reason="test requires torch_xla to be absent")
|
2020-10-06 17:54:37 +00:00
|
|
|
def test_tpu_device_absence():
|
|
|
|
"""Check tpu_device_exists returns None when torch_xla is not available"""
|
2020-11-01 00:04:25 +00:00
|
|
|
assert xla_utils.XLADeviceUtils.tpu_device_exists() is None
|
2020-10-06 17:54:37 +00:00
|
|
|
|
|
|
|
|
2020-12-14 14:49:05 +00:00
|
|
|
@pytest.mark.skipif(not _TPU_AVAILABLE, reason="test requires torch_xla to be installed")
|
2020-12-02 13:05:11 +00:00
|
|
|
@pl_multi_process_test
|
2020-10-06 17:54:37 +00:00
|
|
|
def test_tpu_device_presence():
|
|
|
|
"""Check tpu_device_exists returns True when TPU is available"""
|
2020-11-01 00:04:25 +00:00
|
|
|
assert xla_utils.XLADeviceUtils.tpu_device_exists() is True
|
2020-10-06 17:54:37 +00:00
|
|
|
|
|
|
|
|
2020-12-02 13:05:11 +00:00
|
|
|
def test_result_returns_within_20_seconds():
|
2020-11-01 00:04:25 +00:00
|
|
|
"""Check that pl_multi_process returns within 10 seconds"""
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
result = xla_utils.pl_multi_process(time.sleep)(25)
|
|
|
|
end = time.time()
|
|
|
|
elapsed_time = int(end - start)
|
2020-12-02 13:05:11 +00:00
|
|
|
assert elapsed_time <= 20
|
2020-11-01 00:04:25 +00:00
|
|
|
assert result is False
|