2022-03-07 08:14:40 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
2022-12-06 14:30:16 +00:00
|
|
|
from common import CONFIG
|
|
|
|
|
|
|
|
import colossalai
|
2022-03-29 01:09:04 +00:00
|
|
|
from colossalai.logging import get_dist_logger
|
2023-04-06 06:51:35 +00:00
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use, spawn
|
2022-03-14 07:06:02 +00:00
|
|
|
from colossalai.utils.cuda import get_current_device
|
2022-04-11 08:47:57 +00:00
|
|
|
from colossalai.utils.memory import colo_device_memory_used
|
2023-04-04 05:48:16 +00:00
|
|
|
from colossalai.zero.gemini.memory_tracer.utils import colo_model_mem_usage
|
|
|
|
from colossalai.zero.legacy.init_ctx import ZeroInitContext
|
|
|
|
from colossalai.zero.legacy.shard_utils import BucketTensorShardStrategy, TensorShardStrategy
|
2022-03-08 06:45:01 +00:00
|
|
|
from tests.components_to_test.registry import non_distributed_component_funcs
|
2022-03-29 01:09:04 +00:00
|
|
|
|
2022-03-07 08:14:40 +00:00
|
|
|
|
2022-03-25 04:24:18 +00:00
|
|
|
@parameterize("init_device_type", ['cpu', 'cuda'])
|
2022-04-19 06:40:02 +00:00
|
|
|
@parameterize("shard_strategy_class", [TensorShardStrategy, BucketTensorShardStrategy])
|
2022-03-25 04:24:18 +00:00
|
|
|
def run_model_test(init_device_type, shard_strategy_class):
|
2022-03-28 08:38:18 +00:00
|
|
|
logger = get_dist_logger("test_zero_init")
|
|
|
|
|
2022-12-26 09:35:36 +00:00
|
|
|
for name, get_components_func in non_distributed_component_funcs._registry.items():
|
|
|
|
# because the ZeroInitContext automatically turns parameters to fp16
|
|
|
|
# and the beit model use tensor.erfinv_() function to initialize weights
|
|
|
|
# tensor.erfinv_() doesn't support Half in CPU, we omit the beit model
|
|
|
|
if name == 'beit':
|
|
|
|
continue
|
2022-03-08 06:45:01 +00:00
|
|
|
model_builder, _, _, _, _ = get_components_func()
|
2022-03-25 04:24:18 +00:00
|
|
|
if init_device_type == 'cuda':
|
2022-04-11 08:47:57 +00:00
|
|
|
init_device = get_current_device()
|
2022-03-25 04:24:18 +00:00
|
|
|
elif init_device_type == 'cpu':
|
|
|
|
init_device = torch.device("cpu")
|
|
|
|
else:
|
|
|
|
continue
|
2022-03-28 08:38:18 +00:00
|
|
|
|
|
|
|
model_numel_tensor = torch.zeros(1, dtype=torch.int)
|
2022-03-29 01:09:04 +00:00
|
|
|
with ZeroInitContext(target_device=init_device,
|
2022-03-18 08:18:31 +00:00
|
|
|
shard_strategy=shard_strategy_class(),
|
2022-03-10 08:31:02 +00:00
|
|
|
shard_param=True,
|
2022-04-07 09:38:45 +00:00
|
|
|
model_numel_tensor=model_numel_tensor):
|
2022-03-08 06:45:01 +00:00
|
|
|
model = model_builder(checkpoint=True)
|
|
|
|
|
2022-03-08 10:18:06 +00:00
|
|
|
for param in model.parameters():
|
2022-03-31 04:25:45 +00:00
|
|
|
assert hasattr(param, 'colo_attr')
|
|
|
|
assert param.colo_attr.sharded_data_tensor.dtype == torch.half
|
|
|
|
assert param.colo_attr.sharded_data_tensor.is_sharded
|
2022-04-13 06:54:26 +00:00
|
|
|
assert param.colo_attr.data_payload.device.type == init_device.type, \
|
|
|
|
f'{param.colo_attr.data_payload.device.type} vs. {init_device.type}'
|
2022-03-28 08:38:18 +00:00
|
|
|
|
2022-04-01 01:22:33 +00:00
|
|
|
cuda_mem_use, _ = colo_model_mem_usage(model)
|
2022-03-28 08:38:18 +00:00
|
|
|
model_data_cuda_mem_MB = cuda_mem_use / 1e6
|
|
|
|
logger.info(f"Existing ZeRO Context.\nModel Data CUDA Memory {model_data_cuda_mem_MB} MB", ranks=[0])
|
2022-04-01 01:22:33 +00:00
|
|
|
sys_cuda_mem_MB = colo_device_memory_used(get_current_device()) / 1e6
|
2022-03-28 08:38:18 +00:00
|
|
|
logger.info(f"System CUDA Memory Usage {sys_cuda_mem_MB} MB", ranks=[0])
|
|
|
|
logger.info(f"Model Number Parameter {model_numel_tensor.numpy()[0]/1e6} M", ranks=[0])
|
2022-03-10 03:20:04 +00:00
|
|
|
|
2022-03-07 08:14:40 +00:00
|
|
|
|
2022-03-18 03:35:54 +00:00
|
|
|
def run_dist(rank, world_size, port):
|
|
|
|
colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
run_model_test()
|
|
|
|
|
|
|
|
|
2022-03-07 08:14:40 +00:00
|
|
|
@pytest.mark.dist
|
2022-03-10 03:20:04 +00:00
|
|
|
@pytest.mark.parametrize("world_size", [1, 4])
|
2022-04-14 16:33:04 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-03-18 03:35:54 +00:00
|
|
|
def test_zero_init_context(world_size):
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(run_dist, world_size)
|
2022-03-07 08:14:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-12-26 09:35:36 +00:00
|
|
|
test_zero_init_context(1)
|