Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- DeepSeek V4.1 on Metal keeps sampling scratch between picks and scans greedy logits in vectors with the same chosen IDs (#3288).

- Flash Next on CUDA: a bf16 language head's draft rows load as 4-bit affine groups of 32 by default, and `TF_FLASHNEXT_DRAFT_HEAD=bf16` keeps them as stored; a malformed value stops the start by name, and replies are unchanged because drafts are verified (#3477, refs #1870). Thanks to @BadAd84.

- Idol's FLUX.2 VAE image encoder runs natively on Metal in fp32: one 1,024 x 1,024 picture to the denoiser's 4,101 condition rows, with the posterior's noise as an input. It is 0.185 s an encode on an M5 Ultra against 0.095 s for the SDK's bf16 encoder on torch MPS, and lies far closer to a float64 run: worst row 5.1e-5 against the SDK's 2.7e-2. `tf-idol-vae` gates it from a case `tools/zig/idol_case.py vae` records, with the ungated FLUX.2-klein-4B weights (#1774, after #1766, for #1757). Thanks to @jayleaton.
Expand Down
50 changes: 50 additions & 0 deletions zig/src/families/deepseek_v41/metal/pick.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// The greedy and sampled picks over a logits row; kept apart from target.zig to keep it under the 600-line cap.
const std = @import("std");
const host = @import("dsv41");
const target_ = @import("target.zig");
const lanes = host.lanes;
const Target = target_.Target;

/// The keyed choice of a logits row over the whole vocabulary (greedy: the first maximum, ties to the lower id).
pub fn choose(t: *const Target, row: []const f32, draw: u64, smp: ?lanes.Sampling) !u32 {
const s = smp orelse return argmax(row);
if (s.temperature <= 0) return argmax(row);
const vals = t.pick_vals[0..row.len];
for (vals, row) |*v, x| v.* = x;
return @intCast(try lanes.sampling.choose(t.a, vals, t.pick_ids[0..row.len], draw, s));
}

/// The first maximum, ties to the lower id, in 16-wide steps; a row holding a NaN takes the scalar scan.
pub fn argmax(row: []const f32) u32 {
const L = 16;
const V = @Vector(L, f32);
var top: V = @splat(-std.math.inf(f32));
var nan = false;
var i: usize = 0;
while (i + L <= row.len) : (i += L) {
const v: V = row[i..][0..L].*;
nan = nan or @reduce(.Or, v != v);
top = @max(top, v);
}
var m = @reduce(.Max, top);
for (row[i..]) |x| {
nan = nan or x != x;
m = @max(m, x);
}
if (nan) return scan(row);
var j: usize = 0;
while (j + L <= row.len) : (j += L) {
const v: V = row[j..][0..L].*;
if (@reduce(.Or, v == @as(V, @splat(m)))) break;
}
while (j < row.len) : (j += 1) if (row[j] == m) return @intCast(j);
return 0;
}

pub fn scan(row: []const f32) u32 {
var best: usize = 0;
for (row, 0..) |x, i| if (x > row[best]) {
best = i;
};
return @intCast(best);
}
54 changes: 29 additions & 25 deletions zig/src/families/deepseek_v41/metal/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const vw = @import("vision_weights.zig");
const vt = @import("vision_tower.zig");
const vrows = @import("vision_rows.zig");
const prep = @import("vision_prep.zig");
const pick = @import("pick.zig");

const iface = host.iface;
const lanes = host.lanes;
Expand Down Expand Up @@ -75,6 +76,9 @@ pub const Target = struct {
tree: tree_.Tree = .{},
/// the ViT and aligner, when the environment has a tower
tower: ?vt.Tower = null,
/// a sampled pick's float64 logits and its ids 0 .. vocab - 1, kept between picks
pick_vals: []f64 = &.{},
pick_ids: []u64 = &.{},

pub fn init(a: Allocator, env: Env, opt: Options) !*Target {
const c = env.c;
Expand All @@ -87,6 +91,11 @@ pub const Target = struct {
errdefer t.b.deinit();
t.pr = try prompt.Prompt.init(env.device, c, opt.prompt, opt.limit);
errdefer t.pr.deinit();
t.pick_vals = try a.alloc(f64, c.vocab);
errdefer a.free(t.pick_vals);
t.pick_ids = try a.alloc(u64, c.vocab);
errdefer a.free(t.pick_ids);
for (t.pick_ids, 0..) |*id, i| id.* = i;
const slots = try a.alloc(Slot, opt.slots);
errdefer a.free(slots);
var made: usize = 0;
Expand Down Expand Up @@ -161,6 +170,8 @@ pub const Target = struct {
t.cache.deinit();
t.pr.deinit();
t.b.deinit();
t.a.free(t.pick_vals);
t.a.free(t.pick_ids);
t.a.destroy(t);
}

Expand Down Expand Up @@ -317,7 +328,7 @@ pub const Target = struct {
t.ringTaps(s, n);
}
t.promptTaps(s, n);
const choice = try t.choose(logits, draw, smp);
const choice = try pick.choose(t, logits, draw, smp);
s.base = n;
s.live = true;
s.whole = true;
Expand Down Expand Up @@ -366,29 +377,6 @@ pub const Target = struct {
}
}

/// The keyed choice of a logits row over the whole vocabulary (greedy: the first maximum, ties to the lower id).
pub fn choose(t: *const Target, row: []const f32, draw: u64, smp: ?lanes.Sampling) !u32 {
const s = smp orelse return argmax(row);
if (s.temperature <= 0) return argmax(row);
const vals = try t.a.alloc(f64, row.len);
defer t.a.free(vals);
const ids = try t.a.alloc(u64, row.len);
defer t.a.free(ids);
for (vals, ids, row, 0..) |*v, *id, x, i| {
v.* = x;
id.* = i;
}
return @intCast(try lanes.sampling.choose(t.a, vals, ids, draw, s));
}

fn argmax(row: []const f32) u32 {
var best: usize = 0;
for (row, 0..) |x, i| if (x > row[best]) {
best = i;
};
return @intCast(best);
}

pub fn fail(t: *Target, e: anyerror) anyerror {
if (e == error.CommandBufferFailed) t.failed = true;
return e;
Expand Down Expand Up @@ -457,7 +445,7 @@ pub const Target = struct {
for (group, choices) |g, out| {
const s = &t.slots[g.slot];
const n = g.tokens.len;
for (0..n) |r| out[r] = try t.choose(lg[(r0 + r) * V ..][0..V], g.draws[r], g.sampling);
for (0..n) |r| out[r] = try pick.choose(t, lg[(r0 + r) * V ..][0..V], g.draws[r], g.sampling);
s.base = g.start;
s.rows = @intCast(n);
s.open = true;
Expand Down Expand Up @@ -592,3 +580,19 @@ pub const Target = struct {
return out.toOwnedSlice(t.a);
}
};

test "argmax: the first maximum, NaN rows as the scalar scan" {
const a = std.testing.allocator;
var prng = std.Random.DefaultPrng.init(7);
const rnd = prng.random();
const row = try a.alloc(f32, 129280);
defer a.free(row);
for (0..200) |k| {
for (row) |*x| x.* = rnd.floatNorm(f32);
if (k % 5 == 1) row[rnd.uintLessThan(usize, row.len)] = 9.0;
if (k % 5 == 2) row[rnd.uintLessThan(usize, row.len)] = std.math.nan(f32);
if (k % 5 == 3) @memset(row, -std.math.inf(f32));
if (k % 5 == 4) row[rnd.uintLessThan(usize, 7)] = std.math.nan(f32);
try std.testing.expectEqual(pick.scan(row), pick.argmax(row));
}
}
3 changes: 2 additions & 1 deletion zig/src/families/deepseek_v41/metal/tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const host = @import("dsv41");
const kv = @import("kv.zig");
const snapshots = @import("snapshots.zig");
const pipelines = @import("pipelines.zig");
const picks = @import("pick.zig");
const target_ = @import("target.zig");

const iface = host.iface;
Expand Down Expand Up @@ -195,7 +196,7 @@ pub const Tree = struct {
const o = &tr.open[trees[w[0]]];
const ch = &o.chains[w[1]];
for (ch.rows[0..ch.n], 0..) |row, i| {
const got = try t.choose(lg[(r0 + i) * V ..][0..V], o.draws[row], o.sampling);
const got = try picks.choose(t, lg[(r0 + i) * V ..][0..V], o.draws[row], o.sampling);
// a row run again in front of a branch must choose as it did on its own chain
if (o.done[row] and o.out[row] != got) return error.BranchUnstable;
if (o.home[row] == w[1]) {
Expand Down
Loading