Mhonn YbañezSRE, DevOps, AI Engineer
 Today I Learned
Read the full page

What is CUDA and what does it do?

Posted
Tags
ai, gpu, fundamentals

Python runs on the CPU, but training a model is millions of small calculations that a GPU can do in parallel. CUDA is the Nvidia stack that carries the work across: kernels that do the calculation, ready-made libraries of them, a runtime that starts them, and a driver that talks to the GPU.

I came across a KodeKloud short about CUDA while browsing YouTube shorts. I keep seeing "CUDA" in every AI conversation, and I realized I couldn't say what it actually is.

The question I had

What is CUDA, and what does it do between my Python code and the GPU?

What I found

Why a GPU at all

Training a model is basically millions of small calculations on numbers. A CPU has maybe 8 or 16 strong cores, so it can do only a few of those at a time. A GPU like the Nvidia RTX 5090 has about 22,000 much simpler cores, so it can do thousands at once. (The core counts aren't a like for like comparison, since GPU cores are far simpler.)

But my Python code runs on the CPU. So how does the work reach the GPU? That is where CUDA comes in. CUDA is Nvidia's platform for running calculations on Nvidia GPUs.

Follow one line of code

CUDA isn't one thing. I found it easiest to follow a single PyTorch line and see what each part does:

model = model.to("cuda")   # copy the model's weights into GPU memory
input = input.to("cuda")   # same for the data
output = model(input)      # this is the line the GPU ends up working on

Here "cuda" is just the name PyTorch uses for an Nvidia GPU. The first two lines copy the model and the data into GPU memory, which has to happen before the GPU can touch them.

On the third line, PyTorch sees that the first step is to multiply one big table of numbers by another. Say the answer has a million numbers in it. From here, four parts of CUDA take over:

PartWhat it doesIn this example
KernelA small program written for the GPU. Thousands of cores run it on different pieces of the data at onceThe multiplication of two big tables
Kernel librariesReady-made kernels for common operations, written by NvidiaPyTorch calls a library named cuBLAS, which picks the right kernel
RuntimeTakes a request like "run this kernel on this data" and starts it on the GPULaunches the chosen kernel
DriverLets the operating system talk to the GPU hardware, the way a printer needs a driverPuts the request in a queue the GPU keeps reading from

A note on the word kernel: here it means a function that runs on the GPU. It has nothing to do with the operating system kernel.

I never wrote a kernel myself. That is the point of the libraries: the common ones already exist, so PyTorch just calls them.

Getting the answer back

The GPU picks up the request, runs the kernel on thousands of cores, and the million numbers are ready. The result stays in GPU memory. It is copied back to the CPU only when my code needs it, for example when I print it.

(The four parts are the short's way of teaching it, not an official list. CUDA also has a compiler and a way to write your own kernels, which I haven't looked at.)

Why people talk about it so much

CUDA only works on Nvidia GPUs. So much of the AI software world is built on top of it that it is one big reason so many companies want those GPUs. It also connects to how models are made in the first place: the training step in the model lifecycle is exactly this kind of heavy math.

Are there alternatives?

If CUDA only works on Nvidia, what do other GPUs use? I'd guess they do something similar, handing the heavy math to GPU cores instead of CPU cores. The names I found:

  • ROCm (AMD)
  • Metal (Apple)
  • oneAPI and SYCL (Intel, and aimed at working across vendors)
  • OpenCL (works across vendors, older)
  • Vulkan compute (works across vendors)

I haven't looked into any of them yet. I only wanted to know what exists.

Takeaway

CUDA is the whole path from Python to the GPU: kernels do the calculation, libraries hold the kernels, the runtime starts them, and the driver passes the work to the hardware. I write one line and never see any of it.