LLM Inference Costs

Follow language-model inference from tokens to tensors, then connect the operations to runtime and cost.

From tokens to tensors

A prompt becomes token IDs, vectors, attention scores, and finally a stream of generated tokens.

Inference performance

Memory traffic and GPU use determine response time and cost.

A prompt has two inference phases

A prompt passes through the same sequence of events in any autoregressive LLM:

  1. Tokenize your input into a sequence of integer token IDs.
  2. Prefill: feed all input tokens through the model's transformer layers in parallel. This produces the internal representation and the first output token.
  3. Decode: generate output tokens one at a time, each requiring a full pass through every layer.
  4. Repeat step 3 until the model produces a stop token or hits the length limit.
Prefill phase

Processes all input tokens at once.

Compute-bound. Arithmetic speed limits this phase because all prompt tokens are available at once.

Decode phase

Generates one token per step.

Memory-bound. Memory bandwidth limits this phase. The GPU loads all model weights for each new token.

A GPU can perform arithmetic faster than memory can supply data. Many inference methods reduce memory transfers or reuse transferred data.

Matrix multiplication accounts for most work

Inside each transformer layer, almost all the work is matrix multiplications (GEMMs). There are two main groups:

Attention

Feed-Forward Network (FFN)

For an M×K matrix times a K×N matrix, the compute cost is:

FLOPs = 2 × M × K × N

A model like LLaMA 7B has hidden=4096 and FFN dim=11008. That single FFN Up projection is a 4096×11008 matrix. It performs about 45 million multiply-accumulates per token and per layer, repeated across 32 layers.

The FFN layers typically account for ~65% of all FLOPs. The attention projections add ~30%. The actual attention matmuls (QKT, Score×V) are only ~5% until sequence length gets very long.

Compute work and memory traffic

Every operation has two costs:

The ratio between them is arithmetic intensity:

Arithmetic Intensity = FLOPs / Bytes   (FLOP/byte)

Every GPU has a ridge point, the arithmetic intensity where compute speed and memory bandwidth are in balance. Below the ridge, the operation is memory-bound (waiting for data). Above it, the operation is compute-bound (GPU is fully utilized).

For example, the A100 has 312 TFLOP/s compute and ~1.7 TB/s effective bandwidth, giving a ridge point of about 180 FLOP/byte. Below 180 FLOP/byte, memory bandwidth limits the operation before compute speed does.

Decode at batch=1 has an arithmetic intensity of about 1 FLOP/byte. It uses only a small fraction of peak arithmetic speed. Batching increases this ratio, while quantization reduces the bytes transferred.

Useful inference estimates

The Cost Estimator compares these estimates with a more detailed performance model.