ELLMEngineering Large
Language Models
Download PDF

Chapter 06

Estimating Memory and Runtime

On this page

“Will it fit?” and “Will it be fast enough?” are separate questions. A memory ledger answers the first; a model of arithmetic, transfers, and waiting begins to answer the second.

Establish one configuration

We will use an illustrative dense decoder with approximately seven billion parameters and the attention geometry from the preceding chapter. The parameter count is a rounded teaching value, not a claim about a particular published checkpoint. The following quantities will recur in the inference and accelerator parts:

Quantity Assumption
Parameter storage Seven billion values at two bytes each
Decoder geometry 32 layers; hidden width 4096
Attention heads 32 query heads; eight KV heads; width 128
KV element size Two bytes
Reference cached length 4,096 tokens per request

The shared device budget is 80,000,000,000 bytes (80 GB, about 74.51 GiB), with FP16 weights and an explicit 8 GiB non-KV reserve. examples/reference_config.py computes the same ledger used in Part III. A hypothetical 80 GiB device would be a different configuration.

Use decimal units for the 14 GB weight estimate and binary units for the exact 128 KiB-per-token cache calculation. Confusing GB and GiB can obscure an otherwise correct ledger; write the byte count when precision matters.

Count allocations that coexist

Inference needs the weights, the state of active requests, and temporary storage for executing operations. Our weights occupy about 14 GB. Sixteen requests at 4,096 cached tokens each need 16*512 MiB = 8 GiB of logical KV storage. Neither number includes workspace, reserved cache blocks, graph pools, or allocator fragmentation.

If all sixteen requests grow to 16,384 tokens, their logical cache grows to 32 GiB. The weight array is unchanged. This is why an application can load a model successfully and still run out of memory under long concurrent requests. “The weights fit” establishes only the shared part of the allocation.

Training adds other arrays. Under one explicit policy, two-byte weights take 14 GB, two-byte gradients another 14 GB, two FP32 AdamW moments 56 GB, and an FP32 master-weight copy 28 GB. That is 112 GB before activations and workspaces. Implementations that omit master weights or use other state formats change this total. The purpose of the example is to list the arrays, not to promote sixteen bytes per parameter as a universal constant.

Activations are intermediates needed for backward computation. Their size depends on batch shape, sequence length, implementation, and which values are recomputed instead of saved. Part II uses this tradeoff when choosing a batch; Part V explains how some parameter and optimizer arrays can be divided across devices.

Count transfers over a stated interval

Storage asks how many bytes coexist. Traffic asks how many bytes cross a particular memory or network path during an operation. A small array repeatedly read can generate substantial traffic. A large checkpoint read rarely may not lie on a request's critical path.

For an illustrative decode step at small batch size, suppose the execution reads approximately 14 GB of weights from device memory. At an idealized bandwidth ceiling of 3 TB/s, those reads alone require at least 14e9/3e12 seconds, about 4.7 ms. This is a bandwidth floor under the stated traffic assumption, not a measured token interval. KV reads, other operations, launches, and dependencies can add cost.

Batching changes the comparison. A matrix operation can reuse weight data across several requests in the same step, increasing useful arithmetic per transferred weight byte. Each request still brings its own growing state, and admitting more requests can increase waiting. We will need both a computation model and a serving policy to know whether the batch helps.

Compare arithmetic and bandwidth limits

Let an operation require F floating-point operations and transfer M bytes through the memory path under study. Let R be an applicable arithmetic-throughput ceiling and BW a bandwidth ceiling. An idealized time floor is:

2026-10-12T12:02:54.998720 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

The operation must pay for both arithmetic and transfer; this model permits them to overlap.

For F=10^12 FLOPs, M=10^11 bytes, R=10^14 FLOPs/s, and BW=10^12 bytes/s, the two terms are 0.01 and 0.10 seconds. The floor is 0.10 seconds. Doubling arithmetic throughput leaves the larger bandwidth term unchanged.

The ratio F/M is arithmetic intensity, measured in FLOPs per byte. Comparing it with R/BW indicates which ceiling limits the idealized operation. This roofline model is useful because it can reject an implausible optimization claim before a long implementation effort. It does not include every dependency, nor does it guarantee that a kernel reaches either ceiling. Use a valid ceiling for the operation's dtype and memory path, not a throughput observed on an unrelated workload.

Apply the estimate to a clear interval. Prompt processing runs many token positions together and can reuse weights across them. Decode introduces one position per active request at a time and reads retained history. Their shapes differ even when the model does not. Part III studies the resulting service behavior; Part IV studies the kernels executing those shapes.

Add the waiting time

A fast isolated request is not evidence of a fast loaded service. If work arrives while all suitable execution slots are occupied, the request waits. Variability in prompt and answer length means some requests hold resources longer than others.

A simple queue illustrates the effect. An M/M/1 model assumes Poisson arrivals, independent exponential service times, one server, and an unlimited waiting room. If arrival rate is lambda and service rate is mu, its stationary mean time in the system is:

2026-10-12T12:02:55.006720 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

This waiting-plus-service mean exists only when lambda is less than mu under the stated queue assumptions.

At a service rate of 10 requests per second, increasing arrivals from 5 to 9 per second raises the mean from 0.2 to 1 second. The mean service time itself remains 0.1 second. Actual LLM scheduling is more complicated: batching couples requests, memory constrains admission, and generation lengths are variable. The formula explains why headroom matters; it does not predict tail latency for an LLM server.

To compare two servers, preserve the workload that creates those queues. Suppose half the requests have 1,000 input tokens and 100 output tokens, and the other half have 8,000 input and 800 output tokens. Sampling input and output lengths independently invents request combinations that were not in the original traffic. Using only averages removes the largest allocations. Record paired lengths and arrival times, then replay the trace or state clearly which simplified distribution you use.

Turn capacity into a cost estimate

Useful throughput counts tasks that meet the relevant quality and timing requirements. This is often called goodput. For interactive completion, a suggestion arriving after the user has continued typing may be unusable. For an overnight extraction job, the relevant timing condition may be the collection's completion deadline. The resource model is shared, but the condition for useful work comes from the task.

An attempt that fails validation still contributes to cost. For a measured run with a nonzero number of successful tasks:

2026-10-12T12:02:55.014589 image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Include the cost of failed attempts and retries in the run cost.

For planning under a stable workload, cost per attempt divided by success probability gives a long-run cost-per-success ratio. At $1 per attempt and 40% success, that ratio is $2.50. At $1.20 and 55% success it is approximately $2.18. The cheaper attempt does not give the cheaper useful outcome. A finite run with zero successes has no finite observed cost per success, and its expected ratio is not the same mathematical object as this long-run planning ratio.

These estimates expose tradeoffs rather than deciding them. A longer input may improve task success while reducing concurrent capacity; a faster model may produce more unusable results. The ledger and time model predict the resource effects. The next chapter asks how to measure the behavioral effects and decide whether the complete change is worthwhile.

Exercises and worked answers

  1. How much logical KV state do sixteen 16,384-token requests use? 32 GiB under the stated geometry. Add weights and all other coexisting allocations before comparing with device capacity.
  2. Why does a 14 GB model not necessarily generate quickly on a device with enough memory? Capacity says it can be stored. Repeated transfers, arithmetic, and dependencies determine how quickly its forward computation completes.
  3. Why use the maximum of the arithmetic and transfer floors? Both constraints must be satisfied, and the idealized model permits overlap. Serial phases or implementation overhead can make actual time larger.
  4. What must stay fixed when comparing throughput? The request mix, model behavior, admission and latency requirements, and accounting for failures. Otherwise the systems may be completing different work.