Skip to content

feat(server): derive thinking_mode chat-template kwarg from template - #811

Merged
inureyes merged 2 commits into
mainfrom
feat/issue-775-thinking-mode-kwarg
Jul 17, 2026
Merged

inureyes merged 2 commits into
mainfrom
feat/issue-775-thinking-mode-kwarg

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Some HuggingFace chat templates gate reasoning on a thinking_mode string variable rather than the boolean enable_thinking mlxcel forwards by default, so those templates silently took their non-thinking branch even when thinking was requested. This adds detection of the thinking_mode identifier in the template source and injects thinking_mode: "enabled" into the template kwargs whenever thinking is enabled and the template references it, unless the client already supplied its own thinking_mode value.

What changed

  • src/server/chat_template.rs: added ChatTemplateProcessor::wants_thinking_mode_alias, a substring scan for the thinking_mode identifier, mirroring the existing wants_bare_thinking_alias detection used for the DeepSeek-V3.2 bare-thinking idiom.
  • src/server/chat_template.rs: build_template_context now injects thinking_mode: "enabled" when the template references the identifier and the resolved enable_thinking value is true, and does nothing when thinking is disabled (affected templates default the variable themselves). An explicit client-supplied thinking_mode kwarg always wins.
  • src/server/chat_template.rs: added unit tests for identifier detection across template shapes, injection when thinking is enabled, no injection when thinking is disabled, honoring the tokenizer-derived default when no request kwarg is present, client override of the derived alias, parity across apply_with_kwargs and apply_raw_with_kwargs, and a no-op guard for templates that never reference the identifier.

Test plan

  • cargo test --release --features cuda --lib -- server::chat_template server::chat_template_kwargs (89 passed, 0 failed, 1 ignored)
  • cargo fmt --all -- --check
  • Existing qwen3 and gemma4 template rendering tests pass unchanged

Closes #775

Some HuggingFace chat templates gate reasoning on a thinking_mode string variable rather than the boolean enable_thinking mlxcel forwards by default (issue #775), so those templates silently took their non-thinking branch even when thinking was requested.

Adds ChatTemplateProcessor::wants_thinking_mode_alias, a substring scan for the thinking_mode identifier in the template source, mirroring the existing wants_bare_thinking_alias detection for the DeepSeek-V3.2 bare-thinking idiom. build_template_context now injects thinking_mode: "enabled" whenever the template references the identifier and the resolved enable_thinking value is true, unless the caller already supplied an explicit thinking_mode kwarg (client value always wins). Nothing is injected when thinking is disabled, since affected templates default the variable themselves in that case.

Adds unit tests covering: detection of the identifier across template shapes, injection when thinking is enabled, no injection when thinking is disabled (verified via a template that distinguishes "undefined" from any injected value), honoring the tokenizer-derived default_enable_thinking when no request kwarg is present, client-supplied thinking_mode overriding the derived alias, parity across apply_with_kwargs and apply_raw_with_kwargs, and a no-op guard for templates that never reference the identifier. Existing qwen3 and gemma4 template rendering tests are unchanged and pass.

Validated with cargo test --release --features cuda --lib -- server::chat_template server::chat_template_kwargs (89 passed, 0 failed, 1 ignored) and cargo fmt --all -- --check.
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:low Low priority status:review Under review labels Jul 17, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Inject thinking_mode: "enabled" into chat-template kwargs when the template references the thinking_mode identifier and thinking is effectively enabled, unless the client already supplied a thinking_mode kwarg (issue #775).

Findings Addressed

None. Independent review plus a supplementary automated review pass found no actionable correctness, security, or maintainability issues.

Remaining Items

None.

Verification

  • All stated requirements implemented: identifier detection (wants_thinking_mode_alias, chat_template.rs:276), injection gated on effective_enable_thinking, client-wins precedence, injects the spec-mandated "enabled" string, injects nothing when thinking is disabled.
  • No placeholder/mock code remaining: single-file diff, all real logic.
  • Integrated into project code flow: threaded through both apply_with_kwargs (:455) and apply_raw_with_kwargs (:520), both of which are the production render entry points called from chat_request.rs:199,213.
  • Project conventions followed: mirrors the existing wants_bare_thinking_alias heuristic and the thinking alias injection block; effective_enable_thinking hoisted and shared so both aliases resolve identically.
  • Existing modules reused where applicable: reuses the existing template-kwarg plumbing, default_enable_thinking resolution, and the bare-thinking precedent rather than reimplementing.
  • No unintended structural changes: no renames/moves; the only refactor is hoisting effective_enable_thinking out of the bare-thinking block (behavior-preserving).
  • Tests pass: 7 new tests cover detection, enabled/disabled injection, tokenizer-default derivation, client override, both apply paths, and a no-op guard for non-matching templates; PR reports 89 passed / 0 failed on the consolidated run.

Reviewer notes (non-blocking)

  • Precedence: owned is seeded only with reserved/default keys plus the client kwargs loop; thinking_mode is never a default key, so !owned.contains_key("thinking_mode") is true exactly when the client did not supply it. Client thinking_mode (including "disabled") always wins. Correct.
  • Prompt-cache key: injection is internal to build_template_context and does not mutate merged_kwargs, so template_sig hashes the pre-injection kwargs. Identical requests hash identically; thinking on/off already differs via enable_thinking. No key instability.
  • Substring heuristic (template.contains("thinking_mode")) is looser than the wants_bare_thinking_alias phrase match, so it can match a longer identifier or a comment. Impact is nil: a spurious thinking_mode context entry is inert unless the template reads the exact variable, and the injected value only matters when the template compares against "enabled". The docstring documents this trade-off. Consistent with the established alias heuristics.
  • gemma-4 templates use enable_thinking, not thinking_mode, so the alias never fires and their rendering path is unchanged (byte-identical), preserving the bug(gemma4): dense gemma-4 text modeling is catastrophically degraded (ppl ~12k vs gemma-3's 15 on the same corpus); likely root of the temp-0 pad-collapse #686 wants_thinking_default_off behavior.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Jul 17, 2026
@inureyes
inureyes merged commit 6f93183 into main Jul 17, 2026
4 checks passed
@inureyes
inureyes deleted the feat/issue-775-thinking-mode-kwarg branch July 17, 2026 17:01
@inureyes inureyes self-assigned this Aug 31, 2026
inureyes added a commit that referenced this pull request Aug 31, 2026
…#1547)

A DeepSeek-V4 request with thinking enabled rendered the non-thinking branch
and returned a normal completion, with no error anywhere.

#811 injects thinking_mode: "enabled" when a template mentions the identifier.
That is one family's convention. DeepSeek-V4 uses another: alias the variable
through default(), then compare the alias against 'thinking'. "enabled" is not
'thinking', so the injected value fell through to the else branch. The alias
fired, the kwarg was delivered, and the template ignored it.

The sentinel now comes from the template. Collect the identifiers carrying the
value (thinking_mode itself, plus any `set <name> = thinking_mode` alias),
collect the string literals those are compared against with ==, on either side,
and inject the literal when exactly one is found. Zero or several, fall back to
"enabled" so every template that works today keeps working: a wrong sentinel is
silent, so guessing would be worse than the bug it replaces.

The default() argument is deliberately not a candidate. It is the template's OFF
value ('chat' here), and injecting it would be worse than injecting nothing.

#811's precedence is unchanged: an explicit client thinking_mode always wins,
and nothing is injected when thinking is disabled.

Why the existing tests missed it: THINKING_MODE_LIKE_TEMPLATE compares
thinking_mode directly against "enabled", the convention #811 was written for,
and wants_thinking_mode_alias is only a substring scan, so it returns true for
both conventions while the suite could only see one. The new tests cover the
aliased shape, the real checkpoint's own chat_template.jinja, both fallback
cases, whole-word identifier matching, and the reversed comparison order.
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…ablup#811)

## Summary

Some HuggingFace chat templates gate reasoning on a thinking_mode string variable rather than the boolean enable_thinking mlxcel forwards by default, so those templates silently took their non-thinking branch even when thinking was requested. This adds detection of the thinking_mode identifier in the template source and injects thinking_mode: "enabled" into the template kwargs whenever thinking is enabled and the template references it, unless the client already supplied its own thinking_mode value.

## What changed

- `src/server/chat_template.rs`: added `ChatTemplateProcessor::wants_thinking_mode_alias`, a substring scan for the `thinking_mode` identifier, mirroring the existing `wants_bare_thinking_alias` detection used for the DeepSeek-V3.2 bare-thinking idiom.
- `src/server/chat_template.rs`: `build_template_context` now injects `thinking_mode: "enabled"` when the template references the identifier and the resolved `enable_thinking` value is true, and does nothing when thinking is disabled (affected templates default the variable themselves). An explicit client-supplied `thinking_mode` kwarg always wins.
- `src/server/chat_template.rs`: added unit tests for identifier detection across template shapes, injection when thinking is enabled, no injection when thinking is disabled, honoring the tokenizer-derived default when no request kwarg is present, client override of the derived alias, parity across `apply_with_kwargs` and `apply_raw_with_kwargs`, and a no-op guard for templates that never reference the identifier.

## Test plan

- [x] `cargo test --release --features cuda --lib -- server::chat_template server::chat_template_kwargs` (89 passed, 0 failed, 1 ignored)
- [x] `cargo fmt --all -- --check`
- [x] Existing qwen3 and gemma4 template rendering tests pass unchanged

Closes lablup#775
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(server): derive the thinking_mode chat-template kwarg when the template supports it

1 participant