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
|
2022-03-25 06:54:39 +00:00
|
|
|
from colossalai.zero.shard_utils.commons import get_shard
|
2022-03-08 10:18:06 +00:00
|
|
|
from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor
|
2022-04-24 05:08:48 +00:00
|
|
|
from colossalai.gemini.tensor_utils import colo_model_data_tensor_move_inline
|
2022-03-04 03:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TensorShardStrategy(BaseShardStrategy):
|
2022-03-25 10:03:32 +00:00
|
|
|
"""
|
|
|
|
A naive implementation which shard each tensor evenly over all ranks
|
2022-03-18 08:48:20 +00:00
|
|
|
"""
|
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-25 10:03:32 +00:00
|
|
|
""" Shard tensor among processes.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
t (ShardedTensor): a tensor to be sharded.
|
|
|
|
process_group (Optional[dist.ProcessGroup], optional): the process group among which tensor shards.
|
|
|
|
Defaults to 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':
|
2022-04-11 08:47:57 +00:00
|
|
|
assert t.payload.device == get_current_device(), f"shard tensor on cuda device index {t.payload.device.index},"\
|
2022-03-25 04:24:18 +00:00
|
|
|
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-04-24 05:08:48 +00:00
|
|
|
t.payload_reset(sharded_payload)
|
2022-03-04 07:35:07 +00:00
|
|
|
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
|
|
|
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)
|
2022-04-15 06:41:31 +00:00
|
|
|
|
|
|
|
buffer = torch.empty(payload_numel * world_size, dtype=t.payload.dtype, device=get_current_device())
|
|
|
|
buffer_list = list(torch.chunk(buffer, chunks=world_size, dim=0))
|
|
|
|
buffer_list[rank].copy_(t.payload)
|
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-04-15 06:41:31 +00:00
|
|
|
gathered_payload = torch.narrow(buffer, 0, 0, t.origin_numel).reshape(t.origin_shape)
|
2022-04-24 05:08:48 +00:00
|
|
|
t.payload_reset(gathered_payload)
|
2022-03-29 07:45:48 +00:00
|
|
|
colo_model_data_tensor_move_inline(t, target_device)
|
2022-03-04 07:35:07 +00:00
|
|
|
t.is_sharded = False
|