Boosting Transformer Training with NVIDIA Apex: Fused Kernels and torch.amp
In this tutorial, we demonstrate how to accelerate Transformer training using NVIDIA Apex's fused kernels combined with PyTorch's native automatic mixed precision (AMP). We focus on components that remain relevant in modern GPU workflows, such as FusedAdam, FusedLayerNorm, and FusedRMSNorm, and benchmark them against standard PyTorch implementations.
Setup and Capabilities
First, we ensure a CUDA-capable GPU is available and build Apex from source with CUDA and C++ extensions to access high-performance fused kernels. We then check for key modules: amp_C for FusedAdam, fused_layer_norm_cuda for FusedLayerNorm, and FusedRMSNorm. A benchmarking helper function measures step times.
Benchmarking FusedAdam vs. AdamW
We create a model with many linear layers to make optimizer overhead visible. Comparing torch.optim.AdamW with apex.optimizers.FusedAdam shows that FusedAdam can provide a significant speedup (e.g., ~1.5x) on optimizer-bound steps due to its multi-tensor kernel.
Comparing Normalization Layers
We test standard nn.LayerNorm against apex.normalization.FusedLayerNorm on a large tensor simulating transformer hidden states. Numerical accuracy is verified, and benchmarks show FusedLayerNorm achieving 2–3x speedup for forward+backward. Additionally, FusedRMSNorm is tested, which is used in LLaMA-style models.
Mixed Precision Training
We compare legacy apex.amp with modern torch.amp. The tutorial walks through setting up both, demonstrating that torch.amp is the recommended path for new projects, while apex.amp still works and can be useful for backward compatibility.
End-to-End Transformer Training
Finally, we assemble a small Transformer model and train it with two configurations: vanilla FP32 PyTorch and a fused Apex + torch.amp pipeline. The accelerated pipeline achieves up to 2x throughput improvement, confirming the practical benefits of combining fused kernels with mixed precision.
This tutorial provides a practical guide for developers looking to optimize Transformer training without sacrificing correctness or simplicity.