Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1be6dc0
Windows: a platform layer and host builds that pass
BobClawblaw Oct 12, 2026
e5bb3bc
Windows: CUDA kernels, the native server and the GPU suites on sm_89
BobClawblaw Oct 12, 2026
1f87b27
Windows: pull into the hub cache, HOME, and the whole test step
BobClawblaw Oct 12, 2026
996a50f
Windows: build notes, strip before install, formatting
BobClawblaw Oct 12, 2026
aac93d2
Default port 8888
BobClawblaw Oct 12, 2026
81f5576
qwen3_5: serve Qwen3.5-2B on CUDA
BobClawblaw Oct 13, 2026
3ace97e
qwen3_5: serve Qwen3.8-27B on CUDA, one arena for a model's device me…
BobClawblaw Oct 14, 2026
6b85f22
qwen3_5: replay each decode shape as a CUDA graph
BobClawblaw Oct 14, 2026
b19e0d7
qwen3_5: DFlash2 drafts for Qwen3.8-27B on CUDA
BobClawblaw Oct 14, 2026
288ba03
qwen3_5: tree drafts on CUDA, the kept path replayed from a log
BobClawblaw Oct 14, 2026
61fc2bf
docs: the Qwen hybrids on CUDA in the Windows page and the README
BobClawblaw Oct 14, 2026
6aaf241
qwen3_5, platform: comments on one line
BobClawblaw Oct 14, 2026
8107ee2
Windows: checkpoint reads skip the page cache
BobClawblaw Oct 14, 2026
693533e
Windows: the signal handler type allows address 1, for aarch64
BobClawblaw Oct 14, 2026
f53ae20
docs: Windows as a target in the README and the Windows page
BobClawblaw Oct 14, 2026
e875ee2
CI: the host unit tests on Windows
BobClawblaw Oct 14, 2026
38ecce5
qwen3_5: the drafter reads TF_TIMING through the platform layer
BobClawblaw Oct 14, 2026
1817823
qwen3_5: the drafter takes a prompt longer than its tap ring
BobClawblaw Oct 14, 2026
b811a3b
qwen3_5 cuda: prompt chunks past 32 rows see the right keys, and tree…
BobClawblaw Oct 14, 2026
0a0856b
Windows: the server goldens and the Cortheon fuzz step on Windows hosts
BobClawblaw Oct 14, 2026
ccd06b9
cuda: cuBLASLt opens by its Windows DLL name
BobClawblaw Oct 14, 2026
045abbd
Windows: writes on an O_APPEND descriptor land at the end whole
BobClawblaw Oct 14, 2026
bdfeb84
golden check: a request-log mismatch names the first differing line
BobClawblaw Oct 14, 2026
153d6b4
golden check: the request log compares with LF endings on either side
BobClawblaw Oct 14, 2026
8570e06
qwen3_5: attention chunks follow each row's own keys, not the forward…
BobClawblaw Oct 15, 2026
5e1c3fe
qwen3_5: the DFlash2 config's divisors must be positive, and staging …
BobClawblaw Oct 15, 2026
d653cd5
perf(qwen3_5 cuda): deliver the first token before drafting
jrb-ai Oct 16, 2026
1471afe
docs: pin the Spark driver and CUDA toolkit on the first-token receipt
jrb-ai Oct 17, 2026
52639bd
docs: publish the first-token harness and per-request rows
jrb-ai Oct 17, 2026
72462cc
docs: add the startup clock anchors and unbound replay plans
jrb-ai Oct 17, 2026
5248a33
docs: clarify first-token replay clocks and startup receipt
jrb-ai 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
qwen3_5: attention chunks follow each row's own keys, not the forward…
…'s widest stream

The chunk width came from the longest stream or window in the forward, so a 201-key row cut its keys into 128-key
chunks alone and 256-key chunks beside a stream past 8192, and a window crossing 8192 recut its earlier rows; either
changed the fp32 sums. Each row now takes chunk_keys(its logical keys), the width its one-row step uses, in both the
partial and the merge kernels, and the partials sit at a fixed max_chunks stride.

tf-qwen35-cuda takes TF_Q35_PREFILL=N (start after N cycled ids) and TF_Q35_BESIDE=N (step an N-token stream in each
forward), which cover both cases: before this change, windows from 8151 keys kept 36 of 120 rows and trees 35, and a
151-key stream beside one from 8189 kept 4 to 11 of 120; after it all of them equal one-row steps on the 2B and 27B.
  • Loading branch information
BobClawblaw committed Oct 15, 2026
commit 8570e0688ae6118e0e90eee0655940aeef7614bf
25 changes: 15 additions & 10 deletions zig/kernels/cuda/qwen35.cu
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ __device__ __forceinline__ __nv_bfloat16 rotate(const __nv_bfloat16* x, int d, i
// A row's ancestors-and-self: its tree window's row bits, or with the top bit set a chain row's index in its window.
__device__ __forceinline__ int rows_seen(unsigned anc) { return (anc >> 31) ? (int)(anc & 0x7fffffffu) + 1 : __popc(anc); }

// A row's chunk width from its own logical keys alone, as its one-row step's: at most MAXCH chunks, 128-key multiples.
__device__ __forceinline__ int chunk_keys(int keys) { return max(128, (keys + MAXCH * 128 - 1) / (MAXCH * 128) * 128); }

// The window row holding a row's nth seen window key.
__device__ __forceinline__ int seen_row(unsigned anc, int n) {
if (anc >> 31) return n;
Expand Down Expand Up @@ -342,24 +345,24 @@ extern "C" __global__ void __launch_bounds__(256) tf_q35_rope(Geo geo, __nv_bflo
}
}

// Causal attention over the cache in P[0] chunks of P[1] keys, a warp a query head; partials by (row, head, chunk).
// Causal attention in chunks of chunk_keys(the row's keys), a warp a query head; partials by (row, head, chunk).
extern "C" __global__ void tf_q35_attn_partial(Geo geo, const __nv_bfloat16* __restrict__ Q, const Seg* __restrict__ segs,
const int* __restrict__ rowseg, const unsigned* __restrict__ rowanc, int layer,
const int* __restrict__ P, float scale,
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO) {
constexpr int DPL = HD / 32;
const int nch = P[0], ck = P[1];
const int QH = geo.qh, KVH = geo.kvh;
const int lane = threadIdx.x & 31, g = threadIdx.x >> 5;
const int c = blockIdx.x, h = blockIdx.y;
if (c >= nch || past_rows(P, blockIdx.z)) return;
if (c >= MAXCH || past_rows(P, blockIdx.z)) return;
const size_t m = blockIdx.z;
const int qh = h * (QH / KVH) + g;
const Seg sg = segs[rowseg[m]];
const unsigned anc = rowanc[m];
// chunks cut at logical positions (a window key's: the stream's length plus its ancestors), as a step's are
const int k0 = c * ck, k1 = min(k0 + ck, sg.len + rows_seen(anc));
const size_t slot = (m * QH + qh) * nch + c;
const int keys = sg.len + rows_seen(anc), ck = chunk_keys(keys);
const int k0 = c * ck, k1 = min(k0 + ck, keys);
const size_t slot = (m * QH + qh) * MAXCH + c;
if (k0 >= k1) {
if (lane == 0) {
PM[slot] = -INFINITY;
Expand Down Expand Up @@ -401,17 +404,19 @@ extern "C" __global__ void tf_q35_attn_partial(Geo geo, const __nv_bfloat16* __r
for (int i = 0; i < DPL; i++) PO[slot * HD + lane * DPL + i] = o[i];
}

// The P[0] chunks' partials into each (row, query head)'s output, rows qh * HD wide. Grid (qh, rows), a warp each.
// A row's chunks' partials into each (row, query head)'s output, rows qh * HD wide. Grid (qh, rows), a warp each.
extern "C" __global__ void __launch_bounds__(32) tf_q35_attn_merge(Geo geo, const float* __restrict__ PM, const float* __restrict__ PL,
const float* __restrict__ PO, const int* __restrict__ P,
__nv_bfloat16* __restrict__ OUT) {
const float* __restrict__ PO, const Seg* __restrict__ segs,
const int* __restrict__ rowseg, const unsigned* __restrict__ rowanc,
const int* __restrict__ P, __nv_bfloat16* __restrict__ OUT) {
constexpr int DPL = HD / 32;
const int nch = P[0];
const int QH = geo.qh;
const int lane = threadIdx.x, qh = blockIdx.x;
if (past_rows(P, blockIdx.y)) return;
const size_t m = blockIdx.y;
const size_t base = (m * QH + qh) * nch;
const int keys = segs[rowseg[m]].len + rows_seen(rowanc[m]);
const int ck = chunk_keys(keys), nch = (keys + ck - 1) / ck;
const size_t base = (m * QH + qh) * MAXCH;
float mx = -INFINITY;
for (int c = 0; c < nch; c++)
if (PL[base + c] > 0.0f) mx = fmaxf(mx, PM[base + c]);
Expand Down
1 change: 1 addition & 0 deletions zig/kernels/cuda/qwen35.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ constexpr int TAPS = 4; // causal convolution taps
constexpr int HD = 256; // attention head width
constexpr int MAXQ = 32; // attention query heads at most
constexpr int MAXW = 16; // a window's rows at most (the log's rows a stream)
constexpr int MAXCH = 64; // attention chunks a row at most (cuda_engine.zig's max_chunks)

// The model's widths: hidden, linear key heads, linear value heads, attention query and key/value heads, linear layers.
struct Geo {
Expand Down
15 changes: 6 additions & 9 deletions zig/src/families/qwen3_5/cuda_engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const max_segs = 32;
pub const most_verify = 32;
/// A window's rows at most (exact: one-row steps' bits; qwen35.cuh's MAXW, the rows a stream's log holds).
pub const most_window = 16;
/// Partial attention chunks a row at most: the chunk width grows with the window instead.
/// Partial attention chunks a row at most (qwen35.cuh's MAXCH): each row's chunk width grows with its own keys.
const max_chunks = 64;

/// Device allocations are 256-byte aligned, as the driver's are.
Expand Down Expand Up @@ -147,7 +147,7 @@ pub const Engine = struct {
const rules_at = rowanc_at + max_rows * 4;
const meta_at = rules_at + most_verify * @sizeOf(cuda.sample.Rule);
const tokens_at = meta_at + most_verify * 4;
const params_at = tokens_at + most_verify * 4; // the attention chunks, their width, the rows, the windows, record
const params_at = tokens_at + most_verify * 4; // two unused words, the rows, the windows, record
const tables_len = params_at + 8 * 4;

pub fn init(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8) !*Engine {
Expand Down Expand Up @@ -343,12 +343,10 @@ pub const Engine = struct {
pub fn forward(e: *Engine, parts: []const Part, ids: []const u32, keep: Keep) !void {
const g = e.g;
var total: usize = 0;
var widest: usize = 0;
for (parts) |p| {
try p.cache.settle(e);
if (p.rows == 0 or p.cache.len + p.rows > p.cache.cap) return error.QwenContextFull;
total += p.rows;
widest = @max(widest, p.cache.len + p.rows);
}
if (parts.len == 0 or parts.len > max_segs or total != ids.len or total > max_rows) return error.InvalidQwenSegments;
const record = keep != .last;
Expand Down Expand Up @@ -378,11 +376,10 @@ pub const Engine = struct {
m.* = @intCast(p.position -% 1); // sample.cu keys row r at meta + r + 1
}
}
// attention chunks: at most max_chunks a row, each a multiple of 128 keys
const ck = @max(128, (widest + max_chunks * 128 - 1) / (max_chunks * 128) * 128);
// words 0 and 1 are reserved (each row's attention chunks follow its own keys: qwen35.cu's chunk_keys)
const params = std.mem.bytesAsSlice(c_int, st[params_at..][0..32]);
params[0] = @intCast((widest + ck - 1) / ck);
params[1] = @intCast(ck);
params[0] = 0;
params[1] = 0;
params[2] = @intCast(total);
params[3] = @intCast(parts.len);
params[4] = @intFromBool(record);
Expand Down Expand Up @@ -497,7 +494,7 @@ pub const Engine = struct {
try e.run(e.fns.head_norm, .{ total * g.kv_heads, 1, 1 }, .{ 64, 1, 1 }, .{ e.kraw, m.k_norm, e.kn, @as(c_int, @intCast(g.kv_heads)), @as(c_int, @intCast(g.kv_heads * head_dim)), @as(c_int, head_dim), g.eps, t_params });
try e.run(e.fns.rope, .{ total, 1, 1 }, .{ head_dim, 1, 1 }, .{ geo, e.qn, e.kn, e.vraw, t_segs, t_rows, t_anc, ai, t_params });
try e.run(e.fns.attn_partial, .{ max_chunks, g.kv_heads, total }, .{ 32 * group, 1, 1 }, .{ geo, e.qn, t_segs, t_rows, t_anc, ai, t_params, @as(f32, 1.0 / 16.0), e.pm, e.pl, e.po });
try e.run(e.fns.attn_merge, .{ g.q_heads, total, 1 }, .{ 32, 1, 1 }, .{ geo, e.pm, e.pl, e.po, t_params, e.y });
try e.run(e.fns.attn_merge, .{ g.q_heads, total, 1 }, .{ 32, 1, 1 }, .{ geo, e.pm, e.pl, e.po, t_segs, t_rows, t_anc, t_params, e.y });
try e.run(e.fns.attn_gate, .{ (total * aw + 255) / 256, 1, 1 }, .{ 256, 1, 1 }, .{ geo, e.y, e.qraw, e.mix, @as(c_int, @intCast(total)), t_params });
try e.proj(m.o, e.mix, e.r, total, prompt);
ai += 1;
Expand Down
50 changes: 46 additions & 4 deletions zig/tests/qwen35_cuda_main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ pub fn main(init: std.process.Init) !u8 {
defer ctx.deinit();
const e = try E.Engine.init(init.gpa, init.io, &ctx, args[1]);
defer e.deinit();
var cache = try E.Cache.init(init.gpa, e, 4096);
// TF_Q35_PREFILL=N starts the stream after N cycled ids; TF_Q35_BESIDE=N steps an N-token stream beside it
const prefill = try envCount("TF_Q35_PREFILL");
const beside = try envCount("TF_Q35_BESIDE");
var cache = try E.Cache.init(init.gpa, e, 4096 + prefill);
defer cache.deinit(init.gpa);
try fill(e, &cache, ids.items, prefill);
var other = try E.Cache.init(init.gpa, e, 4096 + beside);
defer other.deinit(init.gpa);
try fill(e, &other, ids.items, beside);
var f: Fwd = .{ .e = e, .other = if (beside > 0) &other else null, .ids = ids.items };
var out: std.ArrayList(u8) = .empty;
if (std.mem.eql(u8, args[2], "step")) {
var first: i64 = 0;
var rest: i64 = 0;
for (ids.items, 0..) |id, i| {
const t0 = std.Io.Clock.awake.now(init.io).toNanoseconds();
try e.forward(&.{.{ .cache = &cache, .rows = 1 }}, &.{id}, .logits);
try f.run(.{ .cache = &cache, .rows = 1 }, &.{id});
const dt: i64 = @intCast(std.Io.Clock.awake.now(init.io).toNanoseconds() - t0);
if (i == 0) first = dt else rest += dt;
@memcpy(cache.logits, e.rowLogits(0));
Expand All @@ -42,7 +50,7 @@ pub fn main(init: std.process.Init) !u8 {
var w: usize = 1;
while (at < ids.items.len) : (w = w % e.window_rows + 1) {
const n = @min(w, ids.items.len - at);
try e.forward(&.{.{ .cache = &cache, .rows = n }}, ids.items[at..][0..n], .logits);
try f.run(.{ .cache = &cache, .rows = n }, ids.items[at..][0..n]);
for (0..n) |r| try out.appendSlice(a, std.mem.sliceAsBytes(e.rowLogits(r)));
at += n;
}
Expand Down Expand Up @@ -76,7 +84,7 @@ pub fn main(init: std.process.Init) !u8 {
parents[n] = 1;
n += 1;
}
try e.forward(&.{.{ .cache = &cache, .rows = n, .parents = parents[0..n] }}, toks[0..n], .logits);
try f.run(.{ .cache = &cache, .rows = n, .parents = parents[0..n] }, toks[0..n]);
for (path[0..kept]) |r| try out.appendSlice(a, std.mem.sliceAsBytes(e.rowLogits(r)));
if (n > 1) try cache.keep(e, path[0..kept]);
at += kept;
Expand All @@ -93,3 +101,37 @@ pub fn main(init: std.process.Init) !u8 {
try std.Io.Dir.cwd().writeFile(init.io, .{ .sub_path = args[4], .data = out.items });
return 0;
}

/// One forward keeping logits: the stream's part first (its rows' logits come first), then the second stream's step.
const Fwd = struct {
e: *E.Engine,
other: ?*E.Cache,
ids: []const u32,
n: usize = 0,

fn run(f: *Fwd, part: E.Part, toks: []const u32) !void {
const o = f.other orelse return f.e.forward(&.{part}, toks, .logits);
var all: [E.most_window + 1]u32 = undefined;
@memcpy(all[0..toks.len], toks);
all[toks.len] = f.ids[f.n % f.ids.len];
f.n += 1;
try f.e.forward(&.{ part, .{ .cache = o, .rows = 1 } }, all[0 .. toks.len + 1], .logits);
}
};

fn envCount(name: [:0]const u8) !usize {
const v = std.c.getenv(name) orelse return 0;
return std.fmt.parseInt(usize, std.mem.span(v), 10);
}

/// `n` positions of the ids cycled, through prompt forwards.
fn fill(e: *E.Engine, cache: *E.Cache, ids: []const u32, n: usize) !void {
var buf: [E.max_rows]u32 = undefined;
var at: usize = 0;
while (at < n) {
const k = @min(E.max_rows, n - at);
for (buf[0..k], at..) |*t, i| t.* = ids[i % ids.len];
try e.forward(&.{.{ .cache = cache, .rows = k }}, buf[0..k], .last);
at += k;
}
}