Repository navigation
Conversation
…k lines
Qwen4-35B-A3B's chat template names its own thinking levels (xhigh, high, low, off) and
stops on any other word. It has no medium, so a request with reasoning_effort "medium" -
the level many OpenAI clients send by default - was a 400 ("Unexpected reasoning effort
medium"), and "high" was sent on as xhigh, which is Flash-Next's rule.
The server now asks the template what it is (ChatTemplate._detect_qwen4 renders it once
with reasoning_effort "off") and uses a table of its own for it: medium is the model's
default level (high), the model's own words keep their meaning, and none / off / minimal
turn the thinking off. Every API reads the level through effort_kwargs, so OpenAI chat,
Responses, Anthropic (effort and thinking budgets), chat_template_kwargs and the config's
"reasoning_effort" all follow.
The template writes <think>...</think>answer with nothing between. The texts the server
writes into a reply use that spelling for this model: the closed thinking of a thinking
budget and of reasoning_close_retry, the empty thinking block of a tool-call retry, and a
forced call. "effort_position": "end" takes high as the default level and writes the
template's own sentence in its trailing turn.
No other model is touched: the template is recognised by rendering it, and
serve/test_qwen4_effort.py checks that Flash-Next's, the fourteen strata-poly families'
(fixtures/family_templates.json.gz) and three made-up ones are not taken for it. The
published template is in fixtures/qwen4_chat_template.jinja (unsloth/Qwen4-35B-A3B-GGUF,
Apache 2.0).
Niko1221/Strata PR 7782 solves the same problem for the Strata project's server; this is
written for PolyStrata's per-family effort tables.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
skygunner
added a commit
to skygunner/PolyStrata
that referenced
this pull request
Dec 4, 2026
…nd make the conversion chain crash-safe Serving: Qwen4's template knows xhigh/high/low/off and no medium, so select that dialect from the tokenizer's pre="qwen4" - qwen4exp is shared with Flash-Next, and Service.family keeps its structural None. Anthropic budgets map 2048/8192 to high/xhigh, config/shared/settings use the same table, and the tokenizer is read before the config's effort is validated. With MTP, a Qwen4 request that names no sampling now uses the model card (thinking 1.0/0.95/20) instead of greedy; explicit request fields, shared settings and temperature=0 still win, and /props reports the same defaults. This is the effort bug reported in simplexai-labs#437. Tools: Tokenizer.from_dir reads an empty merge set as [] (splitlines, not split("\n")), extract() records the qwen4 provenance (QWEN35 + ignore_merges), and intake points the Qwen4 drafter at mtp_qwen4_rt.py while Flash-Next keeps mtp_pack.py. iq_pack publishes native_experts.txt last, after every selected expert artifact; the splitter and the MTP runtime write beside the target and rename only on success; the MTP set carries a size-checked complete.json; make_profile writes atomically. The split tests ignore Windows mmap cleanup errors, as reported on the PR (assertions unchanged). Runner: preflight GPU 0's free VRAM, a bindable port, RAM and per-filesystem disk before any download or build; --check changes nothing. Install the full requirements.txt, detect the CUDA architecture, reuse artifacts only when complete, write the config atomically, merge shared settings without overwriting user keys, and carry the models.json aliases into the config. Docs: QWEN4.md and both model pages describe the effort dialect, the MTP sampling exception, the drafter's dense-attention approximation (indexer tensors are not converted), the two meanings of preserve_thinking, and the deliberately unchanged --mtp-hnorm/--spec-min-p defaults; add the English model page. Every new regression failed on the old code first. Final runs: 110 tool tests (4 skipped) and 826 serving tests (3 skipped), all passing. No engine default that shapes the draft distribution was changed, and no new real-inference measurement was taken; the quoted speeds remain the earlier RTX 3080 Ti runs.
Collaborator
|
Thank you for the PR! Qwen4 thinking-level handling (_EFFORT_QWEN4 mapping medium -> high, _detect_qwen4_efforts(), and compact handling) has already landed on main in commit 1a196d9, so we are closing this as superseded. |
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.
Problem and result
Qwen4-35B-A3B's chat template has four thinking levels,
xhigh,high(its default),lowandoff, and stops on any other word. It has nomedium. With the server as it is:"reasoning_effort": "medium"(the level many OpenAI-style clients send by default) is an HTTP 400:Unexpected reasoning effort medium. Supported efforts are xhigh, high, low and off."reasoning_effort": "high"reaches the model asxhigh, because that is Flash-Next's rule.With this change the server recognises the template and uses a table of its own for it:
mediumis the model's default level (high), the model's own words keep their meaning, andnone/off/minimal/enable_thinking: falseturn the thinking off. The texts the server writes into a reply also use this model's spelling of thinking,<think>...</think>answerwith no blank lines.highhighlowlowlowmediumhighhighxhighhighxhigh,maxxhighxhighnone,off,minimalRelated discussion
#425 (Qwen4-35B-A3B support) and #430 (the draft engine port). This PR is only the server side and does not overlap #430's files. Niko1221/Strata#7782 solves the same problem for the Strata project's server; this one is written for PolyStrata's per-family effort tables and shares no code with it.
Approach
ChatTemplate._detect_qwen4renders the template once withreasoning_effort="off"and looks for the generation prompt<|im_start|>assistant\n<think></think>. The check is on the template and not on the tokenizer's name, so a fine-tune that keeps the template is served the same way.frontend.use_family("qwen4")selects_EFFORT_QWEN4. Every API already reads the level througheffort_kwargs, so OpenAI chat, Responses, Anthropic (effort and thinking budgets),chat_template_kwargsand the config'sreasoning_effortfollow without further changes.Service.familystaysNone(the Qwen form), so tool-call retries and the rest behave as for Flash-Next.closed_with(..., compact=True): the closing of a thinking budget and ofreasoning_close_retry, the empty thinking block of a tool-call retry, and a forced call are written without line breaks for this model."effort_position": "end"takeshighas the default level and writes the template's own sentence in the trailing turn.medium→highis a choice: the model has no such level, andhighis what it does when nothing is asked. Refusing it (today's behaviour) breaks clients that always send it.No other model is affected: a template is only treated this way when the probe matches.
Validation
Revision
0ec28e69bonmain4106d1a7c. Ubuntu Server, Python 3.14.Passed:
python -m pytest terminal/serve/test_qwen4_effort.py: 15 passed, 38 subtests. It uses the published template (fixtures/qwen4_chat_template.jinja, from the header of unsloth/Qwen4-35B-A3B-GGUF UD-Q4_K_XL, SHA-25650383841da44…) behind the mock engine, and checks that Flash-Next's template, the fourteen templates offixtures/family_templates.json.gzand three made-up ones are not taken for Qwen4's.python tools/check --only py-serve,py-tools,docs: py-tools 2,260 passed, 0 failed; docs passed; py-serve 2,364 passed, 1 failed (see Failed).strataengine built with-DSTRATA_GEOMETRY=q4_litefrommainplus the engine commits of [Engine] qwen4exp: Qwen4-35B-A3B (q4_lite) support Niko1221/Strata#7774 (89561aaafto8edd10483), this change on top.mediumgave the HTTP 400 quoted above;low,xhigh,none,minimalandenable_thinking: falsealready worked.medium,high,low,xhigh,none,chat_template_kwargs: {"reasoning_effort": "off"}and{"enable_thinking": false, "reasoning_effort": "low"}all answer 200, thinking on or off as asked;bogusis a 400 with the server's own message./v1/chat/completionsand through/v1/messages.reasoning_budget_tokens: 40: the server closed the thinking with its wrap-up sentence and the answer followed.tool_choiceforcing a function, with thinkinghighandnone: the call came back both times.reasoning_contentback reused 101 of 120 prompt tokens.Failed:
serve.test_telemetry_adl.OnlyTheMonitor::test_reader_choicefails on this PC (AMD cards) and fails the same way on unmodifiedmain4106d1a7c.serve.test_resource_warnings.Closed::test_the_cpu_and_ram_sampler_closes_its_filesfailed in 1 of 2 fullpy-serveruns of this branch (3 jobs) and passed in the other. It passes alone (3 of 3) and in one process together with the new test file (2 of 2), and the new file raises noResourceWarningunderpython -X dev -W always::ResourceWarning. One full run of unmodifiedmaindid not show it. I could not tie it to this change; I am listing it in case it depends on test order.Not run:
"effort_position": "end",reasoning_close_retryand the tool-call markup retry on the model itself: these three are covered by the unit tests only.Limits
maindoes not load this model yet ([Engine] support Qwen4-35B-A3B (qwen4exp) with MTP and 128K serving #430 is the port), so this change has nothing to act on until an engine for theq4_liteshape is there. It is independent of which port lands.medium→low, or a 400 with a clearer message, it is one line of_EFFORT_QWEN4.🤖 Generated with Claude Code