Repository navigation
fix(server): force the K2-Horizon budget close matching the opened variant - #3330
Merged
Merged
Conversation
…d variant K2-Horizon's template primes `<ifm|think>`, `<ifm|think_fast>` or `<ifm|think_faster>` by `reasoning_effort`, but an exhausted `thinking_budget_tokens` cap or a `reasoning_end` control request always forced `</ifm|think>`. `ThinkingTokenIds` now carries the variant opens next to their closes, and `ThinkingState` remembers the close that pairs with the open that started the block: from a model-emitted open in `observe`, or from the primed close through `with_primed_close`. Both forcing paths emit that close and fall back to the pair's own close when it is unknown. The routes pass the primed close marker to the scheduler as `ServerGenerateOptions::thinking_primed_close` (chat completions and the `mlxcel chat` / `run` CLI through `prepare_chat_generation`, the router front, responses, responses compaction, Anthropic messages and ASR chat), and `build_thinking_state` resolves it to a token id. The disaggregated handoff keeps forcing the pair's close. Also replace the stale ignored `rollback_speculative_cache_panics_on_ragged_batched_accepts` test: since #2442 a ragged batched accept compacts each row instead of panicking, so the new test checks each row's conv window and gated-delta state against a B = 1 rollback of that row and the attention offset against `max(accepted) + 1`. Closes #3321
Move the scheduler's marker-to-id step into `thinking_budget::resolve_primed_close_id` and test it per effort, unprimed and multi-token. Add edge-case tests for a `--reasoning-budget-message` run before the variant close, a zero budget on a primed variant, and a model's own matching or mismatched close. Correct the docs: the router front feeds the disaggregated handoff, which does not carry the primed close yet, so a primed variant there still gets `</ifm|think>`. Note why a variant open with no close in the vocab opens nothing.
English and Korean report for the K2-Horizon variant budget close and the Qwen3.5 ragged-rollback test, including the real-checkpoint run on k2-horizon-0.9b.
inureyes
force-pushed
the
fix/issue-3321-k2-horizon-variant-close
branch
from
November 23, 2026 08:46
37f9b38 to
7e7e7f3
Compare
inureyes
marked this pull request as ready for review
November 23, 2026 08:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two follow-ups from #3303 and #3304, in one PR as the issue asks.
1. K2-Horizon budget close matches the opened variant
K2-Horizon's template primes
<ifm|think>,<ifm|think_fast>or<ifm|think_faster>byreasoning_effort(high, medium, low). An exhaustedthinking_budget_tokenscap or areasoning_endcontrol request always forced</ifm|think>, so amediumorlowblock was closed with a tag the model never saw paired with its open.ThinkingTokenIdsgainsalt_opens, index-paired withalt_closesand filled from the sameTHINK_EFFORT_VARIANTSentries, plusclose_for_open.ThinkingStatekeeps a privateactive_close.observesets it when the model emits the pair's open or a variant open (a variant open now entersInBlocktoo), andwith_primed_closesets it for a primed prompt, accepted only whenids.is_close(close). Both forcing paths forceactive_close.unwrap_or(ids.close), so the--reasoning-budget-messagerun is unchanged and still precedes the close.ServerGenerateOptions::thinking_primed_closecarries theprimed_open_thinking_close_markervalue the routes already compute. It is set whereverthinking_enter_block_on_startis set from the primed prompt:prepare_chat_generation(/v1/chat/completionsand themlxcel chat/mlxcel runCLI throughInProcessServer::chat), the router front,/v1/responses(run and non-streaming), responses compaction, Anthropic/v1/messages(streaming and not) and the ASR chat path.build_thinking_stateresolves the marker to an id through the newthinking_budget::resolve_primed_close_id(the tokenizer'stoken_to_id, only when the prompt primed the block; a multi-token close resolves to nothing and falls back to the pair's close).build_handoff_thinking_stateand thekv_cache_serdewire format). The router front sets the field, but the prefill frame does not carry it, so a primed variant on that path still gets</ifm|think>; a comment at the assignment,docs/supported-models.mdand the CHANGELOG say so.2. Stale Qwen3.5 ragged-rollback test
rollback_speculative_cache_panics_on_ragged_batched_acceptsexpected a panic that #2442 removed. It is replaced byrollback_speculative_cache_compacts_ragged_batched_accepts: rows[1, 2, 3, 4]and[5, 6, 7, 0]verified with accepts[3, 1]; each row's linear-layerconv_stateandstate_cachemust match a B = 1 rollback of that row with its own accept, and the attention offset must bemax(accepted) + 1= 4. The sharedverify_and_rollbackhelper now checks the returned count againstmax(accepted), which is whatrollback_speculative_cachereturns for both uniform and ragged accepts.Tests
server::k2_horizon_reasoning_tests::a_primed_budget_forces_the_close_of_the_primed_variant: forhigh,medium,low, a primed state with a budget of 2 built throughwith_primed_closeforces that effort's own close, and so does areasoning_endflag.a_model_emitted_variant_open_is_closed_with_its_own_close: a non-primed state that observes each open entersInBlockand forces the matching close at the cap.an_unrelated_primed_close_falls_back_to_the_pair_close.the_primed_close_marker_resolves_to_its_token_id: each effort's close resolves, an unprimed prompt and a multi-token close resolve to nothing.variant_close_edge_cases: a--reasoning-budget-messagerun precedes the variant close, a zero budget forces the variant close at once, and a model's own matching or mismatched close is let through.chat_completions_pass_the_primed_variant_close_to_the_scheduler: the real chat route, streaming and not, hands the scheduler the primed variant's close for each effort.server::thinking_budgettests (Qwen, Gemma 4,<|content_thinking|>pairs) pass unchanged.Verification
Narrow equivalents of the issue's Verification block (the broad gate is left to the orchestrator):
cargo fmt --all -- --check,cargo clippy --lib --tests --features metal,accelerate -- -D warnings: clean.server::thinking_budget server::k2_horizon_reasoning_tests server::routes::chat: 131 passed.models::qwen3_5_tests --ignored --test-threads=1(GPU lock): 7 of 7 passed.coverage_tests: 18 passed; the seven CI contract scripts pass.mlxcel-server --skip-chat-parsingonmodels/k2-horizon-0.9b, greedy,thinking_budget_tokens: 6:highends the reasoning with</ifm|think>after six tokens,mediumwith</ifm|think_fast>after six tokens, andlowcloses on its own with</ifm|think_faster>at the first token. With parsing on, the budgeted tokens land inreasoning_contentfor every effort.Technical report:
TECHNICAL_REPORTS/3330-k2-horizon-variant-close-20261123.en.mdand.ko.md.Closes #3321