Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6636e69
cuda: a one-shot RoCE all-gather for two ranks (roce.zig, roce_gather…
BobClawblaw Oct 14, 2026
39de039
lane core and native CUDA host for two-rank families with a vision to…
BobClawblaw Oct 14, 2026
bdc47c8
Merge branch 'pr/roce-gather' into pr/glm-flash-cuda
BobClawblaw Oct 14, 2026
cbe08cf
GLM-5.3-Flash on two CUDA ranks: the native family (zig/src/families/…
BobClawblaw Oct 14, 2026
a76f16a
glm kernels: the tensor-core fragment header beside them (qmm_frag.cu…
BobClawblaw Oct 14, 2026
abb8bbe
glm: the three Flash Next kernels the family loads (experts, experts_…
BobClawblaw Oct 14, 2026
2903dd7
cuda abi: the driver entries the GLM family uses (module function enu…
BobClawblaw Oct 14, 2026
87ef757
tf-cuda-test glm-*: exit right after the pass or summary line (the te…
BobClawblaw Oct 14, 2026
a759ccd
Merge branch 'pr/roce-gather' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
db750c4
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
a2eefa2
glm cuda: zig fmt
BobClawblaw Oct 15, 2026
22d92e4
lane host: a warm pass that steps aside is not launched after its can…
BobClawblaw Oct 15, 2026
a76651a
glm cuda: comments one line each, files under 600 lines, the long hea…
BobClawblaw Oct 15, 2026
ef73ec3
tf_video: the includes back under its one-line header
BobClawblaw Oct 15, 2026
e0d3baa
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
0e50498
cuda: RoCE gather comments on one line each, the protocol notes in do…
BobClawblaw Oct 15, 2026
cc5d9d1
vision inputs: a server without --vision refuses image parts in the f…
BobClawblaw Oct 15, 2026
89c68c8
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
aaed024
qwen_image.formatSeconds: t * 10 rounded in integers, not f128, so te…
BobClawblaw Oct 15, 2026
2cc423f
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
9e6dfcd
lane host: a set-up job's kept entry stays pinned until its restore r…
BobClawblaw Oct 15, 2026
a8ea350
Merge remote-tracking branch 'fork/pr/lane-host-vision' into pr/glm-f…
BobClawblaw Oct 15, 2026
d069277
cuda: RoCE gather ranks agree one shard cap (each sends its max_bytes…
BobClawblaw Oct 15, 2026
22d2a30
Merge remote-tracking branch 'fork/pr/roce-gather' into pr/glm-flash-…
BobClawblaw Oct 15, 2026
b07b11b
glm cuda: the review's four fixes: a RoCE check after every sync that…
BobClawblaw Oct 15, 2026
52b82fc
prompt cache: an entry removed while pinned is dead to lookups and fr…
BobClawblaw Oct 15, 2026
468fa88
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
c525ea9
glm cuda: the gather check and the agreed settings' doc comments on o…
BobClawblaw Oct 15, 2026
7c9d845
Merge remote-tracking branch 'origin/main' into pr/roce-gather
BobClawblaw Oct 16, 2026
b8d45de
cuda: the RoCE gather joins tp_group: its setup crosses the group's N…
BobClawblaw Oct 16, 2026
bb7e2a2
Merge origin/main (1.0.12) into pr/lane-host-vision: main's image pat…
BobClawblaw Oct 16, 2026
b416ec8
lane core: several queued prompts in one pass for a backend with pref…
BobClawblaw Oct 16, 2026
1ac97f6
Merge remote-tracking branch 'fork/pr/roce-gather' into tmp-855-base
BobClawblaw Oct 16, 2026
b982a3b
Merge #853 and #852 (both on main 1.0.12 with #804) into pr/glm-flash…
BobClawblaw Oct 16, 2026
aa9d4a7
glm cuda: the ranks' communicator is a tp_group.Group; on two ranks t…
BobClawblaw Oct 16, 2026
653bd37
glm cuda: EXL3 routed experts (Mia-AiLab's checkpoint: 4-bit mcg trel…
BobClawblaw Oct 16, 2026
8278074
kernel contract: glm_exl3's eight launches with their sm_80/86/89/120…
BobClawblaw Oct 16, 2026
084c253
glm cuda: the ranks watch each other (tp_watch): a rank that fails or…
BobClawblaw Oct 16, 2026
b090a7b
glm cuda: verify windows to the decode buffers' 8 rows, so the lane c…
BobClawblaw Oct 16, 2026
93f5261
glm cuda: the image tower (model.visual.*, rank 0's) on cuBLASLt and …
BobClawblaw Oct 16, 2026
f9d4f9f
glm cuda: images on two ranks (--vision): rank 0's tower encodes a pr…
BobClawblaw Oct 16, 2026
3cc4581
tools/glm_vision_ref.py: the torch reference tf-cuda-test glm-vision …
BobClawblaw Oct 16, 2026
edc2285
glm cuda: the bf16 linears on glm_mm (ours, from the glm_cuda family)…
BobClawblaw Oct 16, 2026
875b695
glm cuda: TF_GLM_PROF=1 also times prompt chunks by section (gate, fo…
BobClawblaw Oct 16, 2026
526ffd4
Merge the glm_cuda family's head (c525ea99): its work now lives in fa…
BobClawblaw Oct 16, 2026
999ae3b
glm: video on the CUDA family (main's planner shared as video_core, o…
BobClawblaw Oct 16, 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: 3 additions & 1 deletion tools/zig/lean_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"zig/src/families/flashnext/roles_gen.zig",
"zig/kernels/metal/flashnext/sources_gen.zig",
)
# Third-party sources kept as their authors wrote them (stb_image.h) are skipped too.
VENDORED = "zig/vendor/"


def generator_output(path: Path) -> bool:
Expand All @@ -31,7 +33,7 @@ def generator_output(path: Path) -> bool:

def problems(path: Path) -> list[str]:
"""Every rule break in one file, as 'path:line: reason'."""
if generator_output(path):
if generator_output(path) or path.relative_to(ROOT).as_posix().startswith(VENDORED):
return []
lines = path.read_text(errors="replace").splitlines()
rel, found = path.relative_to(ROOT), []
Expand Down
3 changes: 1 addition & 2 deletions zig/build/vision.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! The image-input module (zig/src/vision/qwen_image.zig with stb_image compiled in), shared by every build that
//! carries the engine API (the server reaches it as engine_api.qwen_image) and by the CUDA test runner.
//! The image-input module (vision/qwen_image.zig with stb_image), shared by the engine API and the CUDA test runner.
const std = @import("std");

pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
Expand Down
77 changes: 15 additions & 62 deletions zig/src/core/lane_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,8 @@ pub const LaneHost = struct {
h.mutex.unlock(h.io);
}

/// Prefills the next queued request into a free lane; false when none waits or no lane is free.
/// The next queued request into the admitted ones (null: none waits, no lane is free, or its caches do not fit yet).
fn takeJob(h: *LaneHost) ?*Job {
/// The next queued request into the admitted ones (null: none waits, no lane is free, or its caches do not fit).
pub fn takeJob(h: *LaneHost) ?*Job {
h.lock();
if (h.queued.items.len == 0 or h.admitted.items.len >= @min(h.info_.lanes, h.memory_lanes orelse h.info_.lanes)) {
h.unlock();
Expand All @@ -310,9 +309,12 @@ pub const LaneHost = struct {
}

/// The admitted request's stream (its kept state, drafter and grammar); false when it was dropped instead.
fn setup(h: *LaneHost, job: *Job) bool {
pub fn setup(h: *LaneHost, job: *Job) bool {
const r = job.request;
if (r.background and r.max_tokens == 0 and h.warm_quiet > 0 and !h.quiet(job)) return h.cancel(job); // other work came first: it never ran
if (r.background and r.max_tokens == 0 and h.warm_quiet > 0 and !h.quiet(job)) { // other work came first: it never ran
_ = h.cancel(job);
return false;
}
var reuse: lanes.stream.Reuse = .{};
// the entry stays alive until the backend restores it: nothing keeps between here and this stream's own pass
if (h.cache) |store| if (r.prompt_logprobs == null and !r.untaught) if (store.lookupRewind(h.gpa, r.prompt, born(r), r.history_len, r.rewind_len, r.shared_prefixes, r.chunks, h.spans(r))) |l| {
Expand Down Expand Up @@ -349,9 +351,9 @@ pub const LaneHost = struct {
.grammar = if (r.structure) |st| h.constraint(st) catch |e| {
job.proposer.deinit();
{
_ = h.drop(job, if (e == error.OutOfMemory) "out of memory" else "the reply's grammar could not start (xgrammar)");
return false;
}
_ = h.drop(job, if (e == error.OutOfMemory) "out of memory" else "the reply's grammar could not start (xgrammar)");
return false;
}
} else null,
.images = r.images,
.decode_spans = h.spans(r),
Expand All @@ -369,8 +371,8 @@ pub const LaneHost = struct {
return true;
}

/// An admitted, set-up request into the engine: a warm pass, the lone driver, the fill queue, or its own prompt pass.
fn launch(h: *LaneHost, job: *Job) bool {
/// An admitted request into the engine: a warm pass, the lone driver, the fill queue, or its own prompt pass.
pub fn launch(h: *LaneHost, job: *Job) bool {
const r = job.request;
const began = job.began;
if (r.max_tokens == 0 and h.info_.warm_turns) return h.warmPass(job, began);
Expand All @@ -388,55 +390,6 @@ pub const LaneHost = struct {
if (h.deliver(job)) h.remove(job);
return true;
}
fn admitOne(h: *LaneHost) bool {
const job = h.takeJob() orelse return false;
if (!h.setup(job)) return true;
return h.launch(job);
}

/// Every queued request a lane is free for, their prompts in one pass where the backend takes several
/// (lanes.Engine.addStreams); the engine's special paths (chunked prompts, warm passes, the lone driver) go
/// through admitOne as before.
fn admitMany(h: *LaneHost) bool {
if (!h.core.batchesPrompts() or h.core.fills()) return h.admitOne();
h.lock();
const waiting = h.queued.items.len;
const free = h.info_.lanes -| h.admitted.items.len;
h.unlock();
if (waiting < 2 or free < 2) return h.admitOne();
var jobs: [lanes.Engine.max_together]*Job = undefined;
var n: usize = 0;
while (n < jobs.len and n < free) {
const job = h.takeJob() orelse break;
if (!h.setup(job)) continue;
if ((job.request.max_tokens == 0 and h.info_.warm_turns) or lone.fits(h, job)) {
_ = h.launch(job);
continue;
}
jobs[n] = job;
n += 1;
}
if (n == 0) return true;
if (n == 1) return h.launch(jobs[0]);
var streams: [lanes.Engine.max_together]*lanes.Stream = undefined;
for (jobs[0..n], 0..) |job, i| streams[i] = &job.stream;
var errs: [lanes.Engine.max_together]?anyerror = undefined;
h.core.addStreams(streams[0..n], errs[0..n]);
for (jobs[0..n], errs[0..n]) |job, err| {
if (err) |e| {
_ = switch (e) {
error.Cancelled => h.cancel(job),
error.LaneMemory => h.requeue(job),
else => h.drop(job, h.words(e)),
};
continue;
}
h.prefilled(job, job.began);
if (h.deliver(job)) h.remove(job);
}
return true;
}


/// The next filling job's next prompt chunk: the other streams' rounds run between chunks, not after the prompt.
fn fillOne(h: *LaneHost) void {
Expand Down Expand Up @@ -515,7 +468,7 @@ pub const LaneHost = struct {
}

/// No memory for another lane yet: the job waits at the queue's head, and admission holds until a job ends.
fn requeue(h: *LaneHost, job: *Job) bool {
pub fn requeue(h: *LaneHost, job: *Job) bool {
if (job.stream.grammar) |g| g.deinit(h.gpa); // the next admission compiles its own
job.stream.deinit(h.gpa);
job.proposer.deinit();
Expand Down Expand Up @@ -546,7 +499,7 @@ pub const LaneHost = struct {
const alone_words = "the engine has no memory for this request's caches with nothing else running: lower --context or --parallel";

/// A job cancelled in its prompt pass, its lane already released.
fn cancel(h: *LaneHost, job: *Job) bool {
pub fn cancel(h: *LaneHost, job: *Job) bool {
h.remove(job);
h.finish(job, .cancelled, "");
return true;
Expand All @@ -565,7 +518,7 @@ pub const LaneHost = struct {
fn run(h: *LaneHost) void {
while (true) {
h.takeCancels();
while (h.admitMany()) {}
while (admit.many(h)) {}
h.fillOne();
status.noteLive(h);
h.lock();
Expand Down
50 changes: 50 additions & 0 deletions zig/src/core/lane_host_admit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const api = @import("engine_api.zig");
const LaneHost = @import("lane_host.zig").LaneHost;
const Job = LaneHost.Job;
const lanes = @import("lanes");
const lone = @import("lane_host_lone.zig");

/// The queued job to admit (its index), or null to hold; the caller holds the lock and a lane is free.
pub fn pick(h: *LaneHost) ?usize {
Expand All @@ -21,3 +23,51 @@ pub fn pick(h: *LaneHost) ?usize {
fn fits(a: api.Admit, job: *const Job) bool {
return a.fits(a.ctx, @intCast(job.request.prompt.len), job.request.max_tokens);
}

/// Every queued request a lane is free for, prompts in one pass where the backend takes several; else one at a time.
pub fn many(h: *LaneHost) bool {
if (!h.core.batchesPrompts() or h.core.fills()) return one(h);
h.lock();
const waiting = h.queued.items.len;
const free = h.info_.lanes -| h.admitted.items.len;
h.unlock();
if (waiting < 2 or free < 2) return one(h);
var jobs: [lanes.Engine.max_together]*Job = undefined;
var n: usize = 0;
while (n < jobs.len and n < free) {
const job = h.takeJob() orelse break;
if (!h.setup(job)) continue;
if ((job.request.max_tokens == 0 and h.info_.warm_turns) or lone.fits(h, job)) {
_ = h.launch(job);
continue;
}
jobs[n] = job;
n += 1;
}
if (n == 0) return true;
if (n == 1) return h.launch(jobs[0]);
var streams: [lanes.Engine.max_together]*lanes.Stream = undefined;
for (jobs[0..n], 0..) |job, i| streams[i] = &job.stream;
var errs: [lanes.Engine.max_together]?anyerror = undefined;
h.core.addStreams(streams[0..n], errs[0..n]);
for (jobs[0..n], errs[0..n]) |job, err| {
if (err) |e| {
_ = switch (e) {
error.Cancelled => h.cancel(job),
error.LaneMemory => h.requeue(job),
else => h.drop(job, h.words(e)),
};
continue;
}
h.prefilled(job, job.began);
if (h.deliver(job)) h.remove(job);
}
return true;
}

/// The next queued request admitted alone: its stream set up and launched.
fn one(h: *LaneHost) bool {
const job = h.takeJob() orelse return false;
if (!h.setup(job)) return true;
return h.launch(job);
}
10 changes: 4 additions & 6 deletions zig/src/core/lanes/backend.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! What the round loop asks a backend (Metal, CUDA or the fake) to compute; drawn tokens stay on the GPU behind handles.
//! What the round loop asks a backend (Metal, CUDA or the fake); drawn tokens stay on the GPU behind handles.
const std = @import("std");
const Stream = @import("stream.zig").Stream;
const Shape = @import("shape.zig").Shape;
Expand Down Expand Up @@ -68,8 +68,7 @@ pub const Backend = struct {
prefill: *const fn (ptr: *anyopaque, s: *Stream) anyerror!void,
/// One chunk of the prompt pass, false while chunks remain; null: the pass runs whole.
prefill_step: ?*const fn (ptr: *anyopaque, s: *Stream) anyerror!bool = null,
/// Prefill several streams' prompts in one pass (each then as after `prefill`); false, having done nothing,
/// when they do not go together (each is then prefilled alone).
/// Several streams' prompts in one pass, each as after `prefill`; false, doing nothing, when they don't fit.
prefill_many: ?*const fn (ptr: *anyopaque, ss: []const *Stream) anyerror!bool = null,
/// Draw the first token from the prompt's last row at `position`; a handle to it.
first: *const fn (ptr: *anyopaque, s: *Stream, position: u64) anyerror!u64,
Expand Down Expand Up @@ -105,8 +104,7 @@ pub const Backend = struct {
prompt_rows: bool = false,
/// The stream left the rounds: free its caches and held drafts.
release: *const fn (ptr: *anyopaque, s: *Stream) void,
/// After a failed round: the streams it failed for alone (their caches could not grow), which the caller
/// ends while the round's other streams go on; 0 when the failure is the round's.
/// After a failed round: the streams whose caches could not grow, ended alone; 0 when the round failed.
refused: ?*const fn (ptr: *anyopaque, out: []*Stream) usize = null,
};

Expand Down Expand Up @@ -143,7 +141,7 @@ pub const Backend = struct {
b.vtable.release(b.ptr, s);
}

/// A driver of its own starts a stream: the prompt pass and its first token, committed; a cancel or failure releases it.
/// A driver of its own starts a stream: prompt pass and first token, committed; cancel or failure releases it.
pub fn opening(b: Backend, gpa: std.mem.Allocator, s: *Stream) !u32 {
errdefer b.release(s);
try b.prefill(s);
Expand Down
61 changes: 4 additions & 57 deletions zig/src/core/lanes/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,69 +83,16 @@ pub const Engine = struct {
pub const fills = open.fills;
pub const fillStream = open.fillStream;

/// A stream another driver prefilled and drew `token` for: drafts asked, the token committed, rounds from here.
pub fn adoptPrefilled(e: *Engine, s: *Stream, token: u32) !void {
_ = e.arena.reset(.retain_capacity);
s.context.shrinkRetainingCapacity(s.prompt_len);
s.pending = null;
s.cache_len = s.prompt_len;
if (e.cfg.family_mtp and s.drafts) {
const d: u32 = @intCast(try e.rule.depth(win.who(s)));
try e.backend.draft(&.{.{ .stream = s, .follow = &.{}, .first = .{ .value = token }, .rows = null, .start = s.prompt_len, .position = s.prompt_len + 1, .depth = d }});
s.dropHeld(e.gpa);
s.next = .{ .count = d };
if (e.backend.vtable.tree) |tree| if (try tree(e.backend.ptr, s, e.gpa)) |held| {
s.next = held;
};
}
if (s.logprobs != null) return error.LogprobsUnsupported; // another driver's prompt pass gives no first row
_ = try s.commit(e.gpa, &.{token}, &.{});
s.pending = token;
if (s.finished) return e.release(s);
try e.live.append(e.gpa, s);
}

/// Streams a backend prefills in one pass at most.
pub const max_together = 16;
pub const adoptPrefilled = open.adoptPrefilled;
pub const max_together = open.max_together;
pub const addStreams = open.addStreams;

/// Whether the backend prefills several prompts in one pass (addStreams batches them).
pub fn batchesPrompts(e: *const Engine) bool {
return e.backend.vtable.prefill_many != null;
}

/// open.add for several streams: their prompts in one pass when the backend takes them together (else each
/// alone), then each opened as after its own prefill. ``errs[i]``: stream i's error (a cancelled one is released
/// already; the caller discards a failed one).
pub fn addStreams(e: *Engine, ss: []const *Stream, errs: []?anyerror) void {
for (errs) |*x| x.* = null;
const many = e.backend.vtable.prefill_many orelse return e.addEach(ss, errs);
if (ss.len < 2 or ss.len > max_together) return e.addEach(ss, errs);
_ = e.arena.reset(.retain_capacity);
for (ss) |s| trail.event(e, &.{ f("ev", str("add")), f("stream", str(s.id)) }) catch {};
const together = many(e.backend.ptr, ss) catch |err| {
for (errs) |*x| x.* = err;
return;
};
if (!together) return e.addEach(ss, errs);
for (ss, errs) |s, *x| {
if (s.isCancelled()) { // cancelled in its pass: no first token
e.backend.release(s);
x.* = error.Cancelled;
continue;
}
open.opened(e, s) catch |err| {
x.* = err;
};
}
}

fn addEach(e: *Engine, ss: []const *Stream, errs: []?anyerror) void {
for (ss, errs) |s, *x| open.add(e, s) catch |err| {
x.* = err;
};
}

/// Take over a stream another driver decoded so far: its cache settled, `pending` and `cache_len` set, no drafts held.
/// Take over a stream another driver decoded: cache settled, `pending` and `cache_len` set, no drafts held.
pub fn adopt(e: *Engine, s: *Stream) !void {
try e.live.append(e.gpa, s);
}
Expand Down
55 changes: 55 additions & 0 deletions zig/src/core/lanes/open.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,58 @@ pub fn fillStream(e: *Engine, s: *Stream, first: bool) !bool {
try opened(e, s);
return true;
}

/// A stream another driver prefilled and drew `token` for: drafts asked, the token committed, rounds from here.
pub fn adoptPrefilled(e: *Engine, s: *Stream, token: u32) !void {
_ = e.arena.reset(.retain_capacity);
s.context.shrinkRetainingCapacity(s.prompt_len);
s.pending = null;
s.cache_len = s.prompt_len;
if (e.cfg.family_mtp and s.drafts) {
const d: u32 = @intCast(try e.rule.depth(win.who(s)));
try e.backend.draft(&.{.{ .stream = s, .follow = &.{}, .first = .{ .value = token }, .rows = null, .start = s.prompt_len, .position = s.prompt_len + 1, .depth = d }});
s.dropHeld(e.gpa);
s.next = .{ .count = d };
if (e.backend.vtable.tree) |tree| if (try tree(e.backend.ptr, s, e.gpa)) |held| {
s.next = held;
};
}
if (s.logprobs != null) return error.LogprobsUnsupported; // another driver's prompt pass gives no first row
_ = try s.commit(e.gpa, &.{token}, &.{});
s.pending = token;
if (s.finished) return e.release(s);
try e.live.append(e.gpa, s);
}

/// Streams a backend prefills in one pass at most.
pub const max_together = 16;

/// open.add for several streams, prompts in one pass when the backend allows; ``errs[i]``: stream i's error.
pub fn addStreams(e: *Engine, ss: []const *Stream, errs: []?anyerror) void {
for (errs) |*x| x.* = null;
const many = e.backend.vtable.prefill_many orelse return addEach(e, ss, errs);
if (ss.len < 2 or ss.len > max_together) return addEach(e, ss, errs);
_ = e.arena.reset(.retain_capacity);
for (ss) |s| trail.event(e, &.{ f("ev", str("add")), f("stream", str(s.id)) }) catch {};
const together = many(e.backend.ptr, ss) catch |err| {
for (errs) |*x| x.* = err;
return;
};
if (!together) return addEach(e, ss, errs);
for (ss, errs) |s, *x| {
if (s.isCancelled()) { // cancelled in its pass: no first token
e.backend.release(s);
x.* = error.Cancelled;
continue;
}
opened(e, s) catch |err| {
x.* = err;
};
}
}

fn addEach(e: *Engine, ss: []const *Stream, errs: []?anyerror) void {
for (ss, errs) |s, *x| add(e, s) catch |err| {
x.* = err;
};
}
Loading