beam icon indicating copy to clipboard operation
beam copied to clipboard

[Feature Request]: RunInference: Content-Aware Dynamic Batching for NLP/LLM Workloads

Open Eliaaazzz opened this issue 10 months ago • 6 comments

What would you like to happen?

I’ve been evaluating RunInference for variable-length inputs (NLP/LLMs), and the current batching approach (via BatchElements) feels too coarse for these workloads.

Right now batching is mostly count-based (batch size) or byte-based. That works well for fixed-shape inputs (e.g., images), but for highly variable sequence lengths it creates a few issues:

  1. Padding waste: One long sequence in a batch forces padding for many short sequences, wasting GPU compute on padding tokens.
  2. Unpredictable OOMs: A single outlier (very long input) can spike memory usage for the entire batch and cause hard-to-reproduce OOMs.
  3. Boilerplate for users: I ended up writing a custom DoFn to “bucket by length” before RunInference. This is a common pattern in NLP and would be better supported natively.

Proposed solution

Add content-aware batching to RunInference, so batching can be driven by computational cost rather than only element count.

Conceptually:

  • [ ] Cost-based thresholds: Let users provide a cost_fn (e.g., token count) and a max_cost_per_batch (e.g., 4096 tokens) to form batches.

  • [ ] Optional bucketing: Buffer elements and group similar lengths to reduce padding overhead.

  • [ ] Dynamic padding integration: Ensure the ModelHandler (e.g., PyTorch) pads to the batch max length (per batch), not a global max.

Sketch API (conceptual)

pipeline | RunInference( model_handler=..., batching_kwargs={ "mode": "dynamic", "max_cost": 4096, # e.g., total tokens per batch "cost_fn": lambda x: len(x), # user-defined cost metric "bucket": True, # optional }, )

Alternatives considered

  1. GroupIntoBatches with weights: Works, but is verbose and separates batching from inference/padding logic.
  2. Static padding to max length: Too inefficient for production latency/cost requirements.

Additional context

I’ve skimmed base.py and pytorch_inference.py. My initial thought is a TokenBasedBatcher (or an extension around BatchElements) that can be reused by RunInference.

I’m interested in working on this for GSoC 2026 and can draft a design Any pointers to existing work or preferred direction would be appreciated.

Issue Priority

Priority: 2 (default / most feature requests should be filed as P2)

Issue Components

  • [x] Component: Python SDK
  • [ ] Component: Java SDK
  • [ ] Component: Go SDK
  • [ ] Component: Typescript SDK
  • [ ] Component: IO connector
  • [ ] Component: Beam YAML
  • [ ] Component: Beam examples
  • [ ] Component: Beam playground
  • [ ] Component: Beam katas
  • [ ] Component: Website
  • [ ] Component: Infrastructure
  • [ ] Component: Spark Runner
  • [ ] Component: Flink Runner
  • [ ] Component: Samza Runner
  • [ ] Component: Twister2 Runner
  • [ ] Component: Hazelcast Jet Runner
  • [ ] Component: Google Cloud Dataflow Runner

Eliaaazzz avatar Jan 25 '26 09:01 Eliaaazzz

.take-issue

Eliaaazzz avatar Jan 25 '26 09:01 Eliaaazzz

.take-issue

shaheeramjad avatar Jan 26 '26 08:01 shaheeramjad

.take-issue

Hi @shaheeramjad, I actually attempted to take this issue yesterday (see my comment above), but it seems the github-actions bot failed to assign me, likely because I'm a new contributor and don't have the permissions yet.

Since I'm the reporter and have already started looking into this, could I please be assigned? Thanks!

Eliaaazzz avatar Jan 26 '26 12:01 Eliaaazzz

Hi @Eliaaazzz i have start working on it I have attempted it because it was not assigned to anyone at that time

shaheeramjad avatar Jan 26 '26 14:01 shaheeramjad

Hi @shaheeramjad, thank you so much for your prompt reply!

Since I initiated this feature request, I already have a concrete implementation plan and would love to take charge of it myself. The previous confusion was entirely due to a glitch in my initial comment.

I'd be happy to lead the implementation, but I would greatly appreciate any review or feedback you could provide after submitting the PR! Thank you for your understanding.

Eliaaazzz avatar Jan 26 '26 15:01 Eliaaazzz

@Eliaaazzz Okay please proceed with it Regards

shaheeramjad avatar Jan 26 '26 15:01 shaheeramjad