Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
186 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
f9dab0b
DeepSeek-V4.1 batched rows: several slots in one round, the GPU targe…
jayleaton Oct 11, 2026
f5db10b
DeepSeek-V4.1 serving side: release tokenizer, chat encoding, DSML to…
jayleaton Oct 11, 2026
59af121
DeepSeek-V4.1 served engine and tool: the model at boot, the engine b…
jayleaton Oct 11, 2026
3c93b75
DeepSeek-V4.1 performance extras, host side: side streams, the Engram…
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
850f6b2
Merge stack/11c-device-drafts (current main, shared layers, #861) int…
jayleaton Oct 16, 2026
a0d8181
Merge stack/11d-batched-rows (current main, shared layers, #861) into…
jayleaton Oct 16, 2026
d624aa1
Merge stack/12a-serving-host (current main, shared layers, #861, fami…
jayleaton Oct 16, 2026
d2f0223
Merge stack/12b-serving-engine (current main, shared layers, #861, fa…
jayleaton Oct 16, 2026
95f6b4c
DeepSeek-V4.1 sessions: an unset idle residency stays unlimited
jayleaton Oct 16, 2026
3f392a6
Merge stack/11d-batched-rows (unset session residency stays unlimited…
jayleaton Oct 16, 2026
75eae6c
Merge stack/12a-serving-host (unset session residency stays unlimited…
jayleaton Oct 16, 2026
503eeb3
Merge stack/12b-serving-engine (unset session residency stays unlimit…
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
d46405c
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
87d7912
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
ada4b74
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
9002037
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
5ebfcdd
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
956febe
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
d9df51c
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
4a5263d
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
5faa948
Merge stack/11c-device-drafts (3f0fc404, main 76aafcf2) into stack/11…
jayleaton Oct 18, 2026
c24b124
Merge stack/11d-batched-rows (5faa9489, main 76aafcf2) into stack/12a…
jayleaton Oct 18, 2026
c14a40a
Merge stack/12a-serving-host (c24b124d, main 76aafcf2) into stack/12b…
jayleaton Oct 18, 2026
7169718
Merge stack/12b-serving-engine (c14a40a0, main 76aafcf2) into stack/1…
jayleaton Oct 18, 2026
ab2fa63
DeepSeek-V4.1 serving tests: TF_DSV41_MODEL and TF_DSV41_GOLDEN throu…
jayleaton Oct 18, 2026
b04302a
Merge stack/12a-serving-host (ab2fa636: DeepSeek-V4.1 serving tests r…
jayleaton Oct 18, 2026
232f69e
Merge stack/12b-serving-engine (b04302a7: DeepSeek-V4.1 serving tests…
jayleaton Oct 18, 2026
ba8856a
Build: tensorfold-dsv41's checkpoint CLI imports core, as main's chec…
jayleaton Oct 18, 2026
45d5b13
Merge stack/12b-serving-engine (ba8856a7: tensorfold-dsv41's checkpoi…
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
Graph cache: named caches publish their counts, the floor's state is …
…reported, a meter prices each capture
  • Loading branch information
jayleaton committed Oct 11, 2026
commit a89bd4ff67ef153fc5ae0ad53d273af4c9029fbb
168 changes: 161 additions & 7 deletions zig/src/cuda/graph_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ pub const Settings = struct {

pub const Outcome = enum { replayed, captured, eager };

/// Eager steps under the floor between two "still under" lines.
pub const report_every = 256;

/// One cache's counts as other threads read them (/health); `snapshots` copies the caches in use.
pub const Snapshot = struct { name: []const u8 = "", held: u32 = 0, captured: u64 = 0, replayed: u64 = 0, eager: u64 = 0, floor_hits: u64 = 0, under: bool = false };

/// Static slots, so a reader never meets a freed cache: a cache claims one at its first step, frees it at deinit.
const Slot = struct {
/// claimed by a cache; `used` once its name is set (what readers check)
taken: std.atomic.Value(bool) = .init(false),
used: std.atomic.Value(bool) = .init(false),
name: []const u8 = "",
held: std.atomic.Value(u32) = .init(0),
captured: std.atomic.Value(u64) = .init(0),
replayed: std.atomic.Value(u64) = .init(0),
eager: std.atomic.Value(u64) = .init(0),
floor_hits: std.atomic.Value(u64) = .init(0),
under: std.atomic.Value(bool) = .init(false),
};
var published: [8]Slot = @splat(.{});

/// The caches in use, at most `out.len`; returns how many it filled.
pub fn snapshots(out: []Snapshot) usize {
var n: usize = 0;
for (&published) |*s| {
if (n == out.len) break;
if (!s.used.load(.acquire)) continue;
out[n] = .{ .name = s.name, .held = s.held.load(.monotonic), .captured = s.captured.load(.monotonic), .replayed = s.replayed.load(.monotonic), .eager = s.eager.load(.monotonic), .floor_hits = s.floor_hits.load(.monotonic), .under = s.under.load(.monotonic) };
n += 1;
}
return n;
}

pub fn Cache(comptime Key: type) type {
return struct {
const Self = @This();
Expand All @@ -45,12 +78,18 @@ pub fn Cache(comptime Key: type) type {
settings: Settings,
agree: ?Agree = null,
room: ?*const fn () bool = null, // false when this rank is below its memory floor; null: always room
meter: ?*const fn () ?f64 = null, // MemAvailable now in GiB, to log a capture's cost; null: not measured
name: []const u8 = "graphs", // the cache's name in its log lines and snapshot
entries: std.AutoArrayHashMapUnmanaged(Key, Entry) = .empty,
cap: u32,
tick: u64 = 0,
fingerprint: ?u64 = null,
failed: bool = false, // a capture failed on some rank: eager from here on
stats: struct { replayed: u64 = 0, captured: u64 = 0, eager: u64 = 0, evicted: u64 = 0, dropped: u64 = 0, floor_hits: u64 = 0 } = .{},
under: bool = false, // the last miss met no room on some rank
under_eager: u64 = 0, // eager steps since `under` began
slot: ?*Slot = null, // this cache's slot in `published`, claimed at its first step; null: every slot taken
claimed: bool = false,
stats: struct { replayed: u64 = 0, captured: u64 = 0, eager: u64 = 0, evicted: u64 = 0, dropped: u64 = 0, floor_hits: u64 = 0, capture_mib: f64 = 0 } = .{},

pub fn init(gpa: std.mem.Allocator, engine: Engine, settings: Settings) Self {
var s = settings;
Expand All @@ -59,6 +98,10 @@ pub fn Cache(comptime Key: type) type {
}

pub fn deinit(c: *Self) void {
if (c.slot) |s| {
s.used.store(false, .release);
s.taken.store(false, .release);
}
c.dropAll();
c.entries.deinit(c.gpa);
}
Expand Down Expand Up @@ -94,6 +137,43 @@ pub fn Cache(comptime Key: type) type {
c.stats.evicted += 1;
}

fn publish(c: *Self) void {
if (!c.claimed) {
c.claimed = true;
for (&published) |*x| if (x.taken.cmpxchgStrong(false, true, .acq_rel, .monotonic) == null) {
x.name = c.name;
c.slot = x;
break;
};
}
const s = c.slot orelse return;
s.held.store(@intCast(c.entries.count()), .monotonic);
s.captured.store(c.stats.captured, .monotonic);
s.replayed.store(c.stats.replayed, .monotonic);
s.eager.store(c.stats.eager, .monotonic);
s.floor_hits.store(c.stats.floor_hits, .monotonic);
s.under.store(c.under, .monotonic);
// readers see the slot once its name and counts are this cache's
s.used.store(true, .release);
}

fn avail(c: *const Self) ?f64 {
return if (c.meter) |m| m() else null;
}

/// A miss without room on some rank: the floor entered (logged), or a "still under" line every `report_every`.
fn underFloor(c: *Self, room_here: bool) void {
c.stats.floor_hits += 1;
c.under_eager += 1;
if (!c.under) {
c.under = true;
c.under_eager = 1;
if (!quiet) std.log.warn("graph cache {s}: under the memory floor on {s} rank (MemAvailable {?d:.2} GiB here): new keys eager, {d} graphs held", .{ c.name, if (room_here) "another" else "this", c.avail(), c.entries.count() });
} else if (c.under_eager % report_every == 0) {
if (!quiet) std.log.warn("graph cache {s}: still under the memory floor: {d} eager steps since it began (MemAvailable {?d:.2} GiB here, {d} graphs held)", .{ c.name, c.under_eager, c.avail(), c.entries.count() });
}
}

fn eager(c: *Self, stream: Stream, body: Body) !Outcome {
try body.run(body.ctx, stream);
c.stats.eager += 1;
Expand All @@ -102,6 +182,7 @@ pub fn Cache(comptime Key: type) type {

/// One step: the key's graph, captured first on a miss; a new `fingerprint` (same step on all ranks) drops all.
pub fn run(c: *Self, key: Key, stream: Stream, fingerprint: u64, body: Body) !Outcome {
defer c.publish();
if (!c.settings.on or c.failed or c.settings.max == 0) return c.eager(stream, body);
if (c.fingerprint != null and c.fingerprint.? != fingerprint) c.dropAll();
c.fingerprint = fingerprint;
Expand All @@ -116,26 +197,27 @@ pub fn Cache(comptime Key: type) type {
const reserved = if (c.entries.ensureUnusedCapacity(c.gpa, 1)) |_| true else |_| false;
const room_here = reserved and (if (c.room) |f| f() else true);
if (!try c.agreed(room_here)) {
if (c.settings.hold) {
if (!quiet and c.stats.floor_hits == 0) std.log.warn("graph cache: under the memory floor on {s} rank: this key eager, {d} graphs held (hold)", .{ if (room_here) "another" else "this", c.entries.count() });
c.stats.floor_hits += 1;
return c.eager(stream, body);
}
c.underFloor(room_here);
if (c.settings.hold) return c.eager(stream, body);
if (c.entries.count() > c.settings.min_keep) {
const drop = @max(1, c.entries.count() / 4);
for (0..drop) |_| if (c.entries.count() > c.settings.min_keep) c.evictOldest();
}
const was = c.cap;
c.cap = @intCast(@max(c.entries.count(), c.settings.min_keep)); // min_keep <= max (init)
c.stats.floor_hits += 1;
if (!quiet and c.cap != was) std.log.warn("graph cache: under the memory floor on {s} rank: cap {d} -> {d} ({d} held, {d} evicted, {d} eager so far)", .{ if (room_here) "another" else "this", was, c.cap, c.entries.count(), c.stats.evicted, c.stats.eager + 1 });
return c.eager(stream, body);
}
if (c.under) {
if (!quiet) std.log.info("graph cache {s}: room again on every rank (MemAvailable {?d:.2} GiB here) after {d} eager steps under the floor, {d} graphs held", .{ c.name, c.avail(), c.under_eager, c.entries.count() });
c.under = false;
}
if (c.cap < c.settings.max) {
if (!quiet) std.log.info("graph cache: room again on every rank: cap {d} -> {d}", .{ c.cap, c.settings.max });
c.cap = c.settings.max;
}
while (c.entries.count() >= c.cap) c.evictOldest();
const before = c.avail();
const exec = c.engine.capture(c.engine.ctx, stream, body) catch |err| blk: {
// the only trace of why graphs went off (the step then runs eagerly, which may fail the same way)
if (!quiet) std.log.warn("graph capture failed: {s} (key {any}, {d} graphs held)", .{ @errorName(err), key, c.entries.count() });
Expand All @@ -152,6 +234,11 @@ pub fn Cache(comptime Key: type) type {
owned = null;
try c.engine.launch(c.engine.ctx, exec.?, stream);
c.stats.captured += 1;
// what a graph really costs this rank (the room rule prices it)
if (before) |b| if (c.avail()) |a| {
c.stats.capture_mib = (b - a) * 1024;
if (!quiet) std.log.info("graph cache {s}: captured, {d} held: MemAvailable {d:.2} -> {d:.2} GiB ({d:.0} MiB)", .{ c.name, c.entries.count(), b, a, c.stats.capture_mib });
};
return .captured;
}
};
Expand Down Expand Up @@ -383,6 +470,73 @@ test "graph cache: hold - below the floor a miss runs eagerly, nothing is evicte
try std.testing.expectEqual(@as(usize, 9), c.count());
}

test "graph cache: the floor's enter / under / leave state and its counts, as /health reads them" {
var f: Fake = .{};
var r: Ran = .{};
var c = Cache(u32).init(std.testing.allocator, f.engine(), .{ .max = 48, .hold = true });
c.room = roomFn;
c.name = "test floor";
defer c.deinit();
room_flag = true;
for (0..4) |k| _ = try c.run(@intCast(k), no_stream, 7, r.body());
try std.testing.expect(!c.under);
room_flag = false;
for (0..report_every + 3) |k| _ = try c.run(@intCast(1000 + k), no_stream, 7, r.body());
try std.testing.expect(c.under);
try std.testing.expectEqual(@as(u64, report_every + 3), c.under_eager);
// the held keys replay under the floor and leave the state alone
try std.testing.expectEqual(Outcome.replayed, try c.run(2, no_stream, 7, r.body()));
try std.testing.expect(c.under);
var out: [8]Snapshot = undefined;
const mine = for (out[0..snapshots(&out)]) |x| {
if (std.mem.eql(u8, x.name, "test floor")) break x;
} else return error.NotPublished;
try std.testing.expectEqual(Snapshot{ .name = "test floor", .held = 4, .captured = 4, .replayed = 1, .eager = report_every + 3, .floor_hits = report_every + 3, .under = true }, mine);
// room again: the next miss captures and the floor is left; the counts keep their totals
room_flag = true;
try std.testing.expectEqual(Outcome.captured, try c.run(2000, no_stream, 7, r.body()));
try std.testing.expect(!c.under);
try std.testing.expectEqual(@as(u64, report_every + 3), c.stats.floor_hits);
// a second dip starts its own count
room_flag = false;
_ = try c.run(3000, no_stream, 7, r.body());
try std.testing.expectEqual(@as(u64, 1), c.under_eager);
room_flag = true;
}

var meter_gib: f64 = 100;
var meter_reads: u32 = 0;
fn meterFn() ?f64 {
meter_reads += 1;
defer meter_gib -= 0.25; // each read sees a quarter GiB less: a capture's before and after differ by that
return meter_gib;
}

test "graph cache: the meter prices a capture, replays never read it, a freed cache leaves the snapshots" {
var f: Fake = .{};
var r: Ran = .{};
var out: [8]Snapshot = undefined;
const others = snapshots(&out);
{
var c = Cache(u32).init(std.testing.allocator, f.engine(), .{ .max = 4 });
defer c.deinit();
c.meter = meterFn;
c.name = "metered";
meter_reads = 0;
try std.testing.expectEqual(Outcome.captured, try c.run(1, no_stream, 7, r.body()));
try std.testing.expectEqual(@as(f64, 256), c.stats.capture_mib);
try std.testing.expectEqual(@as(u32, 2), meter_reads);
try std.testing.expectEqual(Outcome.replayed, try c.run(1, no_stream, 7, r.body()));
try std.testing.expectEqual(@as(u32, 2), meter_reads);
try std.testing.expectEqual(others + 1, snapshots(&out));
const mine = for (out[0 .. others + 1]) |x| {
if (std.mem.eql(u8, x.name, "metered")) break x;
} else return error.NotPublished;
try std.testing.expectEqual(@as(u64, 1), mine.replayed);
}
try std.testing.expectEqual(others, snapshots(&out));
}

test "graph cache: off runs every step eagerly" {
var f: Fake = .{};
var r: Ran = .{};
Expand Down