Skip to content

fix(serve): tool requests sample without the preset's presence penalty; tools rendered as trained - #118

Merged
iamwavecut merged 3 commits into
masterfrom
dev/tool-call-sampling
Oct 21, 2026
Merged

iamwavecut merged 3 commits into
masterfrom
dev/tool-call-sampling

Conversation

@iamwavecut

@iamwavecut iamwavecut commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Fixes the actionable part of #114: on Qwen3.8-Flash-Next (and every Qwen preset) a request that offers tools often got one tool call where two or three were needed.

Cause. The non-thinking preset's presence_penalty 1.5 (the model card's recommendation) subtracts 1.5 from the logit of every token the answer already used. A second tool call repeats <tool_call>, <function=, the function name and <parameter=, so near-greedy sampling turned the second call into the end of the turn or prose. The reporter measured BFCL parallel_multiple 59.5 with the preset and 67.0 with --presence-penalty 0 (llama.cpp, which applies no penalty, 70.5).

Changes

  • src/serve/translate.cpp: a request with tools and a tool_choice other than none resolves presence_penalty to 0 unless the request or --presence-penalty names a value; the post-thinking sampler follows the same rule. All three protocols go through this point. No tools, or tool_choice: none, keeps the preset.
  • src/serve/translate.cpp, src/models/qwen3_5/frontend/chat_template.cpp: a tool definition is rendered as {"type":"function","function":{"name","description","parameters"}} in that order, the order the Qwen templates were trained on; strict stays in the tool JSON for the output contract (--structured-output grammar) and the frontend strips it from what the template renders. The reporter found NInfer rendering name, parameters, strict, description; the effect of that on the score was not isolated.
  • Tests: test_openai_schema covers the rendered member order and the penalty rule (tools → 0, no tools → preset, tool_choice: none → preset, request value and --presence-penalty kept, post-thinking preset and request value); test_frontend renders a strict tool through the Qwen3.6 and Qwen3.8 templates and checks the order, the absence of strict and that the contract still sees strict.
  • Docs: serving (function tools, sampling presets), cli (preset table pointer).

Verification. ninfer_qwen3_5_frontend_test built and run on the Mac through a scratch host-only CMake (passes; fails against the old renderer). translate.cpp and test_openai_schema.cpp pass clang -fsyntax-only; the serve tests link the engine and were not run here. CTest, CUDA builds and CI were not run locally. BFCL was not re-run: the expected effect is the reporter's --presence-penalty 0 result (59.5 → 67.0 on parallel_multiple, irrelevance unchanged); the remaining gap to llama.cpp (~3.5 points, two cases answering in prose) is not addressed here.

CI. The MSVC host-compile checks were red on master before this PR (since 86dd954: _cvtsh_ss in src/ops/sparse_moe/streamed_cpu_avx2.cpp and M_PI in tests/ops/test_gguf_expert_cpu.cpp). The third commit fixes both with the vector F16C form the GGUF expert kernel already uses and std::numbers::pi; both files pass a clang x86-64 cross syntax check with AVX2/FMA/F16C locally, MSVC itself runs only in CI.

…resence penalty

The Qwen non-thinking preset's presence penalty of 1.5 subtracts from every token the answer already used, and a second tool call repeats the first call's markup and function name, so the model dropped calls it would otherwise make (issue #114: BFCL parallel_multiple 59.5 against 67.0 with --presence-penalty 0). A request with tools and a tool_choice other than none now resolves presence_penalty to 0 unless the request or --presence-penalty names a value; the same rule covers the post-thinking sampler.
… strict

The rendered definition follows the member order the Qwen templates were trained on and leaves out strict, which only selects the server's output contract; the contract still reads it from the tool JSON.
…der MSVC

MSVC declares no scalar _cvtsh_ss and no M_PI without _USE_MATH_DEFINES; the streamed rows use the vector F16C form the GGUF expert kernel already uses, and the test takes pi from <numbers>. Master's MSVC host-compile checks have failed on these two lines since 86dd954.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant