2021-04-08 14:27:48 +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-05-22 20:19:24 +00:00
|
|
|
import os
|
2021-04-13 18:07:40 +00:00
|
|
|
from unittest import mock
|
2023-07-31 14:37:35 +00:00
|
|
|
from unittest.mock import Mock
|
2021-04-08 14:27:48 +00:00
|
|
|
|
2021-05-22 20:19:24 +00:00
|
|
|
import torch
|
2021-04-08 14:27:48 +00:00
|
|
|
|
2023-07-14 23:16:42 +00:00
|
|
|
from lightning.fabric.accelerators.xla import _using_pjrt
|
2023-02-02 10:06:45 +00:00
|
|
|
from lightning.pytorch import Trainer
|
2023-04-28 16:36:22 +00:00
|
|
|
from lightning.pytorch.accelerators import XLAAccelerator
|
2023-04-11 22:04:17 +00:00
|
|
|
from lightning.pytorch.demos.boring_classes import BoringModel
|
2023-02-17 02:06:24 +00:00
|
|
|
from lightning.pytorch.strategies import XLAStrategy
|
2022-06-15 22:10:49 +00:00
|
|
|
from tests_pytorch.helpers.runif import RunIf
|
2021-04-08 14:27:48 +00:00
|
|
|
|
|
|
|
|
2021-05-22 20:19:24 +00:00
|
|
|
class BoringModelTPU(BoringModel):
|
|
|
|
def on_train_start(self) -> None:
|
2023-07-14 23:16:42 +00:00
|
|
|
index = 0 if _using_pjrt() else 1
|
2022-07-27 15:40:40 +00:00
|
|
|
# assert strategy attributes for device setting
|
2023-04-19 14:39:00 +00:00
|
|
|
assert self.device == torch.device("xla", index=index)
|
2021-05-22 20:19:24 +00:00
|
|
|
assert os.environ.get("PT_XLA_DEBUG") == "1"
|
|
|
|
|
|
|
|
|
2022-07-27 15:40:40 +00:00
|
|
|
@RunIf(tpu=True, standalone=True)
|
2022-10-04 22:54:14 +00:00
|
|
|
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
|
2023-04-19 14:39:00 +00:00
|
|
|
def test_xla_strategy_debug_state():
|
2023-02-17 02:06:24 +00:00
|
|
|
"""Tests if device/debug flag is set correctly when training and after teardown for XLAStrategy."""
|
2022-07-27 15:40:40 +00:00
|
|
|
model = BoringModelTPU()
|
2023-04-19 14:39:00 +00:00
|
|
|
trainer_kwargs = {}
|
2023-07-14 23:16:42 +00:00
|
|
|
if not _using_pjrt():
|
2023-04-19 14:39:00 +00:00
|
|
|
# only XRT supports XLA with a single process
|
|
|
|
trainer_kwargs["devices"] = 1
|
|
|
|
trainer = Trainer(fast_dev_run=True, strategy=XLAStrategy(debug=True), **trainer_kwargs)
|
2023-04-28 16:36:22 +00:00
|
|
|
assert isinstance(trainer.accelerator, XLAAccelerator)
|
2023-02-17 02:06:24 +00:00
|
|
|
assert isinstance(trainer.strategy, XLAStrategy)
|
2021-05-22 20:19:24 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
assert "PT_XLA_DEBUG" not in os.environ
|
2023-07-31 14:37:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch.dict(os.environ, os.environ.copy(), clear=True)
|
|
|
|
def test_rank_properties_access(xla_available):
|
|
|
|
"""Test that the strategy returns the expected values depending on whether we're in the main process or not."""
|
|
|
|
strategy = XLAStrategy()
|
|
|
|
strategy.cluster_environment = Mock()
|
|
|
|
|
|
|
|
# we're in the main process, no processes have been launched yet
|
|
|
|
assert not strategy._launched
|
|
|
|
assert strategy.global_rank == 0
|
|
|
|
assert strategy.local_rank == 0
|
|
|
|
assert strategy.node_rank == 0
|
|
|
|
assert strategy.world_size == 1
|
|
|
|
|
|
|
|
# simulate we're in a worker process
|
|
|
|
strategy._launched = True
|
|
|
|
assert strategy.global_rank == strategy.cluster_environment.global_rank()
|
|
|
|
assert strategy.local_rank == strategy.cluster_environment.local_rank()
|
|
|
|
assert strategy.node_rank == strategy.cluster_environment.node_rank()
|
|
|
|
assert strategy.world_size == strategy.cluster_environment.world_size()
|