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

- HIP prompt attention tile (`tf_fa_paged_*`, `tf_fa_wide_*`): a row that meets a NaN attention score before any finite one now gives NaN, as float64 does, instead of 0 or a finite value. Replies change only where a NaN or infinity was already in the attention inputs (#5192, refs #1821).

- Tools: `pr_check.py` takes unchanged files' text from the base blobs instead of reading every file from disk, with the same findings; much faster on Windows. Thanks @Bizuayeu.

- Inkling 2 on Metal: decode runs its six attention projections as one dispatch over the six output segments, each row computed exactly as by its own projection; on an M3 Ultra Inkling 2 Mini decodes 3% faster at one stream and about 1% faster at four, with every token unchanged (#5005). Thanks to @gilby.
Expand Down
5 changes: 2 additions & 3 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ MLX's formula keeps the infinity, because its codes enter the MMAs as q + 128 an
`tf_glm_scores` scores a NaN dot as 0 where float64 keeps the NaN: its relu is `fmaxf(x, 0)`, as `tl.maximum` is in the
Python family's Triton glue it was ported from. Nemotron's Metal `tf_xup_w3` gives 0 for a NaN dot where float64 keeps
the NaN: its relu2 is `metal::max(bfloat(acc), 0.0f)`; the other up pipelines give its bytes on the same inputs.
HIP's prompt attention tile (`tf_fa_paged_*`, `tf_fa_wide_*`) writes 0 for a row whose every score is NaN where
float64 gives NaN, as its running max is `fmaxf` and a row at max -inf takes p = 0; and an infinite value at a key
the row sees under fp32's least weight gives NaN where float64 gives inf, as 0 x inf enters its P x V.
HIP's prompt attention tile (`tf_fa_paged_*`, `tf_fa_wide_*`) gives NaN for an infinite value at a key the row sees
under fp32's least weight, where float64 gives inf, as 0 x inf enters its P x V.
Qwen3.8 Flash Next's `tf_sattn_nax` and `tf_attn256_nax` score masked keys -FLT_MAX, the value each row's running
max starts from, so a row whose every score is -Inf weighs its masked keys 1 and gives a finite average where float64
gives NaN. `tf_sattn_nax`'s padding lanes also read key 0 at p = 0, so a non-finite value in key 0's row reaches every
Expand Down
4 changes: 3 additions & 1 deletion zig/kernels/hip/attention/prefill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,12 @@ __device__ void fa_dot2(const float* __restrict__ q, const Cache* __restrict__ k
if constexpr (!CAUSAL) nan_row = row_max16(nan_row ? 1.f : 0.f) > 0.f;
const float next_m = nan_row ? NAN : fmaxf(m_run[i], tile_max);
const float alpha = next_m == -INFINITY ? 1.f : expf(m_run[i] - next_m);
// a row with no finite score yet subtracts 0: a masked key's p is 0, a NaN score's NaN
const float m_sub = next_m == -INFINITY ? 0.f : next_m;
float p[4], sum = 0.f;
#pragma unroll
for (int j = 0; j < 4; ++j) {
p[j] = next_m == -INFINITY ? 0.f : expf(acc[i][j] - next_m);
p[j] = expf(acc[i][j] - m_sub);
sum += p[j];
}
sum = row_sum16(sum);
Expand Down
38 changes: 38 additions & 0 deletions zig/src/hip/kernel_tests/attention_edge.zig
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,41 @@ test "a non-finite value past a row's causal edge adds nothing to that row, in b
try shiftedPlanted(t, &rng, 237, 87);
}
}

/// A NaN query on head 0's row 0 and a NaN first page on kv head 1: NaN in every dim of their rows, the rest finite.
fn nanRows(t: *Rig, rng: *rig.Rng) !void {
const q_pos0 = 87;
const qlen = 150;
const span = 256;
const c = try Cache.make(t, rng, kv_heads, span, d);
defer c.free();
for (0..64) |key| c.k[(1 * span + key) * d] = t.bits(std.math.nan(f32));
const hq = try gpa.alloc(f32, heads * qlen * d);
defer gpa.free(hq);
for (hq) |*x| x.* = rng.unit();
hq[3] = std.math.nan(f32); // head 0, row 0
const r = try run(t, c, hq, qlen, span, q_pos0);
defer for ([_][2][]f32{ r.paged, r.flat }) |pair| for (pair) |x| gpa.free(x);
try std.testing.expectEqualSlices(u32, @ptrCast(r.flat[0]), @ptrCast(r.paged[0]));
for (0..heads) |h| for (0..qlen) |i| for (0..d) |e| {
const o = (h * qlen + i) * d + e;
const want: Class = if ((h == 0 and i == 0) or h / group == 1) .nan else .finite;
if (classOf(r.paged[0][o]) != want) {
std.debug.print("head {d} row {d} dim {d}: {d} where float64 gives {t}\n", .{ h, i, e, r.paged[0][o], want });
return error.TestUnexpectedResult;
}
};
}

test "a row whose scores are NaN before any finite one gives NaN in every dim, in both cache types, paged and flat" {
const t = try Rig.open(1 << 20);
defer t.close();
var rng: rig.Rng = .{ .state = 0x1821 };
const act = t.bf16;
defer t.bf16 = act;
for ([_]bool{ false, true }) |bf16| {
if (bf16 and !act) continue; // RDNA2 has no bf16 dot2 and serves fp16 caches only
t.bf16 = bf16;
try nanRows(t, &rng);
}
}
7 changes: 3 additions & 4 deletions zig/tests/hip/contract_prefill.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ const control_names = [2][2][]const u8{
/// The served entry, or an edge entry: its own plant and a span a page past its last row.
pub const Mode = enum { served, edge };

/// What gate 1 finds on the tile: NaN scores leave its max, and its P x V takes a seen key whose weight underflows.
const finding = "a row whose every score is NaN writes 0 where float64 gives NaN (its running max is fmaxf, " ++
"which drops NaN, and a row at max -inf takes p = 0), and an infinite value at a key the row sees under " ++
"fp32's least weight gives NaN where float64 gives inf, as 0 x inf enters P x V";
/// What gate 1 finds on the tile: its P x V takes a seen key whose weight underflows.
const finding = "an infinite value at a key the row sees under fp32's least weight gives NaN where float64 " ++
"gives inf, as 0 x inf enters P x V";

/// A row's P x V chain: a dot2 a key pair and a fold a tile, under its keys plus a tile.
fn keyChain(s: c.Shape) usize {
Expand Down
Loading