|
| 1 | +from typing import Tuple |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.distributed as dist |
| 5 | +from torch import Tensor |
| 6 | + |
| 7 | +from .core import (ensure_divisibility, get_tensor_model_parallel_group, |
| 8 | + get_tensor_model_parallel_rank, |
| 9 | + get_tensor_model_parallel_world_size) |
| 10 | + |
| 11 | + |
| 12 | +def divide(numerator, denominator): |
| 13 | + ensure_divisibility(numerator, denominator) |
| 14 | + return numerator // denominator |
| 15 | + |
| 16 | + |
| 17 | +def _reduce(tensor: Tensor) -> Tensor: |
| 18 | + if dist.get_world_size() == 1: |
| 19 | + return tensor |
| 20 | + |
| 21 | + dist.all_reduce(tensor, |
| 22 | + op=dist.ReduceOp.SUM, |
| 23 | + group=get_tensor_model_parallel_group(), |
| 24 | + async_op=False) |
| 25 | + |
| 26 | + return tensor |
| 27 | + |
| 28 | + |
| 29 | +def _split(tensor: Tensor, dim: int = -1) -> Tensor: |
| 30 | + if get_tensor_model_parallel_world_size() == 1: |
| 31 | + return tensor |
| 32 | + |
| 33 | + split_size = divide(tensor.shape[dim], get_tensor_model_parallel_world_size()) |
| 34 | + tensor_list = torch.split(tensor, split_size, dim=dim) |
| 35 | + |
| 36 | + output = tensor_list[get_tensor_model_parallel_rank()].contiguous() |
| 37 | + |
| 38 | + return output |
| 39 | + |
| 40 | + |
| 41 | +def _gather(tensor: Tensor, dim: int = -1) -> Tensor: |
| 42 | + if get_tensor_model_parallel_world_size() == 1: |
| 43 | + return tensor |
| 44 | + |
| 45 | + if dim == 1 and list(tensor.shape)[0] == 1: |
| 46 | + output_shape = list(tensor.shape) |
| 47 | + output_shape[1] *= get_tensor_model_parallel_world_size() |
| 48 | + output = torch.empty(output_shape, dtype=tensor.dtype, device=tensor.device) |
| 49 | + tensor_list = output.chunk(get_tensor_model_parallel_world_size(), dim=1) |
| 50 | + dist.all_gather(list(tensor_list), |
| 51 | + tensor, |
| 52 | + group=get_tensor_model_parallel_group(), |
| 53 | + async_op=False) |
| 54 | + else: |
| 55 | + tensor_list = [ |
| 56 | + torch.empty_like(tensor) for _ in range(get_tensor_model_parallel_world_size()) |
| 57 | + ] |
| 58 | + dist.all_gather(tensor_list, |
| 59 | + tensor, |
| 60 | + group=get_tensor_model_parallel_group(), |
| 61 | + async_op=False) |
| 62 | + output = torch.cat(tensor_list, dim=dim) |
| 63 | + |
| 64 | + return output |
| 65 | + |
| 66 | + |
| 67 | +def copy(input: Tensor) -> Tensor: |
| 68 | + if torch.is_grad_enabled() and input.requires_grad: |
| 69 | + input = Copy.apply(input) |
| 70 | + return input |
| 71 | + |
| 72 | + |
| 73 | +class Copy(torch.autograd.Function): |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def forward(ctx: "Copy", input: Tensor) -> Tensor: |
| 77 | + return input |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def backward(ctx: "Copy", grad_output: Tensor) -> Tensor: |
| 81 | + return _reduce(grad_output) |
| 82 | + |
| 83 | + |
| 84 | +def scatter(input: Tensor, dim: int = -1) -> Tensor: |
| 85 | + if torch.is_grad_enabled() and input.requires_grad: |
| 86 | + input = Scatter.apply(input, dim) |
| 87 | + else: |
| 88 | + input = _split(input, dim=dim) |
| 89 | + return input |
| 90 | + |
| 91 | + |
| 92 | +class Scatter(torch.autograd.Function): |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + def forward(ctx: "Scatter", input: Tensor, dim: int = -1) -> Tensor: |
| 96 | + ctx.save_for_backward(torch.tensor([dim])) |
| 97 | + return _split(input, dim=dim) |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def backward(ctx: "Scatter", grad_output: Tensor) -> Tuple[Tensor]: |
| 101 | + dim, = ctx.saved_tensors |
| 102 | + return _gather(grad_output, dim=int(dim)), None |
| 103 | + |
| 104 | + |
| 105 | +def reduce(input: Tensor) -> Tensor: |
| 106 | + if torch.is_grad_enabled() and input.requires_grad: |
| 107 | + input = Reduce.apply(input) |
| 108 | + else: |
| 109 | + input = _reduce(input) |
| 110 | + return input |
| 111 | + |
| 112 | + |
| 113 | +class Reduce(torch.autograd.Function): |
| 114 | + |
| 115 | + @staticmethod |
| 116 | + def forward(ctx: "Reduce", input: Tensor) -> Tensor: |
| 117 | + return _reduce(input) |
| 118 | + |
| 119 | + @staticmethod |
| 120 | + def backward(ctx: "Reduce", grad_output: Tensor) -> Tensor: |
| 121 | + return grad_output |
| 122 | + |
| 123 | + |
| 124 | +def gather(input: Tensor, dim: int = -1) -> Tensor: |
| 125 | + if torch.is_grad_enabled() and input.requires_grad: |
| 126 | + input = Gather.apply(input, dim) |
| 127 | + else: |
| 128 | + input = _gather(input, dim=dim) |
| 129 | + return input |
| 130 | + |
| 131 | + |
| 132 | +class Gather(torch.autograd.Function): |
| 133 | + |
| 134 | + @staticmethod |
| 135 | + def forward(ctx: "Gather", input: Tensor, dim: int = -1) -> Tensor: |
| 136 | + ctx.save_for_backward(torch.tensor([dim])) |
| 137 | + return _gather(input, dim=dim) |
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def backward(ctx: "Gather", grad_output: Tensor) -> Tuple[Tensor]: |
| 141 | + dim, = ctx.saved_tensors |
| 142 | + return _split(grad_output, dim=int(dim)), None |
| 143 | + |
| 144 | + |
| 145 | +def _all_to_all(tensor: Tensor, in_dim: int = -1, out_dim: int = -1) -> Tensor: |
| 146 | + if dist.get_world_size() == 1: |
| 147 | + return tensor |
| 148 | + |
| 149 | + tensor = tensor.transpose(in_dim, 0).contiguous() |
| 150 | + |
| 151 | + output = torch.empty_like(tensor) |
| 152 | + dist.all_to_all_single(output, tensor, group=get_tensor_model_parallel_group()) |
| 153 | + |
| 154 | + output = output.transpose(in_dim, 0).contiguous() |
| 155 | + |
| 156 | + tensor_list = output.chunk(get_tensor_model_parallel_world_size(), dim=in_dim) |
| 157 | + |
| 158 | + return torch.cat(tensor_list, dim=out_dim) |
| 159 | + |
| 160 | + |
| 161 | +def col_to_row(input_: Tensor) -> Tensor: |
| 162 | + if torch.is_grad_enabled() and input_.requires_grad: |
| 163 | + input_ = All_to_All.apply(input_, 1, 2) |
| 164 | + else: |
| 165 | + input_ = _all_to_all(input_, in_dim=1, out_dim=2) |
| 166 | + return input_ |
| 167 | + |
| 168 | + |
| 169 | +def row_to_col(input_: Tensor) -> Tensor: |
| 170 | + if torch.is_grad_enabled() and input_.requires_grad: |
| 171 | + input_ = All_to_All.apply(input_, 2, 1) |
| 172 | + else: |
| 173 | + input_ = _all_to_all(input_, in_dim=2, out_dim=1) |
| 174 | + return input_ |
| 175 | + |
| 176 | + |
| 177 | +class All_to_All(torch.autograd.Function): |
| 178 | + |
| 179 | + @staticmethod |
| 180 | + def forward(ctx: "All_to_All", input_: Tensor, in_dim: int = -1, out_dim: int = -1) -> Tensor: |
| 181 | + ctx.save_for_backward(torch.tensor([in_dim, out_dim])) |
| 182 | + return _all_to_all(input_, in_dim=in_dim, out_dim=out_dim) |
| 183 | + |
| 184 | + @staticmethod |
| 185 | + def backward(ctx: "All_to_All", grad_output: Tensor) -> Tuple[Tensor]: |
| 186 | + saved_tensors = ctx.saved_tensors[0] |
| 187 | + return _all_to_all(grad_output, in_dim=int(saved_tensors[1]), |
| 188 | + out_dim=int(saved_tensors[0])), None, None |
0 commit comments