Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
3688f3c
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
b7be793
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
faf5901
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
3cc0397
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
c58d83c
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
25c02d3
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
6b5404d
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
06c4e75
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
062af8b
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
d7510ef
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
e16e331
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
649e788
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
664a8e2
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
4ad5029
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9bbc21a
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
cf19042
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
0ba7d30
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
0918809
Let a family's round planner drive the lane host with prompt pieces
jayleaton Oct 10, 2026
361f063
Lane host halt: a stop's drain deadline ends the rounds at the next b…
jayleaton Oct 10, 2026
e9f9210
Round planner test: both prompts queue before the host starts, and it…
jayleaton Oct 10, 2026
96ae865
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
df480e0
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
2c4d837
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
55bf0a7
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
9fbc2b3
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
bb53ff4
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
9eb765d
DeepSeek-V4.1 host side: release config, EXL3 pack headers, a rank's …
jayleaton Oct 11, 2026
865335e
DeepSeek-V4.1 weight forms built at load, and prepared weight folders…
jayleaton Oct 11, 2026
7ccfc5c
DeepSeek-V4.1 Engram host side: table layout, n-gram hashes of a wind…
jayleaton Oct 11, 2026
b775815
DeepSeek-V4.1 phase timers: where a serving round's time goes, by nam…
jayleaton Oct 11, 2026
def4859
DeepSeek-V4.1 Engram rows from packed per-rank shards on local NVMe, …
jayleaton Oct 11, 2026
c7d29ea
DeepSeek-V4.1 CUDA kernel sources: CSA2 attention and top-k, mHC, rou…
jayleaton Oct 11, 2026
b17df97
DeepSeek-V4.1 kernel tooling: nvcc in a container for any checkout la…
jayleaton Oct 11, 2026
73be75f
DeepSeek-V4.1 kernel launch bindings and their loader: each kernel's …
jayleaton Oct 11, 2026
e687ad5
DeepSeek-V4.1 device kernel tests: tf-dsv41-test, bit-exact against t…
jayleaton Oct 11, 2026
a7674d2
DeepSeek-V4.1 kernel prototypes: attention merge and phase timing, x3…
jayleaton Oct 11, 2026
c0605f0
DeepSeek-V4.1 device-side load: named weights in device buffers, run-…
jayleaton Oct 11, 2026
46bff5e
DeepSeek-V4.1 block emitter: a decode window as calls (attention, the…
jayleaton Oct 11, 2026
989ca7e
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
05c5213
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a3303c7
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
69e8a65
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
d7e89df
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
5826f89
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
5806e55
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
d335520
Graph cache: TF_GRAPH_CACHE=0 or off runs every step eagerly in any f…
jayleaton Oct 14, 2026
7ff83d8
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
9af2ea2
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
7c9a26e
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
8ebb0be
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
e9e0056
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
0472cbf
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
9688398
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
72132f0
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
e65583c
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
bf19238
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9a486bb
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
beae7e8
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
467504a
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
602aad5
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
23bf7d8
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
890dfc6
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
5410eac
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a5b8b35
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
a3e2fb1
TP NCCL: all_reduce's doc says NCCL adds in its ring's order, so its …
jayleaton Oct 14, 2026
0d0bdb2
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
2b3cccf
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
62a75ae
TP: keep Linux-only transport host tests off macOS builds
jayleaton Oct 15, 2026
2cf61d3
Sessions: honor zero residency budgets and reserve uncapped requests
jayleaton Oct 15, 2026
9367784
server: keep shared halt and drain on the existing template path
jayleaton Oct 15, 2026
864fe25
Graph cache: effective settings have one digest for ranks to compare …
jayleaton Oct 16, 2026
e2504f6
TP: bootstrap compares the graph cache's effective settings, so a ran…
jayleaton Oct 16, 2026
dcdc30c
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
368a0d3
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
7a6b32f
server: main's drain stop closes the engine without the closed lane-h…
jayleaton Oct 16, 2026
abcad7d
Merge ashhart/main into stack/02-graph-cache-arena
jayleaton Oct 16, 2026
02d455e
Merge stack/02-graph-cache-arena (with ashhart/main) into stack/03-tp…
jayleaton Oct 16, 2026
9266f3f
Merge stack/03-tp-fabric (with ashhart/main) into stack/04-sessions
jayleaton Oct 16, 2026
001ac2e
Merge stack/04-sessions (with ashhart/main) into stack/07-server
jayleaton Oct 16, 2026
33c0ef6
Merge stack/07-server (sessions, TP, graph cache, current main) into …
jayleaton Oct 16, 2026
af0bc53
Server tests: the drain test imports the named json module
jayleaton Oct 16, 2026
7051a3c
Merge stack/07-server's drain-test json import fix into stack/08a-config
jayleaton Oct 16, 2026
e6df8ad
merge origin/main: the round planner and halt on main's split lane host
jayleaton Oct 16, 2026
4da2618
Merge shared/lane-host-core (#861, on current main) into stack/08a-co…
jayleaton Oct 16, 2026
6b9bede
Merge stack/08a-config (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
f17db02
Merge stack/08b-engram (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
0137ac2
Merge stack/08c-kernels (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
8900c7c
Sessions: a failed prefix insert leaves the index as it was
jayleaton Oct 17, 2026
3aeebb9
CUDA arena: a reuse whose address-map insertion fails keeps its range…
jayleaton Oct 17, 2026
46161ac
Merge stack/02-graph-cache-arena (the #635 arena fix) into stack/03-t…
jayleaton Oct 17, 2026
78ce706
Merge stack/03-tp-fabric (the #635 arena and #637 prefix fixes) into …
jayleaton Oct 17, 2026
9c5bd60
Merge stack/04-sessions (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
eaa7953
Merge stack/07-server (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
4370f91
Merge stack/08a-config (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
a0f9598
Merge stack/08b-engram (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
98a1695
Merge stack/08c-kernels (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
88418d8
test(tp): reject malformed runner commands before configuration
cwschroeder Oct 17, 2026
4c69b0d
fix(tp): parse runner commands before opening sessions
cwschroeder Oct 17, 2026
4df28ce
test(tp): retain verbs load failures in HCA diagnostics
cwschroeder Oct 17, 2026
0a80da0
fix(tp): distinguish verbs load errors from absent HCA ports
cwschroeder Oct 17, 2026
999cb03
Merge stack/03-tp-fabric (adopted #636 runner CLI validation and HCA …
jayleaton Oct 18, 2026
5f14a64
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
749240a
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
7122563
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
6390cf1
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
829c4c9
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
cdde1a4
Merge ashhart/main (1.1.0, 76aafcf2) into stack/02-graph-cache-arena
jayleaton Oct 18, 2026
adbdb8a
CUDA launches: a not-ready launch returns NotReady unlogged, as Drive…
jayleaton Oct 18, 2026
3bd6a6a
Merge stack/02-graph-cache-arena (adbdb8ab, main 76aafcf2) into stack…
jayleaton Oct 18, 2026
54141a1
Merge stack/03-tp-fabric (3bd6a6ab, main 76aafcf2) into stack/04-sess…
jayleaton Oct 18, 2026
80d4ed5
Merge stack/04-sessions (54141a1e, main 76aafcf2) into stack/07-server
jayleaton Oct 18, 2026
aebceae
Merge stack/07-server (80d4ed5c, main 76aafcf2) into stack/08a-config
jayleaton Oct 18, 2026
1003f08
Build: DeepSeek-V4.1's host tests on hosts other than Windows, whose …
jayleaton Oct 18, 2026
aa96782
Merge stack/08a-config (1003f08f, main 76aafcf2) into stack/08b-engram
jayleaton Oct 18, 2026
3f758dc
Merge stack/08b-engram (aa967828, main 76aafcf2) into stack/08c-kernels
jayleaton Oct 18, 2026
ea54d42
Merge stack/08c-kernels (3f758dcf, main 76aafcf2) into stack/08e-block
jayleaton Oct 18, 2026
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
Prev Previous commit
Next Next commit
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
… TF_TP_ROCE_HCA nor NCCL_IB_HCA is set

- `tf-tp-test hca` (host only, no GPU): both ranks run the RoCE pick over the bootstrap, the same rule the wire uses,
  and each prints its own devices as `NCCL_IB_HCA=name,...` (no GIDs). It exits 1 with the reason when the host has
  no usable port or no port on a /24 the peer shares, so no rank falls back to its first ports for NCCL.
- run_hosts.sh runs it when neither variable is set, exports the names for every step and logs each rank's pick;
  a failed or malformed `hca` stops the runner (exit 3) with the reason; `hca` is capped at TF_TP_PLAN_TIMEOUT + 31 s, so
  its own reason (no peer joined in time, port in use, the peer closed, a bootstrap error) comes first. An explicit NCCL_IB_HCA, an explicit
  TF_TP_ROCE_HCA (NCCL_IB_HCA derived from it) or both keep their behaviour; the many-devices warning is gone.
  Both hosts use the same mode: with one naming its ports and the other not, the latter's `hca` finds no peer and
  stops after TF_TP_PLAN_TIMEOUT with that reason (README).
- roce_pick: `select` / `selectFrom` return how the devices were chosen (`resolve` is unchanged for the wire),
  `joinNames` and `hcaLine` give the NCCL list. Host tests: the port scan over a stub verbs table (state, Ethernet,
  the IPv4 RoCE v2 GID index), then both ranks through a real bootstrap on the two hosts #636's receipt reports
  (modelled, documentation addresses), an explicit rank, and a host without ports.
  • Loading branch information
jayleaton committed Oct 16, 2026
commit 890dfc6442e1aa04d6ada6b9978a3aadef66495e
8 changes: 8 additions & 0 deletions zig/src/fabric/tp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Each device pairs with the peer device on the same IPv4 /24.
`NCCL_IB_HCA` should name the same devices without the `:gid` suffix.
In NCCL, `:N` after a device name selects port `N`, not a GID index.

`run_hosts.sh` sets `NCCL_IB_HCA` from `TF_TP_ROCE_HCA` when only that is set.
When neither is set, it first runs `tf-tp-test hca` on both ranks: the RoCE pick, made with the peer over the bootstrap.
The same rule picks the wire's devices, and each rank prints its own as `NCCL_IB_HCA=name,...`.
`hca` needs no GPU, and it exits 1 when no port shares a /24 with the peer, so no host falls back to its first ports.
The runner logs each rank's pick and stops with that reason when `hca` fails.
Both hosts must use the same mode: if one names its ports and the other names none, only the latter runs `hca`.
It finds no peer and stops after `TF_TP_PLAN_TIMEOUT` (default 300 s) with that reason.

Two DGX Spark nodes, with the second ConnectX-7 port of each cabled to the other (both PCIe functions of that
port), and the first port cabled to a third node:

Expand Down
40 changes: 38 additions & 2 deletions zig/src/fabric/tp/gpu_main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! `tf-tp-test suite | failfast RANK fatal|kill|transport [N [HOLD_S]] | roce-host | kernel`: one rank of a TP=2 test.
//! `tf-tp-test suite | failfast RANK fatal|kill|transport [N [HOLD_S]] | roce-host | hca | kernel`: one rank of TP=2.
const std = @import("std");
const cuda = @import("cuda");
const tp = @import("tp");
Expand All @@ -11,7 +11,7 @@ const max_bytes = (4 << 20) + 4096;
pub fn main(init: std.process.Init) !u8 {
const args = try init.minimal.args.toSlice(init.arena.allocator());
if (args.len < 2) {
std.debug.print("usage: tf-tp-test suite | failfast RANK fatal|kill|transport [N [HOLD_S]] | roce-host | kernel\n", .{});
std.debug.print("usage: tf-tp-test suite | failfast RANK fatal|kill|transport [N [HOLD_S]] | roce-host | hca | kernel\n", .{});
return 2;
}
// `kernel`: whether the build embeds the mailbox kernel (-Dtp-nvcc or -Dtp-fatbin), for scripts to check first
Expand All @@ -21,6 +21,7 @@ pub fn main(init: std.process.Init) !u8 {
}
const cfg = try tp.Config.fromEnv();
if (std.mem.eql(u8, args[1], "roce-host")) return @import("roce_host.zig").run(cfg, init.gpa);
if (std.mem.eql(u8, args[1], "hca")) return hca(cfg, init.io);
if (std.mem.eql(u8, args[1], "failfast") and (!cfg.failfast or args.len > 5) and !hostOnlyAllowed(cfg)) {
std.debug.print("REFUSED: failfast with TF_TP_FAILFAST=0 or a hold is host-only: TF_TP_ALLOW_HOST_ONLY=1 and no RDMA wire\n", .{});
return 2;
Expand Down Expand Up @@ -50,6 +51,41 @@ pub fn main(init: std.process.Init) !u8 {
return 2;
}

/// `hca` (host only, no GPU): both ranks' RoCE pick over the bootstrap; this rank's `NCCL_IB_HCA=...` line on stdout.
fn hca(cfg: tp.Config, io: std.Io) !u8 {
var b = tp.Bootstrap.open(cfg) catch |e| {
const s = @as(f64, @floatFromInt(cfg.connect_timeout_ns)) / std.time.ns_per_s;
switch (e) {
// a peer that never runs `hca` (it names its ports, or is not up) leaves this rank waiting until here
error.Timeout => std.debug.print("FAIL hca rank {d}: no peer joined within TF_TP_PLAN_TIMEOUT ({d:.1} s): is its runner up, and leaving TF_TP_ROCE_HCA and NCCL_IB_HCA unset as this one does?\n", .{ cfg.rank, s }),
error.AddressInUse => std.debug.print("FAIL hca rank {d}: port {d} (TF_TP_PORT + 7) is in use on this host\n", .{ cfg.rank, cfg.link_port }),
error.Closed => std.debug.print("FAIL hca rank {d}: the peer closed the bootstrap connection\n", .{cfg.rank}),
else => std.debug.print("FAIL hca rank {d}: bootstrap: {t}\n", .{ cfg.rank, e }),
}
return 1;
};
defer b.close();
var specs: [tp.roce_verbs.max_hcas]tp.roce_verbs.HcaSpec = undefined;
const pick = tp.roce_pick.select(cfg, &b, &specs) catch |e| {
std.debug.print("FAIL hca rank {d}: {t} while picking the ports with the peer\n", .{ cfg.rank, e });
return 1;
};
var buf: [tp.roce_pick.list_bytes + 16]u8 = undefined;
const line = tp.roce_pick.hcaLine(pick, &specs, &buf) catch |e| {
const why = switch (e) {
error.NoRoceDevice => "no active Ethernet RDMA port with an IPv4 RoCE v2 GID on this host",
error.NoSharedSubnet => "no RoCE port here shares an IPv4 /24 with the peer's",
};
std.debug.print("FAIL hca rank {d}: {s}: name the peer's ports in TF_TP_ROCE_HCA or NCCL_IB_HCA\n", .{ cfg.rank, why });
return 1;
};
var names: [tp.roce_pick.list_bytes]u8 = undefined;
const how = if (pick.explicit) "TF_TP_ROCE_HCA" else "on a /24 the peer shares";
std.debug.print("INFO hca rank {d}: {s} ({s})\n", .{ cfg.rank, try tp.roce_pick.joinNames(specs[0..pick.n], true, &names), how });
try std.Io.File.stdout().writeStreamingAll(io, line);
return 0;
}

fn suite(s: *tp.Session, d: *const cuda.Driver, gpa: std.mem.Allocator, cfg: tp.Config) !u8 {
var c = try checks.Ctx.init(d, gpa, cfg.rank, max_bytes);
defer c.deinit();
Expand Down
216 changes: 208 additions & 8 deletions zig/src/fabric/tp/roce_pick.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const std = @import("std");
const log = @import("log.zig");
const rv = @import("roce_verbs.zig");
const bootstrap = @import("bootstrap.zig");
const sock = @import("sock.zig");
const Config = @import("config.zig").Config;
const Verbs = @import("verbs").Verbs;

/// Ports a rank lists to its peers (a DGX Spark's ConnectX-7 shows four RDMA devices).
pub const max_ports = 8;
const blob_bytes = 1 + 4 * max_ports;

pub const Pick = struct { n: usize, shared: bool };
/// How many devices, and how: on a /24 the peer shares (`shared`), named by TF_TP_ROCE_HCA (`explicit`), or neither.
pub const Pick = struct { n: usize, shared: bool, explicit: bool = false };

fn usable(p: rv.Port) bool {
return p.active and p.ethernet and p.ipv4 != null;
Expand Down Expand Up @@ -83,26 +85,67 @@ fn decode(blob: []const u8, out: [][4]u8) usize {

/// The devices for the verbs wire. A collective: every rank calls it, including ranks that set TF_TP_ROCE_HCA.
pub fn resolve(cfg: Config, b: *bootstrap.Bootstrap, out: *[rv.max_hcas]rv.HcaSpec) !usize {
return (try select(cfg, b, out)).n;
}

/// `resolve` with how the devices were chosen, over this host's ports (a collective, as `resolve`).
pub fn select(cfg: Config, b: *bootstrap.Bootstrap, out: *[rv.max_hcas]rv.HcaSpec) !Pick {
var ports: [max_ports]rv.Port = undefined;
var n_ports: usize = 0;
if (Verbs.open(null)) |opened| {
var v = opened;
defer v.close();
n_ports = rv.scanPorts(&v, &ports);
} else |_| {}
return selectFrom(cfg, b, ports[0..n_ports], out);
}

/// `select` over given ports (the scan's, or a test's fixture ports).
pub fn selectFrom(cfg: Config, b: *bootstrap.Bootstrap, ports: []const rv.Port, out: *[rv.max_hcas]rv.HcaSpec) !Pick {
var mine: [blob_bytes]u8 = undefined;
var buf: [bootstrap.max_ranks * blob_bytes]u8 = undefined;
var blobs: [bootstrap.max_ranks][]const u8 = undefined;
const all = try b.allGather(encode(ports[0..n_ports], &mine), &buf, &blobs);
if (cfg.roce_hca) |text| return rv.parseSpecs(text, out);
const all = try b.allGather(encode(ports, &mine), &buf, &blobs);
if (cfg.roce_hca) |text| return .{ .n = try rv.parseSpecs(text, out), .shared = false, .explicit = true };
var peer: [bootstrap.max_ranks * max_ports][4]u8 = undefined;
var n_peer: usize = 0;
for (all, 0..) |blob, r| if (r != b.rank) {
n_peer += decode(blob, peer[n_peer..]);
};
const pick = choose(ports[0..n_ports], peer[0..n_peer], out, cfg.roce_hcas);
const pick = choose(ports, peer[0..n_peer], out, cfg.roce_hcas);
if (!pick.shared and pick.n > 0) log.warn("tp roce: no RoCE port shares an IPv4 /24 with the peer's; using the first {d} active (set TF_TP_ROCE_HCA)", .{pick.n});
return pick.n;
return pick;
}

/// Device names joined by commas, with each `:gid` when `gid` (TF_TP_ROCE_HCA's form) or without (NCCL_IB_HCA's).
pub fn joinNames(specs: []const rv.HcaSpec, gid: bool, buf: []u8) error{WriteFailed}![]const u8 {
var w: std.Io.Writer = .fixed(buf);
for (specs, 0..) |s, i| {
try w.print("{s}{s}", .{ if (i == 0) "" else ",", s.name[0..s.name_len] });
if (gid) if (s.gid) |g| try w.print(":{d}", .{g});
}
return w.buffered();
}

/// Room for `joinNames` of every device a rank stripes over (a name is under 64 bytes, a GID index at most 3 digits).
pub const list_bytes = rv.max_hcas * (64 + 4);

pub const HcaError = error{
/// no active Ethernet port with an IPv4 RoCE v2 GID on this host
NoRoceDevice,
/// this host's ports share no IPv4 /24 with the peer's: NCCL would not reach the peer on them either
NoSharedSubnet,
};

/// `tf-tp-test hca`'s line for run_hosts.sh: `NCCL_IB_HCA=` the picked devices; never the first local ports alone.
pub fn hcaLine(pick: Pick, specs: []const rv.HcaSpec, buf: *[list_bytes + 16]u8) HcaError![]const u8 {
if (pick.n == 0) return error.NoRoceDevice;
if (!pick.shared and !pick.explicit) return error.NoSharedSubnet;
const prefix = "NCCL_IB_HCA=";
@memcpy(buf[0..prefix.len], prefix);
const joined = joinNames(specs[0..pick.n], false, buf[prefix.len .. buf.len - 1]) catch unreachable; // list_bytes holds them
buf[prefix.len + joined.len] = '\n';
return buf[0 .. prefix.len + joined.len + 1];
}

fn port(name: []const u8, active: bool, ipv4: ?[4]u8) rv.Port {
Expand All @@ -114,9 +157,7 @@ fn port(name: []const u8, active: bool, ipv4: ?[4]u8) rv.Port {
}

fn names(specs: []const rv.HcaSpec, buf: []u8) []const u8 {
var w: std.Io.Writer = .fixed(buf);
for (specs, 0..) |s, i| w.print("{s}{s}", .{ if (i == 0) "" else ",", s.name[0..s.name_len] }) catch unreachable;
return w.buffered();
return joinNames(specs, false, buf) catch unreachable;
}

// A host with two neighbours: port f0 cabled to one Spark (both functions on one /24), port f1 to the other.
Expand Down Expand Up @@ -184,3 +225,162 @@ test "two rails on one /24 count once per peer port; no shared /24 keeps the lis
try std.testing.expectEqual(@as(usize, 0), decode(&.{ 2, 1, 2, 3, 4 }, &out));
try std.testing.expectEqual(@as(?u8, 3), specs[0].gid);
}

test "hca's line: the picked names for NCCL; no device or no shared /24 is refused, an explicit list is taken" {
var specs: [rv.max_hcas]rv.HcaSpec = undefined;
var line: [list_bytes + 16]u8 = undefined;
var peer_buf: [max_ports][4]u8 = undefined;
const shared = choose(&a_ports, listed(&b_ports, &peer_buf), &specs, 2);
try std.testing.expectEqualStrings("NCCL_IB_HCA=rocep1s0f1,roceP2p1s0f1\n", try hcaLine(shared, &specs, &line));
var buf: [list_bytes]u8 = undefined;
try std.testing.expectEqualStrings("rocep1s0f1:3,roceP2p1s0f1:3", try joinNames(specs[0..shared.n], true, &buf));
// the old fallback (first active ports, no /24 in common) is what NCCL failed on: refused, never printed
try std.testing.expectError(error.NoSharedSubnet, hcaLine(choose(&a_ports, &.{.{ 10, 9, 9, 9 }}, &specs, 2), &specs, &line));
try std.testing.expectError(error.NoRoceDevice, hcaLine(choose(&.{}, &.{}, &specs, 2), &specs, &line));
const n = try rv.parseSpecs("rocep1s0f1:3, roceP2p1s0f1", &specs);
try std.testing.expectEqualStrings("NCCL_IB_HCA=rocep1s0f1,roceP2p1s0f1\n", try hcaLine(.{ .n = n, .shared = false, .explicit = true }, &specs, &line));
}

/// One RDMA device for `rv.scanPorts` through a stub verbs table: port 1 up or down, Ethernet, mlx5's GIDs 0-3.
const FakeDev = struct { name: [:0]const u8, up: bool, ipv4: [4]u8 = @splat(0) };

const ScanFixture = struct {
const vabi = @import("verbs").abi;
var host: []const FakeDev = &.{};
var list: [max_ports:null]?*vabi.Device = undefined;

fn handle(i: usize) usize {
return 0x10000 * (i + 1);
}
fn index(p: *const anyopaque) usize {
return @intFromPtr(p) / 0x10000 - 1;
}
fn getList(num: *c_int) callconv(.c) ?[*:null]?*vabi.Device {
for (host, 0..) |_, i| list[i] = @ptrFromInt(handle(i));
list[host.len] = null;
num.* = @intCast(host.len);
return &list;
}
fn freeList(_: [*:null]?*vabi.Device) callconv(.c) void {}
fn devName(d: *vabi.Device) callconv(.c) ?[*:0]const u8 {
return host[index(d)].name.ptr;
}
fn openDev(d: *vabi.Device) callconv(.c) ?*vabi.Context {
return @ptrFromInt(@intFromPtr(d));
}
fn closeDev(_: *vabi.Context) callconv(.c) c_int {
return 0;
}
fn queryPort(c: *vabi.Context, _: u8, a: *vabi.PortAttr) callconv(.c) c_int {
a.* = std.mem.zeroes(vabi.PortAttr);
a.state = if (host[index(c)].up) vabi.port_active else 1;
a.link_layer = 2; // Ethernet
a.gid_tbl_len = 4;
return 0;
}
fn queryGid(c: *vabi.Context, _: u8, i: c_int, g: *vabi.Gid) callconv(.c) c_int {
g.* = .{ .raw = @splat(0) };
if (i < 2) {
g.raw[0] = 0xfe;
g.raw[1] = 0x80;
g.raw[15] = @intCast(index(c) + 1);
} else {
g.raw[10] = 0xff;
g.raw[11] = 0xff;
g.raw[12..16].* = host[index(c)].ipv4;
}
return 0;
}
fn gidEx(_: *vabi.Context, _: u32, i: u32, e: *vabi.GidEntry, _: u32, _: usize) callconv(.c) c_int {
e.* = std.mem.zeroes(vabi.GidEntry);
e.gid_type = if (i % 2 == 1) 2 else 1; // odd indices RoCE v2
return 0;
}

/// `rv.scanPorts` over `devs` (names that no real /sys/class/infiniband has, so sysfs never answers for them).
fn scan(devs: []const FakeDev, out: *[max_ports]rv.Port) []const rv.Port {
host = devs;
var v: Verbs = undefined;
v.api.ibv_get_device_list = getList;
v.api.ibv_free_device_list = freeList;
v.api.ibv_get_device_name = devName;
v.api.ibv_open_device = openDev;
v.api.ibv_close_device = closeDev;
v.api.ibv_query_port = queryPort;
v.api.ibv_query_gid = queryGid;
v.gid_ex = gidEx;
return out[0..rv.scanPorts(&v, out)];
}
};

/// #636's reported two GB10 hosts, modelled (no capture): device order and picks reported; addresses, states assumed.
const hosts = [2][4]FakeDev{ .{
.{ .name = "fx_rocep1s0f0", .up = true, .ipv4 = .{ 192, 0, 2, 1 } },
.{ .name = "fx_rocep1s0f1", .up = false },
.{ .name = "fx_roceP2p1s0f0", .up = true, .ipv4 = .{ 198, 51, 100, 1 } },
.{ .name = "fx_roceP2p1s0f1", .up = false },
}, .{
.{ .name = "fx_rocep1s0f0", .up = false },
.{ .name = "fx_rocep1s0f1", .up = true, .ipv4 = .{ 192, 0, 2, 2 } },
.{ .name = "fx_roceP2p1s0f0", .up = false },
.{ .name = "fx_roceP2p1s0f1", .up = true, .ipv4 = .{ 198, 51, 100, 2 } },
} };

test "the port scan reads a host's devices through verbs: state, Ethernet, the IPv4 RoCE v2 GID index" {
var out: [max_ports]rv.Port = undefined;
const a = ScanFixture.scan(&hosts[0], &out);
try std.testing.expectEqual(@as(usize, 4), a.len);
try std.testing.expectEqualStrings("fx_rocep1s0f0", a[0].spec.name[0..a[0].spec.name_len]);
try std.testing.expect(a[0].active and a[0].ethernet and !a[1].active);
try std.testing.expectEqual(@as(?[4]u8, .{ 192, 0, 2, 1 }), a[0].ipv4);
try std.testing.expectEqual(@as(?u8, 3), a[0].spec.gid); // the v2 IPv4 GID, not v1's index 2
try std.testing.expectEqual(@as(?[4]u8, null), a[1].ipv4);
}

/// One rank's `selectFrom` over a real bootstrap (rank 0 on `l`), its hca line or error into `out`.
fn hcaRank(cfg: Config, l: ?sock.Fd, ports: []const rv.Port, out: *[list_bytes + 16]u8, line: *HcaError![]const u8, err: *?anyerror) void {
var b = (if (l) |fd| bootstrap.Bootstrap.acceptOn(fd, 2, 5 * std.time.ns_per_s) else bootstrap.Bootstrap.open(cfg)) catch |e| {
err.* = e;
return;
};
defer b.close();
var specs: [rv.max_hcas]rv.HcaSpec = undefined;
const pick = selectFrom(cfg, &b, ports, &specs) catch |e| {
err.* = e;
return;
};
line.* = hcaLine(pick, &specs, out);
}

fn hcaPair(cfgs: [2]Config, ports: [2][]const rv.Port, lines: *[2]HcaError![]const u8, bufs: *[2][list_bytes + 16]u8) !void {
const l = try bootstrap.testListener();
defer sock.close(l.fd);
var errs: [2]?anyerror = @splat(null);
var c1 = cfgs[1];
c1.link_port = l.port;
c1.connect_timeout_ns = 5 * std.time.ns_per_s;
const t = try std.Thread.spawn(.{}, hcaRank, .{ c1, null, ports[1], &bufs[1], &lines[1], &errs[1] });
hcaRank(cfgs[0], l.fd, ports[0], &bufs[0], &lines[0], &errs[0]);
t.join();
for (errs) |e| if (e) |x| return x;
}

test "hca over a real bootstrap: both ranks print their peer-linked ports; an explicit rank keeps its list; no ports fail both" {
var lines: [2]HcaError![]const u8 = undefined;
var bufs: [2][list_bytes + 16]u8 = undefined;
var scanned: [2][max_ports]rv.Port = undefined;
const ports = [2][]const rv.Port{ ScanFixture.scan(&hosts[0], &scanned[0]), ScanFixture.scan(&hosts[1], &scanned[1]) };
const auto: [2]Config = .{ .{ .rank = 0 }, .{ .rank = 1 } };
try hcaPair(auto, ports, &lines, &bufs);
try std.testing.expectEqualStrings("NCCL_IB_HCA=fx_rocep1s0f0,fx_roceP2p1s0f0\n", try lines[0]);
try std.testing.expectEqualStrings("NCCL_IB_HCA=fx_rocep1s0f1,fx_roceP2p1s0f1\n", try lines[1]);
// TF_TP_ROCE_HCA on one rank: its list as given (no GID for NCCL); it still sends its ports to the other
const one: [2]Config = .{ .{ .rank = 0, .roce_hca = "fx_rocep1s0f0:3" }, .{ .rank = 1 } };
try hcaPair(one, ports, &lines, &bufs);
try std.testing.expectEqualStrings("NCCL_IB_HCA=fx_rocep1s0f0\n", try lines[0]);
try std.testing.expectEqualStrings("NCCL_IB_HCA=fx_rocep1s0f1,fx_roceP2p1s0f1\n", try lines[1]);
// a host without usable ports: it has none, its peer shares no /24 with it, and both refuse
try hcaPair(auto, .{ &.{}, ports[1] }, &lines, &bufs);
try std.testing.expectError(error.NoRoceDevice, lines[0]);
try std.testing.expectError(error.NoSharedSubnet, lines[1]);
}
Loading