mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-18 16:00:49 +00:00
[Tensor] add ColoTensor TP1Dcol Embedding (#899)
This commit is contained in:
@@ -2,3 +2,4 @@ from .linear import colo_linear
|
||||
from .element_wise import *
|
||||
from .layernorm import colo_layernorm
|
||||
from .loss import colo_cross_entropy
|
||||
from .embedding import colo_embedding
|
56
colossalai/tensor/_ops/embedding.py
Normal file
56
colossalai/tensor/_ops/embedding.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import torch
|
||||
from colossalai.tensor.op_wrapper import colo_op_impl
|
||||
from colossalai.context import ParallelMode
|
||||
from colossalai.nn.layer.parallel_1d._utils import split_forward_gather_backward, reduce_input, \
|
||||
gather_forward_split_backward, reduce_grad
|
||||
from colossalai.nn.layer.utils import divide
|
||||
from colossalai.core import global_context as gpc
|
||||
from packaging import version
|
||||
from colossalai.tensor import ComputePattern, TensorSpec, ComputePattern, ParallelAction, ColoTensor, ShardPattern
|
||||
|
||||
def colo_embedding_1Dcol(input_tensor: ColoTensor, weight: ColoTensor, args, kwargs) -> ColoTensor:
|
||||
# embedding_1Dcol split the weight(lookup table)
|
||||
# Gather splitted lookup table
|
||||
parallel_action = weight.shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DCol_Embedding)
|
||||
if not input_tensor.is_gathered():
|
||||
input_tensor.gather()
|
||||
|
||||
output_parallel = torch.nn.functional.embedding(input_tensor.torch_tensor(), weight.torch_tensor(),
|
||||
*args, **kwargs)
|
||||
output = ColoTensor.init_from_torch_tensor(output_parallel)
|
||||
out_parallel_action_list = [ParallelAction(priority=1, parallel_mode=parallel_action.parallel_mode)]
|
||||
output_spec = TensorSpec(out_parallel_action_list)
|
||||
output.set_spec(output_spec, shard=False)
|
||||
output.set_shard_pattern(ShardPattern.Col)
|
||||
output.gather()
|
||||
return output
|
||||
|
||||
@colo_op_impl(torch.nn.functional.embedding)
|
||||
def colo_embedding(types, args, kwargs, pg):
|
||||
"""Handles ``__torch_function__`` dispatch for ``torch.nn.functional.embedding``.
|
||||
This method looks up an embedding table.
|
||||
"""
|
||||
input_tensor = args[0]
|
||||
weight = args[1]
|
||||
args = args[2:]
|
||||
|
||||
if not isinstance(input_tensor, ColoTensor):
|
||||
input_tensor = ColoTensor.init_from_torch_tensor(input_tensor)
|
||||
|
||||
if not isinstance(weight, ColoTensor):
|
||||
weight = ColoTensor.init_from_torch_tensor(weight)
|
||||
|
||||
# Handle differen parallel actions.
|
||||
if not weight.has_spec(): # No Model Parallel Applied
|
||||
input_tensor = input_tensor.torch_tensor()
|
||||
weight = weight.torch_tensor()
|
||||
output = torch.nn.functional.embedding(input_tensor, weight, *args, **kwargs)
|
||||
return ColoTensor.init_from_torch_tensor(output)
|
||||
elif weight.shard_spec.num_action == 1: # Single Model Parallel Applied
|
||||
compute_patterns = weight.shard_spec.compute_patterns
|
||||
if ComputePattern.TP1DCol_Embedding in compute_patterns:
|
||||
return colo_embedding_1Dcol(input_tensor, weight, args, kwargs)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
else:
|
||||
raise NotImplementedError
|
@@ -27,7 +27,7 @@ def colo_layernorm(types, args=(), kwargs=None, pg=None):
|
||||
eps = kwargs['eps']
|
||||
|
||||
if isinstance(input_tensor, ColoTensor):
|
||||
if input_tensor.is_activation() and not input_tensor.is_gathered():
|
||||
if not input_tensor.is_gathered():
|
||||
input_tensor.gather()
|
||||
input_tensor = input_tensor.torch_tensor()
|
||||
if isinstance(weight, ColoTensor):
|
||||
|
@@ -9,8 +9,8 @@ from packaging import version
|
||||
from colossalai.tensor import ComputePattern, TensorSpec, ComputePattern, ParallelAction, ColoTensor, ShardPattern
|
||||
|
||||
|
||||
def colo_linear_1Drow(input_tensor: ColoTensor, weight: ColoTensor, bias: ColoTensor) -> ColoTensor:
|
||||
parallel_action = weight.shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DRow)
|
||||
def colo_linear_1Drow(input_tensor: ColoTensor, weight: ColoTensor, bias:ColoTensor) -> ColoTensor:
|
||||
parallel_action = weight.shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DRow_Linear)
|
||||
# Input:S[1] x Weight:S[0] = Output:P
|
||||
# All-Reduce(Output) + bias = res
|
||||
# Input:S[1]
|
||||
@@ -47,7 +47,7 @@ def colo_linear_1Dcol(input_tensor: ColoTensor, weight: ColoTensor, bias: ColoTe
|
||||
# Input:B x Weight:S[1] + Bias:S[1] = Output:S[1]
|
||||
# All-Gather(Output)
|
||||
# Input:B
|
||||
parallel_action = weight.shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DCol)
|
||||
parallel_action = weight.shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DCol_Linear)
|
||||
if input_tensor.is_gathered():
|
||||
# Not splited yet.
|
||||
assert input_tensor.shape[-1] == weight.size(-1), \
|
||||
@@ -108,9 +108,9 @@ def colo_linear(types, args, kwargs, pg):
|
||||
return ColoTensor.init_from_torch_tensor(torch.nn.functional.linear(input_tensor, weight, bias))
|
||||
elif weight.shard_spec.num_action == 1: # Single Model Parallel Applied
|
||||
compute_patterns = weight.shard_spec.compute_patterns
|
||||
if ComputePattern.TP1DRow in compute_patterns:
|
||||
if ComputePattern.TP1DRow_Linear in compute_patterns:
|
||||
return colo_linear_1Drow(input_tensor, weight, bias)
|
||||
elif ComputePattern.TP1DCol in compute_patterns:
|
||||
elif ComputePattern.TP1DCol_Linear in compute_patterns:
|
||||
return colo_linear_1Dcol(input_tensor, weight, bias)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
@@ -142,14 +142,19 @@ class ColoTensor(object):
|
||||
if self._shard_pattern is not ShardPattern.NA: # reshard
|
||||
self.gather()
|
||||
# Model Parameters
|
||||
if ComputePattern.TP1DRow in self._shard_spec.compute_patterns:
|
||||
parallel_action = self._shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DRow)
|
||||
self._shard_1d(parallel_action=parallel_action, dim=-1)
|
||||
self._shard_pattern = ShardPattern.Col # We bind our ComputePattern on weight, which has to be transposed when linear().
|
||||
elif ComputePattern.TP1DCol in self._shard_spec.compute_patterns:
|
||||
parallel_action = self._shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DCol)
|
||||
self._shard_1d(parallel_action=parallel_action, dim=0)
|
||||
self._shard_pattern = ShardPattern.Row
|
||||
if self._shard_spec.num_action == 1:
|
||||
parallel_action = self._shard_spec.get_action_by_compute_pattern(
|
||||
self._shard_spec.compute_patterns[0])
|
||||
if parallel_action.compute_pattern in [ComputePattern.TP1DRow_Linear, \
|
||||
ComputePattern.TP1DCol_Embedding]:
|
||||
self._shard_1d(parallel_action=parallel_action, dim=-1)
|
||||
self._shard_pattern = ShardPattern.Col # We bind our ComputePattern on weight, which has to be transposed when linear().
|
||||
elif parallel_action.compute_pattern in [ComputePattern.TP1DCol_Linear, \
|
||||
ComputePattern.TP1DRow_Embedding]:
|
||||
self._shard_1d(parallel_action=parallel_action, dim=0)
|
||||
self._shard_pattern = ShardPattern.Row
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
def gather(self):
|
||||
assert self.is_activation(), 'Currently we only support gather Activation ColoTensor.'
|
||||
|
@@ -4,10 +4,12 @@ from colossalai.context.parallel_mode import ParallelMode
|
||||
|
||||
|
||||
class ComputePattern(Enum):
|
||||
TP1DRow = 1
|
||||
TP1DCol = 2
|
||||
ZeRO = 3
|
||||
DP = 4
|
||||
TP1DRow_Linear = 1
|
||||
TP1DCol_Linear = 2
|
||||
TP1DRow_Embedding = 3
|
||||
TP1DCol_Embedding = 4
|
||||
ZeRO = 5
|
||||
DP = 6
|
||||
|
||||
|
||||
class ShardPattern(Enum):
|
||||
@@ -43,14 +45,14 @@ class TensorSpec(object):
|
||||
# using ZeRO with DP-degree = 4 and 1DRowTP with TP-degree = 2.
|
||||
# parallel_action_list = [
|
||||
# ParallelAction(10, ComputePattern.ZeRO, gpc.get_group(ParallelMode.DATA)),
|
||||
# ParallelAction(1, ComputePattern.TP1DRow, gpc.get_group(ParallelMode.PARALLEL_1D))
|
||||
# ParallelAction(1, ComputePattern.TP1DRow_Linear, gpc.get_group(ParallelMode.PARALLEL_1D))
|
||||
# ]
|
||||
# When the ColoTensor is initialized,
|
||||
# we first splitting tensor according to ParallelAction of ZeRO,
|
||||
# then splitting tensor according to ParallelAction of TP1DRow.
|
||||
# then splitting tensor according to ParallelAction of TP1DRow_Linear.
|
||||
# During Linear computation
|
||||
# Before Linear Op, we gather the tensors according to ZeRO.
|
||||
# We perform Linear Op according to compute pattern of TP1DRow.
|
||||
# We perform Linear Op according to compute pattern of TP1DRow_Linear.
|
||||
# After Linear Op, we split the tensors according to ZeRO.
|
||||
|
||||
def __init__(self, parallel_action_list: List[ParallelAction] = [], shard_pattern: ShardPattern = ShardPattern.NA):
|
||||
|
Reference in New Issue
Block a user