|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# DeepSpeed Team |
| 5 | + |
| 6 | +import torch |
| 7 | +import deepspeed.comm as dist |
| 8 | +from torch.utils._sympy.functions import FloorDiv |
| 9 | +from .sp_dp_registry import get_group, is_setup, sp_size |
| 10 | + |
| 11 | + |
| 12 | +@torch.library.custom_op("autosp::all_to_all", mutates_args=()) |
| 13 | +def all_to_all( |
| 14 | + input: torch.Tensor, |
| 15 | + scatter_idx: int, |
| 16 | + gather_idx: int, |
| 17 | + name: str, |
| 18 | +) -> torch.Tensor: |
| 19 | + """ |
| 20 | + All-to-all collective for SDPA tensors [B, N, S, H]. |
| 21 | +
|
| 22 | + For QKV (scatter_idx=1, gather_idx=2): |
| 23 | + [B, N, S/P, H] -> [B, N/P, S, H] |
| 24 | + For O (scatter_idx=2, gather_idx=1): |
| 25 | + [B, N/P, S, H] -> [B, N, S/P, H] |
| 26 | + """ |
| 27 | + assert is_setup(), 'Incorrect initialization of SP/DP mesh.' |
| 28 | + B, dim1, dim2, H = input.shape |
| 29 | + gid = dist.get_rank() // sp_size() |
| 30 | + group = get_group(gid) |
| 31 | + |
| 32 | + if scatter_idx == 1: |
| 33 | + N, local_S = dim1, dim2 |
| 34 | + input_t = input.reshape(B, sp_size(), N // sp_size(), local_S, H) |
| 35 | + input_t = input_t.permute(1, 0, 2, 3, 4).contiguous() |
| 36 | + |
| 37 | + output = torch.empty_like(input_t) |
| 38 | + dist.all_to_all_single(output, input_t, group=group) |
| 39 | + |
| 40 | + output = output.permute(1, 2, 0, 3, 4).contiguous() |
| 41 | + output = output.reshape(B, N // sp_size(), sp_size() * local_S, H) |
| 42 | + else: |
| 43 | + local_N, S = dim1, dim2 |
| 44 | + input_t = input.reshape(B, local_N, sp_size(), S // sp_size(), H) |
| 45 | + input_t = input_t.permute(2, 0, 1, 3, 4).contiguous() |
| 46 | + |
| 47 | + output = torch.empty_like(input_t) |
| 48 | + dist.all_to_all_single(output, input_t, group=group) |
| 49 | + |
| 50 | + output = output.permute(1, 0, 2, 3, 4).contiguous() |
| 51 | + output = output.reshape(B, sp_size() * local_N, S // sp_size(), H) |
| 52 | + |
| 53 | + return output |
| 54 | + |
| 55 | + |
| 56 | +@torch.library.register_fake("autosp::all_to_all") |
| 57 | +def all_to_all_fake(input: torch.Tensor, scatter_idx: int, gather_idx: int, name: str): |
| 58 | + |
| 59 | + def maybe_restore_sharded_dim(dim: torch.SymInt, factor: int): |
| 60 | + # Torch 2.9 may keep `P * (s // P)` distinct from the original `s` during |
| 61 | + # fake shape propagation. When the local dim is exactly `FloorDiv(s, P)`, |
| 62 | + # restore the original symbol so downstream ops see a consistent sequence dim. |
| 63 | + node = getattr(dim, "node", None) |
| 64 | + if node is None: |
| 65 | + return dim * factor |
| 66 | + |
| 67 | + expr = node.expr |
| 68 | + if isinstance(expr, FloorDiv) and expr.args[1] == factor: |
| 69 | + hint = node.hint * factor if node.has_hint() else None |
| 70 | + return node.shape_env.create_symintnode(expr.args[0], hint=hint) |
| 71 | + |
| 72 | + return dim * factor |
| 73 | + |
| 74 | + B, dim1, dim2, H = input.shape |
| 75 | + if scatter_idx == 1: |
| 76 | + return input.new_empty(B, dim1 // sp_size(), maybe_restore_sharded_dim(dim2, sp_size()), H) |
| 77 | + else: |
| 78 | + return input.new_empty(B, dim1 * sp_size(), dim2 // sp_size(), H) |
| 79 | + |
| 80 | + |
| 81 | +def _all_to_all_backward_setup(ctx, inputs, output): |
| 82 | + _, scatter_idx, gather_idx, name = inputs |
| 83 | + ctx.scatter_idx = gather_idx |
| 84 | + ctx.gather_idx = scatter_idx |
| 85 | + ctx.name = name + "_grad" |
| 86 | + |
| 87 | + |
| 88 | +def _all_to_all_backward(ctx, grad): |
| 89 | + return (all_to_all(grad, ctx.scatter_idx, ctx.gather_idx, ctx.name), None, None, None) |
| 90 | + |
| 91 | + |
| 92 | +torch.library.register_autograd("autosp::all_to_all", _all_to_all_backward, setup_context=_all_to_all_backward_setup) |
0 commit comments