[Diffusion] Add native SymmMem Fast Ulysses transport (#6340)
Signed-off-by: roG0d <baonudesifeizhai@gmail.com>
This commit is contained in:
1 parent
99a2ae1fc8
commit
8be601ed7f
17 files changed
+749
-6
No files matched your search
@@ -0,0 +1,213 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Fused permute-free Ulysses all-to-all over NCCL symmetric memory.
|
||||
|
||||
JIT-compiles the CUDA kernel from pytorch/pytorch#178230 (``all_to_all_permute``)
|
||||
and exposes it as two *functional* custom ops:
|
||||
|
||||
ulysses_qkv_fwd(x, group_name, world_size) # (B, S/p, H, D) -> (B, S, H/p, D)
|
||||
ulysses_o_rev(y, group_name, world_size) # (B, S, H/p, D) -> (B, S/p, H, D)
|
||||
|
||||
These replace the synchronous ``all_to_all_4D`` (permute + NCCL all_to_all_single)
|
||||
used by Ulysses SP. All symmetric-memory bookkeeping (a shape-keyed, rendezvoused
|
||||
buffer cache + the copy-in) lives *inside* the ops, so from torch.compile's point
|
||||
of view each op is an opaque ``input -> output`` function: no graph break, no
|
||||
visible buffer mutation, no Python control flow in the traced graph. A
|
||||
``register_fake`` provides output metadata so Dynamo keeps the op in-graph.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import glob
|
||||
import math
|
||||
import os
|
||||
import sysconfig
|
||||
import threading
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
import torch.distributed._symmetric_memory as symm_mem
|
||||
from torch.distributed.distributed_c10d import _resolve_process_group
|
||||
from vllm.logger import init_logger
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
_BUILD_LOCK = threading.Lock()
|
||||
_BUILT = False
|
||||
_WORKSPACE_LOCK = threading.Lock()
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _SymmWorkspace:
|
||||
allocation: torch.Tensor
|
||||
handle: Any
|
||||
capacity_bytes: int
|
||||
stream_id: int
|
||||
|
||||
|
||||
# A single grow-only byte workspace per device/process-group. Shape changes
|
||||
# reuse a typed view into the workspace instead of retaining one allocation per
|
||||
# historical request shape.
|
||||
_SYMM_WORKSPACES: dict[tuple[torch.device, str], _SymmWorkspace] = {}
|
||||
|
||||
|
||||
def _ensure_built() -> None:
|
||||
"""JIT-compile + load the kernel once (process-wide). Cached by torch."""
|
||||
global _BUILT
|
||||
if _BUILT:
|
||||
return
|
||||
with _BUILD_LOCK:
|
||||
if _BUILT:
|
||||
return
|
||||
from torch.utils.cpp_extension import load
|
||||
|
||||
here = os.path.dirname(__file__)
|
||||
src = os.path.join(here, "csrc", "a2a_permute.cu")
|
||||
site_packages = sysconfig.get_paths()["purelib"]
|
||||
nvidia_root = os.path.join(site_packages, "nvidia")
|
||||
include_paths = glob.glob(os.path.join(nvidia_root, "*", "include"))
|
||||
nccl_libs = glob.glob(os.path.join(nvidia_root, "nccl", "lib", "libnccl.so*"))
|
||||
if not nccl_libs:
|
||||
raise RuntimeError("a2a_permute: could not locate nvidia-nccl libnccl.so")
|
||||
if not any(os.path.isfile(os.path.join(path, "nccl.h")) for path in include_paths):
|
||||
raise RuntimeError("a2a_permute: could not locate nvidia-nccl nccl.h")
|
||||
load(
|
||||
name="vllm_omni_a2a_permute",
|
||||
sources=[src],
|
||||
# PyTorch wheel-only installations keep headers such as cusparse.h
|
||||
# under nvidia/{cu13,cusparse}/include rather than CUDA_HOME.
|
||||
extra_include_paths=list(include_paths),
|
||||
extra_cflags=["-DUSE_NCCL", "-DUSE_C10D_NCCL", "-O3"],
|
||||
extra_cuda_cflags=["-DUSE_NCCL", "-DUSE_C10D_NCCL", "-O3", "--expt-relaxed-constexpr"],
|
||||
extra_ldflags=[nccl_libs[0]],
|
||||
is_python_module=False,
|
||||
verbose=False,
|
||||
)
|
||||
symm_mem.set_backend("NCCL")
|
||||
logger.info("[a2a_permute] JIT kernel built and loaded; symm-mem backend=NCCL")
|
||||
_BUILT = True
|
||||
|
||||
|
||||
def ensure_a2a_permute_available() -> None:
|
||||
"""Build the extension during worker/model initialization, not a request."""
|
||||
_ensure_built()
|
||||
|
||||
|
||||
def _required_nbytes(shape: tuple[int, ...], dtype: torch.dtype) -> int:
|
||||
return math.prod(shape) * torch.empty((), dtype=dtype).element_size()
|
||||
|
||||
|
||||
def _get_symm_buffer(
|
||||
symm_shape: tuple[int, ...], dtype: torch.dtype, device: torch.device, group_name: str
|
||||
) -> torch.Tensor:
|
||||
"""Get a typed view from a grow-only symmetric-memory workspace.
|
||||
|
||||
Allocation/growth and rendezvous are collective. Every rank reaches them in
|
||||
lockstep because sequence parallel execution is symmetric. Steady-state
|
||||
requests at or below the peak capacity take no synchronization path.
|
||||
"""
|
||||
device = torch.device(device)
|
||||
key = (device, group_name)
|
||||
required_bytes = _required_nbytes(symm_shape, dtype)
|
||||
stream_id = torch.cuda.current_stream(device).cuda_stream
|
||||
with _WORKSPACE_LOCK:
|
||||
workspace = _SYMM_WORKSPACES.get(key)
|
||||
if workspace is not None and workspace.stream_id != stream_id:
|
||||
raise RuntimeError("a2a_permute workspace must be used from a single CUDA stream")
|
||||
if workspace is None or workspace.capacity_bytes < required_bytes:
|
||||
if torch.cuda.is_current_stream_capturing():
|
||||
capacity = 0 if workspace is None else workspace.capacity_bytes
|
||||
raise RuntimeError(
|
||||
"a2a_permute: cannot grow the symmetric-memory workspace "
|
||||
f"from {capacity} to {required_bytes} bytes during CUDA graph capture; "
|
||||
"warm up the maximum request shape before capture"
|
||||
)
|
||||
if workspace is None:
|
||||
# NCCL symmetric-memory rendezvous requires the communicator to
|
||||
# exist before the first allocation.
|
||||
pg = _resolve_process_group(group_name)
|
||||
warm = torch.ones(1, device=device)
|
||||
dist.all_reduce(warm, group=pg)
|
||||
# Growth is rare and must not release the previous allocation while
|
||||
# an earlier kernel is still consuming it.
|
||||
torch.accelerator.synchronize(device)
|
||||
allocation = symm_mem.empty(required_bytes, dtype=torch.uint8, device=device)
|
||||
handle = symm_mem.rendezvous(allocation, group_name)
|
||||
workspace = _SymmWorkspace(
|
||||
allocation=allocation,
|
||||
handle=handle,
|
||||
capacity_bytes=required_bytes,
|
||||
stream_id=stream_id,
|
||||
)
|
||||
_SYMM_WORKSPACES[key] = workspace
|
||||
return workspace.handle.get_buffer(workspace.handle.rank, symm_shape, dtype)
|
||||
|
||||
|
||||
def clear_a2a_permute_workspaces() -> None:
|
||||
"""Release cached symmetric-memory workspaces during worker shutdown."""
|
||||
with _WORKSPACE_LOCK:
|
||||
if not _SYMM_WORKSPACES:
|
||||
return
|
||||
devices = {workspace.allocation.device for workspace in _SYMM_WORKSPACES.values()}
|
||||
for device in devices:
|
||||
torch.accelerator.synchronize(device)
|
||||
count = len(_SYMM_WORKSPACES)
|
||||
_SYMM_WORKSPACES.clear()
|
||||
logger.info("[a2a_permute] Released %d symmetric-memory workspace(s)", count)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Forward: (B, S_local, H, D) -> (B, S_global, H_local, D)
|
||||
# scatter heads (dim 2), gather sequence (dim 1). Matches all_to_all_4D(x,2,1).
|
||||
# ---------------------------------------------------------------------------
|
||||
@torch.library.custom_op("vllm_omni_a2a::ulysses_qkv_fwd", mutates_args=(), device_types="cuda")
|
||||
def ulysses_qkv_fwd(x: torch.Tensor, group_name: str, world_size: int) -> torch.Tensor:
|
||||
_ensure_built()
|
||||
p = world_size
|
||||
B, s_local, H, D = x.shape
|
||||
Hl = H // p
|
||||
lc = Hl * D
|
||||
rows = B * s_local
|
||||
symm_in = _get_symm_buffer((rows, p, lc), x.dtype, x.device, group_name)
|
||||
# (B, S_local, H, D) -> (rows, p, lc); H is row-major so column block r = heads [r*Hl:(r+1)*Hl]
|
||||
symm_in.copy_(x.reshape(rows, p, lc))
|
||||
out = torch.empty(p, rows, lc, device=x.device, dtype=x.dtype)
|
||||
torch.ops.a2ap.all_to_all_permute(symm_in, out, 1, 0, group_name)
|
||||
# (p, rows, lc) -> (B, S_global, H_local, D), sequence ordered rank-major
|
||||
return out.reshape(p, B, s_local, Hl, D).permute(1, 0, 2, 3, 4).reshape(B, p * s_local, Hl, D).contiguous()
|
||||
|
||||
|
||||
@ulysses_qkv_fwd.register_fake
|
||||
def _(x: torch.Tensor, group_name: str, world_size: int) -> torch.Tensor:
|
||||
B, s_local, H, D = x.shape
|
||||
return x.new_empty(B, s_local * world_size, H // world_size, D)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Reverse: (B, S_global, H_local, D) -> (B, S_local, H, D)
|
||||
# scatter sequence (dim 1), gather heads (dim 2). Matches all_to_all_4D(y,1,2).
|
||||
# ---------------------------------------------------------------------------
|
||||
@torch.library.custom_op("vllm_omni_a2a::ulysses_o_rev", mutates_args=(), device_types="cuda")
|
||||
def ulysses_o_rev(y: torch.Tensor, group_name: str, world_size: int) -> torch.Tensor:
|
||||
_ensure_built()
|
||||
p = world_size
|
||||
B, s_global, Hl, D = y.shape
|
||||
s_local = s_global // p
|
||||
H = Hl * p
|
||||
cols = Hl * D
|
||||
rows = B * s_local
|
||||
symm_in = _get_symm_buffer((p, rows, cols), y.dtype, y.device, group_name)
|
||||
# (B, p*S_local, Hl, D) -> (p, B*S_local, cols): block r = sequence shard destined to rank r
|
||||
symm_in.copy_(y.reshape(B, p, s_local, Hl, D).permute(1, 0, 2, 3, 4).reshape(p, rows, cols))
|
||||
out = torch.empty(rows, p, cols, device=y.device, dtype=y.dtype)
|
||||
torch.ops.a2ap.all_to_all_permute(symm_in, out, 0, 1, group_name)
|
||||
# (rows, p, cols) -> (B, S_local, H, D), heads ordered rank-major
|
||||
return out.reshape(B, s_local, p, Hl, D).reshape(B, s_local, H, D).contiguous()
|
||||
|
||||
|
||||
@ulysses_o_rev.register_fake
|
||||
def _(y: torch.Tensor, group_name: str, world_size: int) -> torch.Tensor:
|
||||
B, s_global, Hl, D = y.shape
|
||||
return y.new_empty(B, s_global // world_size, Hl * world_size, D)
|
||||
@@ -0,0 +1,234 @@
|
||||
// JIT build of pytorch/pytorch#178230 all_to_all_permute (Ulysses-style fused
|
||||
// permute-free all-to-all over NCCL symmetric memory).
|
||||
//
|
||||
// Kernel is verbatim from the PR; the host entry point is adapted to the
|
||||
// NCCLDevCommManager API shipped in torch 2.11 (we obtain the host ncclComm_t
|
||||
// via the manager and create our own ncclDevComm with enough LSA barriers,
|
||||
// cached per group — the PR's keyed-devcomm manager API is not in 2.11).
|
||||
|
||||
#include <c10/cuda/CUDAGuard.h>
|
||||
#include <c10/macros/Macros.h>
|
||||
#include <ATen/native/cuda/MemoryAccess.cuh>
|
||||
#include <ATen/cuda/CUDAContext.h>
|
||||
#include <torch/csrc/distributed/c10d/NCCLUtils.hpp>
|
||||
#include <torch/csrc/distributed/c10d/symm_mem/nccl_dev_cap.hpp>
|
||||
#include <torch/csrc/distributed/c10d/symm_mem/nccl_devcomm_manager.hpp>
|
||||
#include <torch/csrc/distributed/c10d/symm_mem/NCCLSymmetricMemory.hpp>
|
||||
#include <torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.hpp>
|
||||
#include <torch/library.h>
|
||||
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace c10d::symmetric_memory;
|
||||
|
||||
#ifndef NCCL_HAS_SYMMEM_DEVICE_SUPPORT
|
||||
#error "Build requires NCCL >= 2.28 with symmetric-memory device support"
|
||||
#endif
|
||||
|
||||
// ---- kernel (verbatim from PR) ----------------------------------------------
|
||||
__device__ inline void copy_bytes_vec16_aligned(
|
||||
const char* src, char* dst, size_t nbytes, size_t tid, size_t stride) {
|
||||
const size_t n_vec = nbytes / 16;
|
||||
constexpr int kUnroll = 4;
|
||||
size_t vec_idx = tid;
|
||||
for (; vec_idx + static_cast<size_t>(kUnroll - 1) * stride < n_vec;
|
||||
vec_idx += static_cast<size_t>(kUnroll) * stride) {
|
||||
at::native::memory::Vec<16> chunk[kUnroll];
|
||||
#pragma unroll 4
|
||||
for (int u = 0; u < kUnroll; ++u) {
|
||||
const size_t i = vec_idx + static_cast<size_t>(u) * stride;
|
||||
chunk[u] = at::native::memory::ld_vec<16>(src + i * 16);
|
||||
}
|
||||
#pragma unroll 4
|
||||
for (int u = 0; u < kUnroll; ++u) {
|
||||
const size_t i = vec_idx + static_cast<size_t>(u) * stride;
|
||||
at::native::memory::st_vec<16>(dst + i * 16, chunk[u]);
|
||||
}
|
||||
}
|
||||
for (; vec_idx < n_vec; vec_idx += stride) {
|
||||
auto v = at::native::memory::ld_vec<16>(src + vec_idx * 16);
|
||||
at::native::memory::st_vec<16>(dst + vec_idx * 16, v);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr int A2A_MAX_SLOTS = 64;
|
||||
constexpr int A2A_MAX_CTAS_PER_SLOT = 128;
|
||||
constexpr int A2A_THREADS_PER_CTA = 128;
|
||||
constexpr int A2A_MAX_CTA_COUNT = A2A_MAX_SLOTS * A2A_MAX_CTAS_PER_SLOT;
|
||||
|
||||
__global__ void all_to_all_lsa_kernel(
|
||||
ncclWindow_t window,
|
||||
size_t base_src_byte_offset,
|
||||
unsigned char* out,
|
||||
int num_rows,
|
||||
size_t src_row_stride_bytes,
|
||||
size_t copy_row_bytes,
|
||||
size_t peer_stride_bytes,
|
||||
size_t dst_row_stride_bytes,
|
||||
ncclDevComm devComm) {
|
||||
const int peer_idx = blockIdx.x;
|
||||
const int local_block = blockIdx.y;
|
||||
const ncclCoopCta coop{};
|
||||
|
||||
ncclLsaBarrierSession<ncclCoopCta> bar{
|
||||
coop, devComm, ncclTeamLsa(devComm), devComm.lsaBarrier,
|
||||
blockIdx.x * gridDim.y + blockIdx.y};
|
||||
bar.sync(coop, cuda::memory_order_acquire);
|
||||
|
||||
unsigned char* dst_peer_base =
|
||||
out + static_cast<size_t>(peer_idx) * peer_stride_bytes;
|
||||
CUDA_KERNEL_ASSERT((base_src_byte_offset & 15) == 0);
|
||||
CUDA_KERNEL_ASSERT((reinterpret_cast<uintptr_t>(dst_peer_base) & 15) == 0);
|
||||
|
||||
for (int i = local_block; i < num_rows; i += gridDim.y) {
|
||||
const size_t row_byte_offset =
|
||||
base_src_byte_offset + static_cast<size_t>(i) * src_row_stride_bytes;
|
||||
const void* src_row = ncclGetLsaPointer(window, row_byte_offset, peer_idx);
|
||||
unsigned char* dst_row =
|
||||
dst_peer_base + static_cast<size_t>(i) * dst_row_stride_bytes;
|
||||
copy_bytes_vec16_aligned(
|
||||
reinterpret_cast<const char*>(src_row),
|
||||
reinterpret_cast<char*>(dst_row),
|
||||
copy_row_bytes, coop.thread_rank(), coop.size());
|
||||
}
|
||||
bar.sync(coop, cuda::memory_order_release);
|
||||
}
|
||||
|
||||
// ---- own devcomm cache (replaces PR's keyed manager API) --------------------
|
||||
ncclDevComm get_or_create_devcomm(ncclComm_t comm, const std::string& group_name) {
|
||||
static std::mutex mu;
|
||||
static std::map<std::string, ncclDevComm> cache;
|
||||
std::lock_guard<std::mutex> lk(mu);
|
||||
auto it = cache.find(group_name);
|
||||
if (it != cache.end()) return it->second;
|
||||
#ifdef NCCL_DEV_COMM_REQUIREMENTS_INITIALIZER
|
||||
ncclDevCommRequirements reqs = NCCL_DEV_COMM_REQUIREMENTS_INITIALIZER;
|
||||
#else
|
||||
ncclDevCommRequirements reqs;
|
||||
memset(&reqs, 0, sizeof(ncclDevCommRequirements));
|
||||
#endif
|
||||
reqs.lsaBarrierCount = A2A_MAX_CTA_COUNT;
|
||||
ncclDevComm dc;
|
||||
ncclResult_t res = ncclDevCommCreate(comm, &reqs, &dc);
|
||||
TORCH_CHECK(res == ncclSuccess, "ncclDevCommCreate (a2a_permute) failed: ", ncclGetErrorString(res));
|
||||
cache.emplace(group_name, dc);
|
||||
return dc;
|
||||
}
|
||||
|
||||
// ---- host entry -------------------------------------------------------------
|
||||
void all_to_all_permute(
|
||||
const at::Tensor& input,
|
||||
at::Tensor& out,
|
||||
int64_t scatter_dim,
|
||||
int64_t gather_dim,
|
||||
std::string group_name) {
|
||||
TORCH_CHECK(input.stride(-1) == 1, "a2a_permute: innermost dim must be contiguous");
|
||||
const bool col_scatter = (scatter_dim == 1 && gather_dim == 0);
|
||||
const bool row_scatter = (scatter_dim == 0 && gather_dim == 1);
|
||||
TORCH_CHECK(col_scatter || row_scatter,
|
||||
"a2a_permute: unsupported (scatter_dim, gather_dim)=(", scatter_dim, ",", gather_dim, ")");
|
||||
|
||||
auto symm_mem = c10d::symmetric_memory::rendezvous(input, group_name);
|
||||
TORCH_CHECK(symm_mem != nullptr, "a2a_permute: input must be NCCL symmetric memory");
|
||||
auto* nccl_hdl = dynamic_cast<NCCLSymmetricMemory*>(symm_mem.get());
|
||||
TORCH_CHECK(nccl_hdl != nullptr, "a2a_permute: requires NCCL symmetric memory backend");
|
||||
|
||||
c10::cuda::CUDAGuard guard(input.device());
|
||||
auto stream = at::cuda::getCurrentCUDAStream();
|
||||
auto device = input.device();
|
||||
|
||||
auto& manager = NCCLDevCommManager::get(device);
|
||||
ncclComm_t comm = manager.get_comm(group_name);
|
||||
ncclDevComm devcomm = get_or_create_devcomm(comm, group_name);
|
||||
|
||||
const int my_rank = nccl_hdl->get_rank();
|
||||
const int p = nccl_hdl->get_world_size();
|
||||
TORCH_CHECK(p <= A2A_MAX_SLOTS, "a2a_permute: group size ", p, " exceeds ", A2A_MAX_SLOTS);
|
||||
TORCH_CHECK(out.is_contiguous(), "a2a_permute: out must be contiguous");
|
||||
TORCH_CHECK(out.scalar_type() == input.scalar_type(), "a2a_permute: dtype mismatch");
|
||||
|
||||
auto window = nccl_hdl->get_window();
|
||||
TORCH_CHECK(window != nullptr, "a2a_permute: NCCL window is null");
|
||||
|
||||
const size_t window_base_offset = nccl_hdl->get_offset();
|
||||
const int64_t esize = input.element_size();
|
||||
const size_t tensor_leading_offset =
|
||||
window_base_offset + static_cast<size_t>(input.storage_offset()) * static_cast<size_t>(esize);
|
||||
TORCH_CHECK(tensor_leading_offset % 16 == 0, "a2a_permute: tensor byte offset must be 16B aligned");
|
||||
TORCH_CHECK(reinterpret_cast<uintptr_t>(input.data_ptr()) % 16 == 0, "a2a_permute: input ptr must be 16B aligned");
|
||||
TORCH_CHECK(reinterpret_cast<uintptr_t>(out.data_ptr()) % 16 == 0, "a2a_permute: out ptr must be 16B aligned");
|
||||
|
||||
const int unroll = 4 * 16 / static_cast<int>(esize);
|
||||
const int elems_per_cta = A2A_THREADS_PER_CTA * unroll;
|
||||
int ctas_per_slot = 1;
|
||||
|
||||
if (col_scatter) {
|
||||
const int rows = static_cast<int>(input.size(0));
|
||||
int64_t total_cols = 0; int local_cols = 0;
|
||||
if (input.dim() == 2) {
|
||||
total_cols = input.size(1);
|
||||
TORCH_CHECK(total_cols % p == 0, "a2a_permute: cols must divide group size");
|
||||
local_cols = static_cast<int>(total_cols / p);
|
||||
} else {
|
||||
TORCH_CHECK(input.dim() == 3, "a2a_permute: input must be 2-D or 3-D");
|
||||
TORCH_CHECK(input.size(1) == p, "a2a_permute: 3-D input dim1 must equal group size");
|
||||
const int64_t lc = input.size(2);
|
||||
total_cols = static_cast<int64_t>(p) * lc;
|
||||
TORCH_CHECK(input.stride(1) == lc && input.stride(0) == total_cols,
|
||||
"a2a_permute: 3-D input must be row-major contiguous");
|
||||
local_cols = static_cast<int>(lc);
|
||||
}
|
||||
const bool ok3 = out.dim() == 3 && out.size(0) == p && out.size(1) == rows && out.size(2) == local_cols;
|
||||
const bool ok2 = out.dim() == 2 && out.size(0) == (int64_t)p * rows && out.size(1) == local_cols;
|
||||
TORCH_CHECK(ok3 || ok2, "a2a_permute: bad out shape for (1,0)");
|
||||
const size_t row_bytes = (size_t)local_cols * (size_t)esize;
|
||||
TORCH_CHECK(row_bytes % 16 == 0, "a2a_permute: local_cols*esize must be 16B-divisible");
|
||||
ctas_per_slot = std::max(1, std::min((rows * local_cols + elems_per_cta - 1) / elems_per_cta, A2A_MAX_CTAS_PER_SLOT));
|
||||
const size_t esz = (size_t)esize;
|
||||
const size_t base_src = tensor_leading_offset + (size_t)my_rank * (size_t)local_cols * esz;
|
||||
all_to_all_lsa_kernel<<<dim3(p, ctas_per_slot), A2A_THREADS_PER_CTA, 0, stream>>>(
|
||||
window, base_src, reinterpret_cast<unsigned char*>(out.data_ptr()), rows,
|
||||
(size_t)total_cols * esz, row_bytes, (size_t)rows * local_cols * esz, (size_t)local_cols * esz, devcomm);
|
||||
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
||||
} else {
|
||||
int local_rows = 0; int cols = 0;
|
||||
if (input.dim() == 2) {
|
||||
const int64_t total_rows = input.size(0);
|
||||
cols = static_cast<int>(input.size(1));
|
||||
TORCH_CHECK(total_rows % p == 0, "a2a_permute: rows must divide group size");
|
||||
local_rows = static_cast<int>(total_rows / p);
|
||||
} else {
|
||||
TORCH_CHECK(input.dim() == 3, "a2a_permute: input must be 2-D or 3-D");
|
||||
TORCH_CHECK(input.size(0) == p, "a2a_permute: 3-D input dim0 must equal group size");
|
||||
local_rows = static_cast<int>(input.size(1));
|
||||
const int64_t c = input.size(2); cols = static_cast<int>(c);
|
||||
const int64_t s01 = (int64_t)local_rows * c;
|
||||
TORCH_CHECK(input.stride(1) == c && input.stride(0) == s01, "a2a_permute: 3-D input must be row-major contiguous");
|
||||
}
|
||||
const bool ok3 = out.dim() == 3 && out.size(0) == local_rows && out.size(1) == p && out.size(2) == cols;
|
||||
const bool ok2 = out.dim() == 2 && out.size(0) == local_rows && out.size(1) == (int64_t)p * cols;
|
||||
TORCH_CHECK(ok3 || ok2, "a2a_permute: bad out shape for (0,1)");
|
||||
const size_t row_bytes = (size_t)cols * (size_t)esize;
|
||||
TORCH_CHECK(row_bytes % 16 == 0, "a2a_permute: cols*esize must be 16B-divisible");
|
||||
ctas_per_slot = std::max(1, std::min((local_rows * cols + elems_per_cta - 1) / elems_per_cta, A2A_MAX_CTAS_PER_SLOT));
|
||||
const size_t esz = (size_t)esize; const size_t cu = (size_t)cols;
|
||||
const size_t base_src = tensor_leading_offset + (size_t)(my_rank * local_rows) * cu * esz;
|
||||
all_to_all_lsa_kernel<<<dim3(p, ctas_per_slot), A2A_THREADS_PER_CTA, 0, stream>>>(
|
||||
window, base_src, reinterpret_cast<unsigned char*>(out.data_ptr()), local_rows,
|
||||
cu * esz, row_bytes, cu * esz, (size_t)p * cu * esz, devcomm);
|
||||
C10_CUDA_KERNEL_LAUNCH_CHECK();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TORCH_LIBRARY_FRAGMENT(a2ap, m) {
|
||||
m.def("all_to_all_permute(Tensor input, Tensor(a!) out, int scatter_dim, int gather_dim, str group_name) -> ()");
|
||||
}
|
||||
TORCH_LIBRARY_IMPL(a2ap, CUDA, m) {
|
||||
m.impl("all_to_all_permute", TORCH_FN(all_to_all_permute));
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <ATen/ATen.h>
|
||||
#include <c10/macros/Macros.h>
|
||||
|
||||
namespace c10d::nccl_extension {
|
||||
|
||||
TORCH_API bool is_nccl_symmem_available();
|
||||
|
||||
TORCH_API void nccl_put(at::Tensor& tensor, const int64_t peer);
|
||||
|
||||
TORCH_API void nccl_get(at::Tensor& tensor, const int64_t peer);
|
||||
|
||||
TORCH_API void nccl_wait_for_signal(at::Tensor& sigpad, int64_t signal);
|
||||
|
||||
TORCH_API void nccl_put_with_signal(
|
||||
at::Tensor& tensor,
|
||||
int64_t signal,
|
||||
int64_t peer);
|
||||
|
||||
// Simultaneously reduce N blocks of a 2-D input tensor from a shared symmetric
|
||||
// memory buffer, routing each to a specific destination rank. Blocks are
|
||||
// described by inclusive-prefix-sum offsets along `dim` (0 or 1); all blocks
|
||||
// must have equal size.
|
||||
TORCH_API void nccl_reduce_scatter_offset(
|
||||
const at::Tensor& input,
|
||||
at::TensorList out,
|
||||
const std::string& group_name,
|
||||
int64_t dim,
|
||||
std::optional<at::IntArrayRef> offsets,
|
||||
std::optional<at::IntArrayRef> dst_ranks,
|
||||
const std::string& red_op);
|
||||
|
||||
// Permute-free all-to-all: scatter shards along `scatter_dim`, gather peer
|
||||
// chunks along `gather_dim`. Supported: (scatter_dim=1, gather_dim=0) -> [rows,
|
||||
// p*lc] to [p, rows, lc]; (scatter_dim=0, gather_dim=1) -> [p*lr, cols] to [lr,
|
||||
// p, cols].
|
||||
TORCH_API void nccl_all_to_all_permute(
|
||||
const at::Tensor& input,
|
||||
at::Tensor& out,
|
||||
int64_t scatter_dim,
|
||||
int64_t gather_dim,
|
||||
const std::string& group_name);
|
||||
} // namespace c10d::nccl_extension
|
||||
Reference in new issue
Block a user