Overview

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.

FAQ

What is CUDA, and why is it important for LLM workloads?CUDA is NVIDIA’s programming model for writing code that runs directly on the GPU. For LLMs, it matters because the biggest speed gains often come from how effectively GPU hardware is used. PyTorch calls into CUDA under the hood, but CUDA gives you direct control when built-in framework operations are not fast enough.
Why is PyTorch often enough for deep learning, and when is it not?PyTorch handles the vast majority of deep learning use cases well because it dispatches to highly optimized libraries and kernels. You usually need custom CUDA only when you hit a bottleneck, need a new operation the framework does not provide, or need performance beyond what standard kernels can deliver.
What kinds of engineers typically write custom CUDA kernels?Examples include framework contributors implementing new attention variants, kernel engineers optimizing long-context LLM training, edge deployment engineers reducing latency on embedded GPUs, and algorithms researchers building new architectures such as FlashAttention that need custom implementations to reach competitive performance.
What is the difference between the host and the device in CUDA?The host is the CPU and system memory where your main program runs. The device is the GPU and its dedicated memory, where the parallel work executes. CUDA programs coordinate work between these two separate environments.
What is a CUDA kernel?A CUDA kernel is a function that runs on the GPU. Unlike a normal function that runs once, a kernel is launched across thousands of threads, with each thread handling a small part of the overall computation independently.
How do I know if a deep learning operation is a good candidate for GPU parallelization?Look for patterns where the same operation is applied to many independent pieces of data. Good signs include element-wise operations, large matrix multiplications, convolutions, attention, and gradient computations. These are all data-parallel and map well to GPU execution.
When does GPU acceleration not help?GPU acceleration may not help for small workloads, low arithmetic intensity operations, or irregular workloads with unpredictable branching. In those cases, overhead from data transfer, kernel launches, and control-flow divergence can outweigh the benefits of parallel execution.
Why are GPUs so effective for deep learning?Deep learning involves massive numbers of similar operations on large arrays, especially matrix multiplications, convolutions, and attention. GPUs are built for throughput, with many simpler cores and much higher memory bandwidth than CPUs, which makes them ideal for these parallel workloads.
What is the GPU memory hierarchy, and why does it matter?GPU memory is layered from slowest and largest to fastest and smallest: global memory, L2 cache, shared memory or L1, and registers. Performance depends heavily on keeping data in the fastest possible tier for as long as possible, because memory access is often the main bottleneck.
What is the optimization ladder in this book?The book builds optimization in layers: naive kernels, memory and compute optimization, tensor cores, kernel fusion, quantization, and distributed computing. These techniques compound, so the performance gains from combining them can be much larger than any single improvement alone.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • CUDA for LLMs CUDA for LLMs ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • CUDA for LLMs CUDA for LLMs ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • CUDA for LLMs CUDA for LLMs ebook for free