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
|
2021-01-24 11:44:54 +00:00
|
|
|
from unittest.mock import patch
|
2020-11-01 00:04:25 +00:00
|
|
|
|
2020-10-06 17:54:37 +00:00
|
|
|
import pytest
|
2022-09-07 15:25:23 +00:00
|
|
|
from tests_lite.helpers.runif import RunIf
|
2020-10-06 17:54:37 +00:00
|
|
|
|
2022-10-04 22:54:14 +00:00
|
|
|
from lightning_lite.accelerators.tpu import _multi_process, _XLA_AVAILABLE, TPUAccelerator
|
2020-10-06 17:54:37 +00:00
|
|
|
|
|
|
|
|
2021-01-25 12:57:06 +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():
|
2022-10-04 22:54:14 +00:00
|
|
|
"""Check `is_available` returns True when TPU is available."""
|
|
|
|
assert not TPUAccelerator.is_available()
|
2020-10-06 17:54:37 +00:00
|
|
|
|
|
|
|
|
2021-03-02 16:21:20 +00:00
|
|
|
@RunIf(tpu=True)
|
2020-10-06 17:54:37 +00:00
|
|
|
def test_tpu_device_presence():
|
2022-10-04 22:54:14 +00:00
|
|
|
"""Check `is_available` returns True when TPU is available."""
|
|
|
|
assert TPUAccelerator.is_available()
|
2020-10-06 17:54:37 +00:00
|
|
|
|
|
|
|
|
2021-03-29 17:59:20 +00:00
|
|
|
def sleep_fn(sleep_time: float) -> bool:
|
|
|
|
time.sleep(sleep_time)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-10-04 22:54:14 +00:00
|
|
|
@patch("lightning_lite.accelerators.tpu.TPU_CHECK_TIMEOUT", 3)
|
2021-03-29 17:59:20 +00:00
|
|
|
@pytest.mark.skipif(not _XLA_AVAILABLE, reason="test requires torch_xla to be present")
|
2021-01-24 11:44:54 +00:00
|
|
|
def test_result_returns_within_timeout_seconds():
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Check that pl_multi_process returns within 3 seconds."""
|
2022-10-04 22:54:14 +00:00
|
|
|
fn = _multi_process(sleep_fn)
|
2021-03-29 17:59:20 +00:00
|
|
|
|
2020-11-01 00:04:25 +00:00
|
|
|
start = time.time()
|
2022-10-04 22:54:14 +00:00
|
|
|
from lightning_lite.accelerators.tpu import TPU_CHECK_TIMEOUT
|
|
|
|
|
|
|
|
result = fn(TPU_CHECK_TIMEOUT * 0.5)
|
2020-11-01 00:04:25 +00:00
|
|
|
end = time.time()
|
|
|
|
elapsed_time = int(end - start)
|
2021-03-29 17:59:20 +00:00
|
|
|
|
2022-10-04 22:54:14 +00:00
|
|
|
assert elapsed_time <= TPU_CHECK_TIMEOUT
|
2021-03-29 17:59:20 +00:00
|
|
|
assert result
|