From b46125c8b0e7234212178b1b537b99b4845b95fe Mon Sep 17 00:00:00 2001 From: woooflet Date: Sun, 29 Nov 2026 18:03:15 +0000 Subject: [PATCH] perf(b12x/comm/roce): raise ring mailbox window to 16, validate in handshake, and drain eagerly On a four-node switchless 200 Gb/s ConnectX-7 RoCE ring, RING_WINDOW=8 (512 KiB in flight per origin and rail) saturates below the two-hop relay bandwidth-delay product during large prefill collectives, and run_pipe_op deferred drain_all(c) while actively posting chunks, delaying relay forwarding on intermediate ranks. Raising RING_WINDOW without a connect-time check would let mixed-build ranks compute mismatched remote mailbox and ACK offsets, while retaining the previous send-queue cap (ROCE_SEND_DEPTH / 4) would leave insufficient headroom for up to 256 unreserved per-origin ACKs at world=16. Changes in flashinfer/experimental/b12x/comm/roce/_roce_proxy.c: - Raise RING_WINDOW from 8 to 16 (1 MiB in flight per origin and rail; 2.008 MiB per rank in the group, 8.03 MiB/rank at world=4). - Append uint32_t ring_window and uint64_t ring_bytes to roce_blob_t (growing sizeof(roce_blob_t) from 1168 to 1176 bytes while keeping ROCE_ABI_VERSION=14 unchanged so previously exported cores stay compatible). Populate both fields in roce_local_blob (0 in mesh mode) and reject any mismatch in roce_connect. - Tighten the send-queue reservation threshold in reserve() and ring_service_slot() to ROCE_SEND_DEPTH / 8 (64 signaled ops, at most 192 chained WRs, leaving 256 WRs for unreserved per-origin ACKs at world=16 plus 64 WRs of provider SQE overhead) and enforce this bound with a compile-time _Static_assert. - Call drain_all(c) inside run_pipe_op when posted is nonzero and c->ring is active; non-ring transports (c->ring == NULL) are unchanged. - Add test_ring_blob_handshake_rejects_window_and_size_mismatch in tests/experimental/b12x/comm/test_roce_proxy.py covering ABI 14 retention, 1176-byte blob sizing, legacy 1168-byte blob rejection, and live-HCA roce_connect rejection of mismatched ring_window and ring_bytes. Measured effect on four DGX Spark GB10 nodes over 200 Gb/s ConnectX-7 (--roce-ring, H=4608 BF16): ReduceScatter drops 7.1% at M=8192 (3.487 to 3.239 ms, 17.48 GB/s bus BW) and 7.4%-9.7% at M=2048..4096; GatherShards at M=8192 drops 6.2% (3.491 to 3.275 ms); Twoshot at M=8192 drops 6.6% (6.889 to 6.435 ms). One-shot decode latency (M=1..24) is unchanged (19.7-66.8 us). Validation on four NVIDIA GB10 (ARM64) nodes: - pytest -v tests/experimental/b12x/comm/test_roce_proxy.py: 21/21 passed on dgx2 with live ConnectX-7 HCA verification of window/size mismatch rejection. - validate_roce_ring_native.py --world-size 4 --iterations 120 --skew-rank 2 --scatter-gather (ring) and --iterations 60 --ring-open (chain): all four ranks passed with 0/16 ConnectX-7 RDMA port errors. - Rings larger than four nodes were not benchmarked on hardware; world=16 send-queue headroom is enforced statically. Signed-off-by: woooflet --- .../experimental/b12x/comm/roce/_roce_proxy.c | 29 ++++++-- .../experimental/b12x/comm/test_roce_proxy.py | 68 +++++++++++++++++++ 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/flashinfer/experimental/b12x/comm/roce/_roce_proxy.c b/flashinfer/experimental/b12x/comm/roce/_roce_proxy.c index 1be38213ea0..7930fd76bb3 100644 --- a/flashinfer/experimental/b12x/comm/roce/_roce_proxy.c +++ b/flashinfer/experimental/b12x/comm/roce/_roce_proxy.c @@ -171,8 +171,10 @@ _Static_assert(CTRL_CREDIT >= CTRL_READY_VL + ROCE_VARLEN_MAX_CHUNKS && // The startup region, sized for the world, holds per (origin, rail, slot): // mailboxes, then 8-byte acknowledgment words, then outgoing descriptors. #define RING_CHUNK_BYTES 65536 -#define RING_WINDOW 8 +#define RING_WINDOW 16 #define RING_COMPLETION (UINT64_C(1) << 63) +_Static_assert((ROCE_SEND_DEPTH / 8) * 3 + ROCE_MAX_PEERS * RING_WINDOW <= ROCE_SEND_DEPTH, + "send queue depth must hold reserved chains plus all per-origin ring ACKs"); typedef struct { uint64_t serial, target, offset, flag_offset, flag_value, nbytes, previous; uint8_t padding[72]; @@ -209,6 +211,8 @@ typedef struct { uint32_t mtu[ROCE_MAX_HCAS]; uint32_t qp_num[ROCE_MAX_HCAS][ROCE_MAX_PEERS][ROCE_MAX_LANES]; uint32_t lanes; + uint32_t ring_window; + uint64_t ring_bytes; } roce_blob_t; typedef struct { @@ -568,6 +572,8 @@ int roce_local_blob(roce_ctx_t* c, void* out, uint64_t out_len) { blob.region_addr = (uint64_t)(uintptr_t)c->region; blob.ring_addr = (uint64_t)(uintptr_t)c->ring; blob.lanes = (uint32_t)c->n_lane; + blob.ring_window = c->ring == NULL ? 0 : (uint32_t)RING_WINDOW; + blob.ring_bytes = c->ring == NULL ? 0 : (uint64_t)ring_bytes(c->world); for (int h = 0; h < c->n_hca; h++) { blob.rkey[h] = c->hca[h].mr->rkey; blob.ring_rkey[h] = c->ring == NULL ? 0 : c->ring_mr[h]->rkey; @@ -645,6 +651,8 @@ int roce_connect(roce_ctx_t* c, const void* blobs, uint64_t blobs_len, const int snprintf(c->err, sizeof(c->err), "invalid rail count %d", n_rail); return -1; } + const uint32_t expected_ring_window = c->ring == NULL ? 0 : (uint32_t)RING_WINDOW; + const uint64_t expected_ring_bytes = c->ring == NULL ? 0 : (uint64_t)ring_bytes(c->world); const roce_blob_t* all = (const roce_blob_t*)blobs; for (int p = 0; p < c->world; p++) { if ((all[p].ring_addr != 0) != (c->ring != NULL)) { @@ -656,6 +664,13 @@ int roce_connect(roce_ctx_t* c, const void* blobs, uint64_t blobs_len, const int p, all[p].lanes, c->n_lane); return -1; } + if (all[p].ring_window != expected_ring_window || all[p].ring_bytes != expected_ring_bytes) { + snprintf(c->err, sizeof(c->err), + "rank %d uses ring window %u (%llu bytes) where this rank uses %u (%llu bytes)", p, + all[p].ring_window, (unsigned long long)all[p].ring_bytes, expected_ring_window, + (unsigned long long)expected_ring_bytes); + return -1; + } c->peer_addr[p] = all[p].region_addr; c->ring_addr[p] = all[p].ring_addr; } @@ -760,11 +775,12 @@ static int drain_cq(roce_ctx_t* c, int h) { } // Wait until lane `lane` of HCA h can take one more stripe to peer p. Direct stripes need two -// requests and relay packets need three. A quarter of the queue leaves room -// for provider overhead and per-origin acks posted while draining completions. +// requests and relay packets chain three. One eighth of the send queue (64 signaled ops, at most +// 192 chained WRs) leaves room for up to 256 unreserved per-origin ACKs at world=16 with +// RING_WINDOW=16 plus provider SQE overhead. static int reserve(roce_ctx_t* c, int h, int p, int lane) { roce_hca_t* hca = &c->hca[h]; - while (hca->outstanding[p][lane] >= ROCE_SEND_DEPTH / 4) { + while (hca->outstanding[p][lane] >= ROCE_SEND_DEPTH / 8) { if (drain_all(c) != 0) { return -1; } @@ -899,7 +915,7 @@ static int ring_service_slot(roce_ctx_t* c, int origin, int rail, int slot) { c->ring_inflight[origin][rail][slot] = serial; int next = ring_next(c, target); int h = c->rail_hca[next][rail]; - if (c->hca[h].outstanding[next][0] >= ROCE_SEND_DEPTH / 4) return 1; + if (c->hca[h].outstanding[next][0] >= ROCE_SEND_DEPTH / 8) return 1; if (next != target) { if (ring_submit(c, origin, rail, slot, next, box->data, c->ring_mr[h]->lkey, d) != 0) return -1; *pending = 2; @@ -1347,6 +1363,9 @@ static int run_pipe_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t ch } } if (posted) { + if (c->ring != NULL && drain_all(c) != 0) { + return -1; + } idle = 0; continue; } diff --git a/tests/experimental/b12x/comm/test_roce_proxy.py b/tests/experimental/b12x/comm/test_roce_proxy.py index 7ce225f26a0..3b15aad04cc 100644 --- a/tests/experimental/b12x/comm/test_roce_proxy.py +++ b/tests/experimental/b12x/comm/test_roce_proxy.py @@ -137,3 +137,71 @@ def test_layout_places_variable_length_area_after_two_shot(): for bad in (1000, (1 << 32) + 4096): with pytest.raises(ValueError): Layout(4, 2 << 20, 0, bad) + + +def test_ring_blob_handshake_rejects_window_and_size_mismatch(): + """Ring connect rejects mismatched RING_WINDOW or ring region sizes while keeping ABI 14.""" + import ctypes + import mmap + import struct + from pathlib import Path + + from b12x.comm.roce._proxy import ABI_VERSION, BLOB_STRUCT_ERR, Layout, Proxy, load + + try: + lib = load() + except RuntimeError as exc: # no C compiler or libibverbs headers + pytest.skip(str(exc)) + + assert lib.roce_abi_version() == ABI_VERSION == 14 + blob_bytes = int(lib.roce_blob_bytes()) + assert blob_bytes == 1176 + + ib_root = Path("/sys/class/infiniband") + active_hca = None + if ib_root.is_dir(): + for dev in sorted(ib_root.iterdir()): + state = dev / "ports" / "1" / "state" + try: + if "ACTIVE" in state.read_text(): + active_hca = dev.name + break + except OSError: + continue + if active_hca is None: + return + + layout = Layout(2, 4096) + memory = mmap.mmap(-1, layout.total_bytes) + buf = (ctypes.c_char * layout.total_bytes).from_buffer(memory) + proxy = Proxy( + world_size=2, + rank=0, + hca_names=(active_hca,), + gid_index=0, + region_ptr=ctypes.addressof(buf), + region_bytes=layout.total_bytes, + slot_bytes=4096, + ring=True, + ) + try: + local = proxy.local_blob() + assert len(local) == blob_bytes + ring_window, ring_bytes = struct.unpack_from("