Repository navigation
Limit perplexity tokenization and device copies to each batch - #897
Open
snygginghani wants to merge 1 commit into
Open
snygginghani wants to merge 1 commit into
snygginghani wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 -qpassed 16 cases across both entry points, including both padding sides, BOS settings, truncation and batch sizes one and two. Black, isort, flake8 andgit diff --checkpassed.Addresses #573.
AI assistance was used to investigate the issue and prepare the patch, tests and benchmark.