Skip to content

fix(server): force the K2-Horizon budget close matching the opened variant - #3330

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3321-k2-horizon-variant-close
Nov 23, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3321-k2-horizon-variant-close

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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> by reasoning_effort (high, medium, low). An exhausted thinking_budget_tokens cap or a reasoning_end control request always forced </ifm|think>, so a medium or low block was closed with a tag the model never saw paired with its open.

  • ThinkingTokenIds gains alt_opens, index-paired with alt_closes and filled from the same THINK_EFFORT_VARIANTS entries, plus close_for_open.
  • ThinkingState keeps a private active_close. observe sets it when the model emits the pair's open or a variant open (a variant open now enters InBlock too), and with_primed_close sets it for a primed prompt, accepted only when ids.is_close(close). Both forcing paths force active_close.unwrap_or(ids.close), so the --reasoning-budget-message run is unchanged and still precedes the close.
  • ServerGenerateOptions::thinking_primed_close carries the primed_open_thinking_close_marker value the routes already compute. It is set wherever thinking_enter_block_on_start is set from the primed prompt: prepare_chat_generation (/v1/chat/completions and the mlxcel chat / mlxcel run CLI through InProcessServer::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_state resolves the marker to an id through the new thinking_budget::resolve_primed_close_id (the tokenizer's token_to_id, only when the prompt primed the block; a multi-token close resolves to nothing and falls back to the pair's close).
  • A variant open whose close is missing from the vocabulary opens nothing, so a mismatched close is never forced.
  • Out of scope, as the issue says: the disaggregated handoff (build_handoff_thinking_state and the kv_cache_serde wire 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.md and the CHANGELOG say so.

2. Stale Qwen3.5 ragged-rollback test

rollback_speculative_cache_panics_on_ragged_batched_accepts expected a panic that #2442 removed. It is replaced by rollback_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-layer conv_state and state_cache must match a B = 1 rollback of that row with its own accept, and the attention offset must be max(accepted) + 1 = 4. The shared verify_and_rollback helper now checks the returned count against max(accepted), which is what rollback_speculative_cache returns for both uniform and ragged accepts.

Tests

  • server::k2_horizon_reasoning_tests::a_primed_budget_forces_the_close_of_the_primed_variant: for high, medium, low, a primed state with a budget of 2 built through with_primed_close forces that effort's own close, and so does a reasoning_end flag.
  • a_model_emitted_variant_open_is_closed_with_its_own_close: a non-primed state that observes each open enters InBlock and 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-message run 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.
  • Existing server::thinking_budget tests (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.
  • Real checkpoint, mlxcel-server --skip-chat-parsing on models/k2-horizon-0.9b, greedy, thinking_budget_tokens: 6: high ends the reasoning with </ifm|think> after six tokens, medium with </ifm|think_fast> after six tokens, and low closes on its own with </ifm|think_faster> at the first token. With parsing on, the budgeted tokens land in reasoning_content for every effort.

Technical report: TECHNICAL_REPORTS/3330-k2-horizon-variant-close-20261123.en.md and .ko.md.

Closes #3321

@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed and removed status:review Under review labels Nov 23, 2026
…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
inureyes force-pushed the fix/issue-3321-k2-horizon-variant-close branch from 37f9b38 to 7e7e7f3 Compare November 23, 2026 08:46
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 08:55
@inureyes
inureyes merged commit 48f510a into main Nov 23, 2026
34 checks passed
@inureyes
inureyes deleted the fix/issue-3321-k2-horizon-variant-close branch November 23, 2026 08:55
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:low Low priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: K2-Horizon variant budget close and stale Qwen3.5 rollback test

1 participant