Phase 0 · Lesson 3 of 13

GPU Setup & Cloud

Training on CPU is fine for learning. Training for real needs a GPU.

Lesson 3 of 13Build~45 minutesPython

Before this: Lesson 1 of Phase 0

On this page

Learning Objectives

  • Verify local GPU availability using nvidia-smi and PyTorch's CUDA API
  • Configure Google Colab with a T4 GPU for free cloud-based experiments
  • Benchmark matrix multiplication on CPU vs GPU and measure the speedup
  • Estimate the largest model that fits in your VRAM using the fp16 rule of thumb

The Problem

Most lessons in phases 1-3 run fine on CPU. But once you start training CNNs, transformers, or LLMs (phases 4+), you need GPU acceleration. A training run that takes 8 hours on CPU takes 10 minutes on GPU.

You have three options: local GPU, cloud GPU, or Google Colab (free).

The Concept

Your options:

1. Local NVIDIA GPU
   Cost: $0 (you already have it)
   Setup: Install CUDA + cuDNN
   Best for: Regular use, large datasets

2. Google Colab (free tier)
   Cost: $0
   Setup: None
   Best for: Quick experiments, no GPU at home

3. Cloud GPU (Lambda, RunPod, Vast.ai)
   Cost: $0.20-2.00/hr
   Setup: SSH + install
   Best for: Serious training, large models
s0-gpu-dispatch

Build It

Option 1: Local NVIDIA GPU

Check if you have one:

nvidia-smi

Install PyTorch with CUDA. On Linux the default wheel ships with CUDA. On Windows, copy the CUDA command from the selector at pytorch.org.

pip install torch torchvision torchaudio

Then check that PyTorch sees the GPU:

import torch

print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
if torch.cuda.is_available():
    print(f"GPU: {torch.cuda.get_device_name(0)}")
    print(f"Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")

Option 2: Google Colab

  1. Go to colab.research.google.com
  2. Runtime > Change runtime type > T4 GPU
  3. Run !nvidia-smi to verify

Upload notebooks from this course directly to Colab.

Option 3: Cloud GPU

For Lambda Labs, RunPod, or Vast.ai:

ssh user@your-gpu-instance

pip install torch torchvision torchaudio
python -c "import torch; print(torch.cuda.get_device_name(0))"

No GPU? No problem.

Most lessons work on CPU. The ones that need GPU will say so and include Colab links.

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using: {device}")

Build It: GPU vs CPU benchmark

import torch
import time

size = 5000

a_cpu = torch.randn(size, size)
b_cpu = torch.randn(size, size)

start = time.time()
c_cpu = a_cpu @ b_cpu
cpu_time = time.time() - start
print(f"CPU: {cpu_time:.3f}s")

if torch.cuda.is_available():
    a_gpu = a_cpu.to("cuda")
    b_gpu = b_cpu.to("cuda")

    torch.cuda.synchronize()
    start = time.time()
    c_gpu = a_gpu @ b_gpu
    torch.cuda.synchronize()
    gpu_time = time.time() - start
    print(f"GPU: {gpu_time:.3f}s")
    print(f"Speedup: {cpu_time / gpu_time:.0f}x")

Exercises

  1. Run the benchmark above and compare CPU vs GPU times
  2. If you don't have a GPU, run it on Google Colab and compare
  3. Check how much GPU memory you have and estimate the largest model you can fit (rule of thumb: 2 bytes per parameter for fp16)

Hints

Exercise 1. Run the code from the Build It section as it is, then write down the CPU time, the GPU time, and the speedup line. Run it a second time; the first GPU run includes start-up cost, so the second numbers are the ones to compare.

Exercise 2. In Colab, select Runtime > Change runtime type > GPU, then paste the benchmark into a cell. Run !nvidia-smi in another cell first; it must show a GPU name before the benchmark result means anything.

Exercise 3. Read the total memory with torch.cuda.get_device_properties(0).total_memory / 1e9, as in code/gpu_check.py. Divide the memory in bytes by 2 to get the parameter count for fp16, then keep a margin, because activations and the CUDA context also use memory.

Key Terms

Term What people say What it actually means
CUDA "GPU programming" NVIDIA's parallel computing platform that lets you run code on the GPU
VRAM "GPU memory" Video RAM on the GPU, separate from system RAM. Limits model size.
fp16 "Half precision" 16-bit floating point, uses half the memory of fp32 with minimal accuracy loss
Tensor Core "Fast matrix hardware" Specialized GPU cores for matrix multiplication, 4-8x faster than regular cores

Check yourself

Quick quiz

  1. Why is a GPU faster than a CPU for training neural networks?

    1. GPUs have higher clock speeds than CPUs
    2. GPUs can perform thousands of parallel matrix operations simultaneously
    3. GPUs use a more efficient programming language
    4. GPUs have more RAM than CPUs
    Show the answer

    B. GPUs can perform thousands of parallel matrix operations simultaneously GPUs have thousands of cores optimized for parallel computation, making them ideal for the matrix multiplications that dominate neural network training.

  2. What does VRAM refer to?

    1. The total RAM available across all devices
    2. A type of CPU cache memory
    3. Video RAM on the GPU, separate from system RAM
    4. Virtual RAM used by the operating system for swap space
    Show the answer

    C. Video RAM on the GPU, separate from system RAM VRAM (Video RAM) is the dedicated memory on a GPU. It limits the size of models and batch sizes you can use during training, separate from your system's main RAM.

  3. What command verifies that your NVIDIA GPU is detected and shows its current status?

    1. torch.cuda.list_devices()
    2. nvidia-smi
    3. lspci | grep gpu
    4. gpu --status
    Show the answer

    B. nvidia-smi nvidia-smi (NVIDIA System Management Interface) displays GPU utilization, memory usage, temperature, and running processes. It is the standard tool for verifying GPU availability.

  4. When benchmarking GPU vs CPU matrix multiplication, why must you call torch.cuda.synchronize() before measuring GPU time?

    1. To transfer data from CPU to GPU memory
    2. To ensure all GPU operations have completed before stopping the timer
    3. To free unused GPU memory
    4. To reset the GPU clock speed to its base frequency
    Show the answer

    B. To ensure all GPU operations have completed before stopping the timer GPU operations are asynchronous -- Python returns immediately while the GPU is still computing. synchronize() blocks until all GPU operations finish, giving accurate timing.

  5. Using the fp16 rule of thumb, approximately how many parameters can fit in 24 GB of VRAM?

    1. 6 billion parameters
    2. 24 billion parameters
    3. 12 billion parameters
    4. 48 billion parameters
    Show the answer

    C. 12 billion parameters In fp16, each parameter uses 2 bytes. 24 GB / 2 bytes = 12 billion parameters. This is a rough estimate; actual usage includes activations, gradients, and optimizer states.

Questions people ask

Do I need a GPU to use AI in my business?

Usually no. If you call hosted models through an API, the provider runs the GPUs for you. You only need your own or rented GPUs when you train or fine-tune models, or run open models yourself for cost, privacy or speed reasons.

What is CUDA and why does AI software keep asking for it?

CUDA is NVIDIA's software layer that lets programs like PyTorch run math on NVIDIA GPUs. Most deep learning tools are built on it, so the drivers and CUDA version must match the library version. Apple chips use a different backend called MPS.

Next lessonAPIs & KeysPrevious: Git & Collaboration

Lessons from AI Engineering from Scratch by Rohit Ghumare and contributors, MIT license. Notes for owners by Lozo. License

Next step

Rather have it built for you?

Reading is optional. Tell me what's stuck and I'll build it into the business you already run.

  • A real conversation, not a pitch
  • You keep your accounts
  • Nothing sends without your yes