Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

AI Model Size: Memory & Inference Cost

· 7 min read · Home Tech

In this article
  1. Formula
  2. Worked Example
  3. Why Model Size Matters
  4. Memory Requirements and Hardware Limits
  5. Inference Cost – Compute and Latency
  6. How to Choose the Right Model Size
  7. Common Misconceptions
  8. Frequently Asked Questions

AI model size directly determines how much memory (RAM or VRAM) you need to load it and how much compute (and therefore money) each prediction, or inference, costs. Larger models with more parameters require exponentially more memory and produce slower, more expensive inferences, while smaller models trade some accuracy for speed and efficiency.

in p | Parameters (billions) | B | 7 | 0
in m | Memory per parameter (bytes) | bytes | 2 | 0
in c | Compute per token (FLOPs) | FLOPs | 2 | 0
out mem | Model memory (GB) | GB | p * m / 1e9 | 2
out flops | Inference FLOPs per token | GFLOPs | p * c | 2

Formula

Model memory (GB) = Parameters (billions) × Memory per parameter (bytes) ÷ 1,000,000,000. Inference compute per token (GFLOPs) = Parameters (billions) × Compute per token (FLOPs).

  • p – number of parameters in billions (e.g., 7 for a 7B model).
  • m – memory used per parameter in bytes (2 for FP16, 4 for FP32, 1 for INT8).
  • c – approximate FLOPs per parameter per token (typically 2 for a forward pass).

Worked Example

Take the default values: a 7B parameter model, 2 bytes per parameter (FP16), and 2 FLOPs per parameter per token.

  1. Memory = 7 × 2 ÷ 1e9 = 0.014 GB (14 MB). Wait – that seems too low. Actually, 7 billion × 2 bytes = 14 billion bytes = 14 GB. The formula above divides by 1e9, giving 14 GB. The calculator shows 0.01 because the input is in billions (7), so the formula p * m / 1e9 = 7 * 2 / 1e9 = 0.000000014? No – let’s correct: p is 7 (billions), m is 2 bytes, so p * m = 14 (billion-bytes). Divide by 1e9 to get GB: 14 GB. The calculator expression should be p * m / 1e9, but that gives 14e-9? Actually 7 * 2 = 14, then /1e9 = 1.4e-8. That’s wrong. The correct expression is p * 1e9 * m / 1e9 = p * m. So the calculator formula is wrong. Let me fix it.

Correct formula: Memory (GB) = Parameters (billions) × 1e9 × bytes per parameter ÷ 1e9 = Parameters (billions) × bytes per parameter. So for 7B and FP16 (2 bytes) = 14 GB. Inference FLOPs per token = Parameters (billions) × 1e9 × FLOPs per parameter per token = 7e9 × 2 = 14 GFLOPs (but the calculator shows 14? Actually 7 * 2 = 14 GFLOPs, but that’s only if we treat p as billions and multiply by 1e9? No – the expression p * c gives 7 * 2 = 14, but that’s 14 GFLOPs? Actually 7 billion parameters * 2 FLOPs per parameter = 14 billion FLOPs = 14 GFLOPs. So the expression p * c works if p is in billions and c is FLOPs per parameter per token. The memory expression should be p * m (since p is in billions and m is bytes per parameter, giving billions of bytes, which is GB). So the calculator block needs fixing.

Let me correct the block now.

in p | Parameters (billions) | B | 7 | 0
in m | Memory per parameter (bytes) | bytes | 2 | 0
in c | Compute per token (FLOPs) | FLOPs | 2 | 0
out mem | Model memory (GB) | GB | p * m | 2
out flops | Inference FLOPs per token | GFLOPs | p * c | 2

Now the worked example: p=7, m=2, c=2. Memory = 7 × 2 = 14 GB. FLOPs = 7 × 2 = 14 GFLOPs. That matches.

Why Model Size Matters

Model size is usually measured in parameters – the number of weights the model learned during training. A 7B model has 7 billion parameters. Each parameter is stored as a number, typically in a floating-point format. The most common formats are FP32 (4 bytes), FP16/BF16 (2 bytes), and INT8 (1 byte). The total memory to load the model is roughly parameters × bytes per parameter. For inference, you also need memory for activations, KV cache, and overhead, but the weights dominate for large models.

Memory Requirements and Hardware Limits

To run a 7B model in FP16, you need at least 14 GB of VRAM (or RAM if using CPU). That fits on a single consumer GPU like an RTX 4090 (24 GB) but not on a 12 GB card. A 70B model in FP16 needs 140 GB – that requires multiple GPUs or a server. Quantization reduces memory: INT8 halves it (70 GB for 70B), INT4 quarters it (35 GB). But lower precision can degrade accuracy, especially for complex reasoning.

Inference Cost – Compute and Latency

Inference cost is the compute needed to generate one token (a word piece). Roughly, each token requires 2 × parameters FLOPs for a forward pass. For a 7B model, that’s 14 GFLOPs per token. A modern GPU can do about 100 TFLOP/s (FP16), so you could generate ~7,000 tokens per second theoretically, but real-world is much lower due to memory bandwidth and overhead. The cost in dollars depends on the hardware, electricity, and cloud pricing. For example, a 7B model might cost $0.002 per 1K tokens, while a 70B model might cost $0.02 per 1K tokens – a 10x difference.

How to Choose the Right Model Size

  • Start with the smallest model that meets your accuracy needs. Often a 7B or 13B is enough for simple tasks.
  • If you need high-quality reasoning or code generation, consider 30B or 70B, but be ready for higher cost.
  • Use quantization to fit a larger model into limited memory, but test accuracy on your specific data.
  • Consider distillation – train a small model to mimic a large one – to get better accuracy per parameter.

Common Misconceptions

One common mistake is thinking that more parameters always mean better performance. In practice, a well-trained 7B model can outperform a poorly trained 13B model. Also, memory for inference is not just the weights – you need to account for the KV cache, which grows with sequence length. For long contexts, the KV cache can be larger than the weights. Another misconception is that FP16 is always half the memory of FP32 – that’s true, but many models are trained in BF16, which has the same memory footprint as FP16 but different range.

Frequently Asked Questions

How much VRAM do I need for a 7B model?

For FP16, you need at least 14 GB of VRAM for the weights alone. Add about 1-2 GB for activations and overhead, so 16 GB is safe. For INT8, 7 GB is enough, and INT4 would be around 4 GB, but you need to use quantization libraries.

What is the difference between FP16 and INT8 quantization?

FP16 stores each weight in 16 bits (2 bytes) and preserves more precision. INT8 stores weights in 8 bits (1 byte), halving memory and speeding up inference, but can lose some accuracy. INT4 (4 bits) is even smaller but often requires careful calibration to avoid degradation.

Why does inference cost more for larger models?

Larger models require more FLOPs per token, so they take longer to generate each token. That means more GPU time, which translates to higher cost. Also, they need more memory, which may require more expensive hardware or multiple GPUs.

Can I run a 70B model on a single GPU?

Not in FP16 – it needs 140 GB. With INT4 quantization, it needs about 35 GB, which fits on a high-end GPU like an A100 40GB or RTX 6000 Ada (48GB). But even then, inference will be slow because of memory bandwidth.

How does sequence length affect memory?

The KV cache grows linearly with sequence length and the number of layers. For a 7B model, a 4K token context might use about 1-2 GB of extra memory, but for 32K tokens it can be 8-16 GB. This is why long-context models need more memory.

Is there a rule of thumb for cost per token?

A rough estimate: cost per token is proportional to the number of parameters. If a 7B model costs $X per 1K tokens, a 70B model will cost about 10X, assuming the same hardware and utilization. Cloud providers often price based on GPU hours, so faster inference on a smaller model is cheaper.