mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[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
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4555
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
c2aeb86metal: the request's sampling (the CUDA sampler's chain, on the CPU over the window's head logits)cc7407ctests: 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.cpp9384b21docs: 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,seedand thepenalties reach the engine on its
GENline but change nothing, andlogit_biasis dropped. Since 0.1.45 the serveralso 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; thisPR 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_biaskeeps the window's argmax, so its tokens are exactlythe 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 andtie 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 eachslot'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 sampledslot's row is drawn as CUDA's
sample_rowsdraws it: Philox at the slot's seed and position, no penalties and nologit_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, sole_stayed -1,set_logit_biasread it as an earlier stage of a layer split, and the bias was never stored. Bothchanges are inside
#if defined(STRATA_GPU_METAL):unifdef -USTRATA_GPU_METALon thisverify.cppgives main'sfile 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 inverify.cppand the test target inmetal/CMakeLists.txt. At the new tip9384b214, same Mac and model:unifdef -USTRATA_GPU_METAL src/core/verify.cppis identical to main's. The other files outsidemetal/aredocs/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 2engine: two slots withdifferent 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).
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 2with MTP,--mmap-experts,--max-context 262144. Base =main3fa1b524(0.2.0-beta + 7 commits), built from the same tree.Rebased since onto
maine88ee39d(2026-10-20; its 5 new commits change no engine, Metal or server file): the3 commits build again with all Metal targets,
host_sampler_paritypasses again; the runs below are on3fa1b524.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 thesame script fails 11 of 13 (every seed gives the greedy text; penalties and
logit_biaschange nothing). 208one-token requests drew exactly the token a reference sampler (written in the test from
sampler.cu) draws from thesame dumped logits.
serve/server.py(9 requests, the curl script in the issue): base, 9 of 9 print thegreedy 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_bias760:-100) each print anothersentence; the banned first token is not picked.
the base build.
Failed: none observed.
Not run:
STRATA_SPEC_COUPLED,STRATA_SPEC_PROB,STRATA_SPEC_GUMBEL(the Mac draftsgreedily).
Limits
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.
logit_biasis refused, as on the PC.temperature: 0andpresence_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.