Skip to content

[Engine] Metal: verify windows take the one-token kernels (opt-in STRATA_INK_M_INVARIANT=1, #6671) - #7639

Open
HundredBillion wants to merge 1 commit into
Niko1221:mainfrom
HundredBillion:fix/metal-verify-m1-order
Open

HundredBillion wants to merge 1 commit into
Niko1221:mainfrom
HundredBillion:fix/metal-verify-m1-order

Conversation

@HundredBillion

Copy link
Copy Markdown

Problem and result

On Metal, a verify window (M = 2..8 rows of one slot, which is how DFlash 2 and DSpark check a draft) does not return the logits a one-token step returns for the same positions. Drafting keeps a token when the verify argmax matches it, so these differences can change greedy text (#6671 on Metal).

I measured it with --spec-check on an Apple M5 Pro: verify windows of 2, 3, 4 and 8 rows differed from the one-token steps in every row (max |dlogit| 0.003 at a 300-token prompt), and the hidden state already differed after visit 5.

With this PR and STRATA_INK_M_INVARIANT=1, every window row matches the one-token step bit for bit (max |dlogit| = 0, all four taps equal, RESULT: identical), at 300, 960, 1,100 and 3,000 prompt tokens. Past 1,024 tokens this needs #7098 as well (see Limits). Without the variable nothing changes.

Related discussion

#6671 (drafting changes the greedy result; Metal, HIP and CUDA reports). #6960 is the CUDA side and uses the same STRATA_INK_M_INVARIANT=1 switch; this is the Metal side. #7098 fixes the SWA ring overwrite that also affects verify windows past 1,024 tokens.

Approach

I traced which kernels a window takes that a one-token step does not (Inkling-2-Mini-SQv2-20, BF16 trunk):

# difference change with STRATA_INK_M_INVARIANT=1
1 Every GPU: BF16 linears with M = 2..8 run ink_linear_bf16_mv (4 weights per lane step) and, for the 200K unembed, ink_linear_bf16_mm8; M = 1 runs ink_linear_bf16_mv1 (8 weights per lane step, two float4 dots added before the accumulate). The per-row sums round differently. New ink_linear_bf16_mv_m1order: M = 2..8 rows, each row summed in exactly mv1's order (same lane-to-k assignment, same K & 4 tail on lane 0, same simd_sum). Used for all BF16 linears with M = 2..8, the unembed included.
2 Apple10: the attention trunk switches to MPP from M = 4 (BF16) / 5 (Q8_ROW). Trunk MPP threshold 9, as the M5 Max EXACT mode already does with exact_verify_m1_match.
3 Apple10: S6 linears take sk6_mm8 from M = 2. Skipped (the generic S6 kernel, as other GPUs).

F32 linears already sum M = 1 and M > 1 rows in the same order and are unchanged. MoE switches to MPP only from M = 10 on Apple10 (16 elsewhere), above a DFlash 2 window (kBlock = 8), so it is unchanged.

The switch is read at runner init, is not applied in the M5 Max FAST mode (lossy by design; its EXACT mode has exact_verify_m1_match), and is printed as verify_m1=0|1 in the runner ready line. The settings register has the new entry (tools/settings_register.py --check passes).

Validation

Apple M5 Pro (20-core GPU, 64 GB), macOS 27.2.1, Inkling-2-Mini-SQv2-20.strata, Release build (cmake -S engine/src/inkling2 ... && cmake --build ... --target strata-inkling). On the local branch the same code was switched by a variable named STRATA_INK_VERIFY_M1; it is renamed here to match #6960.

Passed:

  • Build of this commit on 8db8753 (main), Metal only (no CUDA, HIP or SYCL file is touched).

  • python3 tools/settings_register.py --check on this commit: exit 0.

  • --spec-check 32 --prompt-ids @ids-300.txt --ctx 4096 --kv fp16 on this commit on 8db8753 (DFlash 2 does not load here, so it checks the windows only):

    • without the variable: windows M = 2, 3, 4, 8 differ in every row (max |dlogit| 0.0030 / 0.0030 / 0.0030 / 0.0036), all four taps differ, RESULT: DIFFERS, plain fingerprint 7ecb44c57fd56dd6;
    • with STRATA_INK_M_INVARIANT=1: 0 rows differ at every M (max |dlogit| = 0), all four taps equal, RESULT: identical (exit 0), same plain fingerprint.
  • --spec-check 32 --prompt-ids @ids-300.txt --ctx 4096 --kv fp16, on a local branch from 4560d03 (0.2.23) with this change, [Engine] Metal: honor --kv fp8 (FP8_E4M3) on any GPU, not only the M5 Max fast path #6983, [Engine] inkling2 Metal: enable DFlash 2 on Apple M4 Max >= 64 GiB (2.95x decode) #7366 and a local gate that lets DFlash 2 load on the M5 Pro:

    • without the variable: windows M = 2, 3, 4, 8 differ in every row (max |dlogit| 0.0030 / 0.0030 / 0.0030 / 0.0036), taps after visits 5 / 23 / 41 / 48 differ (max |d| 0.011 / 0.0087 / 0.0058 / 0.0041), RESULT: DIFFERS;
    • with only change 2 and 3: still all rows differ, taps max |d| 0.00032 / 0.0018 / 0.0008 / 0.0004;
    • with this change: 0 rows differ at every M, all taps equal, RESULT: identical.
  • The same with prompts of 400, 600, 800 and 960 tokens: RESULT: identical. At 1,100 and 3,000 tokens the windows differ by up to 3.6 / 3.95 in the logits until [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 is added; with [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 on top, RESULT: identical at 960, 1,100 and 3,000.

  • Default path: without the variable, the plain B = 1 fingerprint (7ecb44c57fd56dd6) and the window and tap differences are the same as the build without this change.

  • End to end through strata serve on that local branch, DFlash 2 on, temperature 0, MBPP-sanitized test split (all 257 tasks), compared with the --draft off run in the last row by full answer text (reasoning and answer), tests run in a sandbox:

    DFlash 2 build answers identical to --draft off tests passed
    unchanged 119 / 257 246 / 257
    changes 2 and 3 only 183 / 257 246 / 257
    this change 235 / 257 (all 22 differences start late in long answers) 245 / 257
    this change + [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 250 / 257 (see below) 245 / 257 (the same 245 tasks as --draft off)
    --draft off — 245 / 257

    The --draft off column was generated on the build without [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098. [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 also changes --draft off once a context passes 1,024 tokens: all 7 answers that differed in the last row also differ between --draft off with and without [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098. Regenerated with --draft off on the same build as the last row, 5 of the 7 match it exactly, so 255 of 257 are identical; --draft off repeats exactly on the other two (tasks 160 and 286), which differ after about 2,150 generated tokens. I have not found why. --spec-check 2600 (300-token prompt) on that build: speculative vs plain identical over 2,600 tokens (909 steps, 3,925 drafted).

    Decode with this change (no [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098), DFlash 2 on: 55.8 tok/s against 26.9 tok/s with --draft off over the 257 answers, end to end, single request, idle machine.

Failed:

  • none observed

Not run:

  • Any Mac other than the M5 Pro; no Apple9 GPU (M4 Max / M3 Max), where only change 1 applies.
  • DSpark end to end (the SQv2-20 pack here uses DFlash 2); --spec-check covers the forward pass both drafters verify with.
  • Q8_ROW, S5 and other trunk formats: this pack's M = 2..8 linears are BF16 and F32 only (traced), so the Q8_ROW mv1/mv pair was not checked and is not changed.
  • --parallel above 1 and --kv fp8.
  • Speed of the M = 2..8 path in isolation; the end-to-end decode above is the only speed measurement.
  • A clean speed run with [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 on top (that MBPP run shared the machine with other work).

Limits

🤖 Generated with Claude Code

…ATA_INK_M_INVARIANT=1, Niko1221#6671)

A verify window (M = 2..8 rows of one slot, as DFlash 2 and DSpark check
drafts) did not return the logits a one-token step returns for the same
positions, so drafting could change greedy tokens (Niko1221#6671 on Metal).

- On every GPU the BF16 linear for M = 2..8 (ink_linear_bf16_mv, and
  ink_linear_bf16_mm8 for the 200K unembed) sums a row in another order
  than ink_linear_bf16_mv1: 4 weights per lane step against 8.
- On Apple10 the attention trunk switches to MPP from M = 4..5 and S6
  linears to sk6_mm8 from M = 2.

With STRATA_INK_M_INVARIANT=1 (the switch Niko1221#6960 uses on CUDA), BF16
linears with M = 2..8 take the new ink_linear_bf16_mv_m1order (mv1's
per-row summation order), the trunk MPP threshold is 9 (as the M5 Max
EXACT mode's exact_verify_m1_match), and sk6_mm8 is skipped. It is not
applied in the M5 Max FAST mode. Without the variable nothing changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant