Skip to content

fix(sampler): set default freq_penalty to 0.0 across model loaders - #1133

Open
LittleCrabby wants to merge 3 commits into
ROCm:mainfrom
LittleCrabby:fix/default-freq-penalty
Open

LittleCrabby wants to merge 3 commits into
ROCm:mainfrom
LittleCrabby:fix/default-freq-penalty

Conversation

@LittleCrabby

Copy link
Copy Markdown

Motivation

Sampler::sampler_penalty_apply() and Sampler::sampler_penalty_apply_sparse() (src/common/modules/sampler.cpp) apply freq_penalty additively:

this->logits[token_id] -= (float(count) * this->freq_penalty + float(count > 0) * this->pre_penalty);

Per sampler_config (src/include/modules/sampler.hpp) and docs/docs/instructions/server/request_parameters.md, 0.0f is the disabled/no-op value for frequency_penalty. However, 15 modeling_*.cpp loaders initialize config.freq_penalty to 1.0, 1.05, or 1.1 (likely confused with multiplicative rep_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 to 0.0f.

During structured code generation inside tool calls (write / bash), repeated tokens (e.g., Python indentation, SQL column types like TEXT,\n, repeated file paths) accumulate -5 to -13 logit penalties within the default repeat_last_n = 64 window. Under XGrammar tool-call constraints, this forces models such as qwen3.5:9b to prematurely emit </parameter> mid-line (producing truncated files with finish_reason: "tool_calls"), corrupt repeated paths/identifiers, or destabilize Gemma 4 tool-use turns — affecting any OpenAI-compatible client that omits frequency_penalty (since PR #978 resets defaults on every request).

Technical Details

Set config.freq_penalty = 0.0f; in all 15 affected model loaders under src/common/AutoModel/.

Notes:

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), sending frequency_penalty: 0 (alongside presence_penalty: 0, repetition_penalty: 1) via /v1/chat/completions eliminated premature </parameter> cutoffs and produced a complete 98-line (3,055-char) write tool call.

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 tawei-amd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@tawei-amd
tawei-amd requested a review from zaneni6 November 25, 2026 21:17
@LittleCrabby

Copy link
Copy Markdown
Author

Thanks for reproducing this and running the benchmarks! I've updated the branch (65788ada) to revert the 9 loaders as requested and keep config.freq_penalty = 0.0f only in the 6 rep_penalty = 1.0 loaders (modeling_qwen3_5vl.cpp, modeling_qwen3_5_omni.cpp, modeling_gemma4e.cpp, modeling_gemma4_12b.cpp, modeling_minicpm_v.cpp, modeling_minicpm_ocr.cpp) so this can land cleanly.

A few observations from cross-checking the benchmark results against the loader classes that might be useful when you do the follow-up retune:

  1. qwen3:0.6b and llama3.2:1b already have freq_penalty = 0.0f on main:

    • In modeling_qwen3.cpp, Qwen3 (line 24), Qwen3_IT (line 308), and Qwen3_TK (line 384) don't set freq_penalty (leaving it at 0.0f with rep_penalty = 1.0f). The config.freq_penalty = 1.05 at line 632 actually belongs to DeepSeek_r1_0528_8b::load_model (deepseek-r1-0528:8b).
    • Similarly, in modeling_llama3.cpp, Llama3 (line 19, used by llama3.2:1b) already leaves freq_penalty at 0.0f — line 137 (1.05) belongs to DeepSeek_r1_8b::load_model (deepseek-r1:8b).
    • Because the benchmark simulated main on the PR binary by passing frequency_penalty: 1.05 explicitly, 4/18 and 1/18 are actually main's current defaults on those two models, while all three tested models whose loaders were genuinely changed (gemma3:1b, lfm2.5-tk:1.2b, minicpm-v-4.7:1b) had 0/24 loops (likely because gemma3:1b and lfm2.5-tk:1.2b still keep rep_penalty = 1.05). Also worth noting: LFM2_5_TK (modeling_lfm2.cpp:107) has rep_penalty = 1.05 paired with freq_penalty = 1.0 (not 1.05).
  2. 1.05 / 1.1 on the remaining loaders (especially gpt-oss:20b and deepseek-r1):

    • Having freq_penalty = 1.05 mirror rep_penalty = 1.05 (and 1.1 mirror 1.1 in modeling_qwen2.cpp) looks like the same 1.0-centered scale mix-up — i.e., 1.05 / 1.1 intended as +0.05 / +0.10 (0.05 / 0.10) rather than an additive -1.05 * count penalty (~20× stronger, and currently listed as 0 under "every other model" in docs/docs/instructions/server/request_parameters.md).
    • Larger coding/reasoning models among the reverted loaders — notably gpt-oss:20b (modeling_gpt_oss.cpp) and deepseek-r1:8b / deepseek-r1-0528:8b — weren't in the benchmark set and will still hit -1.05 * count on repeated indentation, SQL columns, or YAML keys.
  3. Unconstrained code generation in plain chat:

    • Skipping sampling penalties inside grammar-constrained tool calls and response_format sounds like a great systemic fix for agents. Worth keeping in mind that ordinary chat replies containing long fenced code blocks, SQL schemas, docker-compose.yml, or Markdown tables run without a grammar constraint, so an additive freq_penalty of 1.05–1.1 can still trigger the same vocabulary drift ("resurrection resuscitation...") outside tool calls until those loaders are retuned (e.g. to 0.0–0.1 alongside rep_penalty > 1).

This branch has not been deployed

No deployments
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.

2 participants