Inferensys

Glossary

Batch Size

Batch size is the number of independent requests or sequences processed simultaneously in a single forward pass through a neural network.
Enterprise console with connected nodes and monitoring panels for orchestrated systems.
INFERENCE OPTIMIZATION

What is Batch Size?

Batch size is a fundamental hyperparameter in neural network training and inference that dictates how many data samples are processed simultaneously.

Batch size is the number of independent data samples—such as text prompts, images, or sequences—processed in a single, parallel forward pass through a neural network. In inference, it directly governs the trade-off between throughput (total requests processed per second) and latency (time to complete a single request). Larger batches maximize GPU utilization and throughput by amortizing fixed computational overheads but increase memory consumption and per-request latency. Smaller batches reduce latency and memory use but can leave hardware underutilized, leading to idle cycles.

The optimal batch size is determined by the memory-bound nature of the decoding phase in autoregressive models and the available GPU memory for activations and the KV cache. Techniques like continuous batching and variable-length batching dynamically adjust the effective batch composition to optimize this trade-off in real-time, grouping requests based on a batching policy to balance tail latency and system throughput under variable load.

INFERENCE OPTIMIZATION

Key Characteristics of Batch Size

Batch size is a fundamental hyperparameter that governs how many data samples are processed in a single forward pass. Its selection creates a direct trade-off between computational efficiency, memory usage, and model convergence.

01

Throughput vs. Latency Trade-off

Batch size directly mediates the classic trade-off between throughput (samples processed per second) and latency (time to process a single request).

  • Larger batches maximize hardware parallelism, saturating GPU/TPU compute units to achieve higher throughput. This is ideal for offline processing.
  • Smaller batches reduce the time-to-first-token for individual requests, minimizing latency for interactive applications like chatbots.

In inference, techniques like continuous batching aim to optimize this trade-off dynamically.

02

Memory Consumption

Batch size is a primary determinant of peak memory usage during both training and inference. Memory scales approximately linearly with batch size for:

  • Activations: Intermediate values stored during the forward pass for backpropagation (training).
  • Key-Value (KV) Cache: In transformer inference, the cache storing attention states for generated tokens. Larger batches require more cache memory.
  • Gradients (training only): Accumulated gradients for each parameter.

Exceeding GPU memory (VRAM) limits causes out-of-memory (OOM) errors, making batch size a critical constraint.

03

Generalization and Convergence

In training, batch size influences optimization dynamics and final model quality.

  • Small Batches: Provide a noisy gradient estimate, which can act as a regularizer and help models converge to flatter, potentially more generalizable minima. However, they require more iterations to converge.
  • Large Batches: Provide a more accurate gradient estimate, enabling faster convergence per epoch but risk converging to sharp minima with poorer generalization. This may require adjusting other hyperparameters like learning rate.

The relationship is summarized by the generalization gap, where very large batches can sometimes lead to reduced test accuracy.

04

Hardware Utilization & Parallelism

Batch size determines how effectively modern parallel hardware is utilized.

  • Data Parallelism: The most common form, where the batch is split across multiple devices (e.g., GPUs). Larger batches improve compute utilization by keeping thousands of arithmetic logic units (ALUs) busy, reducing idle cycles.
  • Underutilization: Small batches fail to fully utilize the massive parallelism of accelerators, leaving hardware memory-bound or idle, drastically reducing FLOPS/TOPS efficiency.

Optimal batch size is often the largest that fits in memory without causing other degradations.

05

Numerical Stability

Batch size can affect the numerical behavior of training, particularly for normalization layers.

  • Batch Normalization: Relies on per-batch statistics (mean/variance). With very small batches (e.g., size 1 or 2), these statistics become noisy and unreliable, degrading performance. Large batches provide stable statistics.
  • Alternative Normalizations: For small-batch scenarios, techniques like Layer Normalization, Group Normalization, or Instance Normalization are used, as they do not depend on batch dimensions.
  • Gradient Precision: In mixed-precision training, larger batches can improve the numerical stability of gradient accumulation in FP16/BF16 formats.
06

Inference-Specific Dynamics

During model serving, batch size is a key lever for inference cost optimization.

  • Static vs. Dynamic Batching: Static batching uses a fixed size, causing delays. Dynamic batching groups requests from a queue, optimizing size in real-time.
  • Continuous Batching (Iteration-Level Scheduling): Advanced technique where sequences in a batch are processed independently; new requests can join and finished ones can exit at each decoding step, maximizing GPU utilization despite variable request lengths.
  • Tail Latency Impact: Poor batching policies can cause head-of-line blocking, where a single long sequence delays an entire batch, spiking p99 latency.
INFERENCE OPTIMIZATION

The Batch Size Trade-Off: Throughput vs. Latency

Batch size is a fundamental hyperparameter in machine learning inference that dictates the number of independent sequences processed in a single forward pass, creating a direct engineering trade-off between system throughput and per-request latency.

Batch size is the number of independent requests or sequences processed simultaneously in a single forward pass through a neural network. This parameter is a primary lever for inference optimization, directly governing the trade-off between throughput (requests processed per second) and latency (time to complete a single request). Larger batches maximize GPU utilization and hardware throughput by amortizing fixed computational overheads, such as kernel launch costs, across more work. However, they increase latency as requests wait to form a full batch and because larger computational graphs take longer to execute.

The optimal batch size is determined by the operational service-level agreement (SLA) and underlying hardware constraints. Memory-bound operations, common in the decoding phase of autoregressive models, see diminishing returns from larger batches as they stress memory bandwidth. Techniques like continuous batching and dynamic batching dynamically adjust batch composition to navigate this trade-off in real-time, balancing tail latency guarantees against aggregate throughput. This makes batch size a critical configuration for inference cost optimization and meeting user-facing performance targets.

INFERENCE SCHEDULING

Batch Size Strategies: Static vs. Dynamic

A comparison of core strategies for grouping inference requests, highlighting their impact on key performance and operational metrics.

Feature / MetricStatic BatchingDynamic BatchingContinuous Batching

Scheduling Granularity

Entire Request

Request (Time/Queue Window)

Per-Token Iteration

Batch Composition

Fixed for request lifetime

Fixed per batch window

Fluid; changes each iteration

GPU Utilization

Low during decoding

Moderate

High

Tail Latency (p99)

High (head-of-line blocking)

Moderate

Low

Ideal For

Offline processing, high-throughput non-interactive jobs

Moderate-latency services with predictable traffic

Interactive chat, real-time applications with strict SLAs

Handles Variable-Length Sequences

Mitigates Head-of-Line Blocking

Implementation Complexity

Low

Moderate

High

BATCH SIZE

Frequently Asked Questions

Batch size is a fundamental hyperparameter that directly governs the trade-off between throughput, latency, and memory usage during neural network training and inference. These questions address its core mechanics and optimization.

Batch size is the number of independent training examples or inference requests processed simultaneously in a single forward and backward pass through a neural network. It is a critical hyperparameter that balances computational efficiency, memory consumption, and statistical learning dynamics.

During training, a larger batch size allows for more parallel computation, leading to faster iteration times and more stable gradient estimates, but it requires more GPU memory and can converge to sharper minima. During inference, particularly for autoregressive models like LLMs, batch size is a primary lever for maximizing GPU utilization and throughput (tokens/second), though it increases per-request latency due to padding and synchronization.

The optimal batch size is determined by hardware constraints (e.g., GPU VRAM), the model architecture, and the target service-level objective (SLO), whether it's high throughput for offline processing or low latency for interactive applications.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.