Advanced GPU Optimization: How can I tech an LLM with CUDA and ROCm? - Part 3 A developer's third installment in a GPU optimization series details implementing distributed training for large language models using CUDA and ROCm, covering All-Reduce, Ring-AllReduce, and ZeRO sharding with NCCL and RCCL libraries. The post provides portable HIP/C++ code to synchronize gradients across GPUs, enabling training of models too large for a single device. Welcome back to the final for now chapter of our GPU optimization saga In Part 1 , we mastered single-GPU matrix multiplication and built the transformer block. In Part 2 , we implemented backpropagation, the AdamW optimizer, and mixed-precision training. But here is the harsh reality: a single GPU—even an H100 or an MI300X—can barely hold a 70B parameter model in VRAM, let alone train it within a human lifetime. To train state-of-the-art LLMs, we must span hundreds or thousands of GPUs . In this third part, we will tear down the walls of a single device and venture into the world of distributed training. We will implement All-Reduce for data parallelism, explore Ring-AllReduce algorithms, dive into ZeRO Zero Redundancy Optimizer sharding, and write real HIP/C++ code using NCCL NVIDIA and RCCL AMD to synchronize gradients across nodes. By the end of this part, you will understand: Prerequisites: Completion of Parts 1 & 2, a cluster with at least 2-4 GPUs they don't have to be same vendor, but we will write portable HIP code , and nccl / rccl libraries installed. CUDA uses NCCL NVIDIA Collective Communications Library , while ROCm uses RCCL ROCm Collective Communications Library . Fortunately, they share the exact same API signatures with a prefix change nccl vs rccl . We can unify them using preprocessor macros. // Unified header selection ifdef HIP PLATFORM AMD include