Skip to content

fix(models): close eos range, Z1T sequence and K2-Horizon reasoning gaps - #3303

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3299-eos-z1t-k2-horizon
Nov 23, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3299-eos-z1t-k2-horizon

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Three small gaps from recent model ports, fixed together as #3299 asks.

1. eos_token_id range checks (Limite, Xing4.0)

Both configs narrowed eos_token_id with as i32, so 4294967298 wrapped to 2 and -1 was accepted although it can never match. A shared loading::checked_eos_token_ids now converts with i32::try_from, refuses negative and out-of-range integers with an error naming the value, and skips non-integer entries as parse_eos_token_ids already did.

  • Limite: ModelArgs::validate() calls it, and eos_token_ids() reads through it.
  • Xing4.0: parse_eos returns the error from deserialization, which is where Xing40Config::validate() already runs, and validate() itself also refuses a negative id in a config built in code. eos_token_ids stays Vec<i32>; only parse_eos and validate changed in xing4_0.rs.

2. Z1T lazy sequence state

Z1t::forward_rows_for_sequence now uses with_or_create_sequence_state(seq_id, || self.fresh_states(), ..), matching the sibling families, instead of panicking on an unprepared id. The batched path's validation is unchanged. The batched-decode harness test for Z1T moves from the strict prepared-B=1 variant to the lazy one, since B=1 with an unprepared id is now accepted.

3. K2-Horizon reasoning split

The template primes <ifm|think>, <ifm|think_fast> or <ifm|think_faster> by reasoning_effort. The pairs are registered in:

  • SINGLE_TOKEN_PAIRS (tokenizer/mod.rs), which resolves <ifm|think> / </ifm|think> as the marker pair.
  • A new tokenizer::THINK_EFFORT_VARIANTS table and ThinkingMarkers::primeable_pairs(), so prompt_primed_open_thinking detects all three primed opens and prompt_primed_open_close_marker returns the matching close. For every other family primeable_pairs() is the one resolved pair, so detection is unchanged.
  • resolve_thinking_token_ids (thinking_budget.rs): ThinkingTokenIds gains alt_closes and is_close(), so a budget-tracked block primed with <ifm|think_fast> closes on </ifm|think_fast> instead of counting the answer as reasoning. Other families construct it with ThinkingTokenIds::new(open, close) and have no alternates.
  • CHAT_DELIMITERS and canonical_thinking_pair (stream_filter.rs), THINKING_MARKER_PAIRS (parser.rs), and OPEN_THINKING_CLOSE_MARKERS (routes/chat.rs), the last so a closed K2-Horizon block is not treated as unclosed and its content emptied.

The primed-open handling is the same one Kimi K3 uses: the stream filter starts in the thinking state, and the parser's primed-open pass keys on the close marker. The limitation note is removed from docs/supported-models.md.

Known limit: when a thinking_budget_tokens cap runs out under medium or low effort, the forced close is </ifm|think> (the resolved pair's close), not the variant's. Every filter treats it as a close, so the split stays correct.

Tests

  • models::limite_tests::validate_refuses_eos_ids_outside_the_i32_range, models::xing4_0::tests::config_refuses_eos_ids_outside_the_i32_range: 4294967298, [-1] and [n, 2147483648] are refused with the value in the error; [0, 2147483647] parses.
  • models::z1t::tests::z1t_unprepared_sequence_id_starts_from_fresh_state: forward_with_sequence_id with a fresh id returns logits equal to the no-id forward of the same prompt (exact), and the following decode step matches too.
  • server::k2_horizon_reasoning_tests: marker resolution and variants, primed detection per effort, the non-streaming and per-char streaming split of abc\n</ifm|think*>\nanswer for all three efforts, budget close on every variant, and the real /v1/chat/completions handler (scripted model) streaming and non-streaming for each effort.

Narrow selectors run (the issue's workspace-wide commands exceed the per-command watchdog; the broad gate is left to CI): models::limite, models::xing4_0, models::z1t, coverage_tests (Metal, under the GPU lock), server::k2_horizon_reasoning_tests, server::tool_calls, server::thinking_budget, thinking, tokenizer::, reasoning_display, server::routes::chat, server::reasoning_echo, server::kimi_k3, server::chat_request, primed, server::batch::speculative_burst_tests, loading::. cargo clippy --lib --tests --features metal,accelerate -- -D warnings and every contract script pass.

Real checkpoint: mlxcel-server -m models/k2-horizon-0.9b, /v1/chat/completions with reasoning_effort high, medium and low, streaming and non-streaming. High and medium return the trace in reasoning_content and 42 in content; low closes the block at once and returns an empty reasoning_content with only the answer in content. No response contains ifm|think text. mlxcel generate smoke on limite-1b-violetto and z1t-0-mlx loads and generates; xing4.0-29b-a4b-mlx-8bit was not run (config parsing only, covered by unit tests). The bilingual technical report is in TECHNICAL_REPORTS/3303-eos-z1t-k2-horizon-reasoning-20261123.*.md.

Closes #3299

@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 labels Nov 23, 2026
inureyes added a commit that referenced this pull request Nov 23, 2026
…e PR #3303 report

The THINK_EFFORT_VARIANTS const had been inserted between the ThinkingMarkers doc comment and the struct, so the struct lost its doc. The bilingual technical report for PR #3303 is added under TECHNICAL_REPORTS.
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
Limite and Xing4.0 narrowed `eos_token_id` with `as i32`, so an id past `i32::MAX` wrapped to a different token and a negative id was accepted. Both configs now read it through a shared `loading::checked_eos_token_ids`, which refuses such ids with an error naming the value.

Z1T's single-sequence path panicked on a sequence id that was never prepared. It now creates the state lazily with `with_or_create_sequence_state`, as its sibling families do; the batched path keeps its validation.

K2-Horizon primes `<ifm|think>`, `<ifm|think_fast>` or `<ifm|think_faster>` by `reasoning_effort`, and no marker table knew these pairs, so the reasoning and its close tag stayed in `content`. The three pairs are now registered in the tokenizer marker probe, the thinking-budget resolver, the stream filter delimiters and pair lookup, the parser's marker pairs and the unclosed-primed close list. A shared `THINK_EFFORT_VARIANTS` table lets primed-open detection return the close that matches the primed open, and the budget tracker accepts every variant's close.

Closes #3299
…e PR #3303 report

The THINK_EFFORT_VARIANTS const had been inserted between the ThinkingMarkers doc comment and the struct, so the struct lost its doc. The bilingual technical report for PR #3303 is added under TECHNICAL_REPORTS.
@inureyes
inureyes force-pushed the fix/issue-3299-eos-z1t-k2-horizon branch from 72c5ca4 to 9a0b87f Compare November 23, 2026 03:20
@inureyes
inureyes marked this pull request as ready for review November 23, 2026 03:26
@inureyes
inureyes merged commit 017f4ec into main Nov 23, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3299-eos-z1t-k2-horizon branch November 23, 2026 03:27
inureyes added a commit that referenced this pull request Nov 23, 2026
The previous commit was created against a main that had moved underneath it (#3303 and others landed), so its tree reverted those changes. This commit sets the tree to current main plus only the #3295 changes; the PR's squashed diff touches only the MLA rope wiring, its tests and docs.
inureyes added a commit that referenced this pull request Nov 23, 2026
…riant (#3330)

## 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 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: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(models): close eos range, Z1T sequence and K2-Horizon reasoning gaps

1 participant