Skip to content

Skip empty logits processors in prompt lookup decoding - #51250

Open
tgghzyh wants to merge 1 commit into
huggingface:mainfrom
tgghzyh:perf/skip-empty-prompt-lookup-processors
Open

tgghzyh wants to merge 1 commit into
huggingface:mainfrom
tgghzyh:perf/skip-empty-prompt-lookup-processors

Conversation

@tgghzyh

@tgghzyh tgghzyh commented Nov 25, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

What does this PR do?

Skip the forbidden-token check in prompt lookup decoding when the logits processor list is empty. Greedy generate() passes an empty list when no processors are configured. On each prompt match, the candidate generator creates float32 scores for the full vocabulary, looks up each proposed token's score and concatenates its prefix. An empty processor list returns the scores unchanged, so every candidate passes this check.

I timed the full generate() call with CUDA synchronization at both boundaries, batch size 1, greedy decoding and a prompt lookup of 5 tokens. Each row uses 3 warm-up pairs followed by 32 pairs with alternating order on a single GPU (PyTorch 2.14.0+cu130). TinyStories continues a story. The Qwen models copy a short schedule from the prompt.

GPU Model Input / generated tokens Median on main Median with patch Median paired latency reduction
A10 TinyStories-1M, FP32 38 / 64 323.50 ms 312.04 ms 3.51%
RTX 3080 TinyStories-1M, FP32 38 / 64 320.62 ms 309.69 ms 3.43%
A10 Qwen2.5-1.5B-Instruct, FP16 99 / 61 419.17 ms 412.38 ms 1.63%
RTX 3080 Qwen3-0.6B, FP16 82 / 63 564.99 ms 557.89 ms 1.26%

The patch was faster in all 32 measured pairs for each workload. Both revisions produced the same token IDs as plain greedy decoding. A separate trace counted 26 vocabulary-sized aten.ones calls in candidate generation for TinyStories, 16 for Qwen2.5, and 17 for Qwen3 on main. All three counts fell to zero.

The following reproduces the TinyStories workload on either revision.

import statistics
import time

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

torch.set_num_threads(1)
torch.set_num_interop_threads(1)
name = "roneneldan/TinyStories-1M"
rev = "77f1b168e219585646439073245fe87e56b3023e"
tokenizer = AutoTokenizer.from_pretrained(name, revision=rev)
model = AutoModelForCausalLM.from_pretrained(name, revision=rev, dtype=torch.float32).cuda().eval()
prompt = ("Once upon a time, there was a little girl named Lily. Lily had a little dog named Max. "
          "Every day, Lily and Max went to the park. One day, Lily and Max")
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
times = []
for step in range(35):
    torch.cuda.synchronize()
    start = time.perf_counter()
    ids = model.generate(**inputs, do_sample=False, max_new_tokens=64,
                         prompt_lookup_num_tokens=5, pad_token_id=tokenizer.eos_token_id)
    torch.cuda.synchronize()
    if step >= 3:
        times.append(time.perf_counter() - start)
print(ids[0].tolist(), statistics.median(times))

For the Qwen measurements, load the checkpoint in FP16 and set repetition_penalty=1.0. I used the tokenizer's chat template with add_generation_prompt=True and enable_thinking=False, then tokenized the rendered text with add_special_tokens=False. The user message was:

Copy the following schedule exactly, without adding commentary.

Monday: take the morning train to Oxford, check in at the library, and review the manuscript.
Tuesday: take the morning train to Cambridge, check in at the archive, and review the photographs.
Wednesday: take the morning train to York, check in at the museum, and review the collection.

I also checked identical candidates on both revisions for empty and populated processor lists, including a suppressed token, EOS trimming and a prompt with no match.

Who can review?

@Cyrilvallez

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

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.

1 participant