liveBook · Manning

Batch size

As explained in LLMs in Production

Batch size is the number of input examples or inference requests processed together in one computation batch.

Batch size is the number of training examples or inference requests processed together in one model computation. Grouping work can improve throughput and GPU efficiency, but it consumes more memory and may increase latency. section 3.4.7 section 6.1.3

Use batch size as a practical speed–memory–latency trade-off:

  • Choose powers of two when possible, such as 16, 32, or 64, to improve speed and memory efficiency on GPUs. section 5.4.2 section 6.1.3
  • Expect larger inference batches to require additional memory for the tokens processed concurrently; training requires substantially more memory for gradients and optimizer tensors. section 3.4.7
  • Use adaptive or dynamic batching for services: pool requests until the maximum batch size or delay is reached, then return results to each client. section 6.1.3
  • Increase the maximum batch size or batch delay only when the system has latency budget; doing so generally improves throughput while adding waiting time. section 6.1.3

A production constraint

A development environment achieved single-digit millisecond responses, but production imposed a batch size of 20. The fixed value limited optimization because surrounding systems depended on it. section 5.4.2

Table 5.2 Training choices to consider
Method
Improve speed
Improves memory utilization
Difficulty
Batch size choice
Yes
Yes
Easy
Gradient accumulation
No
Yes
Medium
Gradient checkpointing
No
Yes
Medium
Mixed precision
Yes
No
Hard
Optimizer choice
Yes
Yes
Easy
Data preloading
Yes
No
Medium
Compiling
Yes
No
Easy

Compare batch size with gradient accumulation, checkpointing, mixed precision, optimizer choice, data preloading, and compilation across speed, memory utilization, and difficulty. section 5.4.2

How LLMs in Production covers it

How deep it goes
Explains power-of-two choices, memory effects, dynamic batching, and latency trade-offs without detailing batching algorithms or hardware internals.
Examples
Uses 16-batch memory calculations, powers-of-two values, a production batch size of 20, and BentoML or Triton dynamic batching.
What it assumes
Assumes familiarity with model training, inference, GPUs, and memory while introducing batch-size effects through practical examples.
What you can do after
Operate/configure training and inference systems by selecting batch sizes and tuning dynamic batching against memory, throughput, and latency limits.
How it connects
Connects batch size with GPU memory, inference, training, throughput, latency, gradient accumulation, checkpointing, and mixed precision.

Also explained in

Compare how 12 books explain batch size →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.