Skip to content

Studio: reuse the native prompt count in a GGUF chat's context fit - #19159

Merged
Lyxot merged 5 commits into
unslothai:mainfrom
NilayYadav:fit-reuses-native-count
Nov 24, 2026
Merged

Lyxot merged 5 commits into
unslothai:mainfrom
NilayYadav:fit-reuses-native-count

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Every GGUF chat turn counts the same prompt twice: once for the admission reservation and once for the context fit. Both return the same number. #18931 caches the native count, but the fit never reads that cache, so it still renders and tokenizes the whole prompt every turn.

The fit now reuses the cached native count when it would render the exact same request. Anything different is still counted fresh, and a failed native count is never cached.

RTX 5000 Ada, Qwen3-4B-Instruct-2507 Q4_K_M:

main this branch
llama-server counts per turn 2 1
Time to first token, 7.4k token chat 176 ms 132 ms
Time to first token, 21.6k token chat 322 ms 220 ms
Time to first token, 48k token chat 539 ms 387 ms

@Lyxot Lyxot self-assigned this Nov 24, 2026
…nd native counts

Both count paths now compute the native key once and look it up before the native call; the miss test also covers a changed system prompt and template options.
llama-server renders tool schema keys in the order the request sends them, so reordered parameters change the prompt and its count (267 vs 266 tokens on Qwen3.5). Sorting keys made both orders share one memo entry, letting the context fit reuse a count for a different prompt.
…e count

A render count hitting the native memo returned before any cancellation check, so a cancelled turn's context fit could take the cached count and go on to trim and archive. Poll should_abort before the shared lookup for every caller, and reorder nested schema keys in the key-order miss test.
@Lyxot

Lyxot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Pushed three small follow-ups on top of the original commit, plus a merge of the latest main:

  • cb3cea5f2be refactor: the render and native counts now share one native memo lookup instead of computing the same key in two branches. No behavior change.
  • 180b534039c fix: _count_memo_key no longer sorts JSON keys. llama-server renders tool schema keys in the order the request sends them, so reordered parameters are a different prompt with a different count (267 vs 266 tokens on Qwen3.5-0.8B with the shipped llama-server). With sorted keys both orders shared one entry, and now that the fit reads the native memo it could trim against the other prompt's count.
  • c06bf8b182b fix: should_abort is polled before the shared lookup for every caller. Before, a render count that hit the native memo returned without a cancellation check, so a cancelled turn's context fit could take the cached count and go on to trim and archive.

Validation: on main, the native and rendered counts matched exactly in 40/40 real-server cases (Qwen3.5-0.8B and gemma-4-E2B, with and without mmproj; tools with tool calls and results, thinking on/off, reasoning effort, continue_final_message, tool_choice, response_format, non-ASCII, ~50k tokens). Image turns never use the native count, so they are unchanged. Through the real Unsloth Studio backend with context_overflow=truncate_oldest (what Unsloth Studio chat sends), each turn drops from native + /apply-template + /tokenize to the native count alone, and first-token time went from 56 to 48 ms (7k tokens), 104 to 83 ms (20k) and 200 to 154 ms (48k) on Apple Silicon. The saving applies to Unsloth Studio chat turns, which run the context fit; API calls without overflow handling never made the second count.

@Lyxot

Lyxot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 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-24T06:24:09.705789Z 622df10 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. What shall we delve into next?

Reviewed commit: 622df10aa9

ℹ️ 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 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Codex converged on 622df10aa9 (current head): #19159 (comment)

CI: upstream runners were backed up, so CI ran on a staging copy with the same head and base (now closed): Lyxot#280. 46 jobs passed. The 5 failures are unrelated to this change: the same 10 tests (Spark-TTS codec handoff, the diffusion HIP gate guard, the stub-heavy-imports guard) also fail on current main, and the Core zoo job died in torch worker aborts inside the unsloth_zoo suite, which this Unsloth Studio-only change does not reach. Its sibling zoo job was cancelled by fail-fast.

Changed during this workflow: merged the latest main into the branch (it was 53 commits behind), plus the three follow-up commits described above (shared native memo lookup, an order-sensitive memo key, and should_abort before a native-memo hit). The saving applies to Unsloth Studio chat turns, which use truncate_oldest and run the context fit, not to every GGUF API call.

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

LGTM. Thanks!

@Lyxot
Lyxot merged commit 74a4ecc into unslothai:main Nov 24, 2026
52 of 57 checks passed
Hubert9204 pushed a commit to Hubert9204/Ganjalf-Studio that referenced this pull request Nov 24, 2026
…-11-23..25)

Merge of unslothai/unsloth main 2e1afd5..bd1ddf2. No textual conflicts; audited for
Ganjalf invariants (Claude API alignment, thinking budget, Qwen thinking presence 0,
check_page, guards, plugins/hooks/run_agent stop, branding via messages.ts, Ganjalf updater):
all intact.

Taken: llama.cpp/GGUF fixes (thinking budget counted with the real template unslothai#19143, native
prompt count in context fit unslothai#19159, host RAM growth priced before pinning unslothai#19231, second quant
of a loaded repo unslothai#19169, small Auto context on low-RAM CPU hosts unslothai#19142), security and
stability hardening (media bytes checked, remote-code gate, sandbox cache, managed accounts,
IPv6 loopback listeners), chat fixes (Files menu, deleted turns from other tabs, composer,
tool approvals tied to their tool), MCP, chat templates (phi-3, mistral), training/core
kernels, installer AMD/ROCm mixed hosts, Tauri security bumps, unsloth-zoo>=2026.11.11.
Branding stays Ganjalf (upstream strings are rebranded at runtime by i18n/messages.ts).

Ganjalf follow-up: the thread sampling simulation oracle expects presence 0 for Qwen
3.5/3.6 with thinking (non-thinking 1.5), as the chat table does since 369d05c.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EVYBhJeMQGvLT6Ka4nMMUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants