perf: keep homogeneous batches on the vectorized sampling path - #3
Open
Vissirexa wants to merge 1 commit into
Open
perf: keep homogeneous batches on the vectorized sampling path#3Vissirexa wants to merge 1 commit into
Vissirexa wants to merge 1 commit into
Conversation
Every request was inserted into BatchGenerator with its own sampler, so GenerationBatch._step always took the any(self.samplers) branch: a per-row logprobs slice + sampler call + mx.concatenate on every decode step, even when all rows share identical sampling params (the common local case). With omlx's mx.compile-free sampler that is ~8-10 extra tiny Metal kernel launches per row per token. _create_batch_generator now records the param key of the fallback sampler it installs, and both insert sites pass a None row sampler when the request's params match — all-None rows keep mlx-lm's vectorized fallback_sampler(logprobs) path (one sampling op for the whole batch). Rows with differing params keep their own sampler exactly as before, and the per-step uid-registry realign covers the sampler sibling of the known stale-filter alignment issue (jundot#1799/jundot#1825), pinned by a new test. Bench (gemma-4-e4b-it-qat-OptiQ-4bit, temp=0.7 top_p=0.9, 256 tokens, median of 3): batch=1 107.7 -> 109.9 tok/s (+2.1%), batch=4 315.1 -> 322.3 tok/s (+2.3%). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VFknQkdZoPFqsH6vtNS11q
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.
What
Every request was inserted into
BatchGeneratorwith its own per-row sampler, soGenerationBatch._stepalways took theany(self.samplers)branch: per-row logprobs slice + sampler call +mx.concatenateon every decode step — even when all rows share identical sampling params (the overwhelmingly common local case). With omlx's mx.compile-free sampler that is ~8–10 extra tiny Metal kernel launches per row per token._create_batch_generatornow records the sampling-param key of the fallback sampler it installs, and both insert sites pass aNonerow sampler when the request's params match. All-Nonerows keep mlx-lm's vectorizedfallback_sampler(logprobs)path — one sampling op for the whole batch.Safety
GenerationBatch.filterskips reindexing an all-Nonesamplers list (any()is False) — the sampler sibling of the known stale-filter alignment issue (fix: normalise logits_processors row slots dropped to None by batch merge jundot/omlx#1799/thinking_budget is silently ignored on /v1/completions jundot/omlx#1825). The existing per-step uid-registry realign already rebuilds sampler rows positionally; a new test pins the remove-then-extend case..temp/.top_pfor draft acceptance).Bench
gemma-4-e4b-it-qat-OptiQ-4bit, temp=0.7 top_p=0.9, 256 decode tokens, median of 3 (old = per-row samplers forced via monkeypatch in the same process):
Testing
TestHomogeneousRowSamplerElisionintests/test_scheduler_logits_processors.py: elide/keep decisions, param-key insensitivity to non-sampler fields, realign-after-remove-then-extend, source-level guards on both insert sites.pytest tests/ -m "not slow and not integration"green (except the two pre-existingtest_glm_mtp_patch.pytolerance failures, identical onmain).Part of the inference-speed work tracked in
docs/perf-analysis/inference-speed-analysis.md(finding F2 stage 1).🤖 Generated with Claude Code
https://claude.ai/code/session_01VFknQkdZoPFqsH6vtNS11q