2022-03-04 03:59:35 +00:00
|
|
|
from typing import List, Optional
|
2022-03-04 07:35:07 +00:00
|
|
|
|
2022-03-08 10:18:06 +00:00
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
2022-03-18 08:18:31 +00:00
|
|
|
from colossalai.utils import get_current_device
|
2022-03-04 07:35:07 +00:00
|
|
|
from colossalai.zero.shard_utils import BaseShardStrategy
|
|
|
|
from colossalai.zero.sharded_model._zero3_utils import get_shard
|
2022-03-08 10:18:06 +00:00
|
|
|
from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor
|
2022-03-04 03:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TensorShardStrategy(BaseShardStrategy):
|
2022-03-18 08:48:20 +00:00
|
|
|
"""A naive implementation which shard each tensor evenly over all ranks
|
|
|
|
"""
|
2022-03-04 03:59:35 +00:00
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
def shard(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None):
|
2022-03-04 03:59:35 +00:00
|
|
|
for t in tensor_list:
|
2022-03-18 08:18:31 +00:00
|
|
|
self._shard_tensor(t, process_group)
|
2022-03-04 03:59:35 +00:00
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
def gather(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None):
|
2022-03-04 03:59:35 +00:00
|
|
|
for t in tensor_list:
|
2022-03-18 08:18:31 +00:00
|
|
|
self._gather_tensor(t, process_group)
|
2022-03-04 07:35:07 +00:00
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
def _shard_tensor(self, t: ShardedTensor, process_group: Optional[dist.ProcessGroup] = None):
|
2022-03-04 07:35:07 +00:00
|
|
|
if t.is_sharded:
|
|
|
|
return
|
2022-03-25 04:24:18 +00:00
|
|
|
if t.payload.device.type == 'cuda':
|
|
|
|
assert t.payload.device.index == get_current_device(), f"shard tensor on cuda device index {t.payload.device.index},"\
|
|
|
|
f" but current cuda device is {get_current_device()}"
|
2022-03-18 08:18:31 +00:00
|
|
|
sharded_payload, _ = get_shard(t.payload, dist.get_rank(process_group), dist.get_world_size(process_group))
|
2022-03-04 07:35:07 +00:00
|
|
|
t.reset_payload(sharded_payload)
|
|
|
|
t.is_sharded = True
|
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
def _gather_tensor(self, t: ShardedTensor, process_group: Optional[dist.ProcessGroup] = None):
|
2022-03-04 07:35:07 +00:00
|
|
|
if not t.is_sharded:
|
|
|
|
return
|
2022-03-10 06:08:58 +00:00
|
|
|
target_device = t.device
|
2022-03-04 07:35:07 +00:00
|
|
|
buffer_list = []
|
|
|
|
payload_numel = t.payload.numel()
|
2022-03-18 08:18:31 +00:00
|
|
|
world_size = dist.get_world_size(process_group)
|
|
|
|
rank = dist.get_rank(process_group)
|
|
|
|
for i in range(world_size):
|
|
|
|
if i == rank:
|
2022-03-11 10:12:46 +00:00
|
|
|
buffer_list.append(t.payload.cuda(get_current_device()))
|
2022-03-04 07:35:07 +00:00
|
|
|
else:
|
2022-03-11 10:12:46 +00:00
|
|
|
buffer_list.append(torch.zeros(payload_numel, dtype=t.dtype, device=get_current_device()))
|
2022-03-04 07:35:07 +00:00
|
|
|
|
2022-03-18 08:18:31 +00:00
|
|
|
dist.all_gather(buffer_list, buffer_list[rank], group=process_group, async_op=False)
|
2022-03-04 07:35:07 +00:00
|
|
|
gathered_payload = torch.narrow(torch.cat(buffer_list), 0, 0, t.origin_numel).reshape(t.origin_shape)
|
|
|
|
t.reset_payload(gathered_payload)
|
2022-03-10 06:08:58 +00:00
|
|
|
t.to(target_device)
|
2022-03-04 07:35:07 +00:00
|
|
|
t.is_sharded = False
|