Gemma 4 Memory & Throughput on TPU v5e
In this article
This guide explains how to estimate the memory footprint and inference throughput of a Gemma 4 model on a Google TPU v5e slice. You'll learn to calculate the parameters, memory per token, and throughput based on batch size and sequence length, so you can plan capacity before deployment.
What You'll Need
- A TPU v5e slice (e.g., 1x1, 1x4, 2x4) with known total HBM (high-bandwidth memory). Each v5e chip has 16 GB HBM, so a 1x4 slice has 64 GB total.
- Model details for Gemma 4: number of parameters (e.g., 7B, 27B, 103B), hidden size, number of layers, and number of attention heads. These are listed in the model card.
- Basic knowledge of transformer architecture and matrix multiplication.
Step-by-Step
- Identify the model size. Determine the total parameter count (e.g., 7 billion, 27 billion). This is the primary driver of memory and compute.
- Calculate the base memory for weights. Multiply the number of parameters by the bytes per parameter. For bf16 (the typical TPU precision), that's 2 bytes. For int8, it's 1 byte. Example: 7B parameters × 2 bytes = 14 GB for weights.
- Add optimizer and training overhead (if training). Training requires extra memory for gradients and optimizer states. A common estimate is 12 bytes per parameter for Adam (weights, gradients, first and second moments). For inference, this is zero.
- Estimate activation memory. For inference, the main memory consumer after weights is the KV cache. The KV cache size per token is roughly 2 × (number of layers) × (hidden size) × (bytes per element). For example, a 7B model with 32 layers and hidden size 4096, in bf16, gives 2 × 32 × 4096 × 2 = 524,288 bytes per token (0.5 MB). Multiply by the sequence length and batch size to get total KV cache memory.
- Sum up the memory. Total memory = weights + KV cache + activations (usually small, ~10% of weights). Compare this to the slice's HBM. If it exceeds, you need to reduce batch size, sequence length, or use quantization.
- Estimate compute time. The main compute is matrix multiplications. A rough formula for inference FLOPs per token is 2 × (number of parameters) × (sequence length). For a 7B model and 2048 tokens, that's 2 × 7e9 × 2048 ≈ 2.87e13 FLOPs per token.
- Calculate throughput. TPU v5e has a peak of ~197 teraFLOPs per chip (in bf16). Divide the FLOPs per token by the peak FLOPs to get the theoretical latency per token. Multiply by batch size to get tokens per second. For example, 2.87e13 FLOPs / 197e12 FLOPs/s ≈ 0.146 s per token. With batch size 8, that's 8 / 0.146 ≈ 55 tokens per second.
- Apply a real-world efficiency factor. Achievable throughput is typically 30-50% of theoretical peak due to memory bandwidth, communication, and kernel overhead. Multiply your theoretical result by 0.3 to 0.5.
Common Mistakes
- Forgetting the KV cache. Many people only count weights, but for long sequences, KV cache can dominate memory.
- Using fp32 instead of bf16. TPUs run bf16 by default; using fp32 doubles memory and halves throughput.
- Ignoring the difference between training and inference. Training uses far more memory due to gradients and optimizer states.
- Assuming 100% efficiency. Real-world throughput is always lower than theoretical peak.
Tips
- Use the model card's exact hidden size and layer count for accurate KV cache calculations.
- If you're unsure about the slice size, start with a single v5e chip (16 GB) and scale up.
- Consider using int8 quantization for weights to halve memory and increase throughput.
- For long sequences, use a lower precision for the KV cache (e.g., int8) to save memory.
Troubleshooting
- Out of memory (OOM) errors: Reduce batch size or sequence length. If you're training, reduce the optimizer memory by using a memory-efficient optimizer like Adafactor.
- Throughput much lower than expected: Check if you're using a single host. Multi-host slices require efficient data parallelism. Also, ensure you're not hitting a memory bottleneck that causes swapping.
- Incorrect KV cache size: Double-check the number of layers and hidden size. Some models use grouped-query attention, which reduces the KV cache size; adjust accordingly.
Frequently Asked Questions
How much memory does a 7B Gemma 4 model need on a TPU v5e?
For inference in bf16, the weights take 14 GB. Add KV cache for your sequence length. A 2K sequence with batch 1 needs about 1 GB, so ~15 GB total, which fits on a single 16 GB chip.
Can I run a 27B model on a single v5e chip?
No, because the weights alone need 54 GB in bf16, exceeding the 16 GB HBM. You'd need at least 4 chips (64 GB) for inference, and more for training.
What is the theoretical peak throughput of a v5e chip?
Each v5e chip has a peak of ~197 teraFLOPs in bf16. For a 7B model, that's roughly 7,000 tokens per second theoretical, but real-world is around 2,000-3,000 tokens per second.
Does sequence length affect memory or throughput more?
Sequence length affects both. Memory grows linearly with sequence length due to KV cache. Throughput also decreases because the model must process each token sequentially.
How do I know my slice's total HBM?
Multiply the number of chips by 16 GB. For example, a 2x4 slice has 8 chips, so 128 GB total.
Is it better to use int8 quantization for Gemma 4?
Yes, for inference it halves memory and can increase throughput, but may slightly reduce accuracy. Use it if your model tolerates the precision loss.