Repository navigation
fix(studio): price MLX attention on the route the runtime takes - #16517
Conversation
…e takes unsloth-zoo now runs long head_dim 256 prefill on mlx's fused kernel, so the estimate stops charging those calls the full scores and charges the shorter tail that still builds them. Qwen3.5-4B at 262K goes from 38.80 to about 19.4 GiB in total. An older zoo keeps its previous pricing.
…ull width A TurboQuant load was estimated and fitted as a full-width cache, which overstates the cache about 5x and omits the prefill's dequantized copy. The probe now converts the cache with mlx-vlm's own routine, and a prefill is charged the measured copy plus the scores of any tail the fused route does not take. A text load is priced on the mlx-vlm route it takes.
Unfused calls measure 1.01 to 1.13 times their scores on mlx 0.32.4, so 1.25 overcharged every model that still builds them.
A forced route now starts at 8192 keys, so calls below it still build scores: the estimate walks every prompt's tail up to that floor rather than asking only at full context, and probes the quantized route at a long context. Padded K and V copies are charged where the route can run.
|
This PR changes which route |
The widest-call search now asks whether a full chunk over two chunks of keys is unfused, and bisects on key count only when it is, instead of probing a fixed mid-context chunk.
|
Thanks, this one is real and the new route is right, so the test's expected probe was stale. Fixed in 77d1f06. The old search probed a fixed mid-context chunk, The other red jobs are not from this branch:
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
The runtime probe that can still refuse TurboQuant ran after the context was fitted at the compressed width, so a refused cache was allocated at full width for a context priced compressed. The probe now runs first, the fit uses the compressed width only when it accepts, and the KV policy reuses its answer.
## Summary `MLX CI on Mac M1` (the `dispatch` job) is red for every unsloth PR it runs on since unsloth-zoo #2365 (`57b40cc1`) landed. It hit #16517 at 08:16Z: ``` FAILED tests/test_mlx_inference_backend.py::test_the_stub_answers_every_helper_zoo_imports_from_inference E AssertionError: zoo imports ['_FIRST_USE_WORK'] from mlx.inference and the stub cannot answer them ``` zoo #2365 added `from .inference import _FIRST_USE_WORK` in `unsloth_zoo/mlx/speculative.py`, a private counter that `inference.py` defines and bumps. That is internal to zoo and fine. The guard is doing its job: Studio's backend tests replace `unsloth_zoo.mlx.inference` with a stub, the stub's fallback refuses private names on purpose, and the guard checks that the stub still answers every name zoo imports from that module. Without the entry, a real-model macOS test that reaches zoo's speculative path through the stub would fail with an `ImportError`. ## Change `studio/backend/tests/test_mlx_inference_backend.py`: `_FIRST_USE_WORK` joins `PLAIN_HELPERS` next to the other private names `speculative.py` imports (`_SparseDrafter`, `_VERIFY_GDN_MAX_ROWS`). The value is a `[0]` list, the same shape zoo uses. No product code changes. ## Testing Linux, with unsloth-zoo main (`682b3108`) on the path, `tests/test_mlx_inference_backend.py`: | | result | |---|---| | main | `test_the_stub_answers_every_helper_zoo_imports_from_inference` fails with the error above | | this branch | it passes | Apart from that test, both sides fail the same 7 cases. All of them need `mlx_lm`, which only the macOS runner has.
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Review and CI status for
|
| Job | Failure | Cause |
|---|---|---|
| dispatch | test_the_stub_answers_every_helper_zoo_imports_from_inference |
zoo main (unslothai/unsloth-zoo#2365) imports _FIRST_USE_WORK from mlx.inference, and the inference stub doesn't provide it yet |
| Repo tests (CPU, studio) | PowerShell runner lint, no-torch pair resolution | tests/python/test_desktop_unsloth_spec_no_deps_pair.py calls subprocess.run directly |
| Repo tests (CPU, studio-install) | Could not resolve a compatible unsloth / unsloth-zoo pair for no-torch mode |
install resolution |
| Repo tests (CPU, rest-2) | file-read lint on tests/test_multi_lora_cold_compile_cpu.py |
unrelated test file |
| (Python 3.13, rest) | offline scoped download tests | Cannot resolve an immutable revision for black-forest-labs/FLUX.1-dev (network metadata) |
The Core / Core zoo jobs were still queued when this was posted.
Changes during review:
77d1f066281: updated the stale probe assertion from the CI comment. The estimator now asks whether a full chunk over two chunks of keys is unfused, instead of probing a fixed mid-context chunk. The price assertion is unchanged.8a7464a238c(thread): the load now runs the TurboQuant probe before fitting the context, and fits at full width when the probe refuses. The KV policy reuses that answer, so the probe still runs once.- Not changed (thread): the pre-load admission estimate can't run that probe without a loaded model, and loads run in a separate worker, so the verdict doesn't reach the API process. The gap is limited to pinned contexts on checkpoints whose cache converts but whose quantized forward fails.
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
NilayYadav
left a comment
There was a problem hiding this comment.
Thank you for the PR! The goal of this PR is to make Studio's MLX memory estimate follow the attention route the runtime really takes once unsloth-zoo forces long causal prefill onto mlx's fused kernel, and to price a TurboQuant KV cache instead of fitting it at full width. As a summary, this PR asks zoo's runtime_fuses which calls still build full scores (walking each prompt's tail down to the forced route's key floor), charges the padded K/V copies of the 192/128 route, converts the probe cache with mlx-vlm's own maybe_quantize_kv_cache for TurboQuant and charges its dequantized prefill copy, fits the load at the TurboQuant width only after the load's probe accepts it, and lowers the unfused score multiple from 1.25 to 1.13.
| Reviewers | Severity | Finding |
|---|---|---|
| - | - | No open findings on the current head. The NAX-specific test expectations, the stale tiny Llama byte count and the pre-load TurboQuant pricing for models whose attention cannot read the converted cache are fixed on the branch. |
Concrete suggestions for each finding below.
Review + reproduction evidencePinned review: I reproduced the long-context estimate for
Conclusion: real and fixed for the estimate, and it stays above the measured peak. It has to land after unsloth-zoo#2367 (with the padded-route change suggested on that PR); with an older zoo the estimate keeps the previous pricing. |
[skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |

Summary
The MLX memory estimate in Unsloth Studio charged attention as if every long prefill chunk built its full score tensor. Depends on unslothai/unsloth-zoo#2367, which moves those calls onto mlx's fused kernel. On Qwen3.5-4B at 262K tokens the estimate drops from 38.80 to about 19.4 GiB. The estimate then follows the route the runtime actually takes, so the fit can offer longer contexts without under-promising.
With an older unsloth-zoo, the estimate keeps its previous pricing: it asks zoo's
runtime_fusesand falls back tofused_kernel_existswhen that is missing.What changed
maybe_quantize_kv_cache. A prefill is charged its measured dequantized copy (10 bytes per cached K+V element) plus any scores the fused route leaves. A text model with TurboQuant is priced on the mlx-vlm route it actually takes._SCORE_LIVE1.25 → 1.13: unfused calls measured 1.01–1.13× their score tensor on mlx 0.32.4. The one known exception is a NAX head_dim 512 tail (about 1.3×, under 100 MiB beyond this at 65K keys), which the fixed compute charge covers.Effect on the estimate (off NAX, compute only unless noted)
DeepSeek-V4 stays high because its tails under 1024 queries have no kernel off NAX. Models that build their own
pe_scoresmatrix (DeepSeek-V3, GLM-5.2, Kimi) are still charged full scores, which matches what they hold.Measured against the estimate: Qwen3.5-4B with 4-bit TurboQuant at 16K, estimated 2.87 GiB (cache plus compute) against a measured peak of 2.51 GiB.
Testing
The selected tests pass on M3 Max. Every added test was checked against a mutation of the code it guards.
TestALoadWithNoFootprintToQuoteand the Qwen2.5-VL-3B and gemma-3n byte tests fail identically onmain.