Repository navigation
Description
Prerequisites
- I am running the latest code (0.3.36, the same commit as main).
- I installed it with the README's default command (
pip install llama-cpp-python, pinned to 0.3.36). - I searched open and closed issues and PRs and found no existing report.
- I checked the Discussions and found nothing related.
Environment
- llama-cpp-python 0.3.36 (
pip install llama-cpp-python==0.3.36 huggingface_hub, CPU build). main, checked on 2026-10-28, is the release commit 1652066. - Python 3.12.16, gcc 13.3.0, Linux 6.6.87.2-microsoft-standard-WSL2 (Ubuntu 24.04.4 LTS), AMD Ryzen 9 7950X3D, CPU only
- Model:
ggml-org/modelstinyllamas/stories260K.gguf
Reproduction
A logits processor that depends on the absolute position (forbid even token ids at even positions), greedy decoding, with and without LlamaPromptLookupDecoding. Each call checks whether input_ids equals the prompt plus the tokens accepted so far:
import numpy as np
from huggingface_hub import hf_hub_download
from llama_cpp import Llama, LogitsProcessorList, __version__
from llama_cpp.llama_speculative import LlamaPromptLookupDecoding
path = hf_hub_download("ggml-org/models", "tinyllamas/stories260K.gguf")
print("llama-cpp-python", __version__)
PROMPT = b"Once upon a time, there was a little girl. " * 4 + b"Once upon a time,"
def run(draft, use_processor=True, n_new=48):
llm = Llama(model_path=path, n_ctx=512, logits_all=True, verbose=False,
draft_model=LlamaPromptLookupDecoding(num_pred_tokens=4) if draft else None)
prompt, out, calls, wrong_prefix = llm.tokenize(PROMPT), [], 0, 0
def parity(input_ids, scores):
# Forbid even token ids at even absolute positions, taking len(input_ids) as the position.
nonlocal calls, wrong_prefix
calls += 1
wrong_prefix += list(input_ids) != prompt + out
if len(input_ids) % 2 == 0:
scores[0::2] = -np.inf
return scores
for tok in llm.generate(prompt, temp=0.0, logits_processor=LogitsProcessorList([parity]) if use_processor else None):
out.append(tok)
if len(out) == n_new:
break
violations = sum(t % 2 == 0 and (len(prompt) + i) % 2 == 0 for i, t in enumerate(out))
return out, calls, wrong_prefix, violations
# Control: without a processor, prompt lookup does not change greedy output.
assert run(False, use_processor=False)[0] == run(True, use_processor=False)[0]
print("control without processor: greedy outputs identical with and without prompt lookup")
outs = {}
for draft in (False, True):
outs[draft], calls, wrong_prefix, violations = run(draft)
print(f"prompt lookup={draft}: processor calls={calls}, calls whose input_ids != prompt + accepted tokens={wrong_prefix}, "
f"tokens violating the processor's rule={violations}")
print("greedy outputs identical:", outs[False] == outs[True])Relevant output:
control without processor: greedy outputs identical with and without prompt lookup
prompt lookup=False: processor calls=48, calls whose input_ids != prompt + accepted tokens=0, tokens violating the processor's rule=0
prompt lookup=True: processor calls=48, calls whose input_ids != prompt + accepted tokens=25, tokens violating the processor's rule=3
greedy outputs identical: False
Expected behavior
A logits processor gets the prefix of the token being sampled (prompt plus accepted tokens), as without a draft model.
Current behavior
With prompt lookup, input_ids differs from the prompt plus accepted tokens in 25 of 48 processor calls.
Three generated tokens violate the processor's rule, and the greedy output differs from the run without a draft model.
Cause
With a draft model, generate() evaluates the last accepted token together with the draft block, so self._input_ids already holds the unverified draft tokens when the next position is sampled. The custom sampler in _init_sampler passes self._input_ids to every processor (llama.py:742) whatever idx is being sampled. Processors whose output depends on the prefix can therefore behave incorrectly. The built-in MinTokensLogitsProcessor is one of them: it counts draft tokens as generated tokens and can unmask EOS too early. In earlier tests on 0.3.19 and 0.3.35, it changed the distribution of sampled completions through create_completion; the script above tests prefix parity on 0.3.36 only.
Possible fix
Pass the prefix of the sampled position. With this change, the script reports 0 and 0 for prompt lookup and the greedy outputs are identical:
@@ def _init_sampler(
for logit_processor in logits_processor:
- recarray.logit[:] = logit_processor(self._input_ids, recarray.logit)
+ recarray.logit[:] = logit_processor(self._input_ids[: self._processor_prefix_len], recarray.logit)
@@ def sample(
+ self._processor_prefix_len = idx + 1 if idx is not None else self.n_tokens
ridx = idx - self.n_tokens if idx is not None else -1I can open a PR if this looks right.
Activity
sdy0243853-collab commentedon Nov 8, 2026
from llama_cpp import Llama
llm = Llama(
model_path="./models/7B/llama-model.gguf",
# n_gpu_layers=-1, # Uncomment to use GPU acceleration
# seed=1337, # Uncomment to set a specific seed
# n_ctx=2048, # Uncomment to increase the context window
)
output = llm(
"Q: Name the planets in the solar system? A: ", # Prompt
max_tokens=32, # Generate up to 32 tokens, set to None to generate up to the end of the context window
stop=["Q:", "\n"], # Stop generating just before the model would generate a new question
echo=True # Echo the prompt back in the output
) # Generate a completion, can also call create_completion
print(output)