Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bda4834
DeepSeek V4.1: the Engram token map built in Zig, byte-identical to t…
akol1 Oct 14, 2026
b1effbb
DeepSeek V4.1 Flash: configuration, Engram tables and resident weight…
akol1 Oct 13, 2026
c51e6aa
DeepSeek V4.1 Flash: Metal operators with fp64-dot references and the…
akol1 Oct 13, 2026
2a22d68
THIRD_PARTY_NOTICES: the native DS4.1 reference follows DeepSeek-V4.1…
akol1 Oct 13, 2026
c408cb7
DeepSeek V4.1 load: a shard contributes only the tensors the index ma…
akol1 Oct 13, 2026
d52f9cd
DeepSeek V4.1 load: the router gate and bias ride the pack's dtypes
akol1 Oct 13, 2026
73eb0fb
DeepSeek V4.1 load: the Engram gate weights ride f32 or bf16
akol1 Oct 13, 2026
e630d73
DeepSeek V4.1: the kept-dense EXL3 form, for rank-split repacks
akol1 Oct 13, 2026
9fe64f8
DeepSeek V4.1 reference: the kept-dense apply
akol1 Oct 13, 2026
a6d7f86
DeepSeek V4.1 load: the shard-plan tests pass the index-ownership arg…
akol1 Oct 14, 2026
84386c9
DeepSeek V4.1: the rebase residue — one counters binding, the state-w…
akol1 Oct 14, 2026
d3c94ee
mHC fp64 ideal reference: the mix at rounding level, 1-ULP noise atte…
akol1 Oct 14, 2026
bb4cbc3
CSA2 reference: FP64-ideal parameterization of the pool, RMS and spar…
akol1 Oct 14, 2026
7b19ae1
csa2: FP64 stage harness, compressor pool + RMS, six adversarial inpu…
akol1 Oct 14, 2026
20e3880
csa2: FP64 stage harness over the sparse attention kernel, with kerne…
akol1 Oct 14, 2026
16e101b
csa2: indexer stage harness — FP32/FP64 score comparison and top-k se…
akol1 Oct 14, 2026
4bbb896
csa2: indexer stage receipts — carry the BF16 einsum/score boundaries…
akol1 Oct 14, 2026
585cd5a
csa2: sparse stage reads its own FP32 rounding level before the BF16 …
akol1 Oct 14, 2026
d33f74c
csa2: per-stage harnesses for sinks, tile denominators, output mix an…
akol1 Oct 14, 2026
75cbd19
csa2: measure how much score noise the top-k selection absorbs
akol1 Oct 14, 2026
edf9c7a
The fp64 harness ports: dedupe the shared math helpers, fix the shard…
akol1 Oct 14, 2026
1f0aed0
The oracle records the indexer's selection margins while it teaches
akol1 Oct 14, 2026
ed7ba76
The indexer-margin capture at depth: one diagnostic token's selection…
akol1 Oct 14, 2026
4006c85
DeepSeek V4.1: lane slots, each with its own caches and Engram cursor
akol1 Oct 13, 2026
10026d4
tf-dsv41-run: the counters read the full rusage_info_v4
akol1 Oct 13, 2026
c1f7e63
DeepSeek V4.1: the lane backend, a stream a slot
akol1 Oct 13, 2026
7ba5fa6
DeepSeek V4.1: the lane host behind the native server
akol1 Oct 13, 2026
34811b7
serve: --engram-table-dir reaches the native engine
akol1 Oct 13, 2026
b51503d
DeepSeek V4.1: the lane host builds the token map when it is missing
akol1 Oct 14, 2026
5f62bb6
DeepSeek V4.1: the family guide
akol1 Oct 14, 2026
9e8269a
the operator harnesses through zig fmt
akol1 Oct 14, 2026
81b013b
DeepSeek V4.1: the token map ships its table generator and its unit t…
akol1 Oct 18, 2026
a2a2874
DeepSeek V4.1 reference: the operator harnesses split at their seams
akol1 Oct 18, 2026
589937c
DeepSeek V4.1: the reference, family, native and Metal comments on on…
akol1 Oct 18, 2026
b7b23ba
DeepSeek V4.1: the lane runner accepts and documents its context argu…
akol1 Oct 18, 2026
1ccd7bc
DeepSeek V4.1: the FP32 and FP64 oracles run side by side
akol1 Oct 19, 2026
99c7c54
DeepSeek V4.1 reference: the FP64 narrowing keeps NaN, and the FP32 o…
akol1 Oct 19, 2026
34d6eb9
DeepSeek V4.1 reference: the index tie fixture broadcasts row 0 into …
akol1 Oct 21, 2026
c55c246
DeepSeek V4.1 reference: the FP32 sign packing matches the consumer w…
akol1 Oct 21, 2026
c379221
Pass the span metadata the counter recorder now requires
akol1 Oct 21, 2026
3032baa
DeepSeek V4.1: Metal targets onto 1.1.2's build modules
akol1 Oct 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
Prev Previous commit
Next Next commit
DeepSeek V4.1: the lane backend, a stream a slot
Each verify window runs as its slot's own sub-forward, so a row's bits are its
solo bits whatever shares the round; drafts off, greedy draws through a handle
ring, and a kept prefix shorter than its window rolls back and re-feeds the kept
rows. Facts report the workspace row budget as the exact width; trees, held
drafts and grammar masks are refused until their pieces land.
  • Loading branch information
akol1 committed Oct 21, 2026
commit c1f7e636db3f1fb47eaf9eb8472e1bb0986eced9
229 changes: 229 additions & 0 deletions zig/src/families/deepseek_v41/backend.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
//! V4.1's lane slots behind the lane core: a stream a slot, greedy draws, drafts off.
//! One window runs as its slot's own sub-forward, so every row's bits are its solo bits;
//! a kept prefix shorter than the window rolls back and re-feeds the kept rows.
const std = @import("std");
const ds = @import("model.zig");
const kv = @import("kv_state.zig");
const lanes = @import("lanes");
const be = lanes.backend;
const Stream = lanes.Stream;

const RING = 1024;

pub const Backend = struct {
gpa: std.mem.Allocator,
model: *ds.Model,
by: std.AutoHashMapUnmanaged(*Stream, u32) = .empty,
/// Drawn tokens by handle: every read waits for its forward anyway (one sub-forward a window).
ring: [RING]u32 = undefined,
puts: u64 = 0,
first: []u32,
/// Each slot's cache cursor before its in-flight verify, for the rollback-and-refeed keep.
verify_len: []u32,

pub fn init(gpa: std.mem.Allocator, model: *ds.Model) !Backend {
return .{
.gpa = gpa,
.model = model,
.first = try gpa.alloc(u32, model.lanes),
.verify_len = try gpa.alloc(u32, model.lanes),
};
}

pub fn deinit(b: *Backend) void {
b.by.deinit(b.gpa);
b.gpa.free(b.first);
b.gpa.free(b.verify_len);
}

pub fn backend(b: *Backend) be.Backend {
return .{ .ptr = b, .vtable = &.{
.prefill = prefill,
.first = firstFn,
.queue = queue,
.read = read,
.verify = verify,
.keep = keep,
.draft = draft,
.release = release,
} };
}

/// A window of any width runs exactly as its slot's own forward; the lane core may bundle streams freely.
pub fn facts(b: *const Backend) lanes.Model {
const rows = b.model.workspace.plan.rows;
return .{
.exact_width = rows,
.first_copy_rows = 1,
.mtp = false,
.speculate = false,
.speculate_early = false,
.gpu_tokens = false,
.plain_guard = true,
.drafts = 0,
.window_costs = &.{},
.streams_exact = true,
.hidden_rows = true,
.batch_rows = rows,
.max_streams = @intCast(b.model.lanes),
.draft_streams = false,
};
}

fn self(ptr: *anyopaque) *Backend {
return @ptrCast(@alignCast(ptr));
}

fn slotOf(b: *Backend, s: *Stream) !u32 {
return b.by.get(s) orelse error.UnknownStream;
}

fn put(b: *Backend, v: u32) u64 {
const h = b.puts;
b.ring[h % RING] = v;
b.puts += 1;
return h;
}

fn free(b: *const Backend) ?u32 {
var used: [256]bool = @splat(false);
var it = b.by.valueIterator();
while (it.next()) |v| used[v.*] = true;
for (used, 0..) |u, i| if (b.model.lanes > i and !u) return @intCast(i);
return null;
}

fn draw(b: *Backend, slot: u32) !u32 {
const row = b.model.workspace.logits(1);
var best: u32 = 0;
var maximum = -std.math.inf(f32);
for (row, 0..) |value, id| {
if (!std.math.isFinite(value)) return error.NonfiniteLogits;
if (value > maximum) {
maximum = value;
best = @intCast(id);
}
}
_ = slot;
return best;
}

fn prefill(ptr: *anyopaque, s: *Stream) anyerror!void {
const b = self(ptr);
if (s.sampling) |p| if (p.temperature > 0) return error.GreedyOnly;
const prompt = s.prompt();
if (prompt.len == 0) return error.EmptyPrompt;
const i = b.free() orelse return error.NoFreeSlot;
const state = &b.model.caches[i];
if (prompt.len + s.max_new + 1 > state.plan.capacity) return error.ContextFull;
try b.by.put(b.gpa, s, i);
try b.model.resetSlot(i);
s.cached = 0;
var at: u32 = 0;
const rows = b.model.workspace.plan.rows;
while (at < prompt.len) {
if (s.isCancelled()) return error.Cancelled;
const end = @min(at + rows, @as(u32, @intCast(prompt.len)));
const last = end == prompt.len;
if (last) {
_ = try b.model.forwardOn(i, prompt[at..end], .last);
b.first[i] = try b.draw(i);
} else {
_ = try b.model.forwardOn(i, prompt[at..end], .none);
}
at = end;
}
}

fn firstFn(ptr: *anyopaque, s: *Stream, position: u64) anyerror!u64 {
const b = self(ptr);
const i = try b.slotOf(s);
if (position != b.model.caches[i].position) return error.PositionMismatch;
return b.put(b.first[i]);
}

fn queue(ptr: *anyopaque, s: *Stream, feed: be.Feed, position: u64) anyerror!u64 {
const b = self(ptr);
const i = try b.slotOf(s);
const state = &b.model.caches[i];
if (position != state.position) return error.PositionMismatch;
const token: u32 = switch (feed) {
.value => |v| v,
.handle => |h| try read(ptr, h),
};
_ = try b.model.forwardOn(i, &.{token}, .last);
return b.put(try b.draw(i));
}

fn read(ptr: *anyopaque, handle: u64) anyerror!u32 {
const b = self(ptr);
if (handle >= b.puts or b.puts - handle > RING) return error.StaleHandle;
return b.ring[handle % RING];
}

fn verify(ptr: *anyopaque, windows: []const be.Window, out: []be.Verified) anyerror!void {
const b = self(ptr);
for (windows) |w| {
if (w.parents != null or w.early or w.masks.len > 0) return error.TreeWindowsUnsupported;
}
for (windows, out) |w, o| {
const i = try b.slotOf(w.stream);
const state = &b.model.caches[i];
if (w.held > 0) return error.WindowOutOfStep; // no draft head: nothing is held on the GPU
const rows = w.rows();
if (w.positions.len != rows) return error.WindowOutOfStep;
for (w.positions, 0..) |p, r| if (p != state.position + r + 1) return error.PositionMismatch;
try b.model.beginSlot(i);
errdefer b.model.commitSlot(i) catch {};
var feeds = try b.gpa.alloc(u32, rows);
defer b.gpa.free(feeds);
feeds[0] = w.pending;
@memcpy(feeds[1..], w.tokens);
const row = try b.model.forwardOn(i, feeds, .all);
for (0..rows) |r| {
var best: u32 = 0;
var maximum = -std.math.inf(f32);
for (row[r * 129280 ..][0..129280], 0..) |value, id| {
if (!std.math.isFinite(value)) return error.NonfiniteLogits;
if (value > maximum) {
maximum = value;
best = @intCast(id);
}
}
o.sampled[r] = best;
}
// The window's fed tokens after the pending one landed: the verified drafts, GLM's contract.
if (o.drafts.len > 0) @memcpy(o.drafts, feeds[1 .. 1 + o.drafts.len]);
try b.model.commitSlot(i);
}
}

fn keep(ptr: *anyopaque, windows: []const be.Window, paths: []const []const u32) anyerror!void {
const b = self(ptr);
for (windows, paths) |w, path| {
const i = try b.slotOf(w.stream);
const rows = w.rows();
const kept = path.len;
if (kept >= rows) continue; // the whole window landed
// Roll the slot back to before the window and re-feed the kept prefix:
// the lanes receipt holds that re-fed rows are bit-identical to their first pass.
try b.model.rollbackSlot(i);
if (kept == 0) continue;
var feeds = try b.gpa.alloc(u32, kept);
defer b.gpa.free(feeds);
feeds[0] = w.pending;
@memcpy(feeds[1..], w.tokens[0 .. kept - 1]);
_ = try b.model.forwardOn(i, feeds, .none);
}
}

fn draft(_: *anyopaque, requests: []const be.DraftRequest) anyerror!void {
for (requests) |r| if (r.depth > 0) return error.Unsupported;
}

fn release(ptr: *anyopaque, s: *Stream) void {
const b = self(ptr);
const entry = b.by.fetchRemove(s) orelse return;
b.model.resetSlot(entry.value) catch {};
}
};
1 change: 1 addition & 0 deletions zig/src/tensorfold.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const deepseek_v41_weights = @import("families/deepseek_v41/weights.zig");
pub const deepseek_v41_view = @import("families/deepseek_v41/view.zig");
pub const deepseek_v41 = @import("families/deepseek_v41/model.zig");
pub const deepseek_v41_profiler = @import("families/deepseek_v41/profiler.zig");
pub const deepseek_v41_backend = @import("families/deepseek_v41/backend.zig");
pub const gpu_counters = @import("core/gpu_counters.zig");
pub const expert_groups = @import("core/expert_groups.zig");
pub const deepseek_v41_engram_hash = @import("families/deepseek_v41/engram_hash.zig");
Expand Down