Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d0782a6
server: --execution-witness serves a read-scoped snapshot of what run…
kotevcode Nov 11, 2026
fde43a7
server: the witness race test's reader yields after each read, so the…
kotevcode Nov 11, 2026
0bd01b9
server: the witness race test dates its idle window from now, so a ru…
kotevcode Nov 11, 2026
1b27e78
Keep CLI within main's 600-line cap after the witness additions
ashhart Nov 19, 2026
63ff61c
server: avoid duplicate witness test imports after rebase
ashhart Nov 21, 2026
25d9d36
changelog: #2901 under Unreleased
ashhart Nov 21, 2026
ef8ef2e
dsv41 metal: decode experts decode an expert's weights once for all o…
gilby Nov 18, 2026
57e5350
dsv41 metal: FP8 rows meet tile roots in one simdgroup and take one c…
ashhart Nov 18, 2026
c808202
dsv41 metal: a pick's argmax in 16-wide steps and sampling scratch ke…
ashhart Nov 18, 2026
55cbb3a
dsv41 metal: the decode MoE in four waves, one-row experts decode a w…
ashhart Nov 18, 2026
fd47855
dsv41 metal: the DSpark drafter's head runs on its own FP8 copy, and …
ashhart Nov 18, 2026
f6974a5
dsv41 metal: the DSpark drafter's buffers stay resident in their own set
ashhart Nov 18, 2026
52f7abd
dsv41 metal: the decode FP8 tile takes 16 columns for wide projection…
ashhart Nov 18, 2026
f2e1c3d
dsv41 metal: FP8 row blocks e and f decode each word where it is used…
ashhart Nov 18, 2026
58f5bc4
dsv41 metal: the FP8 row and tile comments of the bandwidth change on…
ashhart Nov 18, 2026
ed54ee8
dsv41 metal: a decode window's Engram reads start before its first la…
ashhart Nov 18, 2026
06cdd1a
dsv41: DSpark candidates skip each block of logits none of which beat…
ashhart Nov 18, 2026
caf2ad6
dsv41 metal: a window's keyed picks run a row a thread, each row's pi…
ashhart Nov 18, 2026
8d1fde6
dsv41 metal: retain main's prompt admission check beside threaded picks
ashhart Nov 21, 2026
dad0b64
dsv41 metal: a greedy window's next DSpark pass is queued right behin…
ashhart Nov 18, 2026
d60495a
changelog: drafts behind the verify, Engram reads beside the first la…
ashhart Nov 18, 2026
bc075ee
dsv41 metal: drain queued work in main's split prompt start
ashhart Nov 20, 2026
d8ec816
Follow main's Metal module split for target picks
ashhart Nov 20, 2026
a958c44
dsv41 metal: keep the merged target within the source cap
ashhart Nov 21, 2026
193548f
dsv41 metal: a sampled window's next DSpark pass is queued behind its…
ashhart Nov 18, 2026
5de4979
dsv41 metal: TF_DSV41_DSPARK_BLOCK sets the drafts a DSpark pass make…
ashhart Nov 18, 2026
3162cc2
dsv41 metal: a DSpark pass's candidates are picked a row a thread, ea…
ashhart Nov 18, 2026
11d4f03
dsv41 metal: a pass's DSpark drafts come from a chain on the GPU behi…
ashhart Nov 18, 2026
7f449b3
dsv41 metal: a DSpark pass makes 7 drafts after a window that verifie…
ashhart Nov 18, 2026
26f4ff4
dsv41 metal: keep rebased served regressions in small files
ashhart Nov 18, 2026
4e71cb8
dsv41: leave room for TLS in candidate worker stacks
ashhart Nov 18, 2026
9b0786c
changelog: #3405 under Unreleased
ashhart Nov 21, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- DeepSeek V4.1 on Metal selects DSpark drafts over the whole vocabulary on the GPU with an adaptive block size (#3405).

- Server: `serve --execution-witness` (off by default) adds `GET /v1/execution/witness` under the `read` key scope: a snapshot naming the running binary, the checkpoint's text files, the serve defaults and `TF_*` settings, the engine's epoch and a per-process instance, or `unknown` with its reason; it needs an API key or `--unix-socket` and hashes only after the server is ready (#2901, refs #2741). Thanks to @kotevcode.

- Nemotron and LFM2 MoE on CUDA: the prompt cache keeps a state at each message start by default, so an agent's next request resumes from where its last message began instead of reading the conversation again, within the existing prompt-cache budget and slots (#3696, part of #3617).

- DeepSeek-V4.1 on CUDA (`dsv41-cuda`, a source build): structured output runs on the lane core's token grammars with drafts on: `response_format`, the `guided_*` fields, `tool_choice: "required"` or a named function, and strict tools under `tool_choice: "auto"`; a grammar's stop token ends the reply with `finish_reason` `stop` even with `ignore_eos` (#3026, closes #1664, from #1665). Thanks to @nuk3s.
Expand Down
2 changes: 2 additions & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TF_DSV41_CALIB_FILE` | DeepSeek-V4.1 on Metal: a cost table file (JSON: `verify`, each window's ms by its rows from 1, and `draft`, the drafter pass's ms) that prices draft depth instead of the built-in table. Measure it on the machine that serves; pricing never changes a token. Unset: the built-in table. |
| `TF_DSV41_ENGRAM_VERIFY` | DeepSeek-V4.1 on Metal: exactly `sample` or `full`; `full` also checks every byte of the Engram tables against their manifest at boot; default `sample` |
| `TF_DSV41_ENGRAM_CACHE` | DeepSeek-V4.1 on Metal: whole u32 count of Engram records a layer kept cached; default 65536 |
| `TF_DSV41_MARKOV_GPU` | DeepSeek-V4.1 on Metal: `1` draws DSpark drafts (greedy, and keyed ones whose sampling races the whole row) on the GPU right behind the drafter's head from the Markov-biased logits over the whole vocabulary, the release's rule; `0` draws them on the host from each row's 64 best logits; drafts only, replies are unchanged; default 1 |
| `TF_DSV41_DSPARK_BLOCK` | DeepSeek-V4.1 on Metal: drafts a DSpark pass makes, 1 to 15, after a window that verified the checkpoint's whole block of 5 (a pass after any other window makes 5); a block under 5 applies to every pass, and the depth may verify up to all of them; drafts only, replies are unchanged; `0` keeps the checkpoint's block; default 7 |
| `TF_DSV41_PREFILL_PIECES` | DeepSeek-V4.1: exactly `0` or `1`, default 0. On CUDA, `1` lets the round planner run prompt pieces and decode rows in one pass; on Metal, `1` runs a prompt a chunk at a time between the other streams' rounds (as `--decode-share` above 0, `TENSORFOLD_PREFILL_ORDER=srpt` or a `TENSORFOLD_PREFILL_POLICY`, `_SHARE` or `_BUDGET` also do), replies taking `--decode-share`, else `TENSORFOLD_PREFILL_SHARE`, else 0.25 of wall time beside it; malformed text stops the start naming it |
| `XPU_A8_BITS` | Exactly `4` or `8`, for the XPU quantization study; unset keeps the study's existing setting |
| `LFM3_FUSE` | Whole u32 fusion mask; default 7 |
Expand Down
4 changes: 4 additions & 0 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ decode its weights once for every row of a pass, each row in the one-row kernel'
the same six experts as well, and a probe holds a launch's act, out and combine output bit-equal to each row launched
alone at 1 to 16 rows on the same, disjoint and random experts, with NaN and Inf planted. Rows that read their pass's
first row are a control in the registrations (red at shapes) and in the probe.
The decode MoE's `tf_dsv41_route_gate_plan` (the router's matvec, route and plan in one launch: the last of its groups
to finish reads the logits as atomics, ranks the picks and plans) registers twice: its matvec in the matvec suite (exact
bytes at 384 experts, 1 to 16 rows, four controls under `TF_GCTL`), and its route and plan in the route suite on given
logits (k 0) with the route's four controls.
The row-split seam's four kernels (`pair_rows.metal`, launched by `pair_rows.zig`) move bf16 frames, never sums:
`pair_stage` copies this rank's half of a frame row into the slot the relay sends, and `pair_gather` writes both halves
of every row, its own natively and the peer's from the relayed frame. `pair_argmax` flags a row whose logits are not
Expand Down
136 changes: 136 additions & 0 deletions zig/kernels/metal/deepseek_v41/early.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// DeepSeek-V4.1 drafts behind a verify window: its greedy or keyed picks, the drafts they accept, the drafter's plan.
#include <metal_stdlib>
using namespace metal;

#define TF_TG 1024u
#define TF_NONE 0xffffffffu

struct TfEarlyPickArgs {
uint vocab; // logits a row (the row stride)
};

// The better of two (value, id) pairs: the larger value, ties to the lower id (no NaN reaches here).
inline void tf_early_better(thread float& v, thread uint& i, float ov, uint oi) {
if (ov > v || (ov == v && oi < i)) {
v = ov;
i = oi;
}
}

// The host's greedy pick of each row: the first maximum of its values that are not NaN, 0 when value 0 is NaN.
[[max_total_threads_per_threadgroup(TF_TG)]] kernel void tf_dsv41_early_pick(
constant TfEarlyPickArgs& args [[buffer(0)]],
device const float* logits [[buffer(1)]],
device uint* picks [[buffer(2)]],
uint row [[threadgroup_position_in_grid]],
uint tid [[thread_index_in_threadgroup]],
uint lane [[thread_index_in_simdgroup]],
uint simd [[simdgroup_index_in_threadgroup]]) {
threadgroup float part_v[32];
threadgroup uint part_i[32];
device const float* x = logits + ulong(row) * args.vocab;
float bv = -INFINITY;
uint bi = TF_NONE;
for (uint i = tid; i < args.vocab; i += TF_TG) {
const float v = x[i];
if (!isnan(v)) tf_early_better(bv, bi, v, i);
}
for (ushort off = 16; off > 0; off >>= 1) tf_early_better(bv, bi, simd_shuffle_down(bv, off), simd_shuffle_down(bi, off));
if (lane == 0u) {
part_v[simd] = bv;
part_i[simd] = bi;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if (simd != 0u) return;
bv = lane < TF_TG / 32u ? part_v[lane] : -INFINITY;
bi = lane < TF_TG / 32u ? part_i[lane] : TF_NONE;
for (ushort off = 16; off > 0; off >>= 1) tf_early_better(bv, bi, simd_shuffle_down(bv, off), simd_shuffle_down(bi, off));
if (lane == 0u) picks[row] = (isnan(x[0]) || bi == TF_NONE) ? 0u : bi;
}

struct TfEarlyKeyedArgs {
uint vocab; // logits a row (the row stride)
float t; // the temperature (at least 1e-6, as the host floors it)
};

// A raced row's keyed pick: the largest x / t + gumbel(key); a value not finite picks none (the host sorts such rows).
[[max_total_threads_per_threadgroup(TF_TG)]] kernel void tf_dsv41_early_keyed(
constant TfEarlyKeyedArgs& args [[buffer(0)]],
device const float* logits [[buffer(1)]],
device uint* picks [[buffer(2)]],
device const ulong* bases [[buffer(3)]],
uint row [[threadgroup_position_in_grid]],
uint tid [[thread_index_in_threadgroup]],
uint lane [[thread_index_in_simdgroup]],
uint simd [[simdgroup_index_in_threadgroup]]) {
threadgroup float part_s[32];
threadgroup float part_v[32];
threadgroup uint part_i[32];
threadgroup uint part_bad[32];
device const float* x = logits + ulong(row) * args.vocab;
const ulong base = bases[row];
float bs = -INFINITY, bv = -INFINITY;
uint bi = TF_NONE, bad = 0u;
for (uint i = tid; i < args.vocab; i += TF_TG) {
const float v = x[i];
const float xv = v / args.t;
if (!tf_key_finite(v) || !tf_key_finite(xv)) {
bad = 1u;
continue;
}
tf_key_beats(bs, bv, bi, xv + tf_key_gumbel(tf_key_mix(base ^ ulong(i))), v, i);
}
for (ushort off = 16; off > 0; off >>= 1) {
tf_key_beats(bs, bv, bi, simd_shuffle_down(bs, off), simd_shuffle_down(bv, off), simd_shuffle_down(bi, off));
bad |= simd_shuffle_down(bad, off);
}
if (lane == 0u) {
part_s[simd] = bs;
part_v[simd] = bv;
part_i[simd] = bi;
part_bad[simd] = bad;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if (simd != 0u) return;
const bool on = lane < TF_TG / 32u;
bs = on ? part_s[lane] : -INFINITY;
bv = on ? part_v[lane] : -INFINITY;
bi = on ? part_i[lane] : TF_NONE;
bad = on ? part_bad[lane] : 0u;
for (ushort off = 16; off > 0; off >>= 1) {
tf_key_beats(bs, bv, bi, simd_shuffle_down(bs, off), simd_shuffle_down(bv, off), simd_shuffle_down(bi, off));
bad |= simd_shuffle_down(bad, off);
}
if (lane == 0u) picks[row] = bad != 0u ? TF_NONE : bi;
}

struct TfEarlyAcceptArgs {
uint rows; // the window's rows: the pending token's, then the drafts'
uint ints; // a plan's ints (each accepted count's plan in `plans`)
uint skew; // tests: 1 swaps an accepted count of 0 and 1, so the chain parts from the host's commit
uint pos_lo, pos_hi; // the next ask's first draft's position less the accepted count
};

// The drafts the picks accept (the host's rule), each count's plan copied in, the bonus into the block and the result.
[[max_total_threads_per_threadgroup(TF_TG)]] kernel void tf_dsv41_early_accept(
constant TfEarlyAcceptArgs& args [[buffer(0)]],
device const uint* tokens [[buffer(1)]],
device const uint* picks [[buffer(2)]],
device const int* plans [[buffer(3)]],
device int* plan [[buffer(4)]],
device uint* ids [[buffer(5)]],
device uint* result [[buffer(6)]],
device ulong* pos0 [[buffer(7)]],
uint tid [[thread_index_in_threadgroup]]) {
uint acc = 0u;
while (acc + 1u < args.rows && tokens[acc + 1u] == picks[acc]) acc++;
if (args.skew != 0u && args.rows > 1u && acc < 2u) acc = 1u - acc;
device const int* from = plans + ulong(acc) * args.ints;
for (uint i = tid; i < args.ints; i += TF_TG) plan[i] = from[i];
if (tid == 0u) {
ids[0] = picks[acc];
result[0] = acc;
result[1] = picks[acc];
pos0[0] = (ulong(args.pos_hi) << 32 | args.pos_lo) + acc;
}
}
89 changes: 83 additions & 6 deletions zig/kernels/metal/deepseek_v41/experts.metal
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ kernel void tf_dsv41_xp_quant(device const bfloat* X [[buffer(0)]], device half*
}


// A word's eight E2M1 codes as k3_fp4_pair's floats by shifts alone: (e0,e4) (e1,e5) (e2,e6) (e3,e7) a half2 each.
inline void ds_fp4_word(uint u, thread float (&w)[8]) {
const half2 a = as_type<half2>(((u << 9) & 0x0E000E00u) | ((u << 12) & 0x80008000u));
const half2 b = as_type<half2>(((u << 5) & 0x0E000E00u) | ((u << 8) & 0x80008000u));
const half2 c = as_type<half2>(((u << 1) & 0x0E000E00u) | ((u << 4) & 0x80008000u));
const half2 d = as_type<half2>(((u >> 3) & 0x0E000E00u) | (u & 0x80008000u));
w[0] = float(a.x), w[4] = float(a.y), w[1] = float(b.x), w[5] = float(b.y);
w[2] = float(c.x), w[6] = float(c.y), w[3] = float(d.x), w[7] = float(d.y);
}

// A row staged a 32-value group on a stride of 40 (16-bit values), so the eight chains of an output read apart.
constant constexpr uint DS_GS = 40;

// tf_dsv41_xp_up over tf_dsv41_xp_quant's values for the slots of one row: the same chains and epilogue.
[[kernel, max_total_threads_per_threadgroup(256)]] void tf_dsv41_xp_upc_one(
device const half* XH [[buffer(0)]], device const ExpertPtrs* table [[buffer(1)]],
Expand All @@ -183,7 +196,7 @@ kernel void tf_dsv41_xp_quant(device const bfloat* X [[buffer(0)]], device half*
device const float* wts [[buffer(7)]], device const float* XS [[buffer(8)]],
uint2 tg [[threadgroup_position_in_grid]], uint t [[thread_index_in_threadgroup]],
uint sg [[simdgroup_index_in_threadgroup]], uint lane [[thread_index_in_simdgroup]]) {
threadgroup half4 xh[DS_KMAX / 4];
threadgroup half4 xh[DS_KMAX / 32 * DS_GS / 4];
threadgroup float xs[DS_KMAX / 32], vals[1][32];
if (tg.y >= nslots[0] || slots[3 * tg.y + 2] != 1) return;
const uint e = slots[3 * tg.y], first = slots[3 * tg.y + 1];
Expand All @@ -192,11 +205,34 @@ kernel void tf_dsv41_xp_quant(device const bfloat* X [[buffer(0)]], device half*
device const uchar* const wp[2] = {w.w1p + n * rowb, w.w3p + n * rowb};
device const uchar* const ws[2] = {w.w1s + n * groups, w.w3s + n * groups};
const uint row = pairs[first] / a.topk;
for (uint i = t; i < a.K / 4; i += 256) xh[i] = ((device const half4*)(XH + row * a.K))[i];
for (uint i = t; i < a.K / 4; i += 256) xh[(i / 8) * (DS_GS / 4) + i % 8] = ((device const half4*)(XH + row * a.K))[i];
for (uint i = t; i < groups; i += 256) xs[i] = XS[row * groups + i];
threadgroup_barrier(mem_flags::mem_threadgroup);
float acc[2][1];
ds_chains<1, half>(acc, (threadgroup const half*)xh, xs, a.K, 1, wp, ws, c);
float acc[2][1] = {{0.0f}, {0.0f}};
for (uint g = c; g < groups; g += 8) {
uint2 q[2][2];
for (int v = 0; v < 2; ++v) q[v][0] = *(device const uint2*)(wp[v] + 16 * g), q[v][1] = *(device const uint2*)(wp[v] + 16 * g + 8);
threadgroup const half4* x = xh + g * (DS_GS / 4);
float p[2] = {0.0f, 0.0f};
for (int h = 0; h < 2; ++h) {
float wv[2][16];
for (int v = 0; v < 2; ++v) {
float lo[8], hi[8];
ds_fp4_word(q[v][h].x, lo);
ds_fp4_word(q[v][h].y, hi);
for (int j = 0; j < 8; ++j) wv[v][j] = lo[j], wv[v][8 + j] = hi[j];
}
for (int i = 0; i < 4; ++i) {
const float4 x4 = float4(x[4 * h + i]);
for (int j = 0; j < 4; ++j)
for (int v = 0; v < 2; ++v) {
const float s2 = fma(x4[j], wv[v][4 * i + j], p[v]);
p[v] = TF_CTL == 3 ? float(half(s2)) : s2;
}
}
}
for (int v = 0; v < 2; ++v) acc[v][0] = k3_scaled_fma(p[v] * xs[g], ws[v][g], acc[v][0]);
}
ds_up_out<1>(acc, vals, act, wts, pairs, a, first, 0, 1, nl, n, c);
}

Expand Down Expand Up @@ -312,8 +348,6 @@ template <int RP, bool ONE>
}
}

template [[host_name("tf_dsv41_xp_down_one")]] [[kernel]] decltype(tf_dsv41_xp_down<1, true>) tf_dsv41_xp_down<1, true>;

// Kimi's down of rows r0..r0+rn of a slot (act by pair position), one weight decode for all rows.
template <uint RP>
inline void ds_down_pass(device const bfloat* act, device bfloat* out, constant XpArgs& a,
Expand Down Expand Up @@ -356,3 +390,46 @@ inline void ds_down_pass(device const bfloat* act, device bfloat* out, constant
DS_UP(1)
DS_UP(2)
DS_UP(4)

// One group of a down chain over bf16 x: 32 codes from zero in k3_xp_chains' order, then its scaled fma.
inline float ds_dgroup(float acc, device const uchar* q, threadgroup const ushort4* x, uchar s) {
const uint2 q0 = *(device const uint2*)q, q1 = *(device const uint2*)(q + 8);
float p = 0.0f;
for (int h = 0; h < 2; ++h) {
float lo[8], hi[8];
ds_fp4_word(h == 0 ? q0.x : q1.x, lo);
ds_fp4_word(h == 0 ? q0.y : q1.y, hi);
for (int i = 0; i < 4; ++i) {
float4 x4 = float4(as_type<bfloat4>(x[4 * h + i]));
if (TF_CTL == 1) x4 = select(x4, float4(0.0f), !isfinite(x4));
for (int j = 0; j < 4; ++j) {
const float s2 = fma(x4[j], 4 * i + j < 8 ? lo[4 * i + j] : hi[4 * i + j - 8], p);
p = TF_CTL == 3 ? float(half(s2)) : s2;
}
}
}
return k3_scaled_fma(p, s, acc);
}

// Kimi's down for the slots of one row (lane 8o + c runs chain c of output o), its codes decoded a word at a time.
[[kernel, max_total_threads_per_threadgroup(256)]] void tf_dsv41_xp_down_one(
device const bfloat* act [[buffer(0)]], device const ExpertPtrs* table [[buffer(1)]],
device const uint* slots [[buffer(2)]], device const uint* nslots [[buffer(3)]],
device const uint* pairs [[buffer(4)]], device bfloat* out [[buffer(5)]], constant XpArgs& a [[buffer(6)]],
uint2 tg [[threadgroup_position_in_grid]], uint t [[thread_index_in_threadgroup]],
uint sg [[simdgroup_index_in_threadgroup]], uint lane [[thread_index_in_simdgroup]]) {
threadgroup ushort4 xb[3072 / 32 * DS_GS / 4];
if (tg.y >= nslots[0] || slots[3 * tg.y + 2] != 1) return;
const uint e = slots[3 * tg.y], first = slots[3 * tg.y + 1];
const ExpertPtrs w = table[e];
const uint n = tg.x * 32 + sg * 4 + (lane >> 3), c = lane & 7, rowb = a.K / 2, groups = a.K / 32;
for (uint i = t; i < a.K / 4; i += 256) xb[(i / 8) * (DS_GS / 4) + i % 8] = ((device const ushort4*)(act + first * a.K))[i];
threadgroup_barrier(mem_flags::mem_threadgroup);
device const uchar* wp = w.w2p + n * rowb;
device const uchar* ws = w.w2s + n * groups;
float acc = 0.0f;
for (uint g = c; g < (TF_CTL == 2 ? groups - 1 : groups); g += 8) acc = ds_dgroup(acc, wp + 16 * g, xb + g * (DS_GS / 4), ws[g]);
const float v = k3_xtree8(acc);
if (c == 0) out[first * a.N + n] = bfloat(v);
if (TF_CTL == 4 && c == 0 && n == 0 && first == 0) out[a.rows_max * a.topk * a.N] = bfloat(1.0f);
}
Loading
Loading