Concepts / Inference service-level techniques: batching and parallelism

Inference service-level techniques: batching and parallelism

Inference optimization has two families: model-level techniques such as quantization, distillation, and attention optimization, and inference service-level techniques such as batching and parallelism strategies.

  • Programming

Why Serving a Model Is a Separate Problem

Training a model is only part of its life. Once a trained model is put to work answering requests, its speed and its cost depend on two things: the model itself, and the way the serving system is organized around it. Two teams can run the very same model and get very different response times and bills, simply because they arranged their machines differently.

What do you think happens?

A team running one copy of a model on one machine finds responses too slow. They add two more machines, each running a full copy of the same model, and spread incoming traffic across all three. What happens to response time and to cost?

  • Response time gets lower and cost goes up

  • Response time gets higher and cost goes down

  • Both response time and cost go down

  • Neither changes, because the model is the same

Reveal answer
Answer:

Response time gets lower and cost goes up

Replica parallelism adds machines so that each one handles fewer requests. Each request then gets more resources, which improves response time. The extra machines are the cost.

Two Families of Inference Optimization

Inference optimization is usually organized into two broad families. The first family works on the model. Quantization and distillation are model-agnostic techniques, which means they can be applied across different model architectures. Other model-level techniques are tailored to the architecture. Because the attention mechanism is a key bottleneck in transformer models, many of these optimizations target attention directly, including KV cache management and writing attention kernels.

The second family works at the level of the inference service: how requests are grouped together and how work is spread across hardware. This family includes batching and parallelism strategies. The parallelism strategies are the subject of this article.

InferenceoptimizationModel-leveltechniquesQuantizationReplica parallelismService-leveltechniquesDistillationTensor parallelismAttentionoptimizationBatchingParallelismstrategiesincludesincludesexampleexampleexampleexampleexampletypetype
Model-level techniques change what the model computes or how cheaply it stores its weights and attention state. Service-level techniques change how many requests are handled at once and where they run.

Tracing Load Across Replicas

Replica parallelism is the simplest strategy to picture. Before reading on, try to reason it through yourself. Suppose one machine serves a model and receives a steady stream of requests. Each request has to wait for that machine's resources. Now suppose you run more replicas of the model on additional machines and share the same load among them. Follow what happens to each machine as the replica count grows.

Worked example

Spreading twelve requests across replicas

A stream of 12 requests arrives in one interval. Compare a setup with one replica to a setup with three identical replicas that share the load.

  1. 1
    One replica

    The single machine must handle all 12 requests. Every request competes for the same resources.

  2. 2
    Three replicas

    Traffic is split across three machines that each run a full copy of the model. If the split is even, each machine handles 4 requests.

  3. 3
    Effect on each request

    Each machine now handles one third of the work. Each request can draw on more resources, which is the source's stated reason replica parallelism improves response time.

  4. 4
    Effect on cost

    The gain came from adding two machines. The source names extra machines as the cost of this strategy.

Result

Each machine handles fewer requests, so each request gets more resources and response time improves, at the cost of two additional machines.

Replica Parallelism and Tensor Parallelism Compared

Replica parallelism adds machines so that each one handles fewer requests. Its trade-off is explicit in the source: scaling it up costs more machines, but it buys lower latency. The source ties the choice to priorities. If low latency matters more than cost, scaling up replica parallelism is the natural move. The source also describes replica parallelism as relatively straightforward to implement, which is one reason it appears among the most impactful techniques.

Tensor parallelism is credited with two named benefits: it reduces latency, and it enables serving larger models. The second benefit is the one replica parallelism cannot offer in the same way. A model too large for a single machine can be served at all. Notice how the framing differs. Replica parallelism is described in terms of spreading requests across identical copies. The source's description of tensor parallelism is framed around the model itself and the latency of a response.

Adds machinesFramed around themodelEach machine handlesfewer requestsReduces latencyLower response timeEnables servinglarger modelsCosts extra machines
Replica parallelism works by adding machines so each handles fewer requests. Tensor parallelism is credited with reducing latency and enabling larger models. Both are credited with lower latency, but only tensor parallelism is credited with enabling larger models.
QuestionReplica parallelismTensor parallelism
What is added or changed?Additional machines, each running a full model copyServing is organized around the model itself
Main effect named in the sourceEach machine handles fewer requestsReduces latency and enables larger models
Cost named in the sourceExtra machinesNot stated in the source
Implementation difficultyDescribed as relatively straightforwardNot stated in the source
Best fit whenLow latency matters more than costA model is too large for one machine, or faster single responses are needed

Comparing the two parallelism strategies using only what the source states

Choosing a Strategy Based on Priorities

The general principle behind the source's advice is that the best technique depends on what you are trying to optimize. Imagine two teams serving the same model. A team whose main goal is low latency would lean toward adding replicas. A team whose main goal is keeping costs down would reach a different conclusion, because adding machines is exactly the step that raises cost. Neither team is wrong. They are optimizing for different things.

MEDIUM

A product team needs to serve a model that is too large to fit on a single machine. Latency also matters to them. Using only the source's statements, which parallelism strategy addresses the size problem directly, and what does that tell you about why the choice depends on the constraint?

Hints
  • Look at the two named benefits the source gives tensor parallelism.
  • Replica parallelism is described in terms of running full copies of the model on separate machines.

Two Meanings of Memory-Bound

Before choosing between strategies, one term needs clearing up because it appears constantly in inference discussions: memory-bound. It is easy to assume the phrase has one fixed meaning, so that any engineer who calls a workload memory-bound means the same thing as any other. The source reports that this assumption does not hold. The word is used with different meanings depending on the speaker's background.

Systems backgroundAI backgroundOptimization and GPUengineersML and AI engineersBandwidth-boundCapacity-bound
People with a systems background tend to mean bandwidth-bound, while people with an AI background tend to mean memory capacity-bound. The Roofline paper uses the term to mean memory-bandwidth bound.

So when you read or hear the phrase, the question to ask is which resource is the limit. Is the constraint how fast data can move, which is the bandwidth meaning? Or is it how much data can fit, which is the capacity meaning? The same two words can point to two different constraints, so confirm the speaker's meaning before acting on it.

Recovering Training Cost Through Inference

Inference costs are also tied to the economics of the model itself. The source explains that inference involves the forward pass, while training involves both the forward and backward passes. It then describes a relationship between training cost and inference revenue from the viewpoint of a model provider. Using the variables described in this relationship, the condition is written as follows.

Formula
Definition

In this relationship, T is the training cost of the model, p is the price charged per inference call, and N is the number of inference calls. The product p times N is the money recovered from inference. The inequality says the money recovered must be at least the training cost.

Worked example

Checking whether training cost is recovered

A model provider has a training cost T of 1,000,000 units. Each inference call is priced at p equal to 0.002 units. How many calls N are needed before the condition T \leq p \times N holds?

  1. 1
    Set up the condition

    We need 1,000,000 \leq 0.002 \times N.

  2. 2
    Isolate N

    Divide both sides by 0.002, giving N \geq 1,000,000 divided by 0.002.

  3. 3
    Compute

    1,000,000 divided by 0.002 equals 500,000,000, so N must be at least 500,000,000 calls.

  4. 4
    Interpret

    Higher usage lowers the number of calls needed relative to a fixed price only if the provider can keep serving them; the inequality itself only tests whether recovery is possible at that usage level.

Result

At this price, the provider needs at least 500,000,000 inference calls before inference revenue covers the training cost.

Mistakes Beginners Make

  • Common mistake

    Treating inference optimization as a single set of tricks

    Why it fails

    The source divides the techniques into two families. Model-level techniques change what the model computes or how it stores weights and attention state. Service-level techniques change how requests are grouped and where they run.

    Do this instead

    Ask first which family a technique belongs to, then ask which constraint it addresses.

  • Common mistake

    Assuming replica parallelism and tensor parallelism are interchangeable

    What it looks like

    Adding replicas to a model that is too large for one machine does not make that model fit on one machine, because each replica is a full copy.

    Why it fails

    Replica parallelism is credited with lowering response time by reducing requests per machine. Tensor parallelism is credited with enabling larger models as well as reducing latency.

    Do this instead

    Match the strategy to the goal. Use the source's priorities as the guide.

  • Common mistake

    Hearing memory-bound and assuming one meaning

    Why it fails

    Systems engineers tend to mean bandwidth-bound, while AI engineers tend to mean capacity-bound.

    Do this instead

    Ask which resource is the limit: how fast data can move, or how much data can fit.

  • Common mistake

    Applying the training-cost inequality to every party that serves a model

    Why it fails

    The source frames the relationship from a model provider's viewpoint and says it does not apply to third-party API providers.

    Do this instead

    Identify who is doing the accounting before using T \leq p \times N.

Checking Your Understanding

EASY

A colleague says, We are memory-bound, so we need more memory. Before you act, what two questions should you ask, and how would your answer change depending on whether your colleague has a systems background or an AI background?

Hints
  • The source says the phrase can mean bandwidth-bound or capacity-bound.
  • Systems engineers tend to use it for bandwidth; AI engineers tend to use it for capacity.

Key Takeaways

  • Inference optimization has two families: model-level techniques such as quantization, distillation, and attention optimization, and inference service-level techniques such as batching and parallelism.
  • Replica parallelism adds machines so each handles fewer requests, lowering response time at the cost of extra machines. Tensor parallelism is credited with reducing latency and enabling the serving of larger models.
  • Memory-bound can mean bandwidth-bound or memory capacity-bound, so confirm which meaning a speaker intends before acting on it.
  • The inequality T \leq p \times N says training cost can be recovered only when inference revenue at least equals it, and it frames the question from a model provider's point of view.

Related concepts

More concepts starting with I