Hardware Acceleration & Custom Kernels
In high-throughput deep learning inference, standard high-level frameworks (e.g. naive PyTorch) introduce severe latency penalties: every elementary operation (such as a matrix multiply followed by a bias add, followed by GeLU activation) writes intermediate tensors back to slow off-chip memory (HBM/DRAM), only to read them back immediately in the next kernel launch.
This chapter details the systems architecture of Hardware Acceleration: GPU memory hierarchies, FlashAttention SRAM tiling and online softmax, writing custom kernels in OpenAI Triton, and compiling static execution graphs with TensorRT-LLM and CUDA Graphs.
Imagine a chef preparing a gourmet salad.
A naive chef takes a tomato from the pantry, carries it across the kitchen to the cutting board, slices it, carries the slices all the way back to the pantry, then walks back to get a cucumber, carries it to the board, slices it, and carries it back to the pantry. They spend 90% of their time walking back and forth (Memory-Bandwidth Roundtrips).
A master chef brings all ingredients to the cutting board once, chops everything in one fluid motion, tosses the salad in the bowl on the spot, and serves it immediately.
In GPU programming, that is Kernel Fusion and FlashAttention: keeping data inside the ultra-fast on-chip cutting board (SRAM) and never walking back to the slow pantry (HBM) until the final answer is ready.
1. The GPU Memory Hierarchy: SRAM vs HBM
To understand why custom kernels matter, inspect the memory hierarchy of an NVIDIA H100 GPU:
The 10x Reality: On-chip SRAM (Shared Memory) is over 4x to 8x faster and has 10x to 30x lower latency than off-chip HBM. If your algorithm writes intermediate matrices to HBM, you are paying a massive 400-cycle latency penalty for every operation.
2. FlashAttention: SRAM Tiling & Online Softmax
Standard attention computes:
In standard implementations, computing produces an attention matrix. For sequence length , requires over 512 MB of VRAM per attention head—materializing massive memory buffers and exceeding on-chip SRAM capacity.
The FlashAttention Breakthrough (Dao et al.)
FlashAttention computes exact attention without ever materializing the matrix in HBM:
- SRAM Tiling: are partitioned into blocks (e.g. and ) that fit entirely inside 228 KB of on-chip SRAM.
- Online Softmax: Using the mathematical identity of online normalization: FlashAttention rescales running attention sums on the fly as new tiles are read, requiring only memory instead of !
3. Authoring Custom GPU Kernels with OpenAI Triton
Historically, writing custom GPU kernels required low-level CUDA C++, manual warp-divergence synchronization (__syncthreads()), and pointer arithmetic.
OpenAI Triton allows developers to write high-performance GPU kernels directly in Python:
Triton's compiler automatically handles memory coalescing, shared memory bank conflict resolution, and Tensor Core scheduling, generating assembly that matches hand-tuned CUDA C++.
4. Compilation & Graph Capture: CUDA Graphs & TensorRT-LLM
In fast inference serving ( per token), CPU launch overhead becomes a significant bottleneck: invoking 80 consecutive PyTorch layer kernels from Python incurs ~3 microseconds of CPU dispatch latency per kernel, causing the GPU to sit idle between launches.
CUDA Graphs
CUDA Graphs record a sequence of GPU kernel launches into a single static execution graph:
- Capture Phase: Run the forward pass once in capture mode; the CUDA runtime records all kernel addresses, thread parameters, and memory dependencies.
- Replay Phase: The CPU dispatches the entire graph with a single driver call, eliminating CPU launch overhead and achieving 100% GPU utilization.
TensorRT-LLM
NVIDIA's TensorRT-LLM builds on this concept by compiling the entire transformer architecture:
- Fuses multi-head attention kernels into custom FlashAttention implementations.
- Replaces standard GEMMs with FP8 / INT4 Tensor Core kernels.
- Captures dynamic KV cache memory layouts directly in hardware DMA engines.
5. Python Simulation: Online Softmax Normalization
Run this live in-browser Python implementation to verify how FlashAttention computes exact softmax incrementally across two separate tiles without ever assembling the full array:
Never materialize intermediate attention matrices. Always utilize fused tiled kernels (FlashAttention-2/3) and compile static execution graphs (CUDA Graphs or TensorRT-LLM) to keep GPU Tensor Cores 100% saturated.
Scale kernels from single GPUs to distributed multi-node clusters in Disaggregated Serving, Prefix Caching & Distributed Inference.