Skip to content

Stop unsloth chat from reloading a model the server already has - #10257

Merged
Lyxot merged 6 commits into
unslothai:mainfrom
NilayYadav:fix-cli-context-default
Sep 4, 2026
Merged

Lyxot merged 6 commits into
unslothai:mainfrom
NilayYadav:fix-cli-context-default

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Running unsloth chat while Studio is open tears down the model Studio already has loaded and starts it again with a 4096 token context, whatever the model actually supports. The CLI prints that the model stays warm while it does this. The cause is that --max-seq-length defaults to a fixed number, so the request never matches the running server.

unsloth start was fixed for this in #10093 and now defaults to 0, meaning use the model default and only ask for a context when the user names one. chat and inference were left as they were. This gives them the same default and says so in the help text.

Tested against a running Studio. Before, the server process is killed and comes back at 4096. After, the same process is still there with its original context and answers normally.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 3, 2026
@NilayYadav

NilayYadav commented Sep 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

GitHub Actions reproduction — before/after

Staged on fresh upstream main (8e26d2c2b); the PR still merges cleanly onto it. Two branches ran the same workflow, probe and dependencies and differ only in unsloth_cli/commands/chat.py and unsloth_cli/commands/inference.py.

The probe is end-to-end on real code, with no model download and no torch: it stands a loopback endpoint that passes the CLI's real /api/auth/identity HMAC check, runs the real unsloth inference with no --max-seq-length, records the actual POST /api/inference/load body, and feeds that body's max_seq_length into the real LlamaCppBackend._runtime_matches_intent against a resident model loaded the way unsloth start loads one (_requested_n_ctx = 0).

Fix reverted — expected-failing run:

unsloth studio run     --max-seq-length default = 0
unsloth chat           --max-seq-length default = 4096
unsloth inference      --max-seq-length default = 2048

POST /api/inference/load payload: {"hf_token": null, "load_in_4bit": true, "max_seq_length": 2048, "model_path": "fake-model", "tensor_parallel": false}
resident server _requested_n_ctx: 0
LlamaCppBackend._runtime_matches_intent -> False
server would: RELOAD the model
FAIL: the CLI asked for n_ctx=2048 against a resident 0, forcing a reload

PR head — passing run:

unsloth studio run     --max-seq-length default = 0
unsloth chat           --max-seq-length default = 0
unsloth inference      --max-seq-length default = 0

POST /api/inference/load payload: {"hf_token": null, "load_in_4bit": true, "max_seq_length": 0, "model_path": "fake-model", "tensor_parallel": false}
resident server _requested_n_ctx: 0
LlamaCppBackend._runtime_matches_intent -> True
server would: REUSE the warm model
PASS: attaching does not reload the model the server already has

The same passing run also runs the repo's own tests: 3 passed for the chat/inference forwarding tests, 1 passed for the default-tracks-studio run test, and 1182 passed, 6 skipped for the full unsloth_cli/tests suite.

That last one matters here: upstream Repo tests (CPU) is red on this PR for reasons unrelated to it — tests/studio/test_update_release_notes.py fails the same 4 tests on main itself at 8e26d2c2b (they pass at b319ba383, and main's tip is #10229, which changed the banner classes those tests assert). Because that step fails earlier in the same job, the job's own CLI tests (unsloth_cli) step is reported as skipped, so upstream CI never ran the suite this PR actually touches. The run above runs it in full.

Downstream of 0, checked rather than assumed: GGUF takes n_ctx = 0 as the model default through the existing auto-fit/VRAM path (GgufLoadIntent.gpu_memory_mode defaults to auto); hub models are normalised at studio/backend/core/inference/inference.py:346 (if max_seq_length <= 0: max_seq_length = 2048), which is what the new help text states; MLX resolves 0 to the native window via _positive_int(max_seq_length) or 0. unsloth chat and unsloth inference are the only commands that call connect_studio_server, so unsloth export's separate 2048 default is deliberately untouched.

Two fixes were pushed onto this branch after the original commit: unsloth_cli/tests/test_inference_chat.py still asserted the old 4096/2048 defaults, so test_chat_forwards_gguf_runtime_options_to_loader and test_inference_forwards_gguf_runtime_options_to_loader failed (both run in studio-backend-ci.yml); they now assert 0 and fail on the merge base, making them the behavioural regression test for this change. The new tests/studio/test_cli_context_defaults.py was also missing the AGPL-3.0 header that every recently added file under tests/studio/ carries.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-04T07:13:54.776399Z 8fa92a6 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

Copy link
Copy Markdown

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

Reviewed commit: 57b5f61701

ℹ️ 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

Copy link
Copy Markdown
Member

Confirmed unsloth_cli/commands/chat.py still defaults --max-seq-length to 4096 and inference.py to 2048 while studio.py uses 0, so a default attach never matches a resident server. One thing before I review: for a hub model with no running server this drops the chat context from 4096 to the 2048 normalisation, could you confirm that is intended?

@Lyxot Lyxot self-assigned this Sep 4, 2026
@Lyxot
Lyxot force-pushed the fix-cli-context-default branch from 57b5f61 to 0ec66cf Compare September 4, 2026 06:55
@Lyxot

Lyxot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Pushed a revision of this branch. The fix itself is unchanged — both commands now default --max-seq-length to the same 0 sentinel unsloth studio run uses, which is what stops the attach from relaunching a resident llama-server.

What changed around it:

  • Dropped tests/studio/test_cli_context_defaults.py. It parses the command sources with ast and asserts the literal 0 is present. I mutation-checked it against the two test_inference_chat.py forwarding tests this PR already updates: reverting both defaults fails all three, so it detects nothing they don't. It would also fail a legitimate refactor that hoists the default into a shared constant, and CLI option tests live in unsloth_cli/tests/ rather than tests/studio/.
  • Added a behavior test for the no-server path, parametrized over chat and inference. That path was untested and it is where the local default actually changes, so it is worth asserting rather than inferring. It checks both directions: the sentinel reaches load_chat_backend, and an explicit --max-seq-length 8192 still gets through.
  • Reworded the comment added to test_chat_forwards_gguf_runtime_options_to_loader. "Matches whatever context a resident Unsloth server already runs" is stronger than what happens — it matches a server that also asked for nothing. A server launched with an explicit --context-length 8192 still reloads, and should: LoadRequest.max_seq_length documents 0 as "let the backend choose the context itself", and _non_gguf_runtime_settings_match notes that a --context-length 0 reset is deliberately honoured on GGUF.
  • Rebased on main and squashed to one commit, folding in the pre-commit run and the AGPL header. The message records the root cause in repo terms: LlamaCppBackend._runtime_matches_intent compares requested contexts rather than served ones, so a server started with the sentinel never matched a CLI asking for 4096.

Two notes for the description:

The 0 default on unsloth studio run predates #10093 — it came from #6154 ("Expose runtime context length for hub models"). #10093 made unsloth start apply context and quant settings when attaching to a running server, which is a related but different fix.

Worth stating explicitly that this changes the no-server path too, since the description only covers attach. Local GGUF and MLX loads improve: they now take the checkpoint's trained window instead of a pinned 4096. The transformers backend has no window to read and keeps its 2048 fallback, so unsloth chat with no server goes 4096 → 2048 there. That is a real reduction, but it aligns chat with what unsloth inference and unsloth studio run already gave on that path, and --help now states it.

Validation: unsloth_cli/tests/test_inference_chat.py 98 passed, plus test_start.py for 703 total.

`tests/studio/test_cli_context_defaults.py` parses the command sources with `ast` and asserts the `--max-seq-length` default is the literal 0. Reverting both defaults already fails the two forwarding tests in `unsloth_cli/tests/test_inference_chat.py`, so the source-shape check detects nothing they do not, and it would fail a refactor that hoists the default into a shared constant while behaviour is unchanged. CLI option tests also live under `unsloth_cli/tests/`.

Replace it with a behaviour test for the path that was untested: with no server, the sentinel reaches `load_chat_backend` and an explicit `--max-seq-length` still gets through. That is where the local default actually changes, so it is worth asserting rather than inferring.

Reword the comment on `test_chat_forwards_gguf_runtime_options_to_loader`: 0 matches a server that also asked for nothing, not whatever context a resident server runs. A server launched with an explicit context still reloads, which `_non_gguf_runtime_settings_match` documents as deliberate.
@Lyxot
Lyxot force-pushed the fix-cli-context-default branch from 0ec66cf to 4fb2ad8 Compare September 4, 2026 06:59
@Lyxot

Lyxot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Correction to my last comment: I should not have squashed your commits. I have restored the four original ones and my changes are now a single commit on top, so the branch history is yours again and only the last commit is mine. The rebase onto main is gone too — the branch is back on its original base.

Nothing about the content changed from what I described above, and the branch still ends in the same tree. Sorry for the churn.

@Lyxot

Lyxot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

@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: 4fb2ad8255

ℹ️ 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 thread unsloth_cli/commands/chat.py Resolved
The help text said 0 gives "2048 for hub models". It does on the transformers backend, whose `<= 0` fallback is that constant, but on Apple Silicon a hub model loads through `MLXInferenceBackend`, where `_resolve_context_lengths` reads the checkpoint's own window: `runtime_context_length` skips the non-positive request and falls through to `max_position_embeddings`. A reader planning around a 2048-token window, and its memory characteristics, would be wrong on that platform.

`unsloth studio run` carries the same wording and is left alone here to keep this change to the two commands the branch touches.
@Lyxot

Lyxot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 8fa92a6c11

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

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. The bug is real and the fix is the right shape.

GGUF reuse compares requested contexts rather than served ones (LlamaCppBackend._runtime_matches_intent), so a server launched with the sentinel never matched a CLI asking for 4096 — the attach tore down the resident llama-server and relaunched it while chat printed "model stays warm after /exit". The safetensors path mismatches the same way via _resident_context_satisfies. Defaulting both commands to 0 is not a workaround: it is the value LoadRequest.max_seq_length documents as "let a backend that sizes its own window choose the context itself", and it puts chat and inference on the same contract unsloth studio run already used.

Worth recording that the no-server path changes too. GGUF and MLX loads now take the checkpoint's trained window instead of a pinned 4096, which is an improvement; the transformers backend has no window to read and keeps its 2048 fallback, so unsloth chat without a server goes 4096 to 2048 there. That matches what unsloth inference and unsloth studio run already gave on that path, and --help states it.

One case deliberately left alone: a server started with an explicit --context-length 8192 still reloads when a CLI attaches with the sentinel. That is correct, not a gap — 0 means "give me the model default", and _non_gguf_runtime_settings_match documents the reset being honoured on GGUF.

CI: one red check, Repo tests (CPU), failing on tests/studio/test_model_picker_contracts.py::test_override_writes_are_ordered_per_model. It is unrelated to this diff, which touches only unsloth_cli/. That test asserts a literal TypeScript source string, and main rewrote model-overrides.ts in #10160 and #10114; this branch's base predates that. Three other tests in the same file fail on a clean main checkout with no PR involved, so the file is currently broken independently of this change. Merging the branch with current main clears the reported failure. Fixing that file is worth its own issue — string-matching assertions against frontend sources break on any formatting churn.

@Lyxot
Lyxot merged commit 0ee6cef into unslothai:main Sep 4, 2026
44 of 45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants