Skip to content

feat(studio): bring MLX Run settings up to GGUF parity - #18426

Merged
danielhanchen merged 15 commits into
unslothai:mainfrom
Lyxot:mlx-run-settings-parity
Nov 19, 2026
Merged

danielhanchen merged 15 commits into
unslothai:mainfrom
Lyxot:mlx-run-settings-parity

Conversation

@Lyxot

@Lyxot Lyxot commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Unsloth Studio's Run settings offered GGUF models controls that MLX models either lacked or accepted and silently ignored. This PR wires every Run setting MLX can honour through the load path end to end: the Run settings panel, the load request, the memory estimate, chat admission, the resident-model match and OpenAI-compatible API overrides. Controls that only make sense for llama-server stay hidden for MLX: GPU layers and tensor split, mmap/mlock, checkpoints and extra server arguments.

The Vision-off memory saving needs unslothai/unsloth-zoo#2600. With an older unsloth-zoo the new load argument is ignored. Vision off then stops image input but frees no memory.

Changes

  • Vision.
    • Turning Vision off on an MLX VLM stops image input.
    • It also asks unsloth-zoo not to read the vision tower and projector at all (drop_vision_weights). Where the load cannot skip them, it frees them after the load (both need the unsloth-zoo change).
    • The memory estimate, offload plan and context fit price such a load without those weights.
    • Adapters and audio-input models keep vision.
  • Reasoning Budget and its message.
    • Every MLX reply path (mlx-lm, mlx-vlm, batched rows) adds a logits processor. Once the budget is spent inside the reasoning block, it forces the message and closes the block, as llama-server's --reasoning-budget does. A budget of 0 closes the block as soon as it opens.
    • A budgeted mlx-vlm reply decodes without the drafter, because the processor cannot follow a draft.
  • Micro-batch Size (n_ubatch).
    • Sets the MLX prefill step (tokens per prefill forward) on every path.
    • The mlx-vlm prompt-cache grid follows it per load, so prompt reuse stays shape-stable.
    • With experts read from disk, the offload planner's chunk caps it, and the estimate prices the same step.
  • Batch Size (n_batch). Caps the tokens one batched prefill forward takes across requests, like llama.cpp's logical batch. Unset keeps the current behaviour of prefilling every waiting row at once.
  • Cache RAM.
    • The MLX prompt cache was fixed at 15% of the Metal working set, and it comes out of the memory the context fit uses. Its size is now selectable:
      • blank keeps the default;
      • 0 disables the cache;
      • llama.cpp's -1 (unlimited) loads as the default, because MLX shares one memory pool with the model.
    • A cache that takes the whole budget now reports that nothing fits, instead of skipping the fit.
  • Applying settings in place. A load that changes only the reasoning budget, Batch Size or a smaller prompt cache updates the loaded worker instead of reloading the weights. It extends the existing in-place context narrowing path (Studio: faster MLX load and reload, ready to serve when loaded #16536).
    • A larger prompt cache or any other setting still reloads, because it changes memory the load gave its context.
    • The retune also refreshes the batching capabilities a reasoning budget decides.
  • Echo and reload rules. The MLX load echoes each setting, so a reselected configuration matches the resident model. A changed setting reloads unless it can be applied in place.

Risks

  • The memory estimate is priced on the assumption that the unsloth-zoo change is present. With an older unsloth-zoo, a Vision-off estimate can be lower than what the load actually holds.
  • A budgeted mlx-vlm reply gives up speculative decoding, by design.

Validation

Peak memory of a load with Vision off:

Model Vision on Vision off
Qwen2.5-VL-3B 2.86 GiB 1.62 GiB
LFM2.5-VL-1.6B 1.39 GiB 0.61 GiB
GLM-4.6V-Flash 6.31 GiB 4.65 GiB

The memory estimate is within 0.01 GiB of the measured peak.

Live /load timings on Qwen3.5-0.8B-4bit:

Change Path Time
Full load fresh 14.2 s
Reasoning budget, Batch Size or a smaller Cache RAM in place 0.9 to 2.7 s
Smaller context and a budget in place 0.9 s
Larger Cache RAM or a Micro-batch change full reload 12 to 14 s

Cache RAM checked live:

  • Cache RAM 0 disables the cache.
  • 512 MiB gives a 0.54 GB cache budget.
  • The panel hydrates from the loaded model, and Reload stays disabled while the settings match.
# backend
python -m pytest -q studio/backend/tests/test_memory_estimate.py studio/backend/tests/test_mlx_inference_backend.py studio/backend/tests/test_mlx_load_warm_and_narrow.py studio/backend/tests/test_multi_model_primary_fit.py studio/backend/tests/test_openai_auto_switch.py
# frontend
node --experimental-strip-types --test tests/chat-load-request-one-builder.test.ts tests/non-gguf-parallel-slots-baseline.test.ts tests/resident-config-match.test.ts tests/tensor-parallel-row-gating.test.ts tests/vision-toggle-gating.test.ts
npm run build

UI before / after

Run settings for unsloth/Qwen2.5-VL-3B-Instruct with Advanced settings on, merge base 14111b6 (BEFORE) vs PR head 304f61e (AFTER). These rows only render on Apple Silicon, so the platform response (/api/health device_type) was set to mac on both sides identically. AFTER adds Vision, Batch Size, Micro-batch Size, Cache RAM, Reasoning Budget and Reasoning Budget Message.

MLX Run settings before and after

Run settings show Vision for MLX models. Turning it off stops image input; where unsloth-zoo builds the text model without the tower, the load also skips the vision weights. Adapters and audio-input models keep vision. The MLX load reports disable_vision and vision_disabled_by_user, and a changed setting reloads.
Run settings show Reasoning Budget and its message for MLX models. The MLX load keeps the pair and every reply path (mlx-lm, mlx-vlm, batched rows) appends a logits processor that, once the budget is spent inside the reasoning block, forces the message and the block's closer, as llama-server's --reasoning-budget does. 0 closes the block as soon as it opens. The processor cannot follow a draft, so a budgeted mlx-vlm reply decodes without the drafter. The load echoes the pair and a changed value reloads.
Run settings show Micro-batch Size for MLX models. An MLX load takes n_ubatch as its prefill step, tokens per prefill forward, on the mlx-lm, mlx-vlm and batched paths. The mlx-vlm prompt-cache grid follows it per load, so every request of the load prefills and banks on one grid and reuse stays shape-stable. With experts read from disk the offload planner's own chunk caps it, since memory was priced there, and the memory estimate prices the same chunk. API overrides forward it to MLX loads; a changed value reloads.
A Vision-off MLX load asks unsloth_zoo to never read the vision tower and projector (drop_vision_weights), falling back to freeing them after the load on an older unsloth_zoo, and records the bytes in vision_weights_freed. The memory estimate, offload plan and context fit price such a load without those weights, so the Run settings estimate matches what the load holds.
On MLX the prompt cache was fixed at 15% of the Metal working set (overridable only by UNSLOTH_MLX_PROMPT_CACHE_BYTES) and comes out of the memory the context fit uses. Run settings now show Cache RAM for MLX loads: blank keeps the default, 0 disables the cache, and llama.cpp's -1 (unlimited) loads as the default. The load, the memory estimate, chat admission and the resident match all use the chosen size, and a cache that takes the whole budget now reports that nothing fits instead of skipping the fit.
…ompt cache in place

A load that changes only these settings on the held MLX model now updates the running worker instead of reloading the weights, extending the in-place context narrowing path. A larger prompt cache, or any other load setting, still reloads, since it changes memory the load gave its context. The retune also refreshes the batching capabilities a reasoning budget decides.
@Lyxot

Lyxot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T10:28:37.675796Z 304f61e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cb549fef4a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

…mar and apply it to audio replies

A JSON grammar that allows no reasoning masks the closer a spent budget would force, leaving no token to sample, so the budget no longer applies there. Audio-input replies on mlx-vlm now honour the load's budget like the other reply paths.
@Lyxot

Lyxot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8bdb55837

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

When a grammar that allows reasoning reaches its own prelude cap it permits only the closer; a budget forcing its message at the same step left no token to sample. The budget now yields when the token it would force is already masked.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…and keep MLX settings after autoload

A retune that does not narrow the context now declines when the checkpoint files changed since the load, as narrowing does. Autoloading an MLX model mirrors the settings it applied (reasoning budget, batch sizes, Cache RAM, Vision) instead of resetting them, so a later reload does not silently revert them.
@Lyxot

Lyxot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c8819d1cdb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

…to speculation

A Vision-off load that builds the text model looks for no automatic drafter; the estimate now classifies the route the same way instead of charging a drafter the load never attaches.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

An image row admitted to an mlx-lm batch prefilled at unsloth-zoo's default step, so the load's Micro-batch Size and the expert-offload chunk the estimate prices did not bound it.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 18c4ef9169

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen danielhanchen self-assigned this Nov 19, 2026
…warding

Non-GGUF model overrides now forward the batch sizes and Cache RAM, which MLX loads read, and the autoload harness stubs the MLX tuning helper the success branch uses.
@Lyxot

Lyxot commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9a61b4242

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: d9a61b4242

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9a61b4242

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Comment on lines +1914 to +1916
ariaLabel="Prompt micro-batch size"
value={config.nUbatch}
onChange={(nUbatch) => update({ nUbatch })}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include the loaded MLX micro-batch in resident memory estimates

For an MLX model loaded with a non-default micro-batch, the prospective memory estimate honors this setting, but the separate resident estimate used for reclaimable-memory credit does not. MLX clears loadedGpuMemoryMode, so selectResidentEstimateSettings() returns null, and resolveResidentEstimateRequest() reconstructs only the context, KV quantization, and offload settings. A model using a 512-token step is consequently credited with the compute footprint of the default step, inflating the memory reported as reclaimable and potentially suppressing a reload's memory-pressure warning. Pass the loaded micro-batch explicitly through the MLX resident-estimate path rather than relying on the unavailable GGUF baselines.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Estimate accuracy only: the reclaimable credit for a resident model differs by one prefill step's compute buffer, and nothing loads or fails because of it.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 19, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 19, 2026
danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Nov 19, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 304f61e938

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Nov 19, 2026
danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Nov 19, 2026
@danielhanchen
danielhanchen merged commit d08712b into unslothai:main Nov 19, 2026
danielhanchen added a commit that referenced this pull request Nov 19, 2026
…ng, MLX batch sizes, QAT CUDA skip) (#18471)

## Summary

Four tests fail on `main` in the "Repo tests (CPU, studio)" shard, so
every PR that runs it is red (seen on the staging runs of #18353 and
others). Reproduced on `main` at `415cdcf1be`:

```
FAILED tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe
FAILED tests/studio/test_model_picker_contracts.py::test_batch_sizes_setting_wired_end_to_end
FAILED tests/studio/test_model_picker_contracts.py::test_batch_sizes_reach_an_api_load_through_the_server_mirror
FAILED tests/studio/test_chat_ui_picker_collapsed_row.py::test_a_loaded_model_still_opens_the_picker_collapsed
```

Each was attributed to the PR that changed the code it reads, and each
was checked for whether the PR or the test is wrong.

## Root causes and changes

### Picker collapsed after a load (#18445): test was wrong

#18445 added the "Show full model list" setting, so the load effect now
calls `setExpanded(useChatRuntimeStore.getState().pickerExpandLoaded)`
instead of a literal `?? false`. The setting defaults to off, so a fresh
browser (every CI driver) still opens the picker collapsed, which is
what this test protects: the drivers' expand click is still needed.

`test_a_loaded_model_still_opens_the_picker_collapsed` now accepts
either form, and when the setting drives it, also asserts the store
default is `loadBool(PICKER_EXPAND_LOADED_KEY, false)`. Flipping that
default to `true` fails the test, which the old literal check could not
see.

### Batch sizes sent on a load (#18426): test was wrong

#18426 brought MLX up to GGUF parity, so `n_batch` / `n_ubatch` are now
sent for `(target.isGguf || mlx)` and copied for MLX overrides too. GGUF
behaviour is unchanged; the tests pinned the old GGUF-only literal.

- `test_batch_sizes_setting_wired_end_to_end`: matches the gate with a
regex that allows widening to MLX but still requires `target.isGguf` and
"only when set". Dropping the gate (sending the fields to every engine)
fails it.
- `test_batch_sizes_reach_an_api_load_through_the_server_mirror`:
replaces the source-text check on the GGUF block with a behavioural
check through `_override_load_kwargs`, which the test already imports.
Dropping `n_ubatch` from the copied keys fails it.

### QAT stream test skip guard (#18434): PR was wrong

`tests/test_qat_weight_memo_guards.py` gated
`test_cached_weight_not_served_to_another_stream` on
`torch.cuda.is_available()`, which `tests/_zoo_aggressive_cuda_spoof.py`
patches to `True` process-wide, so on a CPU runner the test un-skips and
calls `.cuda()` when it shares a session with the spoofing suites. The
existing guard caught this. The skip now gates on `has_real_cuda()`
first, as the other CUDA-only tests do.

## Testing

- The four tests plus their files: `241 passed, 1 skipped` on this
branch; the same four fail on `main`.
- Sabotage, each against this branch:
- store default `pickerExpandLoaded` set to `true`: the picker test
fails;
- `n_batch` sent without the GGUF/MLX gate: the end-to-end batch test
fails;
- `n_ubatch` dropped from the backend override copy: the server-mirror
test fails;
  - QAT skip guard reverted: the skip-guard test fails.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants