2022-06-22 08:57:50 +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.
|
|
|
|
import os
|
|
|
|
from unittest import mock
|
|
|
|
|
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
|
|
|
import lightning.fabric
|
2022-06-22 08:57:50 +00:00
|
|
|
import pytest
|
2024-01-10 17:39:20 +00:00
|
|
|
from lightning.fabric.accelerators.xla import _XLA_GREATER_EQUAL_2_1
|
2023-02-01 20:34:38 +00:00
|
|
|
from lightning.fabric.plugins.environments import XLAEnvironment
|
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
|
|
|
|
2023-03-03 16:55:48 +00:00
|
|
|
from tests_fabric.helpers.runif import RunIf
|
2022-06-22 08:57:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@RunIf(tpu=True)
|
2023-04-11 22:24:11 +00:00
|
|
|
# keep existing environment or else xla will default to pjrt
|
|
|
|
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
|
2023-04-19 14:39:00 +00:00
|
|
|
def test_default_attributes(monkeypatch):
|
2022-06-22 08:57:50 +00:00
|
|
|
"""Test the default attributes when no environment variables are set."""
|
2024-01-10 17:39:20 +00:00
|
|
|
# calling these creates side effects in other tests
|
|
|
|
if _XLA_GREATER_EQUAL_2_1:
|
|
|
|
from torch_xla import runtime
|
|
|
|
|
|
|
|
monkeypatch.setattr(runtime, "world_size", lambda: 2)
|
|
|
|
monkeypatch.setattr(runtime, "global_ordinal", lambda: 0)
|
|
|
|
monkeypatch.setattr(runtime, "local_ordinal", lambda: 0)
|
|
|
|
monkeypatch.setattr(runtime, "host_index", lambda: 1)
|
2023-04-19 14:39:00 +00:00
|
|
|
else:
|
2024-01-10 17:39:20 +00:00
|
|
|
from torch_xla.experimental import pjrt
|
2023-04-19 14:39:00 +00:00
|
|
|
|
2024-01-10 17:39:20 +00:00
|
|
|
monkeypatch.setattr(pjrt, "world_size", lambda: 2)
|
|
|
|
monkeypatch.setattr(pjrt, "global_ordinal", lambda: 0)
|
|
|
|
monkeypatch.setattr(pjrt, "local_ordinal", lambda: 0)
|
2023-07-24 13:13:36 +00:00
|
|
|
os.environ["XRT_HOST_ORDINAL"] = "1"
|
2023-04-19 14:39:00 +00:00
|
|
|
|
2022-06-22 08:57:50 +00:00
|
|
|
env = XLAEnvironment()
|
|
|
|
assert not env.creates_processes_externally
|
2023-07-24 13:13:36 +00:00
|
|
|
assert env.world_size() == 2
|
2022-06-22 08:57:50 +00:00
|
|
|
assert env.global_rank() == 0
|
|
|
|
assert env.local_rank() == 0
|
2023-07-24 13:13:36 +00:00
|
|
|
assert env.node_rank() == 1
|
2022-06-22 08:57:50 +00:00
|
|
|
|
2023-04-18 16:52:36 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2022-06-22 08:57:50 +00:00
|
|
|
_ = env.main_address
|
2023-04-18 16:52:36 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2022-06-22 08:57:50 +00:00
|
|
|
_ = env.main_port
|
|
|
|
|
|
|
|
|
|
|
|
@RunIf(tpu=True)
|
2023-04-18 16:52:36 +00:00
|
|
|
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
|
|
|
|
def test_attributes_from_environment_variables(monkeypatch):
|
2022-06-22 08:57:50 +00:00
|
|
|
"""Test that the default cluster environment takes the attributes from the environment variables."""
|
2024-01-10 17:39:20 +00:00
|
|
|
if _XLA_GREATER_EQUAL_2_1:
|
|
|
|
from torch_xla import runtime
|
|
|
|
|
|
|
|
monkeypatch.setattr(runtime, "world_size", lambda: 2)
|
|
|
|
monkeypatch.setattr(runtime, "global_ordinal", lambda: 0)
|
|
|
|
monkeypatch.setattr(runtime, "local_ordinal", lambda: 2)
|
|
|
|
monkeypatch.setattr(runtime, "host_index", lambda: 1)
|
2023-04-18 16:52:36 +00:00
|
|
|
else:
|
2024-01-10 17:39:20 +00:00
|
|
|
from torch_xla.experimental import pjrt
|
|
|
|
|
|
|
|
monkeypatch.setattr(pjrt, "world_size", lambda: 2)
|
|
|
|
monkeypatch.setattr(pjrt, "global_ordinal", lambda: 0)
|
|
|
|
monkeypatch.setattr(pjrt, "local_ordinal", lambda: 2)
|
|
|
|
os.environ["XRT_HOST_ORDINAL"] = "1"
|
2023-04-18 16:52:36 +00:00
|
|
|
|
2022-06-22 08:57:50 +00:00
|
|
|
env = XLAEnvironment()
|
2023-04-18 16:52:36 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
_ = env.main_address
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
_ = env.main_port
|
2023-07-24 13:13:36 +00:00
|
|
|
assert env.world_size() == 2
|
2022-06-22 08:57:50 +00:00
|
|
|
assert env.global_rank() == 0
|
|
|
|
assert env.local_rank() == 2
|
2023-07-24 13:13:36 +00:00
|
|
|
assert env.node_rank() == 1
|
2022-06-22 08:57:50 +00:00
|
|
|
env.set_global_rank(100)
|
|
|
|
assert env.global_rank() == 0
|
|
|
|
env.set_world_size(100)
|
2023-07-24 13:13:36 +00:00
|
|
|
assert env.world_size() == 2
|
2022-06-22 08:57:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_detect(monkeypatch):
|
|
|
|
"""Test the detection of a xla environment configuration."""
|
2023-04-28 16:36:22 +00:00
|
|
|
monkeypatch.setattr(lightning.fabric.accelerators.xla.XLAAccelerator, "is_available", lambda: False)
|
2022-06-22 08:57:50 +00:00
|
|
|
assert not XLAEnvironment.detect()
|
|
|
|
|
2023-04-28 16:36:22 +00:00
|
|
|
monkeypatch.setattr(lightning.fabric.accelerators.xla.XLAAccelerator, "is_available", lambda: True)
|
2022-06-22 08:57:50 +00:00
|
|
|
assert XLAEnvironment.detect()
|