2022-10-18 08:31:22 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
|
|
|
|
|
|
|
import colossalai
|
|
|
|
from colossalai.nn.optimizer import HybridAdam
|
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, GeminiOptimizer
|
|
|
|
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, "offload_optim_frac": 0.0}, # zero2
|
|
|
|
{"placement_policy": "static", "shard_param_frac": 0.0, "offload_optim_frac": 1.0}, # zero2-offload
|
|
|
|
{"placement_policy": "static", "shard_param_frac": 0.0, "offload_optim_frac": 0.5}, # zero2-offload-half
|
|
|
|
{"placement_policy": "auto"},
|
2023-08-24 01:29:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
@parameterize("placement_config", PLACEMENT_CONFIGS)
|
|
|
|
@parameterize("keep_gathered", [True, False])
|
2023-08-24 01:29:25 +00:00
|
|
|
def exam_zero_optim_state_dict(placement_config, keep_gathered):
|
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("transformers_gpt_lm").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
|
|
|
|
|
|
|
set_seed(451)
|
|
|
|
|
|
|
|
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
|
2022-10-18 08:31:22 +00:00
|
|
|
|
2023-08-24 01:29:25 +00:00
|
|
|
model = GeminiDDP(model, config_dict, **placement_config, pin_memory=True)
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
optimizer = HybridAdam(model.parameters())
|
2023-09-19 06:20:26 +00:00
|
|
|
optim = GeminiOptimizer(optimizer, model, initial_scale=32) # initialize the link between chunk16 and chunk32
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
set_seed(dist.get_rank() * 3 + 128)
|
|
|
|
model.train()
|
2023-10-20 02:35:08 +00:00
|
|
|
data = data_gen_fn()
|
|
|
|
data = {k: v.cuda() if isinstance(v, torch.Tensor) else v for k, v in data.items()}
|
|
|
|
|
|
|
|
optim.zero_grad()
|
|
|
|
outputs = model(**data)
|
|
|
|
outputs = output_transform_fn(outputs)
|
|
|
|
loss = next(iter(outputs.values())).sum()
|
|
|
|
optim.backward(loss)
|
|
|
|
optim.step()
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
optim_state_dict = optim.state_dict()
|
|
|
|
optim.load_state_dict(optim_state_dict)
|
2023-09-19 06:20:26 +00:00
|
|
|
new_state = optim.state_dict()["state"]
|
|
|
|
org_state = optim_state_dict["state"]
|
2022-10-18 08:31:22 +00:00
|
|
|
|
|
|
|
for k, v in org_state.items():
|
|
|
|
w = new_state[k]
|
|
|
|
for n, m in v.items():
|
|
|
|
if isinstance(m, torch.Tensor):
|
|
|
|
o = w[n]
|
|
|
|
assert torch.equal(m, o)
|
|
|
|
else:
|
|
|
|
assert m == w[n]
|
|
|
|
|
|
|
|
|
|
|
|
def run_dist(rank, world_size, port):
|
2024-04-29 02:40:11 +00:00
|
|
|
colossalai.launch(rank=rank, world_size=world_size, host="localhost", port=port, backend="nccl")
|
2022-10-18 08:31:22 +00:00
|
|
|
exam_zero_optim_state_dict()
|
|
|
|
|
|
|
|
|
2023-10-12 06:04:24 +00:00
|
|
|
@pytest.mark.skip
|
2022-10-18 08:31:22 +00:00
|
|
|
@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_optim(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_optim(1)
|