2022-04-21 03:42:37 +00:00
|
|
|
import torch
|
2022-04-21 06:15:48 +00:00
|
|
|
from colossalai.tensor.op_wrapper import colo_op_impl
|
2022-04-24 05:43:12 +00:00
|
|
|
from colossalai.context import ParallelMode
|
2022-04-27 06:13:55 +00:00
|
|
|
from colossalai.nn.layer.parallel_1d._utils import split_forward_gather_backward, reduce_input, \
|
|
|
|
gather_forward_split_backward, reduce_grad
|
2022-04-24 06:12:45 +00:00
|
|
|
from colossalai.nn.layer.utils import divide
|
|
|
|
from colossalai.core import global_context as gpc
|
2022-04-21 03:42:37 +00:00
|
|
|
from packaging import version
|
2022-04-28 02:55:40 +00:00
|
|
|
from colossalai.tensor import ComputePattern, TensorSpec, ComputePattern, ParallelAction, ColoTensor, ShardPattern
|
2022-04-26 05:23:59 +00:00
|
|
|
|
2022-04-25 02:06:53 +00:00
|
|
|
|
2022-04-28 06:43:22 +00:00
|
|
|
def colo_linear_1Drow(input_tensor: ColoTensor, weight: ColoTensor, bias: ColoTensor) -> ColoTensor:
|
2022-04-28 02:55:40 +00:00
|
|
|
parallel_action = weight.shard_spec.get_action_by_compute_pattern(ComputePattern.TP1DRow)
|
|
|
|
# Input:S[1] x Weight:S[0] = Output:P
|
|
|
|
# All-Reduce(Output) + bias = res
|
2022-04-28 06:43:22 +00:00
|
|
|
# Input:S[1]
|
2022-04-28 02:55:40 +00:00
|
|
|
if input_tensor.is_gathered():
|
|
|
|
# Not splited yet.
|
|
|
|
assert divide(input_tensor.shape[-1], gpc.tensor_parallel_size) == weight.size(-1), \
|
|
|
|
'Invalid shapes in 1Drow forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
|
|
|
input_tensor.shape, weight.size, weight.size(-1) * gpc.tensor_parallel_size)
|
2022-04-28 06:43:22 +00:00
|
|
|
input_per_partition = split_forward_gather_backward(input_tensor.torch_tensor(),
|
|
|
|
parallel_action.parallel_mode,
|
|
|
|
dim=-1)
|
2022-04-28 02:55:40 +00:00
|
|
|
elif input_tensor.shard_pattern == ShardPattern.Col:
|
|
|
|
# Splited by 1Dcol
|
|
|
|
assert input_tensor.shape[-1] == weight.size(-1), \
|
|
|
|
'Invalid shapes in 1Drow forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
|
|
|
input_tensor.shape, weight.size, weight.size(-1))
|
|
|
|
input_per_partition = input_tensor.torch_tensor()
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
# Output:P
|
|
|
|
partial_output = torch.nn.functional.linear(input_per_partition, weight.torch_tensor())
|
|
|
|
# Reduce(Output)
|
|
|
|
output = reduce_input(partial_output, parallel_action.parallel_mode)
|
|
|
|
# Bias
|
|
|
|
if bias is not None:
|
|
|
|
assert not bias.has_spec(), 'Invalid bias spec for 1Drow Linear op'
|
|
|
|
output = output + bias.torch_tensor()
|
|
|
|
output = ColoTensor.init_from_torch_tensor(output)
|
|
|
|
return output
|
|
|
|
|
2022-04-28 06:43:22 +00:00
|
|
|
|
|
|
|
def colo_linear_1Dcol(input_tensor: ColoTensor, weight: ColoTensor, bias: ColoTensor) -> ColoTensor:
|
2022-04-28 02:55:40 +00:00
|
|
|
# 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)
|
|
|
|
if input_tensor.is_gathered():
|
|
|
|
# Not splited yet.
|
|
|
|
assert input_tensor.shape[-1] == weight.size(-1), \
|
|
|
|
'Invalid shapes in 1Dcol forward: input={}, weight={}. Expected last dim of input {}.'.format(
|
|
|
|
input_tensor.shape, weight.size, weight.size(-1))
|
|
|
|
input_parallel = reduce_grad(input_tensor.torch_tensor(), parallel_action.parallel_mode)
|
|
|
|
|
|
|
|
# Bias:S[1]
|
|
|
|
if bias is not None:
|
|
|
|
assert bias.has_spec() and bias.shard_spec.num_action == 1 and \
|
|
|
|
bias.shard_pattern in [ShardPattern.Col, ShardPattern.Row], \
|
|
|
|
'Invalid bias spec for 1Dcol Linear op'
|
|
|
|
|
|
|
|
output_parallel = torch.nn.functional.linear(input_parallel, weight.torch_tensor(), bias.torch_tensor())
|
2022-04-28 06:43:22 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
output = ColoTensor.init_from_torch_tensor(output_parallel)
|
2022-04-28 06:43:22 +00:00
|
|
|
out_parallel_action_list = [ParallelAction(priority=1, parallel_mode=parallel_action.parallel_mode)]
|
2022-04-28 02:55:40 +00:00
|
|
|
output_spec = TensorSpec(out_parallel_action_list)
|
|
|
|
output.set_spec(output_spec, shard=False)
|
|
|
|
output.set_shard_pattern(ShardPattern.Col)
|
|
|
|
if parallel_action.gather_out:
|
|
|
|
# All-Gather(Output)
|
|
|
|
output.gather()
|
|
|
|
return output
|
|
|
|
|
2022-04-28 06:43:22 +00:00
|
|
|
|
2022-04-21 06:15:48 +00:00
|
|
|
@colo_op_impl(torch.nn.functional.linear)
|
|
|
|
def colo_linear(types, args, kwargs, pg):
|
2022-04-21 03:42:37 +00:00
|
|
|
"""Handles ``__torch_function__`` dispatch for ``torch.nn.functional.linear``.
|
|
|
|
This method computes a linear.
|
|
|
|
"""
|
|
|
|
input_tensor = args[0]
|
|
|
|
weight = args[1]
|
|
|
|
|
|
|
|
if version.parse(torch.__version__) > version.parse("1.11.0"):
|
|
|
|
if len(args) == 3:
|
|
|
|
bias = args[2]
|
|
|
|
else:
|
|
|
|
bias = None
|
|
|
|
else:
|
|
|
|
bias = kwargs.get('bias', None)
|
2022-04-24 05:43:12 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
if not isinstance(input_tensor, ColoTensor):
|
|
|
|
input_tensor = ColoTensor.init_from_torch_tensor(input_tensor)
|
2022-04-27 06:13:55 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
if not isinstance(weight, ColoTensor):
|
|
|
|
weight = ColoTensor.init_from_torch_tensor(weight)
|
2022-04-27 06:13:55 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
if bias is not None and not isinstance(bias, ColoTensor):
|
|
|
|
bias = ColoTensor.init_from_torch_tensor(bias)
|
2022-04-28 06:43:22 +00:00
|
|
|
|
2022-04-28 02:55:40 +00:00
|
|
|
# Add communication logic before and after linear call.
|
2022-04-28 06:43:22 +00:00
|
|
|
if not weight.has_spec(): # No Model Parallel Applied
|
2022-04-28 02:55:40 +00:00
|
|
|
assert not bias.has_spec(), 'Invalid bias spec for native Linear op'
|
|
|
|
input_tensor = input_tensor.torch_tensor()
|
|
|
|
weight = weight.torch_tensor()
|
|
|
|
bias = bias.torch_tensor()
|
|
|
|
return ColoTensor.init_from_torch_tensor(torch.nn.functional.linear(input_tensor, weight, bias))
|
2022-04-28 06:43:22 +00:00
|
|
|
elif weight.shard_spec.num_action == 1: # Single Model Parallel Applied
|
2022-04-28 02:55:40 +00:00
|
|
|
compute_patterns = weight.shard_spec.compute_patterns
|
|
|
|
if ComputePattern.TP1DRow in compute_patterns:
|
|
|
|
return colo_linear_1Drow(input_tensor, weight, bias)
|
|
|
|
elif ComputePattern.TP1DCol in compute_patterns:
|
|
|
|
return colo_linear_1Dcol(input_tensor, weight, bias)
|
2022-04-24 05:43:12 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2022-04-21 03:42:37 +00:00
|
|
|
else:
|
2022-04-28 02:55:40 +00:00
|
|
|
raise NotImplementedError
|