2022-03-04 07:49:23 +00:00
|
|
|
from copy import deepcopy
|
2022-03-01 10:17:01 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
2023-04-06 01:38:25 +00:00
|
|
|
from common import CONFIG, allclose
|
2023-04-04 05:48:16 +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-04-04 05:48:16 +00:00
|
|
|
from colossalai.zero.legacy.gemini.stateful_tensor import StatefulTensor
|
|
|
|
from colossalai.zero.legacy.shard_utils import BucketTensorShardStrategy, TensorShardStrategy
|
|
|
|
from colossalai.zero.legacy.sharded_param import ShardedTensor
|
|
|
|
from colossalai.zero.legacy.sharded_param.sharded_param import ShardedParamV2
|
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):
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(_run_shard_tensor, world_size)
|
2022-03-04 02:46:13 +00:00
|
|
|
|
|
|
|
|
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):
|
2023-04-06 06:51:35 +00:00
|
|
|
spawn(_run_shard_param_v2, 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)
|