2022-05-19 04:44:59 +00:00
|
|
|
from typing import List, Optional
|
2022-07-06 08:15:16 +00:00
|
|
|
import torch.nn.functional as F
|
2022-04-25 03:49:20 +00:00
|
|
|
from colossalai.tensor.op_wrapper import colo_op_impl
|
2022-07-11 07:51:48 +00:00
|
|
|
from colossalai.tensor import ColoTensor, distspec, ColoTensorSpec, ReplicaSpec
|
2022-05-19 04:44:59 +00:00
|
|
|
from ._utils import GeneralTensor, convert_to_colo_tensor
|
2022-04-25 03:49:20 +00:00
|
|
|
|
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
@colo_op_impl(F.layer_norm)
|
|
|
|
def colo_layernorm(
|
|
|
|
input_tensor: GeneralTensor,
|
|
|
|
normalized_shape: List[int],
|
|
|
|
weight: Optional[GeneralTensor] = None,
|
|
|
|
bias: Optional[GeneralTensor] = None,
|
|
|
|
eps: float = 1e-5,
|
|
|
|
):
|
2022-07-06 08:15:16 +00:00
|
|
|
assert isinstance(weight, ColoTensor)
|
|
|
|
input_tensor = convert_to_colo_tensor(input_tensor, weight.get_process_group())
|
|
|
|
bias = convert_to_colo_tensor(bias, weight.get_process_group())
|
2022-07-11 07:51:48 +00:00
|
|
|
input_tensor = input_tensor.redistribute(ReplicaSpec())
|
2022-04-25 03:49:20 +00:00
|
|
|
|
2022-05-19 04:44:59 +00:00
|
|
|
output = F.layer_norm(input_tensor, normalized_shape, weight=weight, bias=bias, eps=eps)
|
2022-09-08 07:15:52 +00:00
|
|
|
output = ColoTensor.from_torch_tensor(tensor=output,
|
|
|
|
spec=ColoTensorSpec(pg=input_tensor.get_process_group(),
|
|
|
|
dist_attr=input_tensor.dist_spec))
|
2022-05-19 04:44:59 +00:00
|
|
|
return output
|