Repository navigation
fix(sampler): set default freq_penalty to 0.0 across model loaders - #1133
LittleCrabby wants to merge 3 commits into
Conversation
Sampler::sampler_penalty_apply() applies freq_penalty additively (logits[id] -= count * freq_penalty), where 0.0f is disabled as documented in sampler.hpp and docs/docs/instructions/server/request_parameters.md. These 15 model loaders mistakenly initialized config.freq_penalty to 1.0/1.05/1.1 (confusing it with multiplicative rep_penalty).
tawei-amd
left a comment
There was a problem hiding this comment.
Thanks for tracking this down. The additive freq_penalty defaults do break grammar-constrained tool calls, and we reproduced it. They also keep the small models out of repetition loops, though, so we'd like to merge this in a narrower form.
What we measured (Linux, XDNA2, a build of this PR's head). Each pair uses the same request and seed, sent once with frequency_penalty omitted (this PR's default, 0) and once with main's loader value sent explicitly.
Tool calls (a write tool; the written file must compile, parse or have the expected counts):
| model | this PR (freq 0) | main default |
|---|---|---|
| qwen3.5:9b, 5 prompts x 2 seeds | 10/10 | 8/10 |
| qwen3.5:9b, high-repetition prompts (wide SQL, 60 constants, 40 paths, nested ifs) x 2 seeds | 8/8 | 5/8 (the 60-constant file stops after 3-4 entries and veers off, e.g. ITEM_0469758475346987 = 'i am the longest item') |
| gemma4-it:e2b, 5 prompts x 2 seeds | 10/10 | 9/10 |
| minicpm-v-4.7:1b, 5 prompts x 2 seeds | 6/10 | 3/10 (the files come out short, and one compose file collapsed onto a single line) |
One of the main-default failures on qwen3.5:9b was worse than truncation. In a docker-compose content string the model drifted into 11.7k characters of words it had never used before ("... resurrection resuscitation rehabilitation recovery ..."), because every repeated token, including spaces and YAML keys, was being pushed down.
Repetition loops, 6 long-answer prompts. A reply counts as looping when its tail is 3 back-to-back copies of one chunk, or when the last 400 words have fewer than 50 % distinct 4-grams:
| model | sampling | this PR | main default |
|---|---|---|---|
| qwen3:0.6b | model defaults, 3 seeds | 4/18 | 0/18 |
| qwen3:0.6b | greedy | 4/6 | 2/6 |
| llama3.2:1b | model defaults, 3 seeds | 1/18 | 0/18 |
| llama3.2:1b | greedy | 1/6 | 0/6 |
| gemma3:1b | both | 0/24 | 0/24 |
| lfm2.5-tk:1.2b | both | 0/24 | 0/24 |
| minicpm-v-4.7:1b | both | 0/24 | 0/24 |
The qwen3:0.6b loops are real degeneration: one table row repeated until max_tokens, __repr__ redefined over and over, and the same ingredient line repeated. For llama3, qwen3, qwen2, gemma3 and similar loaders, the 1.05/1.1 frequency penalty sits next to rep_penalty > 1 and is doing anti-loop work.
Requested change: keep freq_penalty = 0.0f only in the loaders where 1.0 sits next to rep_penalty = 1.0. That looks like the "1.0 = neutral" mix-up you describe, and qwen3.6-moe already uses 0 there:
- modeling_qwen3_5vl.cpp, modeling_qwen3_5_omni.cpp
- modeling_gemma4e.cpp, modeling_gemma4_12b.cpp
- modeling_minicpm_v.cpp, modeling_minicpm_ocr.cpp
Please revert the other 9 (gemma3, gemma3_text, gpt_oss, lfm2, llama3, qwen2, qwen2vl, qwen3, qwen3vl) for now. Changing them needs a retune that is checked against loops.
Leave pre_penalty (1.5 on Qwen 3.5, 1.0 on Gemma 4) as is in this PR. It is flat per distinct token, not cumulative. We'll look at Gemma 4's 1.0 separately.
Follow-up on our side: we plan to stop applying sampling penalties to tokens generated inside a grammar-constrained tool call (and inside response_format output). That fixes tool calls for every model, including the small ones and clients that send frequency_penalty themselves, while ordinary text keeps its anti-loop penalty.
🤖 Generated with Claude Code
|
Thanks for reproducing this and running the benchmarks! I've updated the branch ( A few observations from cross-checking the benchmark results against the loader classes that might be useful when you do the follow-up retune:
|
Motivation
Sampler::sampler_penalty_apply()andSampler::sampler_penalty_apply_sparse()(src/common/modules/sampler.cpp) applyfreq_penaltyadditively:Per
sampler_config(src/include/modules/sampler.hpp) anddocs/docs/instructions/server/request_parameters.md,0.0fis the disabled/no-op value forfrequency_penalty. However, 15modeling_*.cpploaders initializeconfig.freq_penaltyto1.0,1.05, or1.1(likely confused with multiplicativerep_penalty), while newer loaders (modeling_qwen3_6_moe.cpp,modeling_qwen3_8mtp.cpp,modeling_minicpm_v_moe.cpp,modeling_hy_memory.cpp,modeling_qwen3_asr.cpp) correctly set it to0.0f.During structured code generation inside tool calls (
write/bash), repeated tokens (e.g., Python indentation, SQL column types likeTEXT,\n, repeated file paths) accumulate-5to-13logit penalties within the defaultrepeat_last_n = 64window. Under XGrammar tool-call constraints, this forces models such asqwen3.5:9bto prematurely emit</parameter>mid-line (producing truncated files withfinish_reason: "tool_calls"), corrupt repeated paths/identifiers, or destabilize Gemma 4 tool-use turns — affecting any OpenAI-compatible client that omitsfrequency_penalty(since PR #978 resets defaults on every request).Technical Details
Set
config.freq_penalty = 0.0f;in all 15 affected model loaders undersrc/common/AutoModel/.Notes:
rep_penaltyandpre_penaltyare intentionally left untouched so existing documentedpresence_penaltyload defaults indocs/docs/instructions/server/request_parameters.md(1.5for Qwen 3.5/3.6/3.8,1.0for Gemma 4) remain unchanged. Maintainers may want to double-check whetherpre_penalty = 1.0fongemma4-12b/gemma4eis intentional or also a1.0-as-neutral typo.Test Plan
Reproduce the
qwen3.5:9b(FLM v1.0.14, XDNA2 NPU) tool-call truncation, comparing default request penalties vs.frequency_penalty: 0.Test Result
On
qwen3.5:9b(NPU,FLM v1.0.14), sendingfrequency_penalty: 0(alongsidepresence_penalty: 0,repetition_penalty: 1) via/v1/chat/completionseliminated premature</parameter>cutoffs and produced a complete 98-line (3,055-char)writetool call.