2021-04-16 12:31:56 +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.
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
2021-12-23 07:26:28 +00:00
|
|
|
from pytorch_lightning.plugins import CheckpointIO, TrainingTypePluginsRegistry
|
|
|
|
from pytorch_lightning.strategies import (
|
2021-12-20 16:11:20 +00:00
|
|
|
DDPFullyShardedStrategy,
|
2021-12-21 17:18:25 +00:00
|
|
|
DDPShardedStrategy,
|
2021-12-22 00:27:36 +00:00
|
|
|
DDPSpawnShardedStrategy,
|
2021-12-21 23:06:14 +00:00
|
|
|
DDPSpawnStrategy,
|
2021-12-21 08:55:51 +00:00
|
|
|
DDPStrategy,
|
2021-12-21 15:18:01 +00:00
|
|
|
DeepSpeedStrategy,
|
2021-12-21 16:36:16 +00:00
|
|
|
TPUSpawnStrategy,
|
2021-07-24 04:02:54 +00:00
|
|
|
)
|
2021-04-16 12:31:56 +00:00
|
|
|
from tests.helpers.runif import RunIf
|
|
|
|
|
|
|
|
|
|
|
|
def test_training_type_plugins_registry_with_new_plugin():
|
|
|
|
class TestPlugin:
|
|
|
|
|
2021-05-03 21:03:21 +00:00
|
|
|
distributed_backend = "test_plugin"
|
|
|
|
|
2021-04-16 12:31:56 +00:00
|
|
|
def __init__(self, param1, param2):
|
|
|
|
self.param1 = param1
|
|
|
|
self.param2 = param2
|
|
|
|
|
|
|
|
plugin_name = "test_plugin"
|
|
|
|
plugin_description = "Test Plugin"
|
|
|
|
|
|
|
|
TrainingTypePluginsRegistry.register(
|
|
|
|
plugin_name, TestPlugin, description=plugin_description, param1="abc", param2=123
|
|
|
|
)
|
|
|
|
|
|
|
|
assert plugin_name in TrainingTypePluginsRegistry
|
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["description"] == plugin_description
|
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["init_params"] == {"param1": "abc", "param2": 123}
|
2021-05-03 21:03:21 +00:00
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["distributed_backend"] == "test_plugin"
|
2021-04-16 12:31:56 +00:00
|
|
|
assert isinstance(TrainingTypePluginsRegistry.get(plugin_name), TestPlugin)
|
|
|
|
|
|
|
|
TrainingTypePluginsRegistry.remove(plugin_name)
|
|
|
|
assert plugin_name not in TrainingTypePluginsRegistry
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"plugin_name, init_params",
|
|
|
|
[
|
|
|
|
("deepspeed", {}),
|
2021-08-18 14:10:19 +00:00
|
|
|
("deepspeed_stage_1", {"stage": 1}),
|
2021-07-26 11:37:35 +00:00
|
|
|
("deepspeed_stage_2", {"stage": 2}),
|
|
|
|
("deepspeed_stage_2_offload", {"stage": 2, "offload_optimizer": True}),
|
|
|
|
("deepspeed_stage_3", {"stage": 3}),
|
|
|
|
("deepspeed_stage_3_offload", {"stage": 3, "offload_parameters": True, "offload_optimizer": True}),
|
2021-04-16 12:31:56 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_training_type_plugins_registry_with_deepspeed_plugins(plugin_name, init_params):
|
|
|
|
|
|
|
|
assert plugin_name in TrainingTypePluginsRegistry
|
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["init_params"] == init_params
|
2021-12-21 15:18:01 +00:00
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["plugin"] == DeepSpeedStrategy
|
2021-04-16 12:31:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@RunIf(deepspeed=True)
|
|
|
|
@pytest.mark.parametrize("plugin", ["deepspeed", "deepspeed_stage_2_offload", "deepspeed_stage_3"])
|
2021-05-04 22:40:00 +00:00
|
|
|
def test_deepspeed_training_type_plugins_registry_with_trainer(tmpdir, plugin):
|
2021-04-16 12:31:56 +00:00
|
|
|
|
2021-10-20 15:32:53 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, strategy=plugin, precision=16)
|
2021-04-16 12:31:56 +00:00
|
|
|
|
2021-12-22 02:11:43 +00:00
|
|
|
assert isinstance(trainer.strategy, DeepSpeedStrategy)
|
2021-05-04 22:40:00 +00:00
|
|
|
|
|
|
|
|
2021-06-15 22:32:51 +00:00
|
|
|
def test_tpu_spawn_debug_plugins_registry(tmpdir):
|
|
|
|
|
|
|
|
plugin = "tpu_spawn_debug"
|
|
|
|
|
|
|
|
assert plugin in TrainingTypePluginsRegistry
|
|
|
|
assert TrainingTypePluginsRegistry[plugin]["init_params"] == {"debug": True}
|
2021-12-21 16:36:16 +00:00
|
|
|
assert TrainingTypePluginsRegistry[plugin]["plugin"] == TPUSpawnStrategy
|
2021-06-15 22:32:51 +00:00
|
|
|
|
2021-10-20 15:32:53 +00:00
|
|
|
trainer = Trainer(strategy=plugin)
|
2021-06-15 22:32:51 +00:00
|
|
|
|
2021-12-22 02:11:43 +00:00
|
|
|
assert isinstance(trainer.strategy, TPUSpawnStrategy)
|
2021-07-24 04:02:54 +00:00
|
|
|
|
|
|
|
|
2021-12-20 16:11:20 +00:00
|
|
|
def test_fsdp_strategys_registry(tmpdir):
|
2021-11-05 09:42:58 +00:00
|
|
|
|
|
|
|
plugin = "fsdp"
|
|
|
|
|
|
|
|
assert plugin in TrainingTypePluginsRegistry
|
2021-12-20 16:11:20 +00:00
|
|
|
assert TrainingTypePluginsRegistry[plugin]["plugin"] == DDPFullyShardedStrategy
|
2021-11-05 09:42:58 +00:00
|
|
|
|
|
|
|
trainer = Trainer(strategy=plugin)
|
|
|
|
|
2021-12-22 02:11:43 +00:00
|
|
|
assert isinstance(trainer.strategy, DDPFullyShardedStrategy)
|
2021-11-05 09:42:58 +00:00
|
|
|
|
|
|
|
|
2021-07-24 04:02:54 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"plugin_name, plugin",
|
|
|
|
[
|
2021-12-21 08:55:51 +00:00
|
|
|
("ddp_find_unused_parameters_false", DDPStrategy),
|
2021-12-21 23:06:14 +00:00
|
|
|
("ddp_spawn_find_unused_parameters_false", DDPSpawnStrategy),
|
2021-12-22 00:27:36 +00:00
|
|
|
("ddp_sharded_spawn_find_unused_parameters_false", DDPSpawnShardedStrategy),
|
2021-12-21 17:18:25 +00:00
|
|
|
("ddp_sharded_find_unused_parameters_false", DDPShardedStrategy),
|
2021-07-24 04:02:54 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_ddp_find_unused_parameters_training_type_plugins_registry(tmpdir, plugin_name, plugin):
|
|
|
|
|
2021-10-20 15:32:53 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, strategy=plugin_name)
|
2021-07-24 04:02:54 +00:00
|
|
|
|
2021-12-22 02:11:43 +00:00
|
|
|
assert isinstance(trainer.strategy, plugin)
|
2021-07-24 04:02:54 +00:00
|
|
|
|
|
|
|
assert plugin_name in TrainingTypePluginsRegistry
|
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["init_params"] == {"find_unused_parameters": False}
|
|
|
|
assert TrainingTypePluginsRegistry[plugin_name]["plugin"] == plugin
|
2021-10-29 04:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_custom_registered_training_plugin_to_strategy():
|
|
|
|
class CustomCheckpointIO(CheckpointIO):
|
|
|
|
def save_checkpoint(self, checkpoint, path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def load_checkpoint(self, path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def remove_checkpoint(self, path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
custom_checkpoint_io = CustomCheckpointIO()
|
|
|
|
|
2021-12-21 08:55:51 +00:00
|
|
|
# Register the DDP Strategy with your custom CheckpointIO plugin
|
2021-10-29 04:06:06 +00:00
|
|
|
TrainingTypePluginsRegistry.register(
|
|
|
|
"ddp_custom_checkpoint_io",
|
2021-12-21 08:55:51 +00:00
|
|
|
DDPStrategy,
|
|
|
|
description="DDP Strategy with custom checkpoint io plugin",
|
2021-10-29 04:06:06 +00:00
|
|
|
checkpoint_io=custom_checkpoint_io,
|
|
|
|
)
|
|
|
|
trainer = Trainer(strategy="ddp_custom_checkpoint_io", accelerator="cpu", devices=2)
|
|
|
|
|
2021-12-22 02:11:43 +00:00
|
|
|
assert isinstance(trainer.strategy, DDPStrategy)
|
|
|
|
assert trainer.strategy.checkpoint_io == custom_checkpoint_io
|