2022-03-04 07:49:23 +00:00
|
|
|
from copy import deepcopy
|
2022-03-01 10:17:01 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2022-03-08 10:18:06 +00:00
|
|
|
import colossalai
|
2022-03-01 10:17:01 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
import torch.multiprocessing as mp
|
2022-04-14 16:33:04 +00:00
|
|
|
from colossalai.testing import parameterize, rerun_if_address_is_in_use
|
2022-03-01 10:17:01 +00:00
|
|
|
from colossalai.utils import free_port
|
2022-03-14 07:06:02 +00:00
|
|
|
from colossalai.zero.shard_utils import (BucketTensorShardStrategy, TensorShardStrategy)
|
2022-03-22 06:36:16 +00:00
|
|
|
from colossalai.zero.sharded_param import ShardedTensor
|
2022-03-08 10:18:06 +00:00
|
|
|
from colossalai.zero.sharded_param.sharded_param import ShardedParamV2
|
2022-04-12 06:57:54 +00:00
|
|
|
from tests.test_zero.common import CONFIG, allclose
|
2022-04-24 05:08:48 +00:00
|
|
|
from colossalai.gemini.stateful_tensor import StatefulTensor
|
2022-03-01 10:17:01 +00:00
|
|
|
|
2022-03-03 04:42:57 +00:00
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
@parameterize("shard_strategy_class", [TensorShardStrategy, BucketTensorShardStrategy])
|
|
|
|
def run_shard_tensor_with_strategy(shard_strategy_class, world_size):
|
2022-03-04 02:46:13 +00:00
|
|
|
t = ShardedTensor(tensor=torch.randn(world_size * 2, 3))
|
2022-03-04 07:35:07 +00:00
|
|
|
assert list(t.origin_shape) == [world_size * 2, 3]
|
2022-03-04 02:46:13 +00:00
|
|
|
assert list(t.shape) == [world_size * 2, 3]
|
2022-03-04 07:35:07 +00:00
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
shard_strategy = shard_strategy_class()
|
2022-03-04 02:46:13 +00:00
|
|
|
|
2022-03-04 03:59:35 +00:00
|
|
|
# test shard strategy
|
|
|
|
shard_strategy.shard([t])
|
2022-03-04 07:35:07 +00:00
|
|
|
assert list(t.shape) == [6], f"{list(t.shape)} vs 6"
|
2022-03-04 03:59:35 +00:00
|
|
|
shard_strategy.gather([t])
|
2022-03-04 07:35:07 +00:00
|
|
|
assert list(t.shape) == [world_size * 2, 3], f"{list(t.shape)} vs {[world_size * 2, 3]}"
|
2022-03-04 02:46:13 +00:00
|
|
|
|
|
|
|
|
2022-03-18 03:35:54 +00:00
|
|
|
def _run_shard_tensor(rank, world_size, port):
|
|
|
|
colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
run_shard_tensor_with_strategy(world_size=world_size)
|
|
|
|
|
|
|
|
|
2022-03-04 02:46:13 +00:00
|
|
|
@pytest.mark.dist
|
2022-03-08 04:03:35 +00:00
|
|
|
@pytest.mark.parametrize("world_size", [1, 2])
|
2022-04-14 16:33:04 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-03-18 03:35:54 +00:00
|
|
|
def test_shard_tensor(world_size):
|
|
|
|
run_func = partial(_run_shard_tensor, world_size=world_size, port=free_port())
|
2022-03-04 02:46:13 +00:00
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
|
|
|
|
|
|
|
|
2022-03-04 07:49:23 +00:00
|
|
|
def _run_shard_param_v2(rank, world_size, port):
|
2022-03-03 04:42:57 +00:00
|
|
|
colossalai.launch(config=CONFIG, rank=rank, world_size=world_size, host='localhost', port=port, backend='nccl')
|
|
|
|
|
2022-03-04 07:49:23 +00:00
|
|
|
param = torch.nn.Parameter(torch.randn(2, 3))
|
|
|
|
param_ref = deepcopy(param)
|
2022-03-30 07:57:46 +00:00
|
|
|
sparam = ShardedParamV2(param=param)
|
2022-03-04 07:49:23 +00:00
|
|
|
|
2022-04-13 06:54:26 +00:00
|
|
|
allclose(sparam.data_payload, param_ref.data)
|
2022-03-08 06:45:01 +00:00
|
|
|
|
2022-03-28 07:01:21 +00:00
|
|
|
# Test get memory usage
|
2022-03-30 10:14:50 +00:00
|
|
|
sparam.saved_grad = StatefulTensor(torch.randn(2, 3))
|
2022-03-28 07:01:21 +00:00
|
|
|
cuda_mem_use, cpu_mem_use = sparam.get_memory_usage()
|
2022-03-28 08:19:19 +00:00
|
|
|
assert cpu_mem_use == 2 * 3 * 4 * 2, f"cpu_mem_use: {cpu_mem_use}"
|
|
|
|
|
2022-04-13 06:54:26 +00:00
|
|
|
sparam.set_data_none()
|
2022-04-08 12:23:26 +00:00
|
|
|
assert (param.data.numel() == 0)
|
2022-03-28 08:19:19 +00:00
|
|
|
cuda_mem_use, cpu_mem_use = sparam.get_memory_usage()
|
|
|
|
# 4 is size of dummy tensor of param.data
|
2022-04-08 12:23:26 +00:00
|
|
|
assert cpu_mem_use == 2 * 3 * 4 * 2
|
2022-03-28 07:01:21 +00:00
|
|
|
|
2022-03-30 10:14:50 +00:00
|
|
|
sparam.saved_grad = StatefulTensor(torch.randn(2, 3))
|
2022-04-13 06:54:26 +00:00
|
|
|
sparam.set_data_none()
|
2022-03-28 07:01:21 +00:00
|
|
|
cuda_mem_use, cpu_mem_use = sparam.get_memory_usage()
|
2022-04-08 12:23:26 +00:00
|
|
|
assert cpu_mem_use == 2 * 3 * 4 * 2
|
2022-03-28 08:19:19 +00:00
|
|
|
assert cuda_mem_use == 0
|
|
|
|
|
|
|
|
# append a grad to torch param
|
2022-04-13 06:54:26 +00:00
|
|
|
param.data = sparam.data_payload
|
2022-03-28 08:19:19 +00:00
|
|
|
param.grad = torch.randn(2, 3)
|
|
|
|
cuda_mem_use, cpu_mem_use = sparam.get_memory_usage()
|
|
|
|
assert cpu_mem_use == 2 * 3 * 4 * 2 + 2 * 3 * 4, f"cpu_mem_use {cpu_mem_use}"
|
|
|
|
assert cuda_mem_use == 0
|
|
|
|
|
|
|
|
# reuse torch grad for sparam
|
2022-03-30 10:14:50 +00:00
|
|
|
sparam.saved_grad = StatefulTensor(param.grad)
|
2022-03-28 08:19:19 +00:00
|
|
|
cuda_mem_use, cpu_mem_use = sparam.get_memory_usage()
|
2022-03-28 07:01:21 +00:00
|
|
|
assert cpu_mem_use == 2 * 3 * 4 * 2
|
|
|
|
assert cuda_mem_use == 0
|
|
|
|
|
2022-03-03 04:42:57 +00:00
|
|
|
|
2022-03-04 07:49:23 +00:00
|
|
|
@pytest.mark.dist
|
2022-03-08 04:03:35 +00:00
|
|
|
@pytest.mark.parametrize("world_size", [1, 2])
|
2022-04-14 16:33:04 +00:00
|
|
|
@rerun_if_address_is_in_use()
|
2022-03-08 04:03:35 +00:00
|
|
|
def test_shard_param_v2(world_size):
|
2022-03-04 07:49:23 +00:00
|
|
|
run_func = partial(_run_shard_param_v2, world_size=world_size, port=free_port())
|
|
|
|
mp.spawn(run_func, nprocs=world_size)
|
2022-03-03 04:42:57 +00:00
|
|
|
|
2022-03-04 07:49:23 +00:00
|
|
|
|
2022-03-01 10:17:01 +00:00
|
|
|
if __name__ == '__main__':
|
2022-03-28 07:01:21 +00:00
|
|
|
# test_shard_tensor(2)
|
2022-03-08 04:03:35 +00:00
|
|
|
test_shard_param_v2(2)
|