Sub topic 2
Why Batching Makes Accelerators More Efficient
Assume you know how to call a model through an HTTP or gRPC endpoint and can distinguish latency from throughput. You should also be comfortable with basic tensor shapes and percentile metrics such as p50 and p99.
The basic trade-off
A model request usually moves through several steps:
- The server receives input data.
- It prepares the input as tensors.
- The accelerator runs model operations.
- The server copies and formats the result.
- The response returns to the client.
With a batch size of one, the accelerator processes one request at a time. This can leave part of the GPU or other accelerator idle. Launching kernels, reading model weights, and transferring data create fixed costs for each inference call.
Batching combines multiple independent inputs into one tensor. For example, four inputs shaped [features] can become one tensor shaped [4, features]. The model performs many operations with the same instructions and shared weights.
import numpy as np
# Four independent requests, each with three features.
requests = np.array([
[0.2, 0.4, 0.1],
[0.7, 0.3, 0.9],
[0.5, 0.8, 0.2],
[0.1, 0.6, 0.4],
])
# The model can process the leading batch dimension together.
print(requests.shape) # (4, 3)The total time for a batch is usually greater than the time for one request, but less than four times the single-request time. This raises throughput, measured in requests per second. The cost is waiting: earlier requests may wait for later requests before the batch starts.
| Batch size | Typical accelerator use | Per-request waiting | Throughput |
|---|---|---|---|
| 1 | Low or uneven | Lowest | Often lowest |
| 4 | Better parallel work | Moderate | Higher |
| 32 | High, if memory allows | Potentially high | Often much higher |
Why hardware benefits
Accelerators are designed for parallel arithmetic. Matrix multiplication and convolution can operate on many rows at once. A larger batch supplies more independent rows, so the hardware can keep more compute units busy.
Batching also amortizes fixed work. Loading model weights and launching a computation are costs that can be shared across several requests. Memory access becomes more regular, which can improve utilization.
Larger batches are not always faster. They consume more device memory and can trigger padding when inputs have different lengths. A batch may also become limited by memory bandwidth rather than computation. Measure throughput and latency on the actual model and hardware.
Practical limits
A fixed batch size requires the server or client to wait until enough requests arrive. During low traffic, that can create unnecessary latency. A server-side dynamic batcher instead starts a batch when it reaches a maximum size or when a short queue deadline expires. This keeps the batch useful without waiting indefinitely.
The right setting depends on the service objective. Interactive predictions usually favor a small maximum batch and short delay. Offline scoring can use larger batches because completion time matters more than individual request latency.
Exercise
A single request takes 10 ms, while a batch of four takes 16 ms. What is the approximate throughput in each case, assuming continuous work?
Show answer
Batch size one provides about 100 requests per second. A batch of four provides 4 / 0.016 = 250 requests per second, although individual requests may wait longer.