Hardware and memory estimates
Estimate weight storage before choosing hardware
A model with one billion parameters stored at eight bits per parameter requires one billion bytes for ideal packed weights: 1 GB in decimal units, or about 0.931 GiB (1 GiB = 1,073,741,824 bytes). A parameter is a learned numerical value; bit width describes its representation. This is a lower-bound storage calculation, not a model download size or peak memory guarantee. The function rounds up to the next byte and restricts the supported bit widths for the exercise. Compare 16-bit, 8-bit and 4-bit storage. Three 4-bit values need two bytes even with ideal packing.
def weight_bytes(parameters, bits): if type(parameters) is not int or parameters < 0: raise ValueError("invalid parameter count") if type(bits) is not int or bits not in (4, 8, 16, 32): raise ValueError("unsupported precision in this lab") return (parameters * bits + 7) // 8
for bits in (16, 8, 4): print(bits, weight_bytes(1_000_000_000, bits))print(weight_bytes(3, 4))Choose a compute and memory layout
A CPU handles general-purpose work such as tokenization, orchestration and inference on small or optimized models. GPUs and other accelerators specialize in parallel numerical operations; their performance depends on supported operations, precision, memory bandwidth, kernels and workload. RAM is host memory; VRAM usually means dedicated device memory. Integrated devices may share memory, so check the architecture rather than adding RAM and VRAM as if they were freely interchangeable. Moving tensors between host and device can add latency. Quantization reduces representation width, but scales, zero points, alignment, unquantized layers and runtime workspaces consume extra storage. Lower precision may change quality and may not accelerate a device without appropriate kernels. Test the same quality suite before choosing a representation.
Account for concurrent contexts
For a simplified decoder with uniform attention layers, an uncompressed key/value cache estimate in bytes is 2 × layers × KV heads × head dimension × cached tokens per sequence × active sequences × bytes per element. The factor two counts keys and values. Use KV heads, not necessarily query heads, because grouped-query attention shares key/value heads. This assumes all listed layers cache the same number of tokens and every sequence has the same cached length. It excludes allocator fragmentation, paged blocks, sharing, padding, cross-attention and transient workspace. Here the example uses 16 layers, four KV heads, head dimension 64, 1,024 tokens and two-byte cache elements. Two sequences need 33,554,432 bytes of cache; 32 need 536,870,912. With a hypothetical 200 MB overhead and 2 GB device capacity, both estimates fit, but increasing concurrency further may make the signed headroom negative. A nonnegative estimate is not a fit guarantee. A quantized weight format does not automatically quantize the cache.
def memory_budget(weights, layers, kv_heads, head_dim, tokens, sequences, element_bytes, overhead, capacity): values = (weights, layers, kv_heads, head_dim, tokens, sequences, element_bytes, overhead, capacity) if any(type(x) is not int or x < 0 for x in values): raise ValueError("nonnegative integer units required") if min(layers, kv_heads, head_dim, element_bytes) == 0: raise ValueError("invalid architecture") cache = 2 * layers * kv_heads * head_dim * tokens * sequences * element_bytes total = weights + cache + overhead return cache, total, capacity - total
print(memory_budget(1_000_000_000, 16, 4, 64, 1024, 2, 2, 200_000_000, 2_000_000_000))print(memory_budget(1_000_000_000, 16, 4, 64, 1024, 32, 2, 200_000_000, 2_000_000_000))Plan a bounded hardware test
Start with the actual model configuration, artifact size, supported dtype and context limit. Reserve additional headroom for temporary activations and memory allocated by other processes. On authorized hardware only, record runtime and driver versions, warm-up policy, batch size, input and generated lengths, concurrent requests, actual peak host and device allocation and failures. Limit total requests, stop on memory pressure, and avoid downloading large models just for this arithmetic exercise. Cache offloading may lower device memory at a transfer cost; cache quantization may also hurt latency in some workloads. Compare quality as well as resource use. In this chapter no hardware is measured; the example is a planning tool, not a recommendation for a particular GPU.
Practice and common mistakes
Compute the ideal size of two billion parameters at four bits. Then change the sequence count to 64 in the cache example and examine negative headroom. Explain why more device RAM does not automatically mean more tokens per second. Avoid mixing decimal GB and binary GiB, equating disk file size with VRAM, ignoring output context growth, or assuming lower bit widths always improve latency. Write your assumptions next to each estimate and link to the model card and memory specification when applying this method to real hardware.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Hardware and memory estimates quiz
Assess memory estimation, inference performance and cost accounting.
What is ideal packed storage for one billion 8-bit parameters?
Which storage does the ideal weight estimate exclude?
What happens to this cache estimate when active sequences double?
Why use KV heads in the cache formula?
What does positive estimated headroom prove?