2022-10-18 08:31:22 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
2023-01-11 06:55:41 +00:00
|
|
|
from torch.testing import assert_close
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
import colossalai
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use, spawn
|
2023-09-18 08:31:06 +00:00
|
|
|
from colossalai.utils import set_seed
|
2023-08-24 01:29:25 +00:00
|
|
|
from colossalai.zero import GeminiDDP
|
|
|
|
from colossalai.zero.gemini.chunk import search_chunk_configuration
|
2023-10-20 02:35:08 +00:00
|
|
|
from tests.kit.model_zoo import model_zoo
|
2023-08-24 01:29:25 +00:00
|
|
|
|
|
|
|
PLACEMENT_CONFIGS = [
|
2023-09-19 06:20:26 +00:00
|
|
|
{"placement_policy": "static", "shard_param_frac": 0.0}, # zero2
|
|
|
|
{"placement_policy": "static", "shard_param_frac": 1.0}, # zero3
|
|
|
|
{"placement_policy": "static", "shard_param_frac": 0.5}, # zero3-half
|
|
|
|
{"placement_policy": "auto"},
|
2023-08-24 01:29:25 +00:00
|
|
|
]
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
|
2023-01-11 06:55:41 +00:00
|
|
|
def ignore_the_first_parameter(model: torch.nn.Module):
|
|
|
|
for name, param in model.named_parameters():
|
|
|
|
print(f"parameter `{name}` is set ignored")
|
2023-08-24 01:29:25 +00:00
|
|
|
GeminiDDP.set_params_to_ignore([param])
|
2023-01-11 06:55:41 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
@parameterize("placement_config", PLACEMENT_CONFIGS)
|
|
|
|
@parameterize("keep_gathered", [True, False])
|
2023-10-20 02:35:08 +00:00
|
|
|
@parameterize("model_name", ["transformers_gpt_lm", "transformers_bert_for_sequence_classification"])
|
2023-10-12 02:39:08 +00:00
|
|
|
@parameterize("master_weights", [False, True])
|
|
|
|
def exam_state_dict(placement_config, keep_gathered, model_name: str, master_weights: bool):
|
2022-10-18 08:31:22 +00:00
|
|
|
set_seed(431)
|
2023-10-20 02:35:08 +00:00
|
|
|
model_builder, data_gen_fn, output_transform_fn, *_ = next(iter(model_zoo.get_sub_registry(model_name).values()))
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
model = model_builder()
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-10-20 02:35:08 +00:00
|
|
|
model_size = sum(p.numel() * p.element_size() for p in model.parameters()) / 1024**2
|
|
|
|
|
2022-10-18 08:31:22 +00:00
|
|
|
torch_model = model_builder()
|
|
|
|
for torch_p, p in zip(torch_model.parameters(), model.parameters()):
|
|
|
|
torch_p.data.copy_(p.data)
|
|
|
|
|
|
|
|
world_size = torch.distributed.get_world_size()
|
2023-06-25 05:34:15 +00:00
|
|
|
config_dict, *_ = search_chunk_configuration(model, search_range_m=1, search_interval=100)
|
2023-09-19 06:20:26 +00:00
|
|
|
config_dict[world_size]["chunk_size"] = 5000
|
|
|
|
config_dict[world_size]["keep_gathered"] = keep_gathered
|
2023-10-12 02:39:08 +00:00
|
|
|
model = GeminiDDP(model, config_dict, **placement_config, pin_memory=True, master_weights=master_weights)
|
2022-10-18 08:31:22 +00:00
|
|
|
model.train()
|
|
|
|
|
|
|
|
zero_dict = model.state_dict(only_rank_0=False)
|
|
|
|
torch_dict = torch_model.state_dict()
|
|
|
|
|
|
|
|
for key, value in torch_dict.items():
|
|
|
|
assert key in zero_dict, "{} not in ZeRO dictionary.".format(key)
|
|
|
|
temp_zero_value = zero_dict[key].to(device=value.device, dtype=value.dtype)
|
2023-01-11 06:55:41 +00:00
|
|
|
assert_close(value, temp_zero_value, rtol=1e-3, atol=1e-5)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-10-20 02:35:08 +00:00
|
|
|
# check load state dict
|
2022-10-18 08:31:22 +00:00
|
|
|
model.load_state_dict(torch_dict, strict=False)
|
|
|
|
zero_dict = model.state_dict(only_rank_0=False)
|
|
|
|
|
|
|
|
for key, value in torch_dict.items():
|
|
|
|
assert key in zero_dict, "{} not in ZeRO dictionary.".format(key)
|
|
|
|
temp_zero_value = zero_dict[key].to(device=value.device, dtype=value.dtype)
|
2023-01-11 06:55:41 +00:00
|
|
|
assert_close(value, temp_zero_value, rtol=1e-3, atol=1e-5)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-10-20 02:35:08 +00:00
|
|
|
# check state dict shard
|
2023-08-24 01:29:25 +00:00
|
|
|
accumulated_keys = set()
|
|
|
|
# ensure number of shards > 1
|
|
|
|
for shard, _ in model.state_dict_shard(max_shard_size=(model_size / 3), only_rank_0=False):
|
|
|
|
for key, value in shard.items():
|
|
|
|
assert key not in accumulated_keys, f"key `{key}` is duplicated."
|
|
|
|
accumulated_keys.add(key)
|
|
|
|
assert key in zero_dict, f"{key} not in ZeRO dictionary."
|
|
|
|
assert torch.equal(value, zero_dict[key]), f"{key} not equal."
|
|
|
|
|
|
|
|
|
2022-10-18 08:31:22 +00:00
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
config = {}
|
2023-09-19 06:20:26 +00:00
|
|
|
colossalai.launch(config=config, rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2022-10-18 08:31:22 +00:00
|
|
|
exam_state_dict()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
2023-09-19 06:20:26 +00:00
|
|
|
@pytest.mark.parametrize("world_size", [1, 4])
|
2022-10-18 08:31:22 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
|
|
|
def test_zero_ddp(world_size):
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_dist, world_size)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
if __name__ == "__main__":
|
2022-10-18 08:31:22 +00:00
|
|
|
test_zero_ddp(1)
|