[PR #4126] [Engine] Metal: sampling on the decode path (temperature, top-k, top-p, min-p, penalties, Philox seed) #4911

Open
opened 2026-11-07 16:31:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4126
Author: @mnott
Created: 10/25/2026
Status: 🔄 Open

Base: main ← Head: pr/metal-sampling


📝 Commits (2)

  • ce59fa7 feat(metal): sampling on the decode path (CPU sampler over the head's logits: temperature, top-k, top-p, min-p, penalties, Philox seed; a sampled verify window keeps a draft when the sample equals it)
  • b5660df fix(serve): sampled requests carry a Philox seed (request's, else fresh random) so temperature takes effect

📊 Changes

5 files changed (+203 additions, -3 deletions)

View changed files

➕ metal/src/engine/sampler_host.cpp (+163 -0)
📝 metal/src/engine/stubs.cpp (+0 -1)
📝 serve/server.py (+6 -1)
📝 serve/test_server.py (+9 -0)
📝 src/core/verify.cpp (+25 -1)

📄 Description

Problem and result

The Metal decode path only ever took the argmax (metal/src/engine/window.cpp decoded greedily; sample_tokens was a stub), so temperature, top_k, top_p, min_p, the penalties and seed had no effect on macOS. In agent use the model then repeated itself word for word across turns. With this change Metal samples the way the CUDA engine does: a seed gives a reproducible text, different seeds give different texts, and greedy requests are unchanged.

Overview of our related macOS / Metal contributions: #4128. Focused fix of a Metal gap (the CUDA engine already samples).

Approach

  • A CPU sampler over the head's logits applies temperature, top-k, top-p, min-p and the presence/frequency/repetition penalties and draws from a Philox stream keyed by the seed (CUDA's sampler is the reference).
  • Speculative decode stays correct under sampling: a sampled verify window keeps a draft token only when the sample equals it.
  • serve: a sampled request carries a Philox seed (the request's, else a fresh random one), so temperature takes effect through the API.
  • Greedy requests (temperature 0) take the unchanged argmax path.

Validation

Branch head b5660dfcc92d5964b2527017f37c108e5da2fba1, two commits on v0.2.6. Apple Silicon, 128 GB unified memory, Gyro-M TQ2_0, --spec 2 with the MTP draft, same flags for both builds, no environment overrides.

Passed:

  • Build of strata on macOS.
  • Greedy, 80 tokens: output token ids identical to v0.2.6.
  • Sampled (--temperature 1.5 --top-k 50): seed 1 and seed 2 differ; seed 1 twice is identical.
  • Greedy decode speed, 256 tokens, 3 alternating runs: v0.2.6 63.32 / 63.26 / 63.07 tok/s, this branch 62.91 / 63.47 / 63.16 tok/s (−0.06 %).

Failed:

  • none observed

Not run:

  • CUDA, HIP and SYCL builds (the change is in the Metal engine and the server only).

Limits

The sampler runs on the CPU over the head's logits; a GPU sampler could save the logits copy per token.


🔄 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/4126 **Author:** [@mnott](https://github.com/mnott) **Created:** 10/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `pr/metal-sampling` --- ### 📝 Commits (2) - [`ce59fa7`](https://github.com/Niko1221/Strata/commit/ce59fa78c461a2091f449509959837078723b77d) feat(metal): sampling on the decode path (CPU sampler over the head's logits: temperature, top-k, top-p, min-p, penalties, Philox seed; a sampled verify window keeps a draft when the sample equals it) - [`b5660df`](https://github.com/Niko1221/Strata/commit/b5660dfcc92d5964b2527017f37c108e5da2fba1) fix(serve): sampled requests carry a Philox seed (request's, else fresh random) so temperature takes effect ### 📊 Changes **5 files changed** (+203 additions, -3 deletions) <details> <summary>View changed files</summary> ➕ `metal/src/engine/sampler_host.cpp` (+163 -0) 📝 `metal/src/engine/stubs.cpp` (+0 -1) 📝 `serve/server.py` (+6 -1) 📝 `serve/test_server.py` (+9 -0) 📝 `src/core/verify.cpp` (+25 -1) </details> ### 📄 Description ## Problem and result The Metal decode path only ever took the argmax (`metal/src/engine/window.cpp` decoded greedily; `sample_tokens` was a stub), so `temperature`, `top_k`, `top_p`, `min_p`, the penalties and `seed` had no effect on macOS. In agent use the model then repeated itself word for word across turns. With this change Metal samples the way the CUDA engine does: a seed gives a reproducible text, different seeds give different texts, and greedy requests are unchanged. ## Related discussion Overview of our related macOS / Metal contributions: #4128. Focused fix of a Metal gap (the CUDA engine already samples). ## Approach - A CPU sampler over the head's logits applies temperature, top-k, top-p, min-p and the presence/frequency/repetition penalties and draws from a Philox stream keyed by the seed (CUDA's sampler is the reference). - Speculative decode stays correct under sampling: a sampled verify window keeps a draft token only when the sample equals it. - `serve`: a sampled request carries a Philox seed (the request's, else a fresh random one), so `temperature` takes effect through the API. - Greedy requests (temperature 0) take the unchanged argmax path. ## Validation Branch head `b5660dfcc92d5964b2527017f37c108e5da2fba1`, two commits on `v0.2.6`. Apple Silicon, 128 GB unified memory, Gyro-M TQ2_0, `--spec 2` with the MTP draft, same flags for both builds, no environment overrides. **Passed:** - Build of `strata` on macOS. - Greedy, 80 tokens: output token ids identical to `v0.2.6`. - Sampled (`--temperature 1.5 --top-k 50`): seed 1 and seed 2 differ; seed 1 twice is identical. - Greedy decode speed, 256 tokens, 3 alternating runs: v0.2.6 63.32 / 63.26 / 63.07 tok/s, this branch 62.91 / 63.47 / 63.16 tok/s (−0.06 %). **Failed:** - none observed **Not run:** - CUDA, HIP and SYCL builds (the change is in the Metal engine and the server only). ## Limits The sampler runs on the CPU over the head's logits; a GPU sampler could save the logits copy per token. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#4911
No description provided.