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-08-24 01:29:25 +00:00
|
|
|
from colossalai.zero import GeminiDDP, GeminiOptimizer
|
|
|
|
from colossalai.zero.gemini.chunk import search_chunk_configuration
|
2022-10-18 08:31:22 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2023-08-24 01:29:25 +00:00
|
|
|
from tests.test_tensor.common_utils import set_seed
|
|
|
|
|
|
|
|
PLACEMENT_CONFIGS = [
|
|
|
|
{
|
|
|
|
'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'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@parameterize('placement_config', PLACEMENT_CONFIGS)
|
2022-10-18 08:31:22 +00:00
|
|
|
@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)
|
|
|
|
get_components_func = non_distributed_component_funcs.get_callable('gpt2')
|
|
|
|
model_builder, train_dataloader, test_dataloader, optimizer_class, criterion = get_components_func()
|
|
|
|
|
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)
|
2022-10-18 08:31:22 +00:00
|
|
|
config_dict[world_size]['chunk_size'] = 5000
|
|
|
|
config_dict[world_size]['keep_gathered'] = keep_gathered
|
|
|
|
|
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-08-24 01:29:25 +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()
|
2022-11-24 08:51:45 +00:00
|
|
|
for i, (input_ids, label) in enumerate(train_dataloader):
|
2022-10-18 08:31:22 +00:00
|
|
|
if i > 0:
|
|
|
|
break
|
|
|
|
optim.zero_grad()
|
2022-11-24 08:51:45 +00:00
|
|
|
logits = model(input_ids)
|
2022-10-18 08:31:22 +00:00
|
|
|
logits = logits.float()
|
|
|
|
loss = criterion(logits, input_ids)
|
|
|
|
optim.backward(loss)
|
|
|
|
optim.step()
|
|
|
|
|
|
|
|
optim_state_dict = optim.state_dict()
|
|
|
|
optim.load_state_dict(optim_state_dict)
|
|
|
|
new_state = optim.state_dict()['state']
|
|
|
|
org_state = optim_state_dict['state']
|
|
|
|
|
|
|
|
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):
|
|
|
|
config = {}
|
|
|
|
colossalai.launch(config=config, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
exam_zero_optim_state_dict()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.dist
|
|
|
|
@pytest.mark.parametrize('world_size', [1, 4])
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_zero_optim(1)
|