Why a GPU likes batches, and your user may not

Shared weights, memory cost, and the difference between throughput and latency

Batching shares weights across requests and raises throughput, but each request still waits for the whole batch.

Take a Linear layer: Y = X @ W.T + b. With a batch of B requests, each a vector of 4,096 numbers:

| Part | Shape | |---|---| | Input X | B x 4096 | | Weights W | 4096 x 4096 | | Bias b | 4096 | | Output Y | B x 4096 |

Every row of X is a different request, and the same W serves all of them. A bigger batch adds no parameters.

Why it can be faster

Matrix multiplication needs both arithmetic and moving numbers out of memory. A GPU can be limited by either. With several rows, an implementation can reuse chunks of the weights, so more arithmetic happens per byte moved. That ratio is arithmetic intensity. Larger batches may improve it, but 32x the samples is not 32x the speed: shapes, number format and implementation all matter.

What it costs in memory

In FP16, 2 bytes per number, looking only at dense storage for this layer:

| Part | Batch 1 | Batch 32 | |---|---:|---:| | W | 32 MiB | 32 MiB | | X | 8 KiB | 256 KiB | | Y | 8 KiB | 256 KiB | | b | 8 KiB | 8 KiB |

Weights stay the same, sample data grows 32x. A real network also holds intermediate results and library workspace, and training adds gradients and optimizer state. This is one layer's arithmetic, not a promise that a model fits.

Throughput is not latency

Invented numbers: a batch of 1 finishes in 1 ms, a batch of 32 in 8 ms.

  • Batch 1: 1,000 samples per second.
  • Batch 32: 4,000 samples per second.
  • Throughput is 4x. But every request in the big batch gets its answer after about 8 ms, not 8 / 32 = 0.25 ms. You divided the cost, not the wait. And that is before the time spent waiting for the batch to fill.

    Dynamic batching

    A server can collect requests that arrive separately and run them together, for example up to 32 with a short wait for stragglers. Triton lets you set that delay and send a partial batch when it expires. It suits stateless models. At low load there may not be enough requests to merge, so waiting only to fill a batch hurts users for little gain. At high load, merging can shorten queues. The delay you configure is not a cap on total response time either: a request can also wait for a free server.

    The mistake

    "The biggest batch that fits in memory is best." It may be right for an overnight archive job. It is not necessarily right for live detection.

    Compare a few sizes under a realistic arrival rate, and measure throughput, end-to-end p95 latency and peak memory together. Choose within the product's waiting budget. GPU utilisation alone does not tell you the user was served well.

    Exercise

    Same image classifier, two uses: a 10,000-photo album and a live camera view. Which matters more for each, finishing the whole set fast or getting a fresh result quickly? That gives you two different batching policies for one model.

    Tagged: ai, gpu, serving, batching, latency, performance

    Area: AI

    Related notes

    עברית