You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by jr...@apache.org on 2024/08/20 20:44:46 UTC

(beam) branch master updated: Add BatchElements overview doc to Beam Website (#32239)

This is an automated email from the ASF dual-hosted git repository.

jrmccluskey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 63055a8032f Add BatchElements overview doc to Beam Website (#32239)
63055a8032f is described below

commit 63055a8032f5ddf9df7f17d800b4154d58b9c35c
Author: Jack McCluskey <34...@users.noreply.github.com>
AuthorDate: Tue Aug 20 16:44:39 2024 -0400

    Add BatchElements overview doc to Beam Website (#32239)
    
    * Add BatchElements overview doc to Beam Website
    
    * Remove trailing whitespace
    
    * add link to overview page
---
 .../en/documentation/patterns/batch-elements.md    | 45 ++++++++++++++++++++++
 .../content/en/documentation/patterns/overview.md  |  1 +
 2 files changed, 46 insertions(+)

diff --git a/website/www/site/content/en/documentation/patterns/batch-elements.md b/website/www/site/content/en/documentation/patterns/batch-elements.md
new file mode 100644
index 00000000000..b28870a2c6f
--- /dev/null
+++ b/website/www/site/content/en/documentation/patterns/batch-elements.md
@@ -0,0 +1,45 @@
+---
+title: "Pattern for dynamically grouping elements"
+---
+
+<!--
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+# Dynamically grouping elements using the `BatchElements`-transform
+
+## Why Batch Elements?
+Apache Beam provides the `BatchElements` [transform](https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.util.html?highlight=batchelements#apache_beam.transforms.util.BatchElements) to amortize processing time of following operations. This is best applied ahead of operations that have a substantial fixed cost per invocation and a smaller cost per element processed within that invocation. Put simply, batching elements is an optimization step ahead of operations that are  [...]
+
+### Example: Batching in RunInference
+In the context of `RunInference`, batching elements largely serves to reduce the number of calls being made to the model used for inference. This is important for efficiency in on-worker inference contexts, but has an even greater impact when looking at inference calls being sent to a remote service. When API rate limiting is a consideration, batching elements as possible is an important way to reduce the risk of overrunning quotas. The `RunInference` [framework](https://github.com/apach [...]
+
+## Batching Within Bundles
+The main methodology used for batching elements is to batch within bundles of elements. Bundles are iterated through, appending elements to a bundle before reaching a target batch size and then being emitted. Elements in the same batch are guaranteed to exist in the same event time window, as the batching function is window-aware. If there is an incomplete batch buffered when the end of the bundle is reached, it is emitted.
+
+### Parameter Tuning Considerations
+The majority of tuning for batching in this manner revolves around parameters used to estimate the optimal batch size based on downstream performance. The `_BatchSizeEstimator` [object](https://github.com/apache/beam/blob/3063b55757509dad1c14751c9f2aa5905826d9a0/sdks/python/apache_beam/transforms/util.py#L309) maintains data points around batch size and clock time for emitted bundles, then attempts to solve for the current ideal batch size using linear regression. The [docstring](https:/ [...]
+
+## Batching Across Bundles
+Batching elements within bundles has one major downside: if bundles are small (or even single elements) the operation is effectively a no-op. This is especially true in streaming pipelines, where we expect bundles to only contain 1 to 2 elements. To enable batching with small bundles, we instead have to be able to batch elements across bundles. For an in-depth technical explanation of how this works, the design document for [batching across bundles](https://docs.google.com/document/d/1Ri [...]
+
+To enable stateful batching, pass the `max_batch_duration_secs` [parameter](https://beam.apache.org/releases/pydoc/current/apache_beam.transforms.util.html?highlight=batchelements#apache_beam.transforms.util.BatchElements) to `BatchElements` in Apache Beam version 2.52.0 or later. This will key the input elements to ensure the state API can be used and then use the stateful batching function. Batches will be emitted when they either reach the current target batch size or the batch has be [...]
+
+Batching across bundles does have its own downsides:
+* Buffering batches does introduce a bottleneck into the pipeline, reducing throughput compared batching within bundles
+* The keying step that allows the State API to function can shuffle data around on the workers, which can incur a large amount of network traffic between workers if the data being processed is large
+
+### Parameter Tuning Considerations
+In addition to all of the tuning parameters used for batching within bundles, tuning the `max_batch_duration_secs` parameter will have a significant impact on the throughput of the transform. The trade-off being made when selecting a max batch duration is between throughput and consistent batch sizes. Larger values will generally have reduced throughput, as incomplete batches will be held for longer before being sent to the inference call itself; however, this will produce full batches m [...]
+
+It should also be noted that these trends are not always going to be true! In streaming contexts, there’s a reasonable amount of variance in how often elements are ingested into the pipeline. You may be able to reach a desired batch size with a low maximum batch duration, but that is different from consistently reaching that batch size with a longer maximum duration. It’s best to reason about these described tradeoffs as the average case.
\ No newline at end of file
diff --git a/website/www/site/content/en/documentation/patterns/overview.md b/website/www/site/content/en/documentation/patterns/overview.md
index 61ae6f171cd..a313bae9ddc 100644
--- a/website/www/site/content/en/documentation/patterns/overview.md
+++ b/website/www/site/content/en/documentation/patterns/overview.md
@@ -53,6 +53,7 @@ Pipeline patterns demonstrate common Beam use cases. Pipeline patterns are based
 
 **State & timers patterns** - Patterns for using state & timers
 * [Grouping elements for efficient external service calls](/documentation/patterns/grouping-elements-for-efficient-external-service-calls/#grouping-elements-for-efficient-external-service-calls-using-the-`GroupIntoBatches`-transform)
+* [Dynamically grouping elements](/documentation/patterns/batch-elements/#dynamically-grouping-elements-using-the-`BatchElements`-transform)
 
 **Cache with a shared object** - Patterns for using a shared object as a cache using the Python SDK
 * [Create a cache on a batch pipeline](/documentation/patterns/shared-class/#create-a-cache-on-a-batch-pipeline)