Skip to content

Limit perplexity tokenization and device copies to each batch - #897

Open
snygginghani wants to merge 1 commit into
huggingface:mainfrom
snygginghani:perf/batch-perplexity-tokenization
Open

snygginghani wants to merge 1 commit into
huggingface:mainfrom
snygginghani:perf/batch-perplexity-tokenization

Conversation

@snygginghani

Copy link
Copy Markdown

Summary

Perplexity currently pads the entire corpus to its longest text and moves the resulting tensors to the target device before splitting them into inference batches. A single long outlier can therefore inflate every batch. This is the allocation problem in #573.

For right padding, tokenize each inference batch separately. For left padding, keep the original corpus-wide padding width on the host so positional scores are preserved, then transfer only the current batch. The left-padding scoring behavior itself is covered by #839. The metric and measurement ship separately, so this applies the same change to both.

Benchmark and validation

On CPU, a seeded tiny GPT-2 and a real WordLevel tokenizer scored 129 texts in batches of eight. The first 128 had 2–4 tokens and the last had 240. In ten alternating runs, the right-padded path fell from 104.56 ms to 23.04 ms. The largest tokenizer input-ID tensor shrank from 30,960 to 240 elements. The left-padded control was 97.68 ms before and 100.11 ms after, with the original host tensor dimensions. No GPU performance was measured.

Per-sample scores matched within relative tolerance 1e-6 and absolute tolerance 1e-5. The offline regression reproducer is in tests/test_perplexity_batching.py. python -m pytest tests/test_perplexity_batching.py -q passed 16 cases across both entry points, including both padding sides, BOS settings, truncation and batch sizes one and two. Black, isort, flake8 and git diff --check passed.

Addresses #573.

AI assistance was used to investigate the issue and prepare the patch, tests and benchmark.

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