Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 130/ 160
07. Deployment

GPU Instances

Choose and size GPU VMs for training and self-hosted inference.

IntermediateVRAM · Instance Types3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

A GPU instance is a rented computer with a powerful graphics chip for AI; you pick one big enough to hold your model and fast enough for your users without paying for more than you need.

Think of it like…

Choosing a truck: first make sure the cargo fits (memory), then how fast it must go and how many trips per hour (throughput), and finally whether to rent by the day, lease for a year or use a cheaper truck that may be recalled mid-route (spot).

Where you’ve seen it

The team wants to serve an 8B-parameter model. Weights in FP16 take 8B × 2 = 16 GB. On a 24 GB GPU (L4 or A10G class) with 90% usable memory (21.6 GB), subtracting weights and about 1.5 GB of overhead leaves 4.1 GB for the KV cache; with grouped-query attention (32 layers, 8 KV heads, head dimension 128, FP16) one token needs 2 × 32 × 8 × 128 × 2 = 131,072 bytes (128 KiB), so 4.1 GB holds about 31,000 tokens, roughly 7 concurrent 4,096-token sequences. On an 80 GB A100 or H100, 72 GB usable minus 16 GB weights and 2 GB overhead leaves 54 GB, about 412,000 tokens or 100 concurrent 4k sequences, so one big GPU can have far higher throughput per dollar when traffic is high enough. With 4-bit quantisation the weights shrink to about 4.5 GB, letting a 16 GB card serve it with room for KV. For fine-tuning, full Adam in mixed precision needs about 16 bytes per parameter: 7B × 16 = 112 GB plus activations (multiple GPUs), whereas QLoRA (a 4-bit base of about 3.5 GB plus small adapters) fits on a 24 GB card. At an illustrative $3.50 an hour for an A100-class GPU, 30% utilisation makes the effective price $3.50 / 0.30 = $11.67 per useful hour, which is why batching, autoscaling and scheduling matter.

Words you’ll hear

VRAM
GPU memory that must hold weights, KV cache and activations.
Quantisation
Storing weights in fewer bits (FP8, INT8, 4-bit) to reduce memory and bandwidth.
NVLink / interconnect
High-bandwidth links between GPUs needed for tensor-parallel serving and training.
Spot / preemptible
Discounted capacity that the provider can reclaim with short notice.

Visual diagram

Loading interactive diagram

Theory & deep dive

Sizing memory and compute

Serving: weight memory = parameters × bytes per parameter (70B in FP16 is 140 GB, in FP8 70 GB, in 4-bit about 35 GB). Add the KV cache: per token 2 (K and V) × layers × KV heads × head dimension × bytes (128 KiB for an 8B GQA model in FP16), multiplied by concurrent sequences × average context length, plus runtime overhead (CUDA context, activations, graphs: 1 to 3 GB). Keep headroom (typically 90% memory utilisation settings). Throughput depends on memory bandwidth for decoding (tokens per second ≈ bandwidth ÷ bytes read per token, roughly the model size at batch 1) and on compute for prefill, so higher-bandwidth GPUs (A100, H100) serve larger models and more concurrency far faster than 16 to 24 GB inference cards.

Training and fine-tuning: full fine-tuning with Adam in mixed precision stores FP16 weights (2 bytes), FP16 gradients (2) and optimiser states plus FP32 master weights (12) = about 16 bytes per parameter: a 7B model needs about 112 GB before activations, which means several 80 GB GPUs or sharding (FSDP, ZeRO). LoRA freezes the base model and trains small adapters (the base needs 14 GB in FP16 plus modest optimiser state), and QLoRA stores the base in 4-bit (about 3.5 GB for 7B), so fine-tuning fits on one 24 GB GPU. Multi-GPU jobs need NVLink or fast networking (InfiniBand or equivalent) for tensor and pipeline parallelism; single-GPU or data-parallel inference replicas are simpler and often cheaper when the model fits one card.

KV capacity on two GPUs

128 KiB per token. 24 GB card: 4.1 GB ÷ 131,072 B ≈ 31,300 tokens → 7 sequences of 4,096. 80 GB card: 54 GB ÷ 131,072 B ≈ 412,000 tokens → 100 sequences. About 3.3× the memory but roughly 14× the concurrency, because weights are a fixed cost.

Key takeaway: Compute weight and KV memory first; training needs about 16 bytes per parameter unless you use LoRA or QLoRA.

Choosing GPUs, pricing models and operating them

Match the GPU to the workload: small inference cards (T4, L4, A10G, 16 to 24 GB) suit small or quantised models and modest traffic; A100 or H100 (40 to 80 GB, high bandwidth) suit large models, high concurrency and fine-tuning; multi-GPU nodes with NVLink are for models that don't fit one card; consumer or alternative accelerators may fit development. Compare cost per million tokens at your realistic utilisation, not price per hour: a larger GPU at three times the hourly price may give far more than three times the throughput when batching is effective. Pricing: on-demand (flexible, most expensive), reserved or committed use (substantial discounts for steady load), spot or preemptible (often 50 to 70% cheaper but can be reclaimed; use for batch, evals and fault-tolerant workers with checkpointing and graceful drain). Quotas and regional availability can be binding, so request capacity early.

Operate for utilisation: effective price per useful hour = hourly price ÷ utilisation ($3.50 ÷ 0.30 = $11.67), so batch requests, autoscale on queue depth, schedule non-production pools to scale to zero, co-locate small models with multi-instance GPU (MIG) or time-slicing, and monitor GPU utilisation, memory, power and errors (DCGM). Pin drivers and CUDA versions in images, test cold-start time (pull plus weight load), keep a fallback for capacity shortages (smaller model, another region or provider API) and compare self-hosting with APIs using the break-even analysis (see GPU versus API cost). Watch for hidden costs: egress, storage for checkpoints, idle nodes in a cluster and people time.

Effective price

A100-class GPU at $3.50/hour. At 80% utilisation: $4.38 per useful hour. At 30%: $11.67. Increasing utilisation from 30% to 60% through batching and autoscaling halves the cost per token without changing hardware.

Key takeaway: Choose by cost per token at real utilisation, use spot for tolerant work and raise utilisation before buying bigger GPUs.

How it works, step by step

Step 1 of 4
ML engineer

1. Estimate memory

Compute weight memory for the chosen precision, KV cache per token and overhead; decide whether quantisation is acceptable.

8B FP16 = 16 GB · KV 128 KiB/token
Key numbers at this step
Training
≈ 16 B/param full FT

When to use it & trade-offs

What it is

GPU instances are cloud virtual machines with attached accelerators used to train, fine-tune and serve models. Choosing and sizing them is mostly a memory problem first (do the weights, KV cache and optimiser state fit?), then bandwidth and compute (tokens per second), interconnect (NVLink and fast networking for multi-GPU), availability and quota and price model (on-demand, reserved or committed, spot/preemptible). Practical sizing uses simple formulas: weight memory = parameters × bytes per parameter (FP16 2, FP8 1, 4-bit about 0.5), KV cache per token = 2 × layers × KV heads × head dimension × bytes, and full fine-tuning with Adam needs roughly 16 bytes per parameter before activations.

Why it matters: GPU time is the largest line item in many AI budgets, and the wrong choice is expensive either way: too small and the model doesn't fit or serves slowly, too large and you pay for idle silicon. Utilisation matters as much as price per hour: a GPU used 30% of the time costs more than three times its sticker rate per useful hour. Understanding memory arithmetic, quantisation and the trade-offs among GPU families lets you right-size and justify choices.

When to reach for it

  • Sizing self-hosted inference and fine-tuning
  • Comparing GPU options by cost per token
  • Planning capacity and quotas

When to avoid or be careful

  • Choosing by hourly price without utilisation
  • Ignoring KV cache when sizing for concurrency
  • Running spot GPUs for stateful work without checkpoints

GPU choices

Small inference GPUs (16–24 GB)
Best for: Quantised or small models, development and light traffic

Strengths: Cheap per hour; widely available.

Watch out: Low KV capacity; limited bandwidth.

A100 / H100 class (40–80 GB)
Best for: Large models, high concurrency and fine-tuning

Strengths: High bandwidth and memory; best throughput per dollar at scale.

Watch out: Expensive; scarce.

Multi-GPU NVLink nodes
Best for: Models too large for one GPU

Strengths: Can serve and train very large models.

Watch out: Complexity and cost.

Interview signals & numbers to know

When this topic comes up

  • “Will this model fit on an A10G?”
  • VRAM and KV cache
  • QLoRA on one GPU
  • Spot interruption handling
  • Cost per million tokens

Benchmarks & rules of thumb

params × bytes -
weight memory (2 for FP16, 1 for FP8, ~0.5 for 4-bit)
~16 bytes/param -
full fine-tuning memory with Adam in mixed precision
$3.50/0.30 = $11.67 -
effective price per useful hour at 30% utilisation

Common mistakes & follow-ups

Common interview mistakes

  • Forgetting KV cache and runtime overhead in memory estimates.
  • Assuming a bigger GPU always costs more per token.
  • Leaving idle GPU nodes running overnight.

Likely interviewer follow-ups

  • How would you decide which GPU to use for a 70B model?
  • How much memory does fine-tuning a 7B model need?
  • When is spot capacity appropriate?
Interview tip: Do the memory arithmetic aloud (weights, KV per token, overhead). It's the fastest way to show real sizing skill.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply GPU Instances with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Serverless Functions
Next topic
Ollama for Local Models