AI & ML Quick Path Progress

All cheat sheets

Cheat sheet: hardware, CUDA and running models

What a GPU and CUDA are, whether a model fits, how fast it can write, and what to do when it does not.

CPU or GPU

  • A CPU has a few strong cores that work one thing after another; a GPU has thousands of small cores that do the same sum on many numbers at once.
  • Neural networks are mostly matrix multiplication, which suits a GPU.
  • You do not need a GPU to start learning; rent one for big jobs.
Two panels. On the left, a CPU drawn as a few large cores that each do one task after another, good for general work. On the right, a GPU drawn as a grid of many small cores that all do the same simple sum on different numbers at once, good for the matrix multiplication at the heart of deep learning. CPU a few strong cores core core core core One thing after another, very fast GPU thousands of small cores The same sum on many numbers at once Neural networks are mostly matrix multiplication, which suits the GPU. The drawing is illustrative: real chips differ.
A CPU has a few strong cores; a GPU has thousands of small ones doing the same sum at once, which suits neural networks.

What CUDA is

  • CUDA is NVIDIA's platform for running calculations on its GPUs. Frameworks call it for you.
  • The GPU driver, the framework's CUDA build and the GPU must match. A framework install for CUDA brings its own CUDA libraries.
  • nvidia-smi shows the newest CUDA version the driver supports, not an installed toolkit.
A vertical stack of five layers with arrows between them: your code in Python or C#, a framework such as PyTorch, TorchSharp or ONNX Runtime, CUDA libraries and runtime, the GPU driver, and the GPU itself. Notes say the lower layers must match the GPU and each other, and that a full CUDA Toolkit is only needed to compile CUDA code. Your code Python or C# asking for a device Framework PyTorch, TorchSharp or ONNX Runtime CUDA libraries and runtime NVIDIA's code that runs the sums on the GPU GPU driver lets the operating system talk to the GPU The GPU cores and its own memory (VRAM) You work at the top: model.to("cuda") These layers must match the GPU and each other. A framework install for CUDA brings its own CUDA libraries; you need a driver that is new enough. nvidia-smi shows the newest CUDA version the driver supports, not a toolkit you installed.
You work at the top. The driver and the CUDA build under the framework must match the GPU and each other.

Choose the device in code

  • Ask the framework whether a GPU is available, then use it or the CPU.
  • The model and its inputs must be on the same device.
  • AMD builds of PyTorch still call the device "cuda"; Apple computers use "mps".
  • Print the device: a program can fall back to the CPU with no error.
device = "cuda" if torch.cuda.is_available() else "cpu"
model = model.to(device);  x = x.to(device)

Does it fit in VRAM?

  • Weights = parameters x bytes per weight.
  • The cache grows with the conversation; working space is extra.
  • Leave a safety margin of a few GB.
needed = weights + cache + working space
8 billion x 0.5 byte = 4 GB; cache for 8,192 tokens is about 1.07 GB
4 + 1.07 + 1 = 6.07 GB
A horizontal bar for an 8-billion-parameter model at 4 bits split into weights 4 GB, cache 1.07 GB and working space 1 GB, about 6.1 GB in all, against a line at 16 GB of GPU memory with spare room. Below it, the same model at 16 bits needs 18.07 GB and runs past the 16 GB line, so it does not fit. An 8-billion-parameter model, 8,192 tokens, on a 16 GB GPU At 4 bits 4 6.07 GB: fits, 9.93 GB to spare At 16 bits 18.07 GB: does not fit, 2.07 GB short GPU memory: 16 GB weights cache (grows with the conversation) working space (assumed 1 GB) The numbers are the made-up example in the lesson. Real programs need a safety margin of a few GB.
Weights, cache and working space must fit in VRAM. The same 8-billion model fits at 4 bits and does not fit at 16 bits on a 16 GB GPU.

Number formats

  • FP32: 4 bytes. FP16 and BF16: 2 bytes. INT8: 1 byte. 4-bit: half a byte.
  • BF16 keeps FP32's range with fewer digits, which makes training steadier; FP16 has a narrower range and needs more care.
  • Mixed precision does the heavy sums in 16 bits and keeps a few sensitive numbers in higher precision.

How fast can it write?

  • Every new token reads all the weights once, so memory speed sets an upper limit for one conversation.
  • Fewer bits means fewer bytes to read, so a smaller model is also faster.
  • Real speeds are lower than the limit.
tokens per second (at most) = bandwidth / size of the weights
400 GB/s / 4 GB = 100 tokens per second

When it does not fit

  • Try in order: fewer bits, a shorter context, offload layers to the CPU (slower), a bigger or rented GPU, a smaller model.
  • Accelerate can place layers on GPU, then CPU, then disk with device_map="auto".
  • llama.cpp has a setting for how many layers go to the GPU; runners such as Ollama choose a split.
Five steps from top to bottom, each costing a little more than the last: store the weights in fewer bits, shorten the context, offload some layers to the CPU which is slower, use a bigger GPU or rent one, or choose a smaller model. A note says to stop at the first one that works. 1. Fewer bits per weight a 4-bit file is a quarter of the size costs a little quality 2. Shorter context a smaller cache, fewer users at once costs shorter conversations 3. Offload layers to the CPU layers that do not fit run from system memory costs speed 4. Bigger GPU, or rent one more VRAM, or several GPUs costs money 5. A smaller model often enough for a narrow task costs ability Stop at the first step that works. Each one costs a little more than the last.
Try the cheapest fix first: fewer bits, then a shorter context, then offloading, then more hardware, then a smaller model.

Other hardware

  • AMD: ROCm. Apple: Metal, device "mps". ONNX Runtime picks an execution provider for the hardware it finds.
  • TPU: Google's matrix chip, mostly in Google's cloud. NPU: a small low-power AI unit in many laptops and phones.
  • Check the support list of your framework or runner before you buy.

Watch it and fix it

  • nvidia-smi shows memory used and how busy the GPU is; ollama ps shows how much of a model is on the GPU.
  • Slow or failing: does the system see the GPU, does the framework see it, are the model and inputs on it, does memory fit, is it busy.
  • Out of memory: fewer bits, shorter context, smaller batch, offload.