Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
157 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
6d44dab
CUDA runtime: prioritized streams, external event records, 2D async c…
jayleaton Oct 11, 2026
63e708f
CUDA AOT sets: aot.json specs parsed and matched without cubins, and …
jayleaton Oct 11, 2026
2eeabf7
CUDA memory: a driver allocation count, the arena's first failed chun…
jayleaton Oct 11, 2026
a89bd4f
Graph cache: named caches publish their counts, the floor's state is …
jayleaton Oct 11, 2026
05977fa
Session store: historyOf returns an entry's ids from RAM or from the …
jayleaton Oct 11, 2026
063e855
Graph cache registry: slots claimed, published, released and read und…
jayleaton Oct 11, 2026
d800c62
DeepSeek-V4.1 KV memory: the family's pages, split KV's exchange, the…
jayleaton Oct 11, 2026
bf5a8e8
DeepSeek-V4.1 forward: decode windows over the block emitter, split K…
jayleaton Oct 11, 2026
cd1e3d8
DeepSeek-V4.1 prefill: prompt segments over the prefill emitter, CED …
jayleaton Oct 11, 2026
4b5a078
DeepSeek-V4.1 keyed sampling: the exact host rule, the ranks' pipelin…
jayleaton Oct 11, 2026
1b1e565
DeepSeek-V4.1 drafting, host side: DSpark's chain, cost-derived depth…
jayleaton Oct 11, 2026
18121a3
DeepSeek-V4.1 DSpark on the GPU: the drafting pass as calls, candidat…
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
cf649ff
Merge stack/08e-block (current main, shared layers, #861) into stack/…
jayleaton Oct 16, 2026
4a02606
Merge stack/08r-runtime (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
968a4f2
Merge stack/09a-kv (current main, shared layers, #861) into stack/09b…
jayleaton Oct 16, 2026
1d0b2d7
Merge stack/09b-forward (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
ed4239b
Merge stack/10-prefill (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
a6bc6c4
Merge stack/11a-sampling (current main, shared layers, #861) into sta…
jayleaton Oct 16, 2026
999755d
Merge stack/11b-draft (current main, shared layers, #861) into stack/…
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
28e33cc
Merge stack/08e-block (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
f5a0b8b
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
3b7681f
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
334ce65
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
6b97211
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
fc25bef
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
c58f30c
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
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
b81382d
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
2be028e
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
e47ed0e
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
e548a60
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
2fc04e3
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
92ff736
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
043a4c8
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
d6190dc
Merge stack/08e-block (ea54d42d, main 76aafcf2) into stack/08r-runtime
jayleaton Oct 18, 2026
6742872
Merge stack/08r-runtime (d6190dcf, main 76aafcf2) into stack/09a-kv
jayleaton Oct 18, 2026
a88da5d
Build: DeepSeek-V4.1's KV host tests also skip Windows, like the fami…
jayleaton Oct 18, 2026
696e58a
Merge stack/09a-kv (a88da5de, main 76aafcf2) into stack/09b-forward
jayleaton Oct 18, 2026
4ac0c37
Merge stack/09b-forward (696e58a7, main 76aafcf2) into stack/10-prefill
jayleaton Oct 18, 2026
a4ca6d3
Merge stack/10-prefill (4ac0c37d, main 76aafcf2) into stack/11a-sampling
jayleaton Oct 18, 2026
5e6e8c6
Merge stack/11a-sampling (a4ca6d39, main 76aafcf2) into stack/11b-draft
jayleaton Oct 18, 2026
3f0fc40
Merge stack/11b-draft (5e6e8c68, main 76aafcf2) into stack/11c-device…
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
DeepSeek-V4.1 DSpark on the GPU: the drafting pass as calls, candidat…
…es and chain on the device, several slots' statics.

- dspark_emit: DSpark's drafting pass (ingest, then the pass) as calls on the block emitter; dspark_layout: its statics,
  ring rows and the overlapped ingest's scratch roles; dspark_rows: several slots' grouping, pass statics, plan-link
  messages and candidate order (TF_DSV41_SLOT_DRAFTS).
- dspark_gpu and dspark_gpu_device: draft/iface.zig's `Pass` over those launches on every rank, one slot;
  dspark_dev: candidates on the GPU in CUDA graphs (TF_DSV41_DRAFT_GRAPHS), self-checked against the host.
- Tests: slot_drafts_test, slot_drafts_chain_test and slot_drafts_fixture (one to three slot passes equal the one-slot
  pass; the overlapped ingest, the device chain and the batched main_proj on the real emitter) in
  test-dsv41-slot-drafts, part of test-dsv41 and `zig build test`; the family's host suite compiles the DSpark files.
  • Loading branch information
jayleaton committed Oct 11, 2026
commit 18121a32343b87aaf16750447c19b4e5abeaecfd
3 changes: 2 additions & 1 deletion zig/build/dsv41.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ pub fn add(b: *std.Build, test_step: *std.Build.Step) void {
step.dependOn(&run.step);
test_step.dependOn(&run.step);
}
// the prefill emitter's own suites, each its own test root (also in test-dsv41 and `zig build test`)
// the prefill emitter's and DSpark's own suites, each its own test root (also in test-dsv41 and `zig build test`)
const suites = [_][3][]const u8{
.{ "test-dsv41-gm2pf", "gm2pf_test.zig", "DeepSeek-V4.1 x3gm v2 (TF_DSV41_GM_V2) on the real prefill emitter, host only" },
.{ "test-dsv41-longpf", "longpf_test.zig", "DeepSeek-V4.1 long prompts (stream top-k, split KV's row blocks) on the real prefill emitter, host only" },
.{ "test-dsv41-slot-drafts", "slot_drafts_test.zig", "DeepSeek-V4.1 DSpark drafting over several slots on the real emitter, host only" },
};
for (suites) |x| {
const run = b.addRunArtifact(b.addTest(.{ .root_module = rooted(b, host, .debug, x[1]) }));
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/deepseek_v41/deepseek_v41.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ test {
_ = @import("streamrb_test.zig"); // the stream top-k's read-back twins on the prefill emitter
// the keyed sampler's pipeline and device steps (TF_DSV41_SAMPLING; its golden runs in test-dsv41-sampling)
inline for (.{ @import("sampling_pipe.zig"), @import("sampling_dev.zig"), @import("sampling_gpu.zig") }) |t| std.testing.refAllDecls(t);
// DSpark's pass on the GPU (one slot; several slots' statics and messages): compiled on the host
inline for (.{ @import("dspark_dev.zig"), @import("dspark_emit.zig"), @import("dspark_gpu.zig"), @import("dspark_gpu_device.zig"), @import("dspark_layout.zig"), @import("dspark_rows.zig") }) |t| std.testing.refAllDecls(t);
// prepared weight folders (TF_DSV41_PREPARED)
_ = @import("prepared.zig");
}
445 changes: 445 additions & 0 deletions zig/src/families/deepseek_v41/dspark_dev.zig

Large diffs are not rendered by default.

542 changes: 542 additions & 0 deletions zig/src/families/deepseek_v41/dspark_emit.zig

Large diffs are not rendered by default.

427 changes: 427 additions & 0 deletions zig/src/families/deepseek_v41/dspark_gpu.zig

Large diffs are not rendered by default.

243 changes: 243 additions & 0 deletions zig/src/families/deepseek_v41/dspark_gpu_device.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
//! GpuPass on the device: passes started from the window's pick, graphed passes, the pass's glue steps.

const std = @import("std");
const cuda = @import("cuda");
const calls = @import("block_calls.zig");
const run = @import("run.zig");
const fwd = @import("forward.zig");
const emit = @import("dspark_emit.zig");
const dspark = @import("draft/dspark.zig");
const dsd = @import("dspark_dev.zig");
const ph = @import("phases.zig");
const gpu = @import("dspark_gpu.zig");
const GpuPass = gpu.GpuPass;
const checkCandidates = gpu.checkCandidates;

/// The device path is on and past its checks (a pass launched outside `propose` / `begin` then counts no check).
pub fn devReady(x: *const GpuPass) bool {
const d = x.dev orelse return false;
return d.on() and d.stats.passes >= dsd.check_passes;
}

/// after_pick, every rank: the window's taps into the rings, the statics on device, the pass, the event.
pub fn afterPick(ctx: *anyopaque, pick: fwd.Forward.PickDevice) anyerror!void {
const x: *GpuPass = @ptrCast(@alignCast(ctx));
const a = x.armed orelse return;
if (a.sampled) return; // the sampler's choice starts it (afterSample)
x.armed = null;
if (!pick.chain or pick.n != a.n or pick.start != a.start or pick.host == null or x.begun != null) return;
if (!x.devReady() or a.n > x.ingest_rows) return;
try x.launchFrom(a.start, a.n, pick.out, pick.n);
if (x.f.comm.rank() == 0 or x.f.link == null) x.dl = .{ .start = a.start, .n = a.n, .host = pick.host.?, .rows = a.n };
}

/// after_sample, every rank: the window's accept on the device (ds_accept_rows), then the pass as afterPick.
pub fn afterSample(ctx: *anyopaque, row0: u32, n: u32, chosen: u64) anyerror!void {
const x: *GpuPass = @ptrCast(@alignCast(ctx));
const a = x.armed orelse return;
if (!a.sampled) return;
x.armed = null;
const p = x.f.slot.pending orelse return;
if (row0 != 0 or n != a.n or p.start != a.start or p.n != a.n or x.begun != null) return;
if (!x.devReady() or a.n > x.ingest_rows or x.f.ids.len < n) return;
const r = x.f.runner;
// [ids n | segment 3 | acc 3] int64
const h: []i64 = @alignCast(std.mem.bytesAsSlice(i64, x.shost.?.bytes[0 .. 8 * 128]));
for (x.f.ids[0..n], 0..) |t, i| h[i] = t;
h[64..67].* = .{ 0, n, @intCast(a.start) };
const dev = x.sscr.?.ptr;
try cuda.DeviceBuffer.uploadAsync(.{ .d = r.d, .ptr = dev, .len = 8 * 67 }, 0, std.mem.sliceAsBytes(h[0..67]), r.stream.handle);
try r.kernels.others(r.stream).dsAcceptRows(chosen, dev, dev + 8 * 64, 1, dev + 8 * 68);
try r.d.check(r.d.api.cuMemcpyDtoHAsync_v2(x.shost.?.bytes.ptr + 8 * 68, dev + 8 * 68, 8 * 3, r.stream.handle), "cuMemcpyDtoHAsync");
try x.staged.?.record(r.stream);
// acc [accepted, bonus, next position] reads as a pick of 0 rows: ds_stage takes pick[1], pick[2]
try x.launchFrom(a.start, a.n, dev + 8 * 68, 0);
if (x.f.comm.rank() == 0 or x.f.link == null) x.dl = .{ .start = a.start, .n = 0, .host = @ptrCast(h[68..].ptr), .rows = a.n, .wait = true };
}

/// The device start's launches, every rank: taps, statics (ds_stage from `pick`), the pass, the event.
pub fn launchFrom(x: *GpuPass, start: u64, n: u32, pick: u64, nw: u32) !void {
const a = .{ .start = start, .n = n };
const d = x.dev.?;
const r = x.f.runner;
// every row of the window: rows past the commit are overwritten before any later pass reads them
try x.ingestAsync(a.start, 0, a.n);
// the statics as fillStatics stages them (placeholders), then every position-dependent one from the pick
const st = x.st;
const sv = try d.statics(0, @intCast(st.len64()), @intCast(st.len32()));
x.fillStatics(0, a.start, sv[0], sv[1]);
const s64 = r.addressOf("w.ds.i64") orelse return error.Unbound;
const s32 = r.addressOf("w.ds.i32") orelse return error.Unbound;
try cuda.DeviceBuffer.uploadAsync(.{ .d = r.d, .ptr = s64, .len = 8 * sv[0].len }, 0, std.mem.sliceAsBytes(sv[0]), r.stream.handle);
try cuda.DeviceBuffer.uploadAsync(.{ .d = r.d, .ptr = s32, .len = 4 * sv[1].len }, 0, std.mem.sliceAsBytes(sv[1]), r.stream.handle);
try r.kernels.others(r.stream).dsStage(pick, nw, s64, s32, @intCast(st.n), @intCast(st.window), @intCast(emit.ringRows(x.f.cfg)), x.valid, x.shape.noise, stageOffsets(st));
r.glue = .{ .ctx = x, .run = glueFn };
const extra = [_]u64{ if (x.send) |b| b.ptr else 0, if (x.send) |b| b.len else 0 };
try d.issueReplay(0, dsd.fingerprint(r, d, &extra), .{ .ctx = x, .run = passBody });
try d.mark();
try d.settle(); // counted here on every rank (rank 0's collect does not count it again)
}

/// ds_stage's offsets: positions, tokens, counts, lo, hi, anchor (ids and start at 0).
pub fn stageOffsets(st: emit.Statics) [6]i64 {
return .{ st.positions(), st.tokens(), st.counts(), st.lo(), st.hi(), st.anchor() };
}

/// A device-launched pass nobody took: its results dropped; the next pass follows it on the stream.
pub fn dropDevice(x: *GpuPass) void {
if (x.dl != null) x.stats.device_dropped += 1;
x.dl = null;
x.armed = null;
}

pub fn devOn(x: *const GpuPass) bool {
const d = x.dev orelse return false;
return d.on();
}

/// The device path: the statics from pinned staging (no sync), the pass (its graph once checks passed), the event.
pub fn devLaunch(x: *GpuPass, anchor: u32, start: u64) !void {
const d = x.dev.?;
const st = x.st;
const sv = try d.statics(0, @intCast(st.len64()), @intCast(st.len32()));
x.fillStatics(anchor, start, sv[0], sv[1]);
const r = x.f.runner;
try cuda.DeviceBuffer.uploadAsync(.{ .d = r.d, .ptr = r.addressOf("w.ds.i64") orelse return error.Unbound, .len = 8 * sv[0].len }, 0, std.mem.sliceAsBytes(sv[0]), r.stream.handle);
try cuda.DeviceBuffer.uploadAsync(.{ .d = r.d, .ptr = r.addressOf("w.ds.i32") orelse return error.Unbound, .len = 4 * sv[1].len }, 0, std.mem.sliceAsBytes(sv[1]), r.stream.handle);
r.glue = .{ .ctx = x, .run = glueFn };
const extra = [_]u64{ if (x.send) |b| b.ptr else 0, if (x.send) |b| b.len else 0 };
try d.issue(0, dsd.fingerprint(r, d, &extra), .{ .ctx = x, .run = passBody });
try d.mark();
}

pub fn passBody(ctx: *anyopaque, stream: cuda.Stream) anyerror!void {
const x: *GpuPass = @ptrCast(@alignCast(ctx));
_ = stream; // the runner's stream
try x.f.runner.window(x.pass_calls);
}

/// The device pass's results on the host (`unpack`: rank 0's chain reads them), a check's comparison, the verdict.
pub fn devCollect(x: *GpuPass, unpack: bool) !void {
const d = x.dev.?;
{
const tw = ph.start(x.f.phases, .@"draft.wait");
defer tw.stop();
try d.wait();
}
if (d.checked < dsd.check_passes) try d.check(x.last_logits, x.last_v, @intCast(x.st.n), 0);
if (unpack) try d.unpack(@intCast(x.st.n), 0, x.f.cfg.vocab, x.cand, x.cval, if (x.conf != null) x.hid else null);
try d.settle();
}

/// A device-launched pass's results (rank 0): waited for and unpacked; it was counted at its launch.
pub fn devResults(x: *GpuPass) !void {
const d = x.dev.?;
{
const tw = ph.start(x.f.phases, .@"draft.wait");
defer tw.stop();
try d.wait();
}
try d.unpack(@intCast(x.st.n), 0, x.f.cfg.vocab, x.cand, x.cval, if (x.conf != null) x.hid else null);
}

pub fn device(x: *GpuPass, slot: *?cuda.DeviceBuffer, bytes: usize) !u64 {
if (slot.* == null or slot.*.?.len < bytes) {
if (slot.*) |*b| b.free();
slot.* = try cuda.DeviceBuffer.alloc(x.f.runner.d, bytes);
}
return slot.*.?.ptr;
}

pub fn numel(t: calls.Tensor) usize {
var n: usize = 1;
for (t.shape) |d| n *= @intCast(d);
return n;
}

pub fn glueFn(ctx: *anyopaque, r: *run.Runner, c: *const calls.Call) anyerror!void {
const x: *GpuPass = @ptrCast(@alignCast(ctx));
const step = c.name["glue.".len..];
const s = r.stream.handle;
const k = r.kernels.others(r.stream);
const comm = x.f.comm;
const D: usize = x.f.cfg.hidden;
const Wd: usize = comm.world();
if (std.mem.eql(u8, step, "ds_embed")) {
// the block's ids (staged) through this rank's vocabulary rows, their all-sum, x hc_mult into the streams
const n: usize = @intCast(x.st.n);
const V: usize = x.f.cfg.vocab / Wd;
const embed = r.weights.get("embed") orelse return error.MissingWeight;
const rows = try x.device(&x.send, 2 * n * D);
try k.embedSend(try r.tensorAddr(c.args[2].arg.t), n, embed.ptr, @intCast(comm.rank() * V), V, D, rows);
const recv = try r.tensorAddr(c.args[1].arg.t);
try comm.allGather(rows, recv, n * D, .bf16, s);
return k.embedSum(recv, Wd, n, D, try r.tensorAddr(c.args[0].arg.t));
}
if (std.mem.eql(u8, step, "exchange_f32")) {
const part = c.args[0].arg.t;
const n = numel(part);
const half = try x.device(&x.send, 2 * n);
try k.castBf16(try r.tensorAddr(part), half, n);
return comm.allGather(half, try r.tensorAddr(c.args[1].arg.t), n, .bf16, s);
}
if (std.mem.eql(u8, step, "widen")) // q.to(fp32): exact
return k.widenF32(try r.tensorAddr(c.args[0].arg.t), try r.tensorAddr(c.args[1].arg.t), numel(c.args[0].arg.t));
if (std.mem.eql(u8, step, "ds_out")) {
if (x.dev) |dv| if (dv.on()) {
x.last_logits = try r.tensorAddr(c.args[0].arg.t);
x.last_v = @intCast(c.args[0].arg.t.shape[1]);
return dv.launch(r, c.args[0].arg.t, c.args[1].arg.t, @intCast(x.st.n), 0);
};
return x.hostStep(r, c.args[0].arg.t, c.args[1].arg.t);
}
return error.GlueNotBuilt;
}

/// The host's step after the head: this rank's best k columns, every rank's lists gathered, the head hidden.
pub fn hostStep(x: *GpuPass, r: *run.Runner, logits_t: calls.Tensor, hid_t: calls.Tensor) !void {
const th = ph.start(x.f.phases, .@"draft.host");
defer th.stop();
const comm = x.f.comm;
const n: usize = @intCast(x.st.n);
const V: usize = @intCast(logits_t.shape[1]);
const D: usize = x.f.cfg.hidden;
const k: usize = x.k;
const Wd: usize = comm.world();
const gpa = x.gpa;
const logits = try gpa.alloc(f32, n * V);
defer gpa.free(logits);
const hid16 = try gpa.alloc(u16, n * D);
defer gpa.free(hid16);
try r.stream.synchronize();
try cuda.DeviceBuffer.download(.{ .d = r.d, .ptr = try r.tensorAddr(logits_t), .len = 4 * logits.len }, 0, std.mem.sliceAsBytes(logits));
try cuda.DeviceBuffer.download(.{ .d = r.d, .ptr = try r.tensorAddr(hid_t), .len = 2 * hid16.len }, 0, std.mem.sliceAsBytes(hid16));
for (hid16, x.hid) |b, *h| h.* = dspark.bf16(b);
// packed [n][2k] fp32: values, then ids as bits
const packed_ = try gpa.alloc(f32, n * 2 * k);
defer gpa.free(packed_);
const ids = try gpa.alloc(i32, k);
defer gpa.free(ids);
const id0: u32 = @intCast(comm.rank() * V);
for (0..n) |i| {
const row = packed_[i * 2 * k ..][0 .. 2 * k];
try dspark.candidates(gpa, logits[i * V ..][0..V], id0, ids, row[0..k]);
for (ids, row[k..]) |id, *o| o.* = @bitCast(id);
}
const bytes = 4 * packed_.len;
const src = try x.device(&x.send, bytes);
const dst = try x.device(&x.gathered, bytes * Wd);
try cuda.DeviceBuffer.uploadAsync(.{ .d = r.d, .ptr = src, .len = bytes }, 0, std.mem.sliceAsBytes(packed_), r.stream.handle);
try comm.allGather(src, dst, packed_.len, .f32, r.stream.handle); // bits only (ids as fp32 bit patterns)
try r.stream.synchronize();
const all = try gpa.alloc(f32, packed_.len * Wd);
defer gpa.free(all);
try cuda.DeviceBuffer.download(.{ .d = r.d, .ptr = dst, .len = 4 * all.len }, 0, std.mem.sliceAsBytes(all));
// [W][n][2k] -> per row: rank 0's k, rank 1's k, ...
const c = Wd * k;
for (0..Wd) |w| for (0..n) |i| {
const src_row = all[(w * n + i) * 2 * k ..][0 .. 2 * k];
@memcpy(x.cval[i * c + w * k ..][0..k], src_row[0..k]);
for (src_row[k..], x.cand[i * c + w * k ..][0..k]) |v, *o| o.* = @bitCast(v);
};
try checkCandidates(x.cand, x.f.cfg.vocab);
}
Loading
Loading