Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Docs: the optimization ledger adds rows 30 to 32 (small-kernel fusion, stream-tile prefetch and the filled-item prompt grid), each `failed` for Flash Next on HIP with the measurements from #1603 (#3160, refs #1603). Thanks to @gilby.
- DeepSeek V4.1 on Metal: with `--vision`, the server loads the checkpoint's vision tower (a checkpoint without `vision.*` tensors stops at load), and chats with PNG, JPEG or WebP image parts put the tower's rows into the prompt, within `--vision-image-tokens` (4,096 by default) and `--vision-max-images` (4); an image's span is prefilled in one chunk, the prompt cache keys image rows by the image's hash, text that spells the image token stays text, and a vision config with zero heads, a zero `max_wh_ratio` or a `downsample_ratio` below 2 is refused by name (#3926, part of #1676). Thanks to @gilby.

- Docs: the Qwen3.8-27B recipe says to serve with `--thinking-budget 4096` when thinking is on, and gives its GPQA Diamond result on TensorFold 1.2.7 and one GB10: replies cut at the limit from 78 of 198 to 0, and correct from 58.1% to 82.8% (#3373, fixes #3323).
- Server: `/v1/metrics` reports a `tensorfold:open_connections` gauge, the client connections admitted over TCP or the Unix socket and not yet closed, including idle keep-alive ones (#3925). Thanks to @plotarmordev.

- Docs: the optimization ledger adds rows 30 to 32 (small-kernel fusion, stream-tile prefetch and the filled-item prompt grid), each `failed` for Flash Next on HIP with the measurements from #1603 (#3160, refs #1603). Thanks to @gilby.

- Docs: the Qwen3.8-27B recipe says to serve with `--thinking-budget 4096` when thinking is on, and gives its GPQA Diamond result on TensorFold 1.2.7 and one GB10: replies cut at the limit from 78 of 198 to 0, and correct from 58.1% to 82.8% (#3373, fixes #3323).

- Idol on CUDA: `tf-idol` runs Idol's denoiser on GB10 (sm_121) and on RTX 50-series and RTX PRO Blackwell (sm_120) cards against a reference case, gated step by step against idol-sdk's float32 run as on Metal, and refuses other GPUs by name (#1767, for #1757). Thanks to @jayleaton, with device runs from @GustavBlack and @Nipale-ai.

Expand Down
8 changes: 8 additions & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ common EXL3 operators and their per-slot epilogues; tile decoding is shared.
The ExLlamaV3 MIT attribution, Copyright (c) 2025 Turboderp, applies as described above.
No external reference, captured cubin, model shard or CUDA compiler is bundled by this patch.

## DeepSeek-V4.1 Metal image preparation

The image preparation (`zig/src/families/deepseek_v41/metal/vision_prep.zig`,
`zig/src/families/deepseek_v41/metal/vision_prepare.zig`) follows the image budgets and geometry of
`image_processor.py` in DeepSeek AI's MIT licensed
[deepseek-ai/DeepSeek-V4.1-Flash-1017](https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash-1017)
(revision 08c8657f02291e11b6f6b63e0e16014f8e748518), Copyright (c) 2023 DeepSeek.

## Shared EXL3 resolver setup

The shared EXL3 resolver split and CUDA lane option forwarding retain work from PR #728 by @ajensenwaud,
Expand Down
3 changes: 3 additions & 0 deletions docs/memory-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ device gauges retain their backend scope and reset behavior. Process and device
unified memory: do not add them. RSS, allocator peaks and physical footprint are different measurements.
The Darwin structure follows Apple's `sys/resource.h` `rusage_info_v4` layout; host tests check its size
and both footprint offsets, and exercise the kernel reader on macOS.
`tensorfold:open_connections` gauges admitted client connections — over TCP and the Unix socket alike — until
close, including the scrape itself and idle keep-alive connections; rejected-over-limit connections are
excluded.

Linux has no physical footprint count, so the footprint gauges stay macOS only. On Linux, `/metrics` reads
`/proc/self/status` at scrape time and reports the resident set: `tensorfold:process_resident_bytes` (`VmRSS`),
Expand Down
31 changes: 30 additions & 1 deletion zig/build/dsv41_metal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn package(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Modu
.link_libc = true,
.imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "dsv41", .module = host } },
});
pkg.addImport("core", host.import_table.get("core").?); // the picture decoder image input reads
return .{ .pkg = pkg, .host = host };
}

Expand Down Expand Up @@ -55,6 +56,22 @@ pub fn steps(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module
const run = b.addRunArtifact(b.addTest(.{ .root_module = tests }));
run.has_side_effects = true; // a device run: never cached
step.dependOn(&run.step);
const vis_run = b.addRunArtifact(b.addTest(.{ .root_module = tests, .filters = &.{"dsv41 metal vision"} }));
vis_run.has_side_effects = true;
const vis_step = b.step("test-dsv41-metal-vision", "DeepSeek-V4.1 Metal vision: the tower against float64, image rows in prompts");
vis_step.dependOn(&vis_run.step);
const prepare_run = b.addRunArtifact(b.addTest(.{ .root_module = p.pkg, .filters = &.{"prepare"} }));
const prepare_test_mod = b.createModule(.{
.root_source_file = b.path("zig/tests/dsv41_metal/vision_prepare_test.zig"),
.target = b.graph.host,
.optimize = .Debug,
.link_libc = true,
.imports = &.{.{ .name = "dsv41_metal", .module = p.pkg }},
});
const prepare_test_run = b.addRunArtifact(b.addTest(.{ .root_module = prepare_test_mod }));
const prepare_step = b.step("test-dsv41-metal-prepare", "DeepSeek-V4.1 Metal: image bytes to patches");
prepare_step.dependOn(&prepare_run.step);
prepare_step.dependOn(&prepare_test_run.step);
// the served engine over the tiny model: LaneHost replies, drafts, the prompt cache, seeds, cancels
const serve = b.createModule(.{
.root_source_file = b.path("zig/tests/dsv41_metal/serve_test.zig"),
Expand All @@ -70,9 +87,10 @@ pub fn steps(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module
const serve_step = b.step("test-dsv41-metal-serve", "DeepSeek-V4.1 Metal served through LaneHost: concurrent, drafted, cached, seeded, cancelled");
serve_step.dependOn(&serve_run.step);
// the release-checkpoint steps (TF_DSV41_REAL) stay opt-in, so the umbrella holds the default device suites
const all = b.step("test-dsv41-metal-all", "DeepSeek-V4.1 Metal: every device test step on this GPU (layer, served)");
const all = b.step("test-dsv41-metal-all", "DeepSeek-V4.1 Metal: every device test step on this GPU (layer, served, vision)");
all.dependOn(step);
all.dependOn(serve_step);
all.dependOn(vis_step);
const fast = package(b, metal, sources, .ReleaseSafe);
const real = b.createModule(.{
.root_source_file = b.path("zig/tests/dsv41_metal/real_test.zig"),
Expand Down Expand Up @@ -107,6 +125,17 @@ pub fn steps(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module
const synth_run = b.addRunArtifact(b.addTest(.{ .root_module = synth }));
synth_run.has_side_effects = true;
b.step("time-dsv41-metal-synth", "DeepSeek-V4.1 Metal: synthetic release-shape layers timed per row and dispatch class").dependOn(&synth_run.step);
// the release's own vision tower (TF_DSV41_REAL_VISION) against float64
const vreal = b.createModule(.{
.root_source_file = b.path("zig/tests/dsv41_metal/vision_real_test.zig"),
.target = b.graph.host,
.optimize = .ReleaseSafe,
.link_libc = true,
.imports = &.{ .{ .name = "dsv41_metal", .module = fast.pkg }, .{ .name = "metal", .module = metal }, .{ .name = "dsv41", .module = fast.host } },
});
const vreal_run = b.addRunArtifact(b.addTest(.{ .root_module = vreal }));
vreal_run.has_side_effects = true;
b.step("test-dsv41-metal-vision-real", "DeepSeek-V4.1 Metal: the release's vision tower (TF_DSV41_REAL_VISION) against float64").dependOn(&vreal_run.step);
const ptime = b.createModule(.{
.root_source_file = b.path("zig/tests/dsv41_metal/prompt_time.zig"),
.target = b.graph.host,
Expand Down
7 changes: 7 additions & 0 deletions zig/src/families/deepseek_v41/draft/iface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub const Target = struct {
replays: ?*const fn (ptr: *anyopaque, list: []const Final) anyerror!void = null,
/// Engram reads ahead of the next window (TF_DSV41_ENGRAM_WARM); reads only, window bits are unaffected.
warm: ?*const fn (ptr: *anyopaque, asks: []const Warm) void = null,
/// Before `prefill`: the slot's prompt images (their rows replace placeholder ids); borrowed until it returns.
images: ?*const fn (ptr: *anyopaque, slot: u32, images: []const lanes.stream.Image) anyerror!void = null,
/// After `prefill`: the prompt rows the slot took from kept state (its first row computed is this one).
resumed: ?*const fn (ptr: *anyopaque, slot: u32) u64 = null,
/// Committed rows' taps for positions [start, start + rows), in order; null: not kept. null fn: none kept.
Expand All @@ -61,6 +63,11 @@ pub const Target = struct {
return if (t.vtable.history) |f| f(t.ptr, slot, start, rows) else null;
}

/// The slot's prompt images for its next `prefill`; error.NoImages when the target has no image path.
pub fn setImages(t: Target, slot: u32, images: []const lanes.stream.Image) !void {
return (t.vtable.images orelse return error.NoImages)(t.ptr, slot, images);
}

/// The prompt rows the slot's last `prefill` took from kept state (0: none, or a target that keeps none).
pub fn resumedRows(t: Target, slot: u32) u64 {
return if (t.vtable.resumed) |f| f(t.ptr, slot) else 0;
Expand Down
1 change: 1 addition & 0 deletions zig/src/families/deepseek_v41/draft/lanes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub const Lanes = struct {
x.policy.depth.reset(slot);
const ids = s.prompt();
x.target.admit(slot, s.max_new);
if (s.images.len > 0) try x.target.setImages(slot, s.images);
st.first = try x.target.prefill(slot, ids, s.sampling, ids.len);
s.cached = @intCast(x.target.resumedRows(slot));
st.len = ids.len;
Expand Down
44 changes: 42 additions & 2 deletions zig/src/families/deepseek_v41/metal/boot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const scratch = @import("scratch.zig");
const forward = @import("forward.zig");
const serve = @import("serve.zig");
const target_ = @import("target.zig");
const vision = @import("vision.zig");
const vw = @import("vision_weights.zig");

const Config = host.config.Config;
const eh = host.engram_host;
Expand All @@ -30,6 +32,11 @@ pub const Options = struct {
small: bool = false,
/// Engram row shards (null: TF_DSV41_ENGRAM_DIR, else the config's engram_table_dir beside the checkpoint)
engram_dir: ?[]const u8 = null,
/// --vision: load the checkpoint's image tower (its tensors read beside the backbone)
vision: bool = false,
/// a request's image tokens in all, and its images (--vision-image-tokens, --vision-max-images)
image_tokens: u32 = 4096,
max_images: u32 = 4,
};

/// Everything a served model borrows, owned here and closed after the model's target and pass.
Expand All @@ -47,6 +54,8 @@ pub const Boot = struct {
lbuf: [eh.max_layers]u32 = undefined,
eng: eh.Host = undefined,
rows: ?*er.Rows = null,
kit: ?vision.Kit = null,
vw: ?vw.Weights = null,

/// The checkpoint in `dir` loaded and served by a `serve.Metal` (the caller's `serve_lanes.Served` closes it).
pub fn open(a: Allocator, io: std.Io, dir: []const u8, o: Options) !*serve.Metal {
Expand All @@ -56,6 +65,8 @@ pub const Boot = struct {
b.ds = null;
b.map_bytes = &.{};
b.rows = null;
b.kit = null;
b.vw = null;
b.c = try readConfig(a, io, dir, o.small);
const c = &b.c;
const limit: u32 = @intCast(@min(o.context orelse 262144, c.rope.max_positions));
Expand All @@ -71,6 +82,8 @@ pub const Boot = struct {
errdefer b.w.deinit();
if (o.drafts and c.mtp_layers > 0) b.ds = try dspark.loadWeights(a, io, b.device, &pack, c);
errdefer if (b.ds) |*w| w.deinit();
try b.loadVision(io, dir, &pack, o);
errdefer b.closeVision();
b.tables = try rope.Tables.init(b.device, c, limit);
errdefer b.tables.deinit();
b.s = try scratch.Scratch.init(b.device, c, limit);
Expand All @@ -79,8 +92,34 @@ pub const Boot = struct {
errdefer b.closeEngram();
const r = b.w.report();
std.log.info("dsv41 metal: {d} layers, {d} shards ({d} MiB mapped, {d} MiB copied), {d} slots of {d} positions, DSpark {s}", .{ c.layers, r.shards, r.shard_bytes >> 20, r.arena_bytes >> 20, o.slots, limit, if (b.ds != null) "on" else "off" });
const env: target_.Env = .{ .device = b.device, .queue = b.queue, .c = c, .p = &b.p, .tables = &b.tables, .s = &b.s, .model = b.w.view(), .lookup = lookup };
return serve.Metal.open(a, .{ .env = env, .top = &b.w.top, .dspark = if (b.ds) |*w| w else null, .owner = .{ .ctx = b, .close = close }, .resident = if (b.w.resident) |*wr| wr else null }, .{ .slots = o.slots, .limit = limit, .drafts = o.drafts, .cache_bytes = o.cache_bytes });
const env: target_.Env = .{ .device = b.device, .queue = b.queue, .c = c, .p = &b.p, .tables = &b.tables, .s = &b.s, .model = b.w.view(), .lookup = lookup, .vision = if (b.vw) |*w| .{ .kit = &b.kit.?, .w = w } else null };
return serve.Metal.open(a, .{ .env = env, .top = &b.w.top, .dspark = if (b.ds) |*w| w else null, .owner = .{ .ctx = b, .close = close }, .resident = if (b.w.resident) |*wr| wr else null, .images = if (b.vw != null) .{ .io = io, .tokens = o.image_tokens, .count = o.max_images } else null }, .{ .slots = o.slots, .limit = limit, .drafts = o.drafts, .cache_bytes = o.cache_bytes });
}

/// The tower's kernels and weights, and each layer's image routing bias attached to the backbone's layers.
fn loadVision(b: *Boot, io: std.Io, dir: []const u8, pack: *const host.pack.Pack, o: Options) !void {
if (!o.vision) return;
if (!vw.Weights.present(pack)) {
std.log.err("dsv41 metal: --vision, but {s} has no vision tower (no vision.* tensors)", .{dir});
return error.NoVisionTower;
}
const path = try std.fs.path.join(b.a, &.{ dir, "config.json" });
defer b.a.free(path);
const text = try std.Io.Dir.cwd().readFileAlloc(io, path, b.a, .limited(1 << 24));
defer b.a.free(text);
const cfg = (try vision.Config.parse(b.a, text, b.c.hidden)) orelse vision.Config{ .out_dim = b.c.hidden };
b.kit = try vision.Kit.load(b.device);
errdefer b.closeVision();
b.vw = try vw.Weights.load(b.a, io, b.device, pack, cfg, b.c.layers, b.c.experts);
for (b.w.layers, b.vw.?.bias_vl) |*l, r| l.gate_bvl = r;
std.log.info("dsv41 metal: vision tower on ({d} blocks of {d}, patches of {d}, up to {d} tokens an image)", .{ cfg.layers, cfg.dim, cfg.patch, cfg.max_tokens });
}

fn closeVision(b: *Boot) void {
if (b.vw) |*w| w.deinit();
if (b.kit) |*k| k.deinit();
b.vw = null;
b.kit = null;
}

fn readConfig(a: Allocator, io: std.Io, dir: []const u8, small: bool) !Config {
Expand Down Expand Up @@ -151,6 +190,7 @@ pub const Boot = struct {
b.s.deinit();
b.tables.deinit();
if (b.ds) |*w| w.deinit();
b.closeVision();
b.w.deinit();
b.p.deinit();
b.queue.deinit();
Expand Down
3 changes: 3 additions & 0 deletions zig/src/families/deepseek_v41/metal/entry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub fn open(a: Allocator, gpa: Allocator, io: std.Io, o: api.Open, problem: *[]c
.context = if (o.context) |c| @intCast(@max(c, 1)) else null,
.drafts = o.drafts,
.cache_bytes = if (o.prompt_cache_gib) |g| @intFromFloat(@max(g, 0) * (1 << 30)) else 4 << 30,
.vision = o.vision != null,
.image_tokens = if (o.vision) |v| v.image_tokens else 4096,
.max_images = if (o.vision) |v| v.max_images else 4,
.small = try env.read(bool, "TF_DSV41_SMALL", false),
};
const m = pkg.boot.Boot.open(gpa, io, o.dir, opts) catch |e| {
Expand Down
10 changes: 10 additions & 0 deletions zig/src/families/deepseek_v41/metal/metal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub const target = @import("target.zig");
pub const tree = @import("tree.zig");
pub const pass = @import("pass.zig");
pub const serve = @import("serve.zig");
pub const vision = @import("vision.zig");
pub const vision_weights = @import("vision_weights.zig");
pub const vision_tower = @import("vision_tower.zig");
pub const vision_prep = @import("vision_prep.zig");
pub const vision_rows = @import("vision_rows.zig");
pub const vision_prepare = @import("vision_prepare.zig");
pub const boot = @import("boot.zig");

test {
Expand All @@ -42,4 +48,8 @@ test {
_ = dspark_plan;
_ = pass;
_ = tree;
_ = vision;
_ = vision_prep;
_ = vision_rows;
_ = vision_prepare;
}
27 changes: 19 additions & 8 deletions zig/src/families/deepseek_v41/metal/prompt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const pipelines = @import("pipelines.zig");
const ps = @import("prompt_scratch.zig");
const pb = @import("prompt_bufs.zig");
const pl = @import("prompt_layer.zig");
const vrows = @import("vision_rows.zig");
const prep = @import("vision_prep.zig");

const Config = host.config.Config;
const ced = host.ced;
Expand Down Expand Up @@ -93,6 +95,8 @@ pub const Prompt = struct {
/// rows of logits and taps the last finish wrote (the prompt's last rows)
head: u32 = 0,
taps: u32 = 0,
/// the image rows of the prompt being run (null: text only); its image positions carry virtual ids
images: ?*const vrows.Rows = null,
prof: Prof = .{},

pub fn init(device: mtl.Device, c: *const Config, o: Opts, limit: u32) !Prompt {
Expand Down Expand Up @@ -160,9 +164,12 @@ pub const Prompt = struct {
}
}

/// The end of the chunk starting at `a`: `chunk` rows, at most `upto`.
/// The end of the chunk starting at `a`: `chunk` rows, cut so an image's span runs inside one chunk.
fn chunkEnd(p: *const Prompt, a: u32, upto: u32) !u32 {
return @min(upto, a + p.o.chunk);
var b = @min(upto, a + p.o.chunk);
if (p.images) |im| b = try im.chunkEnd(a, b);
if (b > upto) return error.ImageSpanSplit;
return b;
}

/// Commits the chunk [a, b) reading input set `set` (its ids written; its Engram rows too unless `late`).
Expand Down Expand Up @@ -191,6 +198,7 @@ pub const Prompt = struct {
const x = p.px(env, slot, a, b);
const bb = &p.b;
try g.embed(&env.p.glue, ch.e, env.model.top.embed, bb.ids, bb.streams[0], bb.pres[0], rows, c.hidden, c.vocab);
if (p.images) |im| im.merge(ch.e, bb.streams[0], a, b);
for (0..p.d) |i| {
const w = &env.model.layers[i];
if (w.layer != i) return error.LayerOrder;
Expand Down Expand Up @@ -334,12 +342,13 @@ pub const Prompt = struct {
return .{ .x = p.b.streams[at], .pre = p.b.pres[at], .x_out = p.b.streams[1 - at], .pre_out = p.b.pres[1 - at] };
}

/// Each row's image flag (the route's bias_vl) for rows [a, b): text rows, all clear.
/// Each row's image flag (the route's bias_vl) for rows [a, b), and the merge's sources.
fn imageFlags(p: *const Prompt, a: u32, b: u32) void {
@memset(p.s.bytes(p.s.image, b - a), 0);
const flags = p.s.bytes(p.s.image, b - a);
if (p.images) |im| im.describe(a, b, flags) else @memset(flags, 0);
}

/// The chunk's ids (every row's Engram gate open) and Engram rows.
/// The chunk's ids (image positions embed the image token, the Engram gate shuts there) and Engram rows.
fn prepare(p: *const Prompt, env: Env, hist: []const u32, a: u32, b: u32, set: usize) !void {
try p.writeIds(env, hist, a, b, set);
for (0..env.c.engram_layers.len) |li| try p.engramRows(env, hist, a, b, set, li);
Expand Down Expand Up @@ -368,9 +377,11 @@ pub const Prompt = struct {
const emb: []u32 = @alignCast(std.mem.bytesAsSlice(u32, p.b.bytes(st.ids, 4 * ids.len)));
const mask = p.b.bytes(st.mask, ids.len);
for (ids, emb, mask) |id, *o, *m| {
if (id >= env.c.vocab) return error.BadToken;
o.* = id;
m.* = 1;
const image = prep.isVid(id);
if (image and p.images == null) return error.BadToken;
if (!image and id >= env.c.vocab) return error.BadToken;
o.* = if (image) p.images.?.embed_id else id;
m.* = @intFromBool(!image);
}
}

Expand Down
Loading