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
|
|
|
|
from colossalai.tensor.colo_tensor import ColoTensor
|
2022-04-21 03:42:37 +00:00
|
|
|
from packaging import version
|
|
|
|
|
|
|
|
|
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-22 06:03:26 +00:00
|
|
|
|
2022-04-21 09:18:56 +00:00
|
|
|
if isinstance(bias, ColoTensor):
|
|
|
|
bias = bias.torch_tensor()
|
2022-04-21 03:42:37 +00:00
|
|
|
|
|
|
|
# Add communication logic before and after linear call.
|
2022-04-21 06:15:48 +00:00
|
|
|
if isinstance(weight, ColoTensor):
|
2022-04-22 06:03:26 +00:00
|
|
|
if weight.shard_spec == None:
|
|
|
|
return torch.nn.functional.linear(input_tensor, weight.torch_tensor(), bias)
|
|
|
|
elif weight.shard_spec == '1Drow':
|
|
|
|
# TODO(jzy): implement 1Drow TP linear here.
|
|
|
|
raise NotImplementedError
|
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
2022-04-21 03:42:37 +00:00
|
|
|
else:
|
|
|
|
return torch.nn.functional.linear(input_tensor, weight, bias)
|