Repository navigation
Studio MLX estimate: price attention scores where mlx really holds them, at the width it holds them - #16021
Conversation
…d full-width prefill
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. |
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 context admits
… measured on Metal
…t the measured width
|
Codex Review: Didn't find any major issues. You're on a roll. 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. Chef's kiss. 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. Already looking forward to the next diff. 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". |
… installed (unslothai#16126) ## What was wrong `test_twins_completed_by_a_supplied_component_skip_the_early_download` (added in unslothai#16080) fails in Backend CI's `(Python 3.13, a-f)` shard on main and on every PR: that shard installs transformers but not diffusers, so `_class_pins_fp32("diffusers", "SD3Transformer2DModel")` cannot import the class and conservatively reports an fp32 pin, `plan_fp16_twins` returns None, and `local_fp16_variant` answers None instead of `"fp16"`. The fp32 pin is not what the test checks (it checks that a supplied component completes the twins and skips the early download), so the test now stubs `_class_pins_fp32` to False, the way a diffusers class without `_keep_in_fp32_modules` answers. ## Testing - Shard environment (transformers 5.17.0, no diffusers): main `1 failed, 34 passed, 1 skipped` (this test); this branch `35 passed, 1 skipped`. - With diffusers 0.42.1 and transformers 5.22.1: 36 passed on both. - Negative control with the stub in place: the same snapshot answers `fp16` with the VAE supplied and `None` without it, so the test still fails if the supplied-component handling regresses. - Main's own Backend CI `(Python 3.13, a-f)` failed on this test (job 124087281866), as did `perf/r10-gemma3-decode` (124086773561) and unslothai#16021 (124087032249).
|
@codex review |
|
Codex Review: Didn't find any major issues. Breezy! 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". |
What was wrong
The MLX memory estimate only charged an attention score matrix when the KV cache was quantized. With the default full-width (f16) cache, mlx 0.32.4 still falls back to its unfused attention whenever it has no fused kernel for the call, and that fallback builds the full
[heads, chunk, keys]scores (mlx/fast.cpp,scaled_dot_product_attentionfallback;ScaledDotProductAttention::use_fallbackinmlx/backend/metal/scaled_dot_product_attention.cpp). On Macs without neural accelerators this is every head_dim 256 or 192 prefill chunk except the square causal one, so Qwen3.5 / Qwen3.6 / Qwen3-Next class models were priced as if long prompts cost nothing extra, and the fitted context could be one the prompt cannot actually fill.For
prism-ml/Ternary-Bonsai-27B-mlx-2bit(the model in #15935) with the default cache, the compute buffers were 0.94 GiB at any context.What changed
_compute_bytesasks unsloth-zoo'sfused_kernel_exists(the same routing table the quantized path already uses, and what mlx's fused SDPA really accepts) which full-width calls fuse: the whole prefill chunks, the shorter tail, and the decode step, each with the key count it really sees. The charge is the widest unfused call over every prompt length the context admits (a context is a ceiling, so a shorter prompt whose tail does not fuse counts too, and the total never falls as the context grows), at the queries' width.has_fused_kernel/use_fallbackin mlx 0.32.4).head_dimneed not be the width of their full-context layers (Gemma 4's global layers are 512 wide, and mlx 0.32.4 blocks those), so pricing them needs per-layer geometry this estimate does not have.Effect
Real configs, priced on main vs this branch (real MLX 0.32.4, current unsloth-zoo, no NAX = M1 to M4), sparse local snapshots with the real safetensors headers so weights and KV bytes match the recorded values exactly:
Measured on Apple Silicon (GitHub macOS 15 and 26 runners, mlx 0.32.4 on
Device(gpu, 0), no NAX), one prefill attention call, peak above its inputs over one bf16 copy of[heads, queries, keys]:For #15935's model (Ternary-Bonsai-27B, M1 to M4), compute buffers with this PR and #15970: f16 KV 0.94 -> 4.69 GiB at 32K and 0.94 -> 30.93 GiB at 262K; 8-bit KV 6.94 -> 4.69 GiB at 32K and 48.92 -> 30.93 GiB at 262K.
Testing
test_a_full_width_cache_pays_for_the_widest_call_mlx_does_not_fuse(all fused / M1 to M4 routing / NAX routing with the 1023-token tail),test_the_scores_charged_never_fall_as_the_context_growsandtest_a_windowed_model_and_an_unreachable_zoo_keep_the_old_pricestubmlx.coreand the zoo wrapper, so they run in Linux CI; the first two fail on main.test_a_full_width_cache_is_charged_the_scores_mlx_builds_below_naxagainst the real zoo routing; fails on main.test_a_real_checkpoint_is_priced_to_the_byte: tiny Llama 690_952_857, Molmo 1_282_888_719, and the Qwen3.6 row adds 2_682_961_960 below NAX or 1_298_923_560 on NAX (computed with the real zoo table,nax_availableflipped). Deltas reproduced exactly on the sparse snapshots.test_the_decode_step_still_scores_where_that_is_the_wider_of_the_twonow uses 256 heads, so the decode scores stay wider than the copy at the measured width.Device(gpu, 0)): the new tests pass; the only failures are the 6 MLX route tests main already fails, which Studio MLX estimate: ask the quantized-attention route about a cache longer than the chunk #15970 fixes.tests/test_memory_estimate.py,tests/test_mlx_inference_backend.py,tests/test_multi_model_primary_fit.pywith real MLX on Linux: same failures as main (all environment-specific: byte values recorded on a Mac), plus the new tests passing. Merged with Studio MLX estimate: ask the quantized-attention route about a cache longer than the chunk #15970 the two changes combine cleanly and 8 more tests pass.Related to #15935 (the reporter's estimate with the default cache).