Repository navigation
perf(sampling): share one sampler between requests with equal parameters - #4376
Merged
Merged
Conversation
mlx-lm's GenerationBatch._step samples a decode batch in one call only when every row's sampler is the same object. The scheduler built a fresh sampler per request, so a batch of B requests with identical parameters ran B one-row filter-and-sample chains (top-k, top-p, categorical over the 248K vocabulary: about forty graph ops and GPU launches per row) and concatenated them. make_shared_sampler hands equal (temp, top_p, min_p, min_tokens_to_keep, top_k) the same callable from a bounded cache, and the per-request builder uses it. The samplers are stateless; each row still draws from its own filtered distribution (the filters are row-wise, tested equal to one-row calls), only the RNG stream behind the draws changes. XTC is never shared (its coin flip is one draw per call). OMLX_SHARED_SAMPLERS=0 restores one sampler per request. Co-authored-by: Sören Sanders <81529835+w-Teilchen@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A module-level mx.set_default_device(mx.cpu) moved every later test in the pytest session to the CPU: test_qwen35_verify_sdpa_split then failed 7 tests and the session segfaulted (full qwen4/mtp/scheduler suite, M5 Ultra). A module-scoped autouse fixture now sets the CPU and restores the previous device. Co-authored-by: Sören Sanders <81529835+w-Teilchen@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ampler tests Co-authored-by: Sören Sanders <81529835+w-Teilchen@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jundot
approved these changes
Oct 12, 2026
jundot
left a comment
Owner
There was a problem hiding this comment.
Sharing the sampler lets mlx-lm sample the whole batch in one call, and each row still samples from its own distribution. Here's what I got on M5 Max with Qwen3.6-35B-A3B (temperature 1.0, top_p 0.95, top_k 20, aggregate decode throughput).
| Load | Shared off | Shared on | Change |
|---|---|---|---|
| 8 streams x 2K, 512 tokens | 378.6 tok/s | 414.1 tok/s | +9.3% |
| 8 streams x 32K, 256 tokens | 144.7 tok/s | 155.8 tok/s | +7.7% |
| Check | Result |
|---|---|
| Same prompt x8 at temperature 1.0 | All outputs differ |
| Same seed twice | Identical |
| Greedy x8 | Identical |
I'll drop the OMLX_SHARED_SAMPLERS switch in a small follow-up since I keep env toggles out of production code. Thanks, LGTM, merging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When every row of a decode batch holds the same sampler object, mlx-lm's
GenerationBatch._stepsamples the whole batch in one call (mlx_lm/generate.py:if not samplers or all(s is samplers[0] for s in samplers)). Otherwise it runs each row's sampler on a one-row slice and concatenates the results. The scheduler builds a fresh sampler per request (_build_sampler_and_processors), so a batch of B requests with identical sampling parameters always takes the per-row path. That costs B filter-and-sample chains per step (top-k, top-p, categorical over the full vocabulary: about forty graph ops and their launches per row) instead of one.This PR adds
make_shared_samplerinomlx/utils/sampling.py. It hands requests with equal(temp, top_p, min_p, min_tokens_to_keep, top_k)the same callable from a small bounded cache (256 entries, LRU), and the per-request builder uses it.seedkeeps its existing best-effort semantics, because it already seeds the global MLX RNG.OMLX_SHARED_SAMPLERS=0restores one sampler per request. That is also the baseline below.Alternatively, mlx-lm could group rows by equal sampler parameters itself. I went with the smaller change here, since omlx builds the sampler objects.
Validation
Benchmark. Mac Studio M5 Ultra, 256 GB.
maincc1fdc9 + this PR, native kernels built (OMLX_WITH_CUSTOM_KERNEL=1). Qwen3.8-Flash-Next oQ4e,qwen35_oq_a8_enabled, top_k 20, prefix cache on.OMLX_SHARED_SAMPLERS=0. That is the code path onmain.main)That is +3 % at 8 streams for 2K and about +6 % at 32K × 8. A single stream is unchanged, as expected: one row is one call either way. TTFT and peak memory are unchanged (max TTFT 2.9–3.2 s at 2K × 8, 31.6 s at 32K × 8; footprint within 2 GB).
Tests.
tests/test_shared_sampler.py(11 tests, CPU) covers:The default suite (
-m "not slow and not integration", same machine):maincc1fdc9 gives 17,222 passed, 659 skipped. This branch gives 17,233 passed (the 11 new tests), 659 skipped, 0 failures.Black and Ruff are clean on the new code. The remaining Ruff findings in
sampling.pyand Black's onscheduler.pyalready exist onmain.Checklist
🤖 Generated with Claude Code