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
d4d3a59
DeepSeek V4.1 Flash: configuration, Engram tables and resident weight…
akol1 Oct 13, 2026
3ef88cd
DeepSeek V4.1 Flash: Metal operators with fp64-dot references and the…
akol1 Oct 13, 2026
d9b30d3
THIRD_PARTY_NOTICES: the native DS4.1 reference follows DeepSeek-V4.1…
akol1 Oct 13, 2026
2e23371
DeepSeek V4.1 load: a shard contributes only the tensors the index ma…
akol1 Oct 13, 2026
c7dad8c
DeepSeek V4.1 load: the router gate and bias ride the pack's dtypes
akol1 Oct 13, 2026
91b8a68
DeepSeek V4.1 load: the Engram gate weights ride f32 or bf16
akol1 Oct 13, 2026
9ed1303
DeepSeek V4.1: the kept-dense EXL3 form, for rank-split repacks
akol1 Oct 13, 2026
79856d3
DeepSeek V4.1 reference: the kept-dense apply
akol1 Oct 13, 2026
af191ce
DeepSeek V4.1 load: the shard-plan tests pass the index-ownership arg…
akol1 Oct 14, 2026
dae11d3
DeepSeek V4.1: the rebase residue — one counters binding, the state-w…
akol1 Oct 14, 2026
35723cb
mHC fp64 ideal reference: the mix at rounding level, 1-ULP noise atte…
akol1 Oct 14, 2026
6a69841
CSA2 reference: FP64-ideal parameterization of the pool, RMS and spar…
akol1 Oct 14, 2026
e2dfb0c
csa2: FP64 stage harness, compressor pool + RMS, six adversarial inpu…
akol1 Oct 14, 2026
50f389a
csa2: FP64 stage harness over the sparse attention kernel, with kerne…
akol1 Oct 14, 2026
6154d85
csa2: indexer stage harness — FP32/FP64 score comparison and top-k se…
akol1 Oct 14, 2026
041d492
csa2: indexer stage receipts — carry the BF16 einsum/score boundaries…
akol1 Oct 14, 2026
13ca5c1
csa2: sparse stage reads its own FP32 rounding level before the BF16 …
akol1 Oct 14, 2026
fa1e5ca
csa2: per-stage harnesses for sinks, tile denominators, output mix an…
akol1 Oct 14, 2026
d3efdc9
csa2: measure how much score noise the top-k selection absorbs
akol1 Oct 14, 2026
0a2ab87
The fp64 harness ports: dedupe the shared math helpers, fix the shard…
akol1 Oct 14, 2026
1cae50b
The oracle records the indexer's selection margins while it teaches
akol1 Oct 14, 2026
0d77fac
The indexer-margin capture at depth: one diagnostic token's selection…
akol1 Oct 14, 2026
331e89b
the operator harnesses through zig fmt
akol1 Oct 14, 2026
a881b7f
DeepSeek V4.1: the sparse output accumulates each 64-column tile in c…
akol1 Oct 14, 2026
10fd1c1
The dense EXL3 apply accumulates its dot as a hi/lo FP32 pair
akol1 Oct 15, 2026
8ba0ee9
DeepSeek V4.1: the token map ships its table generator and its unit t…
akol1 Oct 18, 2026
5a3183f
DeepSeek V4.1: the family's comments on one line each, within the 120…
akol1 Oct 18, 2026
e3edac5
DeepSeek V4.1 reference: the operator harnesses split at their seams
akol1 Oct 18, 2026
28c93ef
DeepSeek V4.1: the reference and Metal comments on one line each
akol1 Oct 18, 2026
4cb1b17
DeepSeek V4.1 reference: the index tie fixture broadcasts row 0 into …
akol1 Oct 21, 2026
709574d
Pass the span metadata the counter recorder now requires
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 reference and Metal comments on one line each
Folds the last comment groups in the reference harnesses and the dense
EXL3 kernel into single lines of at most 120 columns, keeping the
technical content. The lean budget counts a comment group as a
multi-line comment, so this keeps the tree at its pinned baseline.
  • Loading branch information
akol1 committed Oct 21, 2026
commit 28c93ef7fc5b61a113e66f6a3754e877e8ccf667
3 changes: 1 addition & 2 deletions zig/kernels/metal/core/exl3_projection.metal
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ kernel void tf_exl3_dense(const device half* rotated [[buffer(0)]],
const device uint* input_ids [[buffer(4)]],
uint2 g [[thread_position_in_grid]]) {
const uint row = a.input_ids ? input_ids[g.y] : g.y;
// The reference accumulates this dot in FP64 sequentially; a hi/lo pair carries the
// same exactness through FP32 (the products of half and bf16 values are exact here).
// The reference's dot is FP64-sequential; this hi/lo pair carries the same exactness through FP32.
float hi = 0, lo = 0;
for (uint k = 0; k < a.k; ++k) {
float prod = float(rotated[ulong(g.y) * a.k + k]) * float(weight[ulong(g.x) * a.k + k]);
Expand Down
16 changes: 4 additions & 12 deletions zig/tests/deepseek_v41_reference/attention_math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ pub const State = struct {
grouped: [8192]u16 = undefined,
observer: ?Observer = null,
sparse_snapshot: ?[]u16 = null,
/// The indexer-selection probe: fires once per scored window with the contract's
/// boundary margin, the FP64-ideal boundary margin, and the pick flips between them.
/// The indexer-selection probe: per scored window, the contract and FP64-ideal margins, and the pick flips.
score_probe: ?ScoreProbe = null,
score_copy: []f32 = &.{},
ideal_scores: []f32 = &.{},
Expand Down Expand Up @@ -215,8 +214,7 @@ pub const State = struct {
std.mem.sort(u32, self.selected[0..self.selected_len], {}, std.sort.asc(u32));
if (self.score_probe) |probe| self.reportSelection(probe, layer, position, count);
}
/// The same selection against the FP64-ideal scores: same BF16 inputs, no
/// intermediate roundings. Flips between the two picks are the support-flip signal.
/// The same selection on FP64-ideal scores: same BF16 inputs, no intermediate roundings; pick flips are the signal.
fn idealScores(self: *State, a: Attention, count: usize) void {
@setFloatMode(.strict);
if (a.indexer == null) return;
Expand Down Expand Up @@ -360,18 +358,12 @@ pub const State = struct {
}
};

/// The online-softmax sparse head at reference precision T. With T=f32 this reproduces the pinned
/// source contract bit-for-bit (wide FP64 dot, one FP32 narrowing per score, FP32 online softmax,
/// BF16-rounded probabilities before the value product, FP32 per-tile accumulator narrowing).
/// With T=f64 every boundary is the ideal FP64 result: no FP32 narrowing, BF16 appears only at the
/// algorithm's quantized inputs, the documented probability rounding and the final output.
/// Sparse online-softmax head: f32 reproduces the pinned source contract bit-for-bit; f64 makes every boundary ideal.
pub fn sparseHeadOf(comptime T: type, q: []const u16, keys: KeyTable, indices: []const i32, sink: f32, out: []u16) void {
return sparseHeadPre(T, q, keys, indices, sink, out, null);
}

/// The same head, with `pre` optionally capturing acc/denominator before the output rounding: the
/// same values in FP32 versus FP64 then differ only by the stage's arithmetic, so their ULP distance
/// is the stage's true rounding level instead of the BF16 output quantum that hides it.
/// Same head; `pre` captures acc/denominator pre-rounding, so FP32 vs FP64 ULP is the true rounding level, not BF16's.
pub fn sparseHeadPre(
comptime T: type,
q: []const u16,
Expand Down
4 changes: 1 addition & 3 deletions zig/tests/deepseek_v41_reference/margins.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! The deep indexer-margin capture: prefill a long prompt, then one diagnostic token
//! whose per-layer selection scratch (score tiles, top-k heap, positions) is copied
//! out verbatim for offline margin analysis. No reference chain, no model reload.
//! Indexer-margin capture: a long prefill, one token, per-layer selection scratch copied verbatim for offline analysis.
const std = @import("std");
const mtl = @import("metal");
const tf = @import("tensorfold");
Expand Down
10 changes: 3 additions & 7 deletions zig/tests/deepseek_v41_reference/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,11 @@ pub fn idealRsqrt(x: f32) f32 {
pub fn rms(x: []const u16, weight: Tensor, eps: f32, out: []u16) void {
rmsOf(f32, x, weight, eps, out);
}
/// Element type widened to the reference precision; FP32 is the source contract, FP64 the ideal.
/// FP64 decode of a BF16 cell, exact at both reference precisions.
/// FP64 decode of a BF16 cell, exact at both reference precisions; FP32 is the source contract, FP64 the ideal.
pub fn d64(x: u16) f64 {
return @as(f64, f(x));
}
/// Round an FP64 value once to BF16 (round-to-nearest-even) with no intermediate FP32 rounding.
/// The RMS boundary at reference precision T: the source-contract FP32 mean projection for f32,
/// a wide FP64 mean, exact division and reciprocal square root for f64.
/// FP64 to BF16 once (RNE), no FP32 intermediate; RMS at T: contract FP32 mean for f32, wide FP64 mean+rsqrt for f64.
pub fn rmsOf(comptime T: type, x: []const u16, weight: Tensor, eps: f32, out: []u16) void {
@setFloatMode(.strict);
var sum: f64 = 0;
Expand All @@ -132,8 +129,7 @@ pub fn rmsOf(comptime T: type, x: []const u16, weight: Tensor, eps: f32, out: []
const r = 1 / @sqrt(sum / @as(f64, @floatFromInt(x.len)) + @as(f64, eps));
for (x, out, 0..) |v, *y, i| y.* = b64(@as(f64, f(v)) * r * @as(f64, value(weight, i)));
}
/// The same boundary with weight cells given explicitly (decoded BF16 or native FP32), so a
/// harness needs no checkpoint tensor.
/// Same boundary with weight cells explicit (decoded BF16 or native FP32), so a harness needs no checkpoint tensor.
pub fn rmsOfValues(comptime T: type, x: []const u16, weight: []const f32, eps: f32, out: []u16) void {
@setFloatMode(.strict);
var sum: f64 = 0;
Expand Down
3 changes: 1 addition & 2 deletions zig/tests/deepseek_v41_reference/projection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub fn applyF32(matrix: weights.AnyMatrix, x: []const u16, y: []f32, scratch: *S
}
}

/// The kept-dense form: the same input rotation boundary and output stages as the quantized
/// path, with the tile decode replaced by an FP64 dense dot over the f16-rounded rotated row.
/// Kept-dense: quantized rotation and output stages; FP64 dot over the f16-rounded row replaces the tile decode.
fn applyDense(comptime T: type, d: weights.Dense, x: []const u16, y: []T, scratch: *Scratch) !void {
@setFloatMode(.strict);
if (x.len != d.k or y.len != d.n) return error.InvalidReferenceProjectionLength;
Expand Down
4 changes: 1 addition & 3 deletions zig/tests/deepseek_v41_reference/qualify.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub const Result = struct {
greedy_gate_passed: bool = false,
};

/// One indexer-selection boundary per scored window: the contract's margin
/// (BF16-rounded FP32 scores), the FP64-ideal margin on the same inputs, and how
/// many top-k picks differ between them — the real-data support-flip measurement.
/// Indexer-selection boundary per window: contract margin (BF16-rounded FP32 scores), FP64-ideal margin, support flips.
const MarginLog = struct {
records: [1024]MarginRecord = undefined,
len: usize = 0,
Expand Down