Skip to content

fix: apply sampling penalties to prompt tokens - #2447

Open
remote-controlled-man wants to merge 2 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-9cb600b4a320
Open

remote-controlled-man wants to merge 2 commits into
abetlen:mainfrom
remote-controlled-man:codex/prflow-9cb600b4a320

Conversation

@remote-controlled-man

Copy link
Copy Markdown
Contributor

Problem

Prompt tokens are missing from repetition, frequency and presence penalties. A freshly created sampler only remembers tokens it has sampled, so even a large frequency penalty cannot discourage repeating a token already in the prompt.

Found by reading the sampling code, then reproduced with a real local stories15M model and deterministic logits through the public Llama API.

Change

Refresh active penalty samplers from the evaluated prefix at the requested sampling position, limited by last_n_tokens_size. Keep grammar and random sampling state intact. Low-level sampling without an explicit prefix keeps its existing accept-based history, and cloned chains retain penalty tracking.

Validation

  • The same 19 focused native-model cases give 15 failures / 4 passing controls on an isolated copy of unmodified main, and all 19 pass with this fix.
  • Coverage includes sample/generate, all three penalties, fresh/cached/continued prompts, window and index boundaries, multi-token history, and grammar isolation.
  • make test: 45 passed, no skips.
  • make lint: Ruff checks and formatting passed across llama_cpp and tests.
  • macOS arm64, Python 3.12.14, pinned llama.cpp vendor; cached upstream test models. Prepared with AI assistance.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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