Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Tensor parallel on CUDA: with `TF_ROCE=1` on every rank, three ranks or more take the one-shot RoCE all-gather for decode-sized gathers, byte-equal to NCCL's, falling back to NCCL with one warning when a peer can't be reached, and GLM-5.3-Flash's DFlash2 drafter MLP splits across every rank; without it they keep NCCL, two ranks run as before, and MiniMax-M3 keeps NCCL at three ranks or more. With `TF_ROCE=1`, GLM-5.3-Flash decode is about 6% faster at one and two streams on three GB10s (four streams level) and 12.9%, 10.4% and 6.6% faster at one, two and four streams on four GB10s, with the target's tokens unchanged; drafter acceptance can move slightly, since the split MLP sums in another order (#2205). Thanks to @shanedertrain.

- Flash Next: twenty-nine `TF_FLASHNEXT_*` settings refuse a malformed value by name at startup, before any device work on CUDA, and a Metal load refuses them too; blank `TF_FLASHNEXT_KV_PAGE_TEST` and `TF_FLASHNEXT_KEEP_LATE` read as unset, and a non-finite confidence is refused (#2585, part of #2370). Thanks to @devopsfortunato.
- Flash Next on CUDA: twenty-nine `TF_FLASHNEXT_*` settings refuse a malformed value by name at startup, before any
device or checkpoint work, through the shared environment table: unset and blank keep the default, a valid value keeps
Expand Down
2 changes: 1 addition & 1 deletion docs/multi-rank-receipts.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ on GLM-5.3-Flash, `TF_GLM_PREFILL_REDUCE` defaults to `split`, which sends every
world whose ranks reach only their neighbours needs `ring` or `gather` instead, as the collective path supports.

Transport choice changes exchange cost; verify token equality at the same world size rather than assuming it. On
native GLM on CUDA with two ranks, `TF_ROCE` takes the small all-gathers over RDMA writes and `TF_ROCE=0` on either rank keeps NCCL;
native GLM on CUDA with two ranks, `TF_ROCE` takes the small all-gathers over RDMA writes and `TF_ROCE=0` on either rank keeps NCCL (three ranks or more ask for it with `TF_ROCE=1` on every rank);
`TF_ROCE_HCA` names the devices and
`TF_ROCE_MAX_KB` bounds the largest gather sent that way. Nemotron's serving path opens the NCCL group without
enabling its optional RoCE gather. On the `fabric/tp` harness, `TF_COMM_BACKEND` picks `nccl`, `mailbox` or `roce`, and
Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/glm-5.3-flash-cuda.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ cold 64K prompts in 36.5 s and decode at 73.1 / 60.8 tok/s (code / chat, tempera
`TF_GLM_WORLD=3` places a third rank the same way. From 1.1.2 the uneven KDA input projections of TP=3 (22, 21 and
21 heads) run instead of being refused; TP=3 device qualification is pending
([#1067](https://github.com/ashhart/TensorFold/issues/1067)). How a prompt's rows reach the other ranks
(`TF_GLM_PREFILL_REDUCE`) and the two-rank RoCE gathers (`TF_ROCE`) are in
(`TF_GLM_PREFILL_REDUCE`) and the RoCE gathers (`TF_ROCE`: on at two ranks, `TF_ROCE=1` at three or more) are in
[placement](../multi-rank-receipts.md#the-settings-the-ranks-agreed-on).

## Prompt chunk size
Expand Down
20 changes: 11 additions & 9 deletions docs/roce-gather.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# RoCE all-gather for two ranks
# RoCE all-gather for a group of ranks

One-shot all-gather between two DGX Sparks over RoCE: the GPU side of b12x's "RoCEnante" exchange (https://github.com/local-inference-lab/b12x, b12x/comm/roce, Apache License 2.0), written for two ranks. The host side (zig/src/cuda/roce.zig) owns a pinned region both GPUs and both NICs reach:
One-shot all-gather between DGX Sparks over RoCE: the GPU side of b12x's "RoCEnante" exchange (https://github.com/local-inference-lab/b12x, b12x/comm/roce, Apache License 2.0), written for two ranks and generalised to any world whose ranks all reach each other (two on one cable; two, four or more on a switch; at most `roce.max_world` = 8). The host side (zig/src/cuda/roce.zig) owns a pinned region the GPU and the NICs reach:

```
recv[slot] the peer's shard, written by the peer's NIC (2 slots of slot_bytes)
flag[slot][hca] the sequence the peer's NIC writes after its stripe on that HCA (128 bytes apart)
send[slot] this rank's shard, staged here for its NIC (2 slots)
ctrl {seq, nbytes of slot 0, nbytes of slot 1, error seq, missing hca}: the proxy's doorbell
recv[slot][peer] each peer's shard, written by that peer's NIC (2 slots of world - 1 of slot_bytes)
flag[slot][peer][hca] the sequence a peer's NIC writes after its stripe on that HCA (128 bytes apart)
send[slot] this rank's shard, staged here for its NICs (2 slots)
ctrl {seq, nbytes of slot 0, nbytes of slot 1, error seq, missing hca, missing rank, probe tokens}
```

One launch: (1) every block stages its share of the shard into send[seq & 1]; (2) the last block to stage publishes the slot's size and rings ctrl.seq after a system fence, and the proxy thread RDMA-writes the shard and then a flag per HCA stripe on the same queue pair (so a flag never lands before its stripe); (3) each block waits for every stripe's flag; (4) every block writes both shards in rank order (the layout NCCL's all-gather gives); (5) the last block to finish advances the device epoch, so a CUDA graph replays the exchange with new sequences. A wait past the spin limit poisons the exchange: later launches return at once and the host reports the error.
A rank's peers are the other ranks in rank order (rank 2's peers 0, 1, 3 are its peer numbers 0, 1, 2); every rank opens one RC queue pair a peer an HCA, and at world 2 (one peer) every offset and every byte is the two-rank design's.

One launch: (1) every block stages its share of the shard into send[seq & 1]; (2) the last block to stage publishes the slot's size and rings ctrl.seq after a system fence, and the proxy thread RDMA-writes the shard to every peer, each copy on this rank's queue pair toward that peer, and then a flag per HCA stripe on the same queue pair (so a flag never lands before its stripe); (3) one thread a (peer, HCA) waits for that stripe's flag, (world - 1) x HCAs of them; (4) every block writes every shard into the output slot of its rank (the layout NCCL's all-gather gives); (5) the last block to finish advances the device epoch, so a CUDA graph replays the exchange with new sequences. A wait past the spin limit poisons the exchange: later launches return at once and the host reports the error, naming the HCA and the rank whose stripe never came.

## Host side

Small all-gathers between two ranks as one-shot RDMA writes over RoCE: b12x's "RoCEnante" exchange (https://github.com/local-inference-lab/b12x, b12x/comm/roce, Apache License 2.0), written for two ranks: the kernel (kernels/cuda/roce_gather.cu) stages this rank's shard in pinned host memory and rings a doorbell; a proxy thread here RDMA-writes it to the peer, striped over the HCAs, each stripe followed by its sequence flag on the same queue pair; the kernel waits for the peer's flags and copies both shards out. A decode step's gathers are a few rows of partials. On two GB10s with one ConnectX-7 cable (NCCL 2.30.7, `tf-cuda-test roce-gather`, 2,000 calls back to back) this path took 10.4 us a call at 4 KiB a rank against NCCL's 12.3, 11.6 against 13.5 at 8 KiB, 17.4 against 20.5 at 32 KiB, and 17.4 against 51.5 at 64 KiB. libibverbs is opened at run time (as the driver and NCCL are): without it, or without an active HCA, the caller keeps NCCL.

## Setup and use

`tp_group.Group.openRoce` brings the path up on a two-rank group: each rank opens its HCAs, and what each brings (its region, keys, queue pairs and the largest shard it would send) crosses the group's own NCCL communicator, as does each rank's verdict after connecting. Both ranks get the path or neither does (`TF_ROCE=0` on either rank keeps NCCL). From then on `Group.allGather` sends a gather of up to the smaller rank's cap (512 KiB by default) this way and leaves larger ones to NCCL; the size alone decides, so both ranks always take the same path. A rank whose buffers are not 16-byte aligned gets `error.Misaligned` instead of falling back alone. Liveness is `tp_watch`'s, as for every group.
`tp_group.Group.openRoce` brings the path up on the group: each rank opens its HCAs and a queue pair a peer an HCA, and what each brings (its region, keys, queue pairs and the largest shard it would send) crosses the group's own NCCL communicator (an all-gather), as does each rank's verdict after connecting. Every rank gets the path or none does. Two ranks ask for it unless `TF_ROCE=0`; three or more ask only with `TF_ROCE=1` on every rank (the GLM start refuses ranks that differ), which also splits the DFlash2 drafter's MLP across them. MiniMax-M3 asks for the gather at two ranks only. Without it a group of three or more runs as before: one small exchange at startup, no probe and no warning. Then a connect probe: every rank RDMA-writes a token to every peer on every HCA and waits for its writes to complete and for every peer's token; a rank with no RDMA path to another on the same HCA (a ring of four without a switch, whose far ranks share no cable) fails this, and every rank keeps NCCL, with one warning naming the rank, rather than a kernel spinning at the first gather. A rank's HCA h writes to every peer's HCA h, so the fabric must pair the same HCA index across every rank (a switch does; `fabric/tp`'s multirank wire, which a GLM rank borrows with `TF_GLM_ROCE_MULTI=1`, is the path for a ring's per-pair ports). From then on `Group.allGather` sends a gather of up to the smallest rank's cap (512 KiB by default) this way and leaves larger ones to NCCL; the size alone decides, so every rank always takes the same path. A rank whose buffers are not 16-byte aligned gets `error.Misaligned` instead of falling back alone. Liveness is `tp_watch`'s, as for every group.

A failed exchange writes nothing into the gathered rows. Call `Group.check` after any synchronize whose result reads a gather: it returns the error the GPU or the proxy recorded.

`tf-cuda-test roce-gather <rank>` runs on both ranks (NCCL_COMM_ID set): every size up to the cap and past it against NCCL's all-gather, byte for byte, then each path's time a call.
`tf-cuda-test roce-gather <rank> [world]` runs on every rank (NCCL_COMM_ID set; world 2 by default): every size up to the cap and past it against NCCL's all-gather, byte for byte, a captured graph replayed, 3,000 mixed-size calls (16, 32 and 64 KiB a rank with tiny ones between) each against NCCL's rows, then each path's time a call at 4 to 64 KiB. Its last line, `RESULT roce-gather world N rank R: 32 KiB a rank RoCE X us a call (GO at most 25.0), 3000 mixed-size calls, M mismatch(es) (GO 0)`, is the gate a recipe reads before setting `TF_ROCE=1` at four ranks (the test needs it set there). Worlds 2 and 4 are measured (four GB10s behind a switch, 32 KiB a rank: RoCE 22.0 us a call against NCCL's 39.4, byte-equal, 0 mismatches in 3,000 calls); three ranks or more take the gather only with `TF_ROCE=1`, and any other value keeps NCCL.
2 changes: 1 addition & 1 deletion zig/build/cuda_kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub const kernels = [_]Kernel{
.{ .name = "torch_indexing", .src = "torch_ops/indexing", .flags = torch_ops },
.{ .name = "torch_movement", .src = "torch_ops/movement", .flags = torch_ops },
.{ .name = "torch_nemotron_constants", .src = "torch_ops/nemotron_constants", .flags = torch_ops },
.{ .name = "roce_gather", .src = "roce_gather", .flags = &.{"-O3"} }, // b12x's one-shot RoCE all-gather, two ranks
.{ .name = "roce_gather", .src = "roce_gather", .flags = &.{"-O3"} }, // b12x's one-shot RoCE all-gather, any world on a mesh
.{ .name = "l2pf", .src = "deepseek_v41/l2pf", .flags = &.{"-O3"} }, // DeepSeek V4.1's L2 prefetch of a weight table, as GLM's too (records: glm_l2pf_pending.zig)
.{ .name = "l2pace", .src = "deepseek_v41/l2pace", .flags = &.{"-O3"} }, // and its paced form
.{ .name = "drama3_staged_gemv", .flags = glue },
Expand Down
31 changes: 26 additions & 5 deletions zig/glm_dflash_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,42 @@ test "glm dflash: gather and read fit for every supported rank count" {
test "glm dflash: the raw scratch holds every 4-bit linear's words, scales and biases at every rank count" {
const df = glm.dflash;
var short: usize = 0;
for (1..17) |world| {
const ff = if (world == 2) df.FF / 2 else df.FF; // two ranks split gate and up by rows; any other world loads them whole
for (1..17) |world| for ([_]bool{ false, true }) |wide| {
const ff = df.mlpSplit(world, wide).ff;
// dflash_load's 4-bit linears, rows by columns: qkv, kv, o, gate and up, the kernel projections, fc
const linears = [_][2]usize{ .{ df.QKV, df.D }, .{ 2 * df.KVW, df.D }, .{ df.D, df.D }, .{ 2 * ff, df.D }, .{ df.KVW, df.D }, .{ df.D, 5 * df.D } };
for (linears) |shape| {
const nk = shape[0] * shape[1];
const need = std.mem.alignForward(usize, nk / 2, 256) + std.mem.alignForward(usize, nk / 32, 256) + nk / 32;
if (need <= df.rawBytes(4, world)) continue;
std.debug.print("world {d}: a {d}x{d} linear needs {d} raw bytes, the scratch has {d}\n", .{ world, shape[0], shape[1], need, df.rawBytes(4, world) });
if (need <= df.rawBytes(4, world, wide)) continue;
std.debug.print("world {d}: a {d}x{d} linear needs {d} raw bytes, the scratch has {d}\n", .{ world, shape[0], shape[1], need, df.rawBytes(4, world, wide) });
short += 1;
}
}
};
try t.expectEqual(@as(usize, 0), short);
}

test "glm dflash: the MLP splits at two ranks as on main; three or more only with TF_ROCE=1" {
const df = glm.dflash;
const whole: df.MlpSplit = .{ .split = false, .ff = df.FF };
try t.expectEqual(whole, df.mlpSplit(1, true));
try t.expectEqual(df.MlpSplit{ .split = true, .ff = df.FF / 2 }, df.mlpSplit(2, false)); // 6144: main's split, no opt-in
try t.expectEqual(df.MlpSplit{ .split = true, .ff = df.FF / 2 }, df.mlpSplit(2, true));
for ([_]usize{ 3, 4, 6, 8 }) |world| try t.expectEqual(whole, df.mlpSplit(world, false)); // main's whole MLP on every rank
try t.expectEqual(df.MlpSplit{ .split = true, .ff = df.FF / 4 }, df.mlpSplit(4, true)); // 3072 on four Sparks
try t.expectEqual(df.MlpSplit{ .split = true, .ff = 4096 }, df.mlpSplit(3, true));
try t.expectEqual(whole, df.mlpSplit(5, true)); // 12288 / 5 is no share
try t.expectEqual(whole, df.mlpSplit(7, true));
for (1..17) |world| for ([_]bool{ false, true }) |wide| {
const m = df.mlpSplit(world, wide);
try t.expect(m.ff % 256 == 0); // the q4 pack's rows, glm_mm's K tiles and df_swiglu's columns all fit
if (m.split) {
try t.expectEqual(df.FF, m.ff * world); // the shares are the whole width
for (0..world) |rank| try t.expect(rank * m.ff + m.ff <= df.FF); // every rank's slice lies inside
} else try t.expectEqual(df.FF, m.ff);
};
}

const Resources = struct {
pred: []u16 = &.{},
succ: []u16 = &.{},
Expand Down
10 changes: 6 additions & 4 deletions zig/kernels/cuda/glm_dflash.cu
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extern "C" __global__ void df_conv(const __nv_bfloat16* __restrict__ z, const __
}
}

// SwiGLU over [gate | up] rows (width F each): bf16(bf16(silu(g)) * u). grid (R, F / 1024), 256 threads.
// SwiGLU over [gate | up] rows (width F each): bf16(bf16(silu(g)) * u). grid (R, ceil(F / 1024)), 256 threads.
extern "C" __global__ void df_swiglu(const __nv_bfloat16* __restrict__ gu, __nv_bfloat16* __restrict__ out, int F) {
const int r = blockIdx.x;
const __nv_bfloat16* g = gu + (long long)r * 2 * F;
Expand Down Expand Up @@ -260,12 +260,14 @@ extern "C" __global__ void df_add(__nv_bfloat16* __restrict__ x, const __nv_bflo
x[r * D + c] = tobf(bf(x[r * D + c]) + bf(y[r * D + c]));
}

// out = bf16(g rank 0 + g rank 1): both ranks' gathered fp32 partials [2][R][D] of a split linear.
extern "C" __global__ void df_sum2(const float* __restrict__ g, __nv_bfloat16* __restrict__ out, int R) {
// The split down's fp32 partials g[world][R][D] summed in rank order into bf16 out[R][D] (grid (R, D / 1024)).
extern "C" __global__ void df_sumn(const float* __restrict__ g, __nv_bfloat16* __restrict__ out, int R, int world) {
const long long r = blockIdx.x;
for (int c = blockIdx.y * 1024 + threadIdx.x; c < (blockIdx.y + 1) * 1024; c += blockDim.x) {
const long long i = r * D + c;
out[i] = tobf(g[i] + g[(long long)R * D + i]);
float s = g[i];
for (int k = 1; k < world; ++k) s += g[(long long)k * R * D + i];
out[i] = tobf(s);
}
}

Expand Down
27 changes: 16 additions & 11 deletions zig/kernels/cuda/roce_gather.cu
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Two-rank one-shot RoCE all-gather after b12x (github.com/local-inference-lab/b12x, Apache-2.0): docs/roce-gather.md.
// One-shot RoCE all-gather after b12x (github.com/local-inference-lab/b12x, Apache-2.0): docs/roce-gather.md.

#include <stdint.h>

namespace {

constexpr int C_SEQ = 0, C_NBYTES = 1, C_ERROR = 3, C_HCA = 4; // ctrl words (C_NBYTES + slot)
constexpr int C_SEQ = 0, C_NBYTES = 1, C_ERROR = 3, C_HCA = 4, C_PEER = 5; // ctrl words (C_NBYTES + slot)
constexpr int FLAG_STRIDE = 128;
// counters (device memory, u32): epoch, poison, then stage and tail arrivals for each grid size class
constexpr int K_EPOCH = 0, K_POISON = 1, K_CLASSES = 4;
Expand Down Expand Up @@ -46,11 +46,12 @@ __device__ __forceinline__ void fence_gpu() { asm volatile("fence.sc.gpu;" ::: "

} // namespace

// in: this rank's shard (packs of 16 bytes); out: [rank 0's shard | rank 1's shard]. klass: log2(gridDim.x).
// in: this rank's shard (16-byte packs); out: every rank's in rank order; recv and flags: by peer number.
extern "C" __global__ void __launch_bounds__(512) roce_gather(
const uint4* __restrict__ in, uint4* __restrict__ out, int packs, int rank, char* recv, char* flags, char* send,
uint32_t* ctrl, long long slot_bytes, uint32_t* counters, int klass, uint32_t spin_limit, int n_hca) {
uint32_t* ctrl, long long slot_bytes, uint32_t* counters, int klass, uint32_t spin_limit, int n_hca, int world) {
const int tid = threadIdx.x;
const int peers = world - 1;
uint32_t* stage = counters + 2 + klass;
uint32_t* tail = counters + 2 + K_CLASSES + klass;
if (ld_relaxed_gpu(counters + K_POISON) != 0u) return;
Expand All @@ -69,26 +70,30 @@ extern "C" __global__ void __launch_bounds__(512) roce_gather(
st_relaxed_sys(ctrl + C_SEQ, seq);
}
}
if (tid < n_hca) { // 3. the peer's stripes
const uint32_t* flag = reinterpret_cast<const uint32_t*>(flags + ((long long)slot * n_hca + tid) * FLAG_STRIDE);
if (tid < peers * n_hca) { // 3. every peer's stripes: one thread a (peer, HCA) flag
const int p = tid / n_hca, h = tid - p * n_hca;
const uint32_t* flag = reinterpret_cast<const uint32_t*>(flags + (((long long)slot * peers + p) * n_hca + h) * FLAG_STRIDE);
uint32_t polls = 0;
while (ld_acquire_sys(flag) != seq) {
if (++polls >= spin_limit) {
st_relaxed_sys(ctrl + C_HCA, (uint32_t)tid);
st_relaxed_sys(ctrl + C_HCA, (uint32_t)h);
st_relaxed_sys(ctrl + C_PEER, (uint32_t)(p < rank ? p : p + 1));
st_relaxed_sys(ctrl + C_ERROR, seq);
st_release_gpu(counters + K_POISON, seq);
break;
}
}
}
__syncthreads();
if (ld_relaxed_gpu(counters + K_POISON) == 0u) { // 4. both shards in rank order
const uint4* theirs = reinterpret_cast<const uint4*>(recv + (long long)slot * slot_bytes);
if (ld_relaxed_gpu(counters + K_POISON) == 0u) { // 4. every shard in rank order: this one from in, each peer's from its slot
uint4* own_out = out + (long long)rank * packs;
uint4* peer_out = out + (long long)(1 - rank) * packs;
for (int i = at; i < packs; i += stride) {
own_out[i] = in[i];
peer_out[i] = ld_relaxed_sys4(theirs + i);
for (int p = 0; p < peers; ++p) {
const int r = p < rank ? p : p + 1;
const uint4* theirs = reinterpret_cast<const uint4*>(recv + ((long long)slot * peers + p) * slot_bytes);
(out + (long long)r * packs)[i] = ld_relaxed_sys4(theirs + i);
}
}
}
fence_gpu(); // 5. the epoch, by the last block to finish
Expand Down
Loading