Skip to content

serve: thinking budget, loop guard and sampling penalties #45

Closed

Description

@Gornist

Hi! I run Qwen3.6-35B-A3B with MTP through serve as a backend for coding agents. Three things were missing for me:

No thinking budget. With thinking on, the model sometimes thinks for 10K+ tokens or loops. max_tokens is the only limit, and it cuts off the answer too.
No presence/repetition penalties. Qwen recommends presence_penalty: 1.0 for 3.5/3.6 to keep thinking from repeating. Right now the server ignores these fields.
reasoning_effort: "none" doesn’t turn thinking off. Some OpenAI-compatible clients send only that field.

I patched this locally (about 200 lines in main.rs, ops.rs, serve.rs):

--reasoning-budget N, the reasoning_budget_tokens request field, and Anthropic thinking.budget_tokens. When the budget runs out, replaces the sampled token and the model goes on to answer. With max_tokens >= 1024, at least max(512, max_tokens/4) tokens stay for the answer.
--reasoning-loop recover|stop|off. If the same 64-token run appears 3 times inside thinking, it forces (or stops the reply).
--presence-penalty, --frequency-penalty, --repeat-penalty, --penalty-last-n, plus the same fields in requests. They count only generated tokens, not the prompt, so copying code from context isn’t penalized. They’re applied in Sampler::cands, so MTP accept sees the same history as plain decoding.
reasoning_effort: "none" or "off" turns thinking off.

Known gaps: penalties do nothing at temperature 0, because the greedy argmax runs on the GPU and never reaches the sampler. The loop thresholds are picked by hand.

Testing: unit tests for the penalties and the guard, plus smoke tests on an RTX 3060 12 GB with a Qwen3.6-35B-A3B MTP finetune (UD-Q4_K_M). No quality eval yet.

Would you take this as a PR? I’m happy to split it up or rename the flags. Patch attached, peace!

rustingstrata-think-guard.patch

Activity

  1. Vasyl-Trefilov commented on Nov 16, 2026

    @Vasyl-Trefilov
    Member

    Hi HI, Yes, a PR is very welcome. Before merge: tests/ops.rs needs the new pen field (it does not compile now), the loop guard and the automatic budget cap must be off by default so existing output does not change. Please also add the flags to docs/USAGE.md.
    Tysm for contribute!

  2. added a commit that references this issue on Nov 18, 2026
  3. Vasyl-Trefilov commented on Nov 19, 2026

    @Vasyl-Trefilov
    Member

    Hi Hi, an update: most of this is on main now and will be in the next release.

    • Thinking budget: --thinking-budget N, or per request thinking_budget /
      chat_template_kwargs.thinking_budget (Anthropic: thinking.budget_tokens). When the budget runs out,
      </think> is inserted and the model answers. Off by default.
    • Penalties: presence_penalty and frequency_penalty (flags --presence-penalty, --frequency-penalty),
      and a DRY sampler (dry_multiplier, dry_base, dry_allowed_length), from Sampler: presence/frequency penalties and DRY to stop repetition loops #54. They count only generated
      tokens, tool-call bodies are exempt, and MTP and draft-model decoding see the same history. Off by default.
      As in your patch, they need a temperature above 0.
    • Loop guard: not added. DRY (dry_multiplier: 0.8) covers repeated runs without hand-picked thresholds.
      If you still see loops with it, please post an example.
    • reasoning_effort: "none": not handled yet. For now chat_template_kwargs.enable_thinking: false turns
      thinking off for a request.
      repeat_penalty and penalty_last_n: added (flags --repeat-penalty, --penalty-last-n), llama.cpp
      rule, counted over the reply only. Default 1 is off.

    Hope you like it, ty for Idea!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions