Repository navigation
Studio GGUF tool turns: overlap each round's two prompt counts and reuse the reservation's native count - #18931
Conversation
…ation's native count Each GGUF tool round counts its prompt twice back to back: the admission re-cost (native /input_tokens) and the preflight fit (/apply-template + /tokenize). The fit's first count now starts in a worker thread with the fit's own arguments while the re-cost runs; it only fills the count memo, which the fit reads back after a join. The reservation and round zero's re-cost send the same native body milliseconds apart. Successful native counts are now kept in a separate two-entry memo keyed on the request body and the llama-server process, so round zero reuses the reservation's answer. Failed native counts are never stored, so endpoint failover still retries.
|
@codex review |
Codex Review Summary: ✅ CompletedThis 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: 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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
…19159) 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 | --------- Co-authored-by: Lyxot <[email protected]>
A GGUF tool turn with admission on counts its whole history twice at the top of every round, one count straight after the other. The admission re-cost asks llama-server's native
/v1/chat/completions/input_tokens, then the preflight fit renders the same conversation with/apply-templateand counts it with/tokenize. On a 10 round turn that is 12 native counts (the reservation plus 11 re-costs) and 21 render counts, all of the full prompt. Two of these are pure waits:prefer_nativecalls, so round zero pays for the whole native count again before the first token.Change
generate_chat_completion_with_tools: when a round will run the preflight fit and has a re-cost to overlap with, the fit's first count starts in a worker thread just before the re-cost, with the same arguments the fit passes (the fit's count is now one helper,_fit_count_tokens, which both call; the round's tool list and reasoning kwargs are likewise one helper each). The thread only fills the existing count memo. The loop joins it right before the fit, which then makes its own call and reads the stored number back. If the prefetch fails or priced something different, the fit counts exactly as before. The re-cost, the iteration cap, the cancel check and every event keep their order. A cancel during the re-cost still returns at once without waiting for the thread.count_chat_tokens(prefer_native = True): a successful native count is kept in its own two entry memo keyed on the exact request body plus the llama-server process (URL, model, process object and pid, date), the same scope as the render memo. A respawn, a model switch or different messages miss. Only native answers are stored: a failed or refused native call still falls back to the render path every time, so endpoint failover is unchanged.should_abortis still checked before a hit. The native memo is separate so per-round re-costs (which never repeat) do not evict render counts.Results
Colab CPU high-RAM VM (8 vCPU, load 0.8), Studio's prebuilt llama.cpp b11643 on CPU, Qwen3-0.6B Q4_K_M. One llama-server is shared by every arm. Each arm is its own process importing its own tree (merge base c7b1865, the same tree again as an A/A arm, and this branch), interleaved in 6 rotated blocks with 2 measured turns each (12 per arm per size). Each turn is a real 10 round tool loop through
_reserve_counted_gguf_chat,_openai_llama_admission_recost(wired like_gguf_recost) andgenerate_chat_completion_with_toolswithcontext_overflow = "truncate_oldest". Only generation is faked, so the numbers are counting time. Qwen3-0.6B tops out at a 40960 token window, so the largest size is 30k tokens.Time from the reservation count to the first generation request (median, min to max):
Top of each later round, re-cost start to the fit's first count done (median of rounds 1 to 10 per turn, then median over turns):
Whole 10 round turn (counting only): 3748 / 3716 / 2768 ms at 30k, 2295 / 2293 / 1730 ms at 15k. On the same server a native count alone takes 81 ms at 30k, a render count 92 ms, the two back to back 172 ms and the two at once 95 ms.
Server calls per turn: base 12 native + 21 render, head 11 native + 21 render (the fit never renders a prompt the prefetch already rendered). With the same inputs, the generation request bodies, the yielded events and every admission
wantare identical between base and head, on that llama-server and on a fake server with fixed counts at 30k tokens.Tests
tests/test_tool_round_count_reuse.py(new, 15 tests): the prefetch fills the memo and the fit renders nothing twice; a turn with the prefetch disabled sends the same requests and events; a failed prefetch falls back to the fit's own count; no prefetch without a re-cost or a fit; a repeated native count is served from memory; a hit still honoursshould_abort; a restart, respawn, model change, different messages or tools miss; a failed native count is asked again; a server without the native endpoint keeps failing over; native counts never evict render counts. 5 of them fail on the merge base, as expected.The 162 existing test files that touch
count_chat_tokens,_gguf_recost,_fit_context, admission or the tool loop were run on base and head and the failing test IDs compared. One test failed only on head:test_exact_count_endpoint_failover_preserves_short_chat_concurrency[legacy]asserted three native calls for three identical chats while the native endpoint works. Those chats now share the first answer, so that case accepts one to three calls (they race); every failure case still asserts three, which is the failover guarantee. All other failures are the same IDs on both sides (host environment, unrelated to counting). The new file,test_llama_count_memo.pyandtest_gguf_admission_token_count.pypass three runs in a row.