Repository navigation
Conversation
Contributor
Author
|
R: @damccorm |
Contributor
|
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment |
BatchElements batches per worker, so an LLM server behind RunInference sees batches that mix unrelated prompt prefixes and its prefix (KV) cache is thrashed. Add key_fn and num_shards to BatchElements. With key_fn set, a new WithBatchKey DoFn keys each element without the per-worker UUID that WithSharedKey and WithLengthBucketKey add, and GroupIntoBatches routes elements that share a key to the same worker, so every batch holds a single key. num_shards (an int, a per-key mapping, or a callable) spreads a hot key over a bounded number of random sub-keys. Incomplete keyed batches flush after max_batch_duration_secs, or 1s when it is unset, so low-traffic keys still make progress in streaming. ModelHandler forwards batch_key_fn / batch_key_num_shards to BatchElements, and the new apache_beam.ml.inference.prefix_batching module gives LLM handlers a PrefixBatchingConfig plus default prompt and chat prefix extractors. The config defaults to 8 shards per prefix: GroupIntoBatches serves each shard on one worker at a time, so a single shard caps a prefix shared by most traffic at one worker's throughput.
AMOOOMA
force-pushed
the
pr-prefix-batching
branch
from
October 30, 2026 17:20
465e446 to
07aac31
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #40905 +/- ##
==========================================
+ Coverage 79.83% 79.84% +0.01%
==========================================
Files 494 495 +1
Lines 83575 83699 +124
==========================================
+ Hits 66722 66830 +108
- Misses 16853 16869 +16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Split out of #40760, following review feedback to separate the generic pieces from the vLLM-specific ones. This PR is the generic prefix-aware batching; vLLM support builds on it in a follow-up.
Why
BatchElementsbatches per worker, so an LLM model handler behind RunInference sends its server batches that mix unrelated prompt prefixes, and the server's prefix (KV) cache gets thrashed instead of reused.What
BatchElements(key_fn=..., num_shards=...)(transforms/util.py)key_fnset, a newWithBatchKeyDoFn keys each element without the per-worker UUID thatWithSharedKey/WithLengthBucketKeyadd.GroupIntoBatchesthen routes elements that share a key to the same worker, so every batch holds a single key.num_shards(an int, a per-key mapping, or a callable) spreads a hot key over a bounded number of random sub-keys, so one key isn't capped at one worker.max_batch_duration_secs, or 1s when unset (GroupIntoBatchestreats 0 as "no limit", which would stall low-traffic keys in streaming).RunInference plumbing (
ml/inference/base.py)ModelHandler(batch_key_fn=..., batch_key_num_shards=...)forwards toBatchElements.apache_beam.ml.inference.prefix_batching(new, generic for LLM handlers)PrefixBatchingConfig, plus default extractorsPromptPrefixKeyFn(completions) andChatPrefixKeyFn(OpenAI-style chat messages; unknown message schemas raise rather than collapsing onto one key).BatchElements/ModelHandlerdefaults stay at 1 shard.Testing
util_test.py: homogeneous keyed batches, shard specs (int / mapping / callable), validation, default buffering.prefix_batching_test.py: extractors and config.base_test.py:batch_key_fn/num_shardsreachBatchElements, and an end-to-end RunInference run produces prefix-pure batches.Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123orfixes #456).CHANGES.mdwith noteworthy changes.