Skip to content

metal: presence and frequency penalties on Flash Next and Qwen3.8-27B, each row over the reply and its own path - #3145

Merged
ashhart merged 4 commits into
mainfrom
lead/metal-penalties
Nov 22, 2026
Merged

ashhart merged 4 commits into
mainfrom
lead/metal-penalties

Conversation

@ashhart

@ashhart ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Closes #3113.

Metal now takes presence_penalty and frequency_penalty on Qwen3.8-Flash-Next and Qwen3.8-27B; before, they were refused. Each row's counts cover the reply so far plus that row's own draft path, so a drafted reply equals an undrafted one. Counting matches CUDA's cuda_penalties. Flash Next applies them in a GPU pass before argmax and draws (fz_penalize_rows: fp32 subtract, then round to bfloat). Qwen3.8-27B applies them in its queue and verify draws on the host, and skips the GPU argmax and fused tail for penalized windows only. With both penalties at 0 nothing is staged, so the default path is unchanged. Penalized Flash Next streams no longer use the lone driver.

Flash Next stages its penalty entries before the command buffer opens, so an error can't leave an encoder open.

Tests: the lone-driver test fails on main (expected 0, found 1); lanes 363 to 369, with new penalty_rows tests; a recording-device test runs the real round/fill/encode and applies the recorded dispatch, and fails when a buffer offset is broken on purpose; a verify row stages exactly what the one-token round at its position stages, for a chain and a tree; new step test-flashnext-penalties-host, in the Linux host list. check-native-cuda-host and compile-cuda-tests pass for both Linux targets. The kernel compiles offline for Metal 3.1, 3.2 and 4.0.

With --instruct-sampling card, both Metal models now serve their card's presence_penalty 1.5, as CUDA already does.

Device check pending (M5): with penalties at 0, token hashes equal main's, greedy and seeded, drafted and not, 1 and 4 streams; with presence 1.5 and frequency 0.5, drafted equals undrafted and 4 streams equal solo; an unpenalized stream beside a penalized one keeps its solo hashes; decode tok/s with penalties at 0 level with main.

@speedcie2-sys

Copy link
Copy Markdown

Klk

@ashhart
ashhart force-pushed the lead/metal-penalties branch from 8122891 to 920e7ce Compare November 17, 2026 21:43
@ashhart
ashhart force-pushed the lead/metal-penalties branch from 920e7ce to c340a5f Compare November 17, 2026 23:28
@ashhart
ashhart force-pushed the lead/metal-penalties branch from c340a5f to 8862cfe Compare November 21, 2026 20:40
ashhart added a commit that referenced this pull request Nov 21, 2026
…n today's main (three blank lines fewer)

Main's metal_main.zig is at 600 lines; #3145's penalties suite adds three, which lean_check counts. Three blank lines
between adjacent declarations go; no code changes.
@ashhart
ashhart force-pushed the lead/metal-penalties branch from 8862cfe to 7f1ef97 Compare November 21, 2026 23:53
ashhart added a commit that referenced this pull request Nov 22, 2026
…n today's main (three blank lines fewer)

Main's metal_main.zig is at 600 lines; #3145's penalties suite adds three, which lean_check counts. Three blank lines
between adjacent declarations go; no code changes.
@ashhart
ashhart force-pushed the lead/metal-penalties branch from 7f1ef97 to 17e3a03 Compare November 22, 2026 03:47
…, each row over the reply and its own path

core/lanes/penalty_rows.zig stages a forward's (row, token, amount) entries as CUDA's cuda_penalties does: the
reply's tokens before the cache, the pending (or fed) token, then each row's own drafts from the window's first row,
following parents for a tree; a distinct token held c times loses presence + frequency * c, once per row. A verify
row's entries are the one-token round's at the same position, so drafted replies equal "draft": false.

Flash Next lowers its shared rounds' logits on the GPU (fz_penalize_rows, fp32 subtract rounded once to bf16, as
tf_penalize_rows) after the grammar masks and before the argmax and the sampled draws; the entries are staged
before the round's command buffer opens. Qwen3.8-27B draws a penalized row on the host from a lowered copy of its
frame row, greedy too, and leaves the round-wide argmax and the fused keep tail to unpenalized windows. The first
token has no reply to count. A lone driver no longer takes a penalized stream, so it stays in the lane core.

With both penalties 0 nothing is staged and every round encodes and draws as before. --instruct-sampling card on
these two now serves the card's presence_penalty 1.5 instead of leaving it out. Other Metal families still refuse
nonzero penalties.
…n today's main (three blank lines fewer)

Main's metal_main.zig is at 600 lines; #3145's penalties suite adds three, which lean_check counts. Three blank lines
between adjacent declarations go; no code changes.
@ashhart
ashhart force-pushed the lead/metal-penalties branch from 17e3a03 to 0071ee3 Compare November 22, 2026 05:38
@ashhart
ashhart merged commit 211cd86 into main Nov 22, 2026
2 of 12 checks passed
ashhart added a commit that referenced this pull request Nov 22, 2026
…n today's main (three blank lines fewer)

Main's metal_main.zig is at 600 lines; #3145's penalties suite adds three, which lean_check counts. Three blank lines
between adjacent declarations go; no code changes.
@ashhart
ashhart deleted the lead/metal-penalties branch November 22, 2026 05:38
ashhart added a commit that referenced this pull request Nov 22, 2026
ashhart added a commit that referenced this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Presence and frequency penalties on Metal

2 participants