1 When PyTorch just isn’t enough
The chapter introduces CUDA as the low-level GPU programming layer beneath PyTorch, explaining that while high-level frameworks handle most deep learning needs, custom CUDA becomes essential when standard operations are not fast enough or do not support a specialized workload. It frames the book’s purpose as teaching readers how to recognize those limits, write their own GPU code, and eventually understand and optimize performance at a much deeper level. The overall message is that PyTorch remains the right default, but CUDA gives you the control needed when performance, hardware constraints, or novel model behavior demand more.
It then builds the core mental model of GPU computing: a CPU-based host coordinates work, while the GPU device executes kernels across thousands of threads with separate memory spaces. CUDA kernels are described as functions written for one thread but launched massively in parallel, making them ideal for deep learning patterns such as matrix multiplication, element-wise activations, convolutions, gradients, and attention. The chapter emphasizes that these workloads are naturally parallel because they repeat the same computation across large amounts of data, and that GPUs excel when operations are regular, data-parallel, and high-volume.
Finally, the chapter explains how performance depends heavily on memory hierarchy and optimization strategy. It highlights the GPU memory tiers from global memory down to registers, showing that much of CUDA optimization is about keeping data in faster memory and reducing costly transfers. From there, it outlines an optimization ladder that progresses from naive kernels to memory improvements, tensor cores, kernel fusion, quantization, and eventually multi-GPU scaling, with each layer compounding the gains of the previous ones. The result is a roadmap for thinking like a performance engineer: knowing when GPUs help, when they don’t, and how to turn deep learning workloads into efficient CUDA implementations.
A CPU dedicates more area to caches and control logic for a few powerful cores; a GPU dedicates more area to many simpler arithmetic cores.
The four-step CUDA workflow: allocate GPU memory, copy data to the device, execute the kernel, and copy results back.
Write code for one thread; CUDA launches thousands of parallel copies, each with a unique thread index.
Element-wise ReLU: each matrix element is processed by a dedicated thread running the same operation.
Memory hierarchy on a GPU
The optimization ladder shows multipliers at each level of optimization covered throughout the book. These represent the compound effects on efficiency and performance from latency, memory savings, occupancy, throughput, and cost savings.
Summary
- GPUs excel at throughput by running the same instruction across massive data sets in parallel, making them ideal for deep learning workloads.
- Host code (CPU) orchestrates work while device code (GPU) executes it. Every CUDA project involves explicit memory transfers between CPU RAM and GPU memory.
- CUDA launches thousands of threads organized into grids and blocks. Each thread computes a unique index to determine which data element it processes.
- Matrix multiplications, convolutions, activations, and attention computations all fit the “same operation, different data” pattern. Recognizing these opportunities is the first skill of a CUDA performance engineer.
- Fast kernels respect the GPU memory hierarchy: registers and shared memory are scarce but fast, and global memory is plentiful but slower. The key optimization strategy is to keep data in the fastest memory tier for as long as possible.
- The rest of this book climbs an optimization ladder, from writing your first CUDA program in chapter 2 to applying advanced memory and computation techniques in later chapters.
CUDA for LLMs ebook for free