Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- DeepSeek-V4.1 on Metal: the lane core's Engram warms now start the next window's row reads early, and a launch joins each Engram layer's rows only before the command buffer that holds that layer, so the reads overlap the GPU's earlier layers; the rows read and the tokens are unchanged (#2953, part of #1676)
- DeepSeek-V4.1 on Metal: rounds with three or more decoding streams decode plain, with no draft pass and no drafter ingest, since drafts lose there; when fewer streams decode again the drafter takes the rows it missed from the target, so its drafts are the ones it would have made (#1720, part of #1676).

- Flash Next: twenty-nine `TF_FLASHNEXT_*` settings refuse a malformed value by name at startup, before any device work on CUDA, and a Metal load refuses them too; blank `TF_FLASHNEXT_KV_PAGE_TEST` and `TF_FLASHNEXT_KEEP_LATE` read as unset, and a non-finite confidence is refused (#2585, part of #2370). Thanks to @devopsfortunato.
- Flash Next on CUDA: twenty-nine `TF_FLASHNEXT_*` settings refuse a malformed value by name at startup, before any
device or checkpoint work, through the shared environment table: unset and blank keep the default, a valid value keeps
Expand Down
5 changes: 5 additions & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TF_FLASHNEXT_SCALE_INDEX` | Flash Next on Metal before tensor units: exactly `0` or `1`; `1` has the 4-bit expert kernels read each group's scale and bias through a 16-bit index into its tensor's table of pairs, the same values with fewer weight bytes; default 0 |
| `TF_FLASHNEXT_GROUPED_ROWS` | Flash Next on Metal without tensor units: `0`, or the rows (`1` through `32`) from which a decode window runs its 4-bit experts grouped by expert, with the same output bits; default 5; ignored with `--ssd-experts` and in speed-up mode |
| `GLM_EHPROJ_NAX` | GLM-5.3-Flash on Metal: exactly `0` or `1`; `0` serves the MTP head's prompt eh_proj on the scalar kernel; default 1, the tile kernel where it builds |
| `TF_DSV41_SMALL` | DeepSeek-V4.1 on Metal: exactly `0` or `1`; `1` reads the checkpoint's config at its own small shapes (the served smoke test's tiny model); default 0 |
| `TF_DSV41_ASSETS` | DeepSeek-V4.1 on Metal: a directory path without control characters that holds the Engram token map; unset or blank reads it beside the checkpoint |
| `TF_DSV41_ENGRAM_DIR` | DeepSeek-V4.1 on Metal: a directory path without control characters that holds the packed Engram tables; unset or blank takes the checkpoint's `engram_table_dir` |
| `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 |
| `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 |
| `TF_SLIDE_POCKET_KEY` | Exactly `auto`, `reply`, `subject` or `both`; default `auto` |
Expand Down
51 changes: 47 additions & 4 deletions zig/build/dsv41_metal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const std = @import("std");
/// The package over the family's neutral host module; `metal` and `sources` are the engine's own modules.
pub fn package(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module, optimize: std.builtin.OptimizeMode) struct { pkg: *std.Build.Module, host: *std.Build.Module } {
const host = @import("dsv41_shared.zig").moduleAt(b, b.graph.host, optimize, "neutral.zig");
// the lanes and the engine API the backend's modules import
// the lanes, the engine API and the served engine (serve_lanes) the backend's target and pass plug into
@import("dsv41_draft.zig").addImportsAt(b, host, b.graph.host, optimize);
const pkg = b.createModule(.{
.root_source_file = b.path("zig/src/families/deepseek_v41/metal/metal.zig"),
Expand All @@ -16,6 +16,20 @@ pub fn package(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Modu
return .{ .pkg = pkg, .host = host };
}

/// The native server's engine over the package: metal/entry.zig, the family's own serving modules beside it.
pub fn engine(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module, optimize: std.builtin.OptimizeMode) *std.Build.Module {
const p = package(b, metal, sources, optimize);
const m = b.createModule(.{
.root_source_file = b.path("zig/src/families/deepseek_v41/metal/entry.zig"),
.target = b.graph.host,
.optimize = optimize,
.link_libc = true,
.imports = &.{ .{ .name = "dsv41_metal", .module = p.pkg }, .{ .name = "dsv41", .module = p.host }, .{ .name = "metal", .module = metal } },
});
for ([_][]const u8{ "engine_api", "core", "tokenizer", "dsv41_serve", "lanes", "json" }) |name| m.addImport(name, p.host.import_table.get(name).?);
return m;
}

/// `test-dsv41-metal`: package, layer and forward checks on this GPU; `test-dsv41-metal-real`: a TF_DSV41_REAL layer.
pub fn steps(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module, test_step: *std.Build.Step) void {
const p = package(b, metal, sources, .Debug);
Expand All @@ -26,13 +40,39 @@ pub fn steps(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module
.link_libc = true,
.imports = &.{ .{ .name = "dsv41_metal", .module = p.pkg }, .{ .name = "metal", .module = metal }, .{ .name = "dsv41", .module = p.host } },
});
// the package's own checks run with the default test step; its device suites run from their named steps
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = p.pkg })).step);
const step = b.step("test-dsv41-metal", "DeepSeek-V4.1 Metal layer and forward: float64 stages, rows at once == one at a time");
const pkg_run = &b.addRunArtifact(b.addTest(.{ .root_module = p.pkg })).step;
step.dependOn(pkg_run);
// serve_lanes over the CPU twin, checked here so no CUDA step has to depend on the neutral served engine
const lanes_test = b.createModule(.{
.root_source_file = b.path("zig/src/families/deepseek_v41/serve_lanes_test.zig"),
.target = b.graph.host,
.optimize = .Debug,
.link_libc = true,
.imports = &.{ .{ .name = "core", .module = p.host.import_table.get("core").? }, .{ .name = "engine_api", .module = p.host.import_table.get("engine_api").? }, .{ .name = "lanes", .module = p.host.import_table.get("lanes").? }, .{ .name = "tokenizer", .module = p.host.import_table.get("tokenizer").? } },
});
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = lanes_test })).step);
const run = b.addRunArtifact(b.addTest(.{ .root_module = tests }));
run.has_side_effects = true; // a device run: never cached
step.dependOn(&run.step);
test_step.dependOn(pkg_run); // the package's host checks only; the device run stays in its own 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"),
.target = b.graph.host,
.optimize = .Debug,
.link_libc = true,
.imports = &.{ .{ .name = "dsv41_metal", .module = p.pkg }, .{ .name = "metal", .module = metal }, .{ .name = "dsv41", .module = p.host } },
});
serve.addImport("dsv41_serve_fixtures", p.host.import_table.get("dsv41_serve").?.import_table.get("dsv41_serve_fixtures").?);
const serve_run = b.addRunArtifact(b.addTest(.{ .root_module = serve }));
serve_run.has_side_effects = true;
step.dependOn(&serve_run.step);
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)");
all.dependOn(step);
all.dependOn(serve_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 @@ -66,4 +106,7 @@ pub fn steps(b: *std.Build, metal: *std.Build.Module, sources: *std.Build.Module
const ptime_run = b.addRunArtifact(b.addTest(.{ .root_module = ptime }));
ptime_run.has_side_effects = true;
b.step("test-dsv41-metal-prompt-time", "DeepSeek-V4.1 Metal: ms a stage of one release layer's prompt chunks (TF_DSV41_REAL)").dependOn(&ptime_run.step);
// tensorfold-dsv41 over Metal: the native server with this family's lane engine (the CUDA pair's macOS twin)
const exe = @import("dsv41_draft.zig").serverOn(b, b.graph.host, .ReleaseSafe, engine(b, metal, sources, .ReleaseFast));
b.step("dsv41-metal-serve", "Install tensorfold-dsv41 over Metal (DeepSeek-V4.1 release checkpoints on this Mac)").dependOn(&b.addInstallArtifact(exe, .{}).step);
}
3 changes: 3 additions & 0 deletions zig/src/core/lanes/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub const Model = struct {
granted_depth: bool = false, // with draft_probabilities: every stream drafts its last grant plus a slack
draft_slack: u32 = 0, // drafts past a stream's last grant, short or not (0: the Constants' own)
draft_streams: bool = false, // the head drafts every stream of a shared round in one batch
plain_streams: u32 = 0, // a shared round with this many streams or more drafts none (0: off)
copy_first: bool = false, // a copy ahead takes the round: the head holds no drafts for it (a drafter's own pass)
hold_costs: []const @import("hold_price.zig").HoldCost = &.{}, // a drafter's own hold pass, timed at load
head_trees: bool = false, // the head drafts trees of lanes (DraftRequest.lanes) and holds them
Expand Down Expand Up @@ -79,6 +80,7 @@ pub const Config = struct {
batch_streams: u32,
node_probabilities: bool,
granted_depth: bool = false, // Model.granted_depth
plain_streams: u32 = 0, // Model.plain_streams
fill_lanes: u32 = 0, // rows a window may grow to with suffix-match branches (0: off; lanes/fill.zig)
fill_match: u32 = 8, // context tokens a suffix-match branch must match (shorter ones are coincidental phrases)
fill_floor: f64 = 0.35, // the share of grafted rows a stream must keep to go on reading its chain back every round
Expand Down Expand Up @@ -166,6 +168,7 @@ pub const Config = struct {
.batch_streams = if (streams) (if (m.max_streams != 0) m.max_streams else 32) else 1,
.node_probabilities = m.draft_probabilities or m.draft_confidence != null,
.granted_depth = m.granted_depth and m.draft_probabilities,
.plain_streams = m.plain_streams,
.draft_streams = m.draft_streams,
.head_trees = m.head_trees,
.join_tail = m.join_tail,
Expand Down
7 changes: 4 additions & 3 deletions zig/src/core/lanes/depth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ const Allocator = std.mem.Allocator;
const Table = @import("table.zig").Table;
const alloc = @import("allocate.zig");
pub const pricedBatched = alloc.pricedBatched;

pub const Constants = @import("depth_constants.zig").Constants;

pub const HoldCost = @import("hold_price.zig").HoldCost;
const holdMs = @import("hold_price.zig").holdMs;

Expand Down Expand Up @@ -59,8 +57,9 @@ pub const Rule = struct {
node_probabilities: bool,
granted_depth: bool = false, // node_probabilities: a stream drafts its last grant plus draft_slack, lone ones too
confidence_floor: ?f64 = null,
plain_streams: u32 = 0, // a shared round with this many streams or more drafts none (0: off; a family's switch)
batch_rows: u32,
batched_heads: bool = false, // one batch drafts every stream of a shared round (its steps: the deepest stream's)
batched_heads: bool = false, // one batch drafts every stream of a shared round (steps: the deepest stream's)
row_cost: @import("row_cost.zig").RowCost = .{}, // what a row costs past shared_costs here, under k.learn_rows
round_ms: Table = .{}, // a round's wall time by drafts, every stream's: what sampled streams price by
overhead: std.ArrayList(Overhead) = .empty, // a shared round's ms past its forward, by streams (insertion order)
Expand Down Expand Up @@ -298,6 +297,8 @@ pub const Rule = struct {

/// Head drafts for the streams of a shared round, sized by allocation over their chain chances.
pub fn budgets(r: *Rule, whos: []Who, probe: Probe, sampled: bool, out: []i64) !void {
// drafts lose here: none asked, no head pass
if (r.plain_streams > 0 and whos.len >= r.plain_streams) return @memset(out, 0);
if (r.confidence_floor != null) {
for (whos, out) |*w, *o| o.* = try r.headDepth(w, null, probe);
return;
Expand Down
2 changes: 1 addition & 1 deletion zig/src/core/lanes/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub const Engine = struct {
grammar_workers: @import("grammar_parallel.zig").State = .{},
arena: std.heap.ArenaAllocator, // a round's temporaries
unread: std.ArrayList(Unread) = .empty, // queued draws the log reads at the step's end

pub fn init(gpa: Allocator, cfg: *const Config, backend: be.Backend, clock: be.Clock) Engine {
return .{
.gpa = gpa,
Expand All @@ -74,6 +73,7 @@ pub const Engine = struct {
.node_probabilities = cfg.node_probabilities,
.granted_depth = cfg.granted_depth,
.confidence_floor = cfg.draft_confidence,
.plain_streams = cfg.plain_streams,
.batch_rows = cfg.batch_rows,
.batched_heads = cfg.draft_streams,
},
Expand Down
12 changes: 12 additions & 0 deletions zig/src/families/deepseek_v41/draft/calib_env.zig
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ pub fn store(gpa: Allocator, io: std.Io, dir: []const u8, want: Shape, c: Costs,
return path;
}

/// TF_DSV41_DRAFT_PLAIN_STREAMS' largest value: the most slots a server runs (TF_DSV41_SLOTS).
pub const max_plain_streams = 16;

/// The depth's knobs (TF_DSV41_DEPTH, _DRAFT_DEPTH, _DRAFT_SKIP, _DEPTH_JOINT, _DEPTH_FAIR, _DEPTH_HOST_MS, rows cap).
pub fn depthSettings(get: *const fn ([*:0]const u8) ?[]const u8) !depth.Settings {
var s: depth.Settings = .{};
Expand All @@ -225,6 +228,10 @@ pub fn depthSettings(get: *const fn ([*:0]const u8) ?[]const u8) !depth.Settings
s.fair = try float(get("TF_DSV41_DEPTH_FAIR"), joint.fair, 0.0, 4.0);
s.host_ms = try float(get("TF_DSV41_DEPTH_HOST_MS"), joint.host_ms, 0.0, 100.0);
if (trimmed(get("TF_DSV41_GRAPH_ROWS_MAX"))) |v| s.max_rows = @max(1, @min(try std.fmt.parseInt(u64, v, 10), costs_mod.max_rows));
if (trimmed(get("TF_DSV41_DRAFT_PLAIN_STREAMS"))) |v| {
s.plain_streams = std.fmt.parseInt(usize, v, 10) catch return error.DraftPlainStreams;
if (s.plain_streams > max_plain_streams) return error.DraftPlainStreams;
}
return s;
}

Expand All @@ -245,6 +252,11 @@ pub fn env(name: [*:0]const u8) ?[]const u8 {
return if (std.c.getenv(name)) |v| std.mem.span(v) else null;
}

/// TF_DSV41_DRAFT_PLAIN_STREAMS as depthSettings reads it (set and non-blank).
pub fn plainStreamsSet() bool {
return trimmed(env("TF_DSV41_DRAFT_PLAIN_STREAMS")) != null;
}

fn trimmed(v: ?[]const u8) ?[]const u8 {
const t = std.mem.trim(u8, v orelse return null, " \t\r\n");
return if (t.len == 0) null else t;
Expand Down
1 change: 1 addition & 0 deletions zig/src/families/deepseek_v41/draft/depth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const Settings = struct {
max_rows: u64 = 64,
code_accept: bool = false, // TF_DSV41_CODE_ACCEPT: expand high-acceptance short-context chains
context_bucket: u64 = 2048, // TF_DSV41_GRAPH_BUCKET; short contexts only use its linear buckets
plain_streams: usize = 0, // TF_DSV41_DRAFT_PLAIN_STREAMS: a round with this many active streams or more decodes plain; 0 = off
};

/// [E(0) .. E(len)]: tokens a round verifying k drafts commits on average, into `out` (len + 1).
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/deepseek_v41/draft/draft.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ test {
_ = branches;
_ = calib_env;
_ = @import("calib_env_test.zig");
_ = @import("plain_streams_test.zig");
_ = @import("plain_catchup_test.zig");
_ = calib_measure;
_ = @import("calib_measure_test.zig");
_ = branches_oracle;
Expand Down
18 changes: 18 additions & 0 deletions zig/src/families/deepseek_v41/draft/iface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,26 @@ 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,
/// 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.
history: ?*const fn (ptr: *anyopaque, slot: u32, start: u64, rows: u32) ?Taps = null,
};

/// The target keeps committed rows' taps, so the drafter may take a plain round's rows later (`history`).
pub fn keepsHistory(t: Target) bool {
return t.vtable.history != null;
}

pub fn history(t: Target, slot: u32, start: u64, rows: u32) ?Taps {
return if (t.vtable.history) |f| f(t.ptr, slot, start, rows) else null;
}

/// 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;
}

/// A slot's whole prompt whose rows but the last are in (its `finish` / `tail` follows).
pub const Final = struct { slot: u32, ids: []const u32 };

Expand Down
39 changes: 39 additions & 0 deletions zig/src/families/deepseek_v41/draft/lane_ingest.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! The drafter's committed rows for the lanes: a window's kept rows, after any rows that were left for later.
const iface = @import("iface.zig");
const branches = @import("branches.zig");
const ln = @import("lanes.zig");

/// A round's kept rows into the drafter: first the rows a skipped absorb left out (`catchUp`), then these.
pub fn ingest(x: *ln.Lanes, st: *ln.St, start: u64, rows: []const u32) !void {
try catchUp(x, st, start, start + rows.len);
st.ring_end = start + rows.len;
const span = iface.ingestSpan(rows.len, x.opts.shape.window);
if (span.count == 0) return;
const taps = x.target.taps(st.slot);
const m = taps.map orelse return x.pass.ingest(st.slot, start + span.skip, taps, rows[span.skip..]);
// a tree resolved as chains: the path's rows in the taps of the chain that holds it
var mapped: [branches.max_rows]u32 = undefined;
if (span.count > mapped.len) return error.TreeRows;
for (rows[span.skip..], mapped[0..span.count]) |r, *o| {
if (r >= m.len or m[r] == branches.unrun) return error.TreeTaps;
o.* = m[r];
}
try x.pass.ingest(st.slot, start + span.skip, taps, mapped[0..span.count]);
}

/// Absorb skipped: the rows stay in the target's history, and the next ingest takes them from there.
pub fn deferred(x: *const ln.Lanes) bool {
return x.target.keepsHistory();
}

/// The committed rows between the drafter's end and `start`, the last `window` before `end` only (all it reads).
fn catchUp(x: *ln.Lanes, st: *ln.St, start: u64, end: u64) !void {
const from = st.ring_end orelse return;
const lo = @max(from, end -| x.opts.shape.window);
if (lo >= start) return;
const n: u32 = @intCast(start - lo);
const taps = x.target.history(st.slot, lo, n) orelse return error.HistoryLost;
const rows = try x.scratch.allocator().alloc(u32, n);
for (rows, 0..) |*r, i| r.* = @intCast(i);
try x.pass.ingest(st.slot, lo, taps, rows);
}
Loading