[PR #3301] [CLOSED] [Engine] metal: the request's sampling on the Mac engine (temperature, top_p, top_k, min_p, seed, penalties, logit_bias) #4555

Closed
opened 2026-11-07 15:37:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3301
Author: @oceanio
Created: 10/20/2026
Status: ❌ Closed

Base: main ← Head: pr/metal-sampling-solo


📝 Commits (3)

  • c2aeb86 metal: the request's sampling (the CUDA sampler's chain, on the CPU over the window's head logits)
  • cc7407c tests: tools/metal_sampling_test.py (the request's sampling through --serve: reproducible seeds, picks draw for draw against a reference sampler on the dumped logits, penalties, logit_bias, two slots with their own settings) and metal/tests/engine/host_sampler_parity.cpp
  • 9384b21 docs: Mac sampling (MAC.md Sampling: the keys, the order, seeds, batch slots, the host pick's cost, greedy vs sampled decode)

📊 Changes

8 files changed (+896 additions, -1 deletions)

View changed files

📝 docs/MAC.md (+31 -0)
📝 metal/CMakeLists.txt (+7 -0)
➕ metal/src/engine/host_sampler.cpp (+178 -0)
➕ metal/src/engine/host_sampler.h (+28 -0)
📝 metal/src/engine/window.cpp (+11 -0)
➕ metal/tests/engine/host_sampler_parity.cpp (+222 -0)
📝 src/core/verify.cpp (+33 -1)
➕ tools/metal_sampling_test.py (+386 -0)

📄 Description

Problem and result

On the Mac (Metal) engine every request decodes greedily. temperature, top_p, top_k, min_p, seed and the
penalties reach the engine on its GEN line but change nothing, and logit_bias is dropped. Since 0.1.45 the server
also sends the model card's sampling when a request gives none, and on a Mac that has no effect either. Nothing in the
log or the response says so (#3006).

With this PR the Mac engine samples as the PC engine does, on the solo path and in v0.2.9's batch slots: the same chain and order (logit_bias, penalties, top_k,
top_p, min_p, temperature), one Philox draw per token keyed by the seed and the position, so a fixed seed repeats its
text and another seed gives another. Temperature 0 (or none) stays greedy and gives the same tokens as before.

#3006 (the bug, reproduced again on 0.2.0-beta; see Validation). A focused fix, so no scope issue first. #4126 takes
on the same bug; the differences are in my comment there (https://github.com/Niko1221/Strata/pull/4126#issuecomment-6409342569).

Approach

Verifier::run's Metal branch returned the window's argmax. The CUDA path samples on the host after its window; this
PR does the same on the Mac, on the CPU, over the window's head logits, which are already in unified memory (no copy).
A request that is greedy and has no penalties and no logit_bias keeps the window's argmax, so its tokens are exactly
the old ones. The MTP drafts stay greedy and a draft is kept when the sampled token equals it, as on the PC. Each kept
token is then the target's own draw at its position (Philox keyed by the seed and the position), so the drafts change
the speed, not the distribution of the text.

  • metal/src/engine/host_sampler.cpp / .h: the CUDA sampler's chain with the kernels' float/double arithmetic and
    tie order. 9-46 µs per row on 248,320 logits (M5 Max), so no GPU kernel.
  • metal/src/engine/window.cpp: an accessor for row t of the last window's head logits. A batch window leaves each
    slot's head logits in its own row; the slot-by-slot path (STRATA_METAL_BATCH_ROWS=0) now moves them there too.
  • src/core/verify.cpp: the sampling after the Metal window. In batch slots (run_slot_rows' Metal branch) a sampled
    slot's row is drawn as CUDA's sample_rows draws it: Philox at the slot's seed and position, no penalties and no
    logit_bias (a batch engine refuses it); a greedy slot keeps the window's argmax. And one line in the Metal init,
    if (le_ < 0) le_ = g.n_layers;, as the CUDA init does further down. The Metal init returned before that line, so
    le_ stayed -1, set_logit_bias read it as an earlier stage of a layer split, and the bias was never stored. Both
    changes are inside #if defined(STRATA_GPU_METAL): unifdef -USTRATA_GPU_METAL on this verify.cpp gives main's
    file byte for byte, so CUDA, HIP and SYCL compile the same code as before.

Validation

Re-run on v0.2.9 (2026-11-01). Rebased onto main c62d5e3a (v0.2.9). v0.2.9 gives the Metal engine batch slots (greedy only), so this PR now samples in them too (see Approach); the solo path is the same lines as before. Two conflicts, both only where a line goes: a forward declaration in verify.cpp and the test target in metal/CMakeLists.txt. At the new tip 9384b214, same Mac and model:

  • Metal build 0.1.55-metal: 0 errors, the same 16 warnings as main.
  • unifdef -USTRATA_GPU_METAL src/core/verify.cpp is identical to main's. The other files outside metal/ are
    docs/MAC.md and the test script.
  • host_sampler_parity: 592 picks, all equal the reference.
  • tools/metal_sampling_test.py: all 18 checks pass. The 5 new ones run in a --batch 2 engine: two slots with
    different settings (temperature, top_p, top_k, min_p, seed) each give the tokens the same request gives alone, a
    sampled slot's tokens differ from its greedy ones, and a greedy slot beside a sampled one gives its solo greedy tokens
    (as does the sampled one).
  • --bench (256 tokens per request; sampled = temperature 0.7, top_p 0.95, top_k 20): solo, 4 short prompts with MTP,
    greedy 96.2 tok/s and sampled 95.0 (means; drafts kept 0.71 in both); two slots, 112.0 tok/s together greedy and
    112.6 sampled (17.76 and 17.67 ms per window of two rows).
  • Not re-run on v0.2.9: the greedy comparison with main through the server (on v0.2.7, 8 of 8 identical). A greedy
    request still keeps the window's argmax, on the solo path and in a slot.

The rest of this section is the first run, on 3fa1b524.

Environment: M5 Max 128 GB, macOS 26.7.1, Command Line Tools 27.0 (Apple clang 21). Qwen3.8-Flash-Next GSQ-RCO IQ3_S
(SC117's abliterated build, the official two-file layout), resident, --spec 2 with MTP, --mmap-experts,
--max-context 262144. Base = main 3fa1b524 (0.2.0-beta + 7 commits), built from the same tree.
Rebased since onto main e88ee39d (2026-10-20; its 5 new commits change no engine, Metal or server file): the
3 commits build again with all Metal targets, host_sampler_parity passes again; the runs below are on 3fa1b524.

Passed:

  • ninja -C build-metal (all Metal targets), at each of the 3 commits: 0 errors, no new warnings.
  • build-metal/host_sampler_parity (CPU): 592 picks, all equal a serial transcription of the CUDA kernels
    (sampler_parity.cpp's mirror); Philox matches 4,096 draws.
  • python3 tools/metal_sampling_test.py --exe build-metal/strata --config <cfg>: 13 of 13 pass. On the base build the
    same script fails 11 of 13 (every seed gives the greedy text; penalties and logit_bias change nothing). 208
    one-token requests drew exactly the token a reference sampler (written in the test from sampler.cu) draws from the
    same dumped logits.
  • #3006's reproduction through serve/server.py (9 requests, the curl script in the issue): base, 9 of 9 print the
    greedy sentence; this PR, the greedy request prints the same sentence as the base, and the other 8 (seeds 1-3 at
    temperature 1.0, top_k/top_p/min_p, the card's defaults, penalties, logit_bias 760:-100) each print another
    sentence; the banned first token is not picked.
  • Greedy unchanged: temperature 0 through the server on a 28-, a 2,257- and a 16,180-token prompt, identical text to
    the base build.

Failed: none observed.

Not run:

  • CUDA, HIP and SYCL builds (no such toolchains on a Mac). The code they compile does not change (see Approach).
  • Other Macs and models; Swift / Unsloth packs; batch slots (Metal had none then; see the v0.2.9 re-run).
  • Not ported: the opt-in draft schemes STRATA_SPEC_COUPLED, STRATA_SPEC_PROB, STRATA_SPEC_GUMBEL (the Mac drafts
    greedily).

Limits

  • Sampled decode can be a little slower than greedy, because fewer drafts may be kept and the texts differ: on v0.2.9
    95.0 against 96.2 tok/s (one run, drafts kept 0.71 in both); on the first run 74.8 / 72.9 / 68.8 against
    76.3 / 83.3 / 78.3 over 3 runs (drafts kept 0.70 against 0.72). The pick itself costs under 0.05 ms a token.
  • In batch slots the penalties are not applied and logit_bias is refused, as on the PC.
  • The card's presence penalty now applies on a Mac too: a client that wants the old greedy text sends
    temperature: 0 and presence_penalty: 0 (documented in docs/MAC.md).

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/3301 **Author:** [@oceanio](https://github.com/oceanio) **Created:** 10/20/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/metal-sampling-solo` --- ### 📝 Commits (3) - [`c2aeb86`](https://github.com/Niko1221/Strata/commit/c2aeb86ad61fb2fce65f75704ba018a403528d1f) metal: the request's sampling (the CUDA sampler's chain, on the CPU over the window's head logits) - [`cc7407c`](https://github.com/Niko1221/Strata/commit/cc7407cc65f0545e1484207d276390b5adbad9cb) tests: tools/metal_sampling_test.py (the request's sampling through --serve: reproducible seeds, picks draw for draw against a reference sampler on the dumped logits, penalties, logit_bias, two slots with their own settings) and metal/tests/engine/host_sampler_parity.cpp - [`9384b21`](https://github.com/Niko1221/Strata/commit/9384b21494c664aeeeb5d4b07243edc270f7442d) docs: Mac sampling (MAC.md Sampling: the keys, the order, seeds, batch slots, the host pick's cost, greedy vs sampled decode) ### 📊 Changes **8 files changed** (+896 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `docs/MAC.md` (+31 -0) 📝 `metal/CMakeLists.txt` (+7 -0) ➕ `metal/src/engine/host_sampler.cpp` (+178 -0) ➕ `metal/src/engine/host_sampler.h` (+28 -0) 📝 `metal/src/engine/window.cpp` (+11 -0) ➕ `metal/tests/engine/host_sampler_parity.cpp` (+222 -0) 📝 `src/core/verify.cpp` (+33 -1) ➕ `tools/metal_sampling_test.py` (+386 -0) </details> ### 📄 Description ## Problem and result On the Mac (Metal) engine every request decodes greedily. `temperature`, `top_p`, `top_k`, `min_p`, `seed` and the penalties reach the engine on its `GEN` line but change nothing, and `logit_bias` is dropped. Since 0.1.45 the server also sends the model card's sampling when a request gives none, and on a Mac that has no effect either. Nothing in the log or the response says so (#3006). With this PR the Mac engine samples as the PC engine does, on the solo path and in v0.2.9's batch slots: the same chain and order (logit_bias, penalties, top_k, top_p, min_p, temperature), one Philox draw per token keyed by the seed and the position, so a fixed seed repeats its text and another seed gives another. Temperature 0 (or none) stays greedy and gives the same tokens as before. ## Related discussion #3006 (the bug, reproduced again on 0.2.0-beta; see Validation). A focused fix, so no scope issue first. #4126 takes on the same bug; the differences are in my comment there (https://github.com/Niko1221/Strata/pull/4126#issuecomment-6409342569). ## Approach `Verifier::run`'s Metal branch returned the window's argmax. The CUDA path samples on the host after its window; this PR does the same on the Mac, on the CPU, over the window's head logits, which are already in unified memory (no copy). A request that is greedy and has no penalties and no `logit_bias` keeps the window's argmax, so its tokens are exactly the old ones. The MTP drafts stay greedy and a draft is kept when the sampled token equals it, as on the PC. Each kept token is then the target's own draw at its position (Philox keyed by the seed and the position), so the drafts change the speed, not the distribution of the text. - `metal/src/engine/host_sampler.cpp` / `.h`: the CUDA sampler's chain with the kernels' float/double arithmetic and tie order. 9-46 µs per row on 248,320 logits (M5 Max), so no GPU kernel. - `metal/src/engine/window.cpp`: an accessor for row t of the last window's head logits. A batch window leaves each slot's head logits in its own row; the slot-by-slot path (`STRATA_METAL_BATCH_ROWS=0`) now moves them there too. - `src/core/verify.cpp`: the sampling after the Metal window. In batch slots (`run_slot_rows`' Metal branch) a sampled slot's row is drawn as CUDA's `sample_rows` draws it: Philox at the slot's seed and position, no penalties and no `logit_bias` (a batch engine refuses it); a greedy slot keeps the window's argmax. And one line in the Metal init, `if (le_ < 0) le_ = g.n_layers;`, as the CUDA init does further down. The Metal init returned before that line, so `le_` stayed -1, `set_logit_bias` read it as an earlier stage of a layer split, and the bias was never stored. Both changes are inside `#if defined(STRATA_GPU_METAL)`: `unifdef -USTRATA_GPU_METAL` on this `verify.cpp` gives main's file byte for byte, so CUDA, HIP and SYCL compile the same code as before. ## Validation **Re-run on v0.2.9 (2026-11-01).** Rebased onto main `c62d5e3a` (v0.2.9). v0.2.9 gives the Metal engine batch slots (greedy only), so this PR now samples in them too (see Approach); the solo path is the same lines as before. Two conflicts, both only where a line goes: a forward declaration in `verify.cpp` and the test target in `metal/CMakeLists.txt`. At the new tip `9384b214`, same Mac and model: - Metal build 0.1.55-metal: 0 errors, the same 16 warnings as main. - `unifdef -USTRATA_GPU_METAL src/core/verify.cpp` is identical to main's. The other files outside `metal/` are docs/MAC.md and the test script. - `host_sampler_parity`: 592 picks, all equal the reference. - `tools/metal_sampling_test.py`: all 18 checks pass. The 5 new ones run in a `--batch 2` engine: two slots with different settings (temperature, top_p, top_k, min_p, seed) each give the tokens the same request gives alone, a sampled slot's tokens differ from its greedy ones, and a greedy slot beside a sampled one gives its solo greedy tokens (as does the sampled one). - `--bench` (256 tokens per request; sampled = temperature 0.7, top_p 0.95, top_k 20): solo, 4 short prompts with MTP, greedy 96.2 tok/s and sampled 95.0 (means; drafts kept 0.71 in both); two slots, 112.0 tok/s together greedy and 112.6 sampled (17.76 and 17.67 ms per window of two rows). - Not re-run on v0.2.9: the greedy comparison with main through the server (on v0.2.7, 8 of 8 identical). A greedy request still keeps the window's argmax, on the solo path and in a slot. The rest of this section is the first run, on `3fa1b524`. Environment: M5 Max 128 GB, macOS 26.7.1, Command Line Tools 27.0 (Apple clang 21). Qwen3.8-Flash-Next GSQ-RCO IQ3_S (SC117's abliterated build, the official two-file layout), resident, `--spec 2` with MTP, `--mmap-experts`, `--max-context 262144`. Base = `main` 3fa1b524 (0.2.0-beta + 7 commits), built from the same tree. Rebased since onto `main` e88ee39d (2026-10-20; its 5 new commits change no engine, Metal or server file): the 3 commits build again with all Metal targets, `host_sampler_parity` passes again; the runs below are on 3fa1b524. **Passed:** - `ninja -C build-metal` (all Metal targets), at each of the 3 commits: 0 errors, no new warnings. - `build-metal/host_sampler_parity` (CPU): 592 picks, all equal a serial transcription of the CUDA kernels (`sampler_parity.cpp`'s mirror); Philox matches 4,096 draws. - `python3 tools/metal_sampling_test.py --exe build-metal/strata --config <cfg>`: 13 of 13 pass. On the base build the same script fails 11 of 13 (every seed gives the greedy text; penalties and `logit_bias` change nothing). 208 one-token requests drew exactly the token a reference sampler (written in the test from `sampler.cu`) draws from the same dumped logits. - #3006's reproduction through `serve/server.py` (9 requests, the curl script in the issue): base, 9 of 9 print the greedy sentence; this PR, the greedy request prints the same sentence as the base, and the other 8 (seeds 1-3 at temperature 1.0, top_k/top_p/min_p, the card's defaults, penalties, `logit_bias` 760:-100) each print another sentence; the banned first token is not picked. - Greedy unchanged: temperature 0 through the server on a 28-, a 2,257- and a 16,180-token prompt, identical text to the base build. **Failed:** none observed. **Not run:** - CUDA, HIP and SYCL builds (no such toolchains on a Mac). The code they compile does not change (see Approach). - Other Macs and models; Swift / Unsloth packs; batch slots (Metal had none then; see the v0.2.9 re-run). - Not ported: the opt-in draft schemes `STRATA_SPEC_COUPLED`, `STRATA_SPEC_PROB`, `STRATA_SPEC_GUMBEL` (the Mac drafts greedily). ## Limits - Sampled decode can be a little slower than greedy, because fewer drafts may be kept and the texts differ: on v0.2.9 95.0 against 96.2 tok/s (one run, drafts kept 0.71 in both); on the first run 74.8 / 72.9 / 68.8 against 76.3 / 83.3 / 78.3 over 3 runs (drafts kept 0.70 against 0.72). The pick itself costs under 0.05 ms a token. - In batch slots the penalties are not applied and `logit_bias` is refused, as on the PC. - The card's presence penalty now applies on a Mac too: a client that wants the old greedy text sends `temperature: 0` and `presence_penalty: 0` (documented in docs/MAC.md). 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:37:22 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#4555
No description provided.