<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>GPU Training on Digi Hunch</title><link>https://www.digihunch.com/tags/gpu-training/</link><description>Recent content in GPU Training on Digi Hunch</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Mon, 20 Oct 2025 23:59:59 -0700</lastBuildDate><atom:link href="https://www.digihunch.com/tags/gpu-training/index.xml" rel="self" type="application/rss+xml"/><item><title>Deep Learning 101 - 2 of 2</title><link>https://www.digihunch.com/2025/10/deep-learning-101-2-of-2/</link><pubDate>Mon, 20 Oct 2025 14:52:34 -0700</pubDate><guid>https://www.digihunch.com/2025/10/deep-learning-101-2-of-2/</guid><description>&lt;img src="https://www.digihunch.com/images/posts/deep-learning-101-2-of-2/distributed-training.webp" alt="Featured image of post Deep Learning 101 - 2 of 2" /&gt;&lt;p&gt;In the last post, we covered from generic AI to neural networks. I mentioned GPU hardware became critical in large language model trainings. In this article, we further dive into how this happened.&lt;/p&gt;&#10;&lt;h2 id="neural-network-is-a-new-modeling-approach"&gt;Neural Network is a new Modeling Approach&#10;&lt;/h2&gt;&lt;p&gt;In contrast to the explicitly defined algorithms of traditional machine learning, deep learning is a novel modeling approach relying on distributed &amp;ldquo;networks&amp;rdquo; of mathematical operations with many layers (hence &amp;ldquo;deep&amp;rdquo;). It provides an unparalleled ability to learn the intricate nuances of very complex data, and can be used in any of the paradigms above.&lt;/p&gt;&#10;&lt;p&gt;Loosely inspired by the human brain, neural networks comprise interconnected layers of &amp;ldquo;neurons&amp;rdquo; (or &lt;strong style="color: red;"&gt;nodes&lt;/strong&gt;), each of which performs its own mathematical operations (aka &lt;strong style="color: red;"&gt;activation function&lt;/strong&gt;). The output of each node’s activation function serves as input to each of the nodes of the following layer and so on, until the final layer, where the network’s final output is computed. Crucially, the activation functions performed at each node are nonlinear, enabling networks to model complex patterns and dependencies.&lt;/p&gt;&#10;&lt;p&gt;Each connection between two neurons is assigned a unique &lt;strong style="color: red;"&gt;weight&lt;/strong&gt;: a multiplier that increases or decreases one neuron’s contribution to a neuron in the following layer. These &lt;strong style="color: red;"&gt;weights&lt;/strong&gt;, along with unique &lt;strong style="color: red;"&gt;bias&lt;/strong&gt; terms added to each neuron’s &lt;strong style="color: red;"&gt;activation function&lt;/strong&gt;, are the parameters to be optimized through ML.&lt;/p&gt;&#10;&lt;p&gt;The &lt;strong style="color: red;"&gt;backpropagation&lt;/strong&gt; algorithm enables the computation of how each individual node contributes to the overall output of the loss function, allowing even millions or billions of model weights to be individually optimized through gradient descent algorithms. Because of the volume and granularity of updates required to achieve optimal results, deep learning requires very large amounts of data and computational resources compared to traditional ML.&lt;/p&gt;&#10;&lt;p&gt;That distributed structure affords deep learning models their incredible power and versatility. Imagine training data as data points scattered on a 2-dimensional graph. Essentially, traditional ML aims to find a single curve that runs through every one of those data points; deep learning pieces together an arbitrary number of smaller, individually adjustable lines to form the desired shape. Neural networks are &lt;strong style="color: red;"&gt;universal approximators&lt;/strong&gt;: it has been theoretically proven that for any function, there exists a neural network arrangement that can reproduce it.&lt;/p&gt;&#10;&lt;h2 id="gradient-descent-and-back-propagation"&gt;Gradient Descent and back-propagation&#10;&lt;/h2&gt;&lt;p&gt;Let&amp;rsquo;s start with the concept of &lt;code&gt;tensor&lt;/code&gt;. A tensor is a generalization of a matrix to more dimensions; every matrix is a tensor. A scalar is a 0-D tensor. A vector (lit of numbers) is a 1-D tensor. A matrix is a 2-D tensor. A tensor can have even higher dimensions. In NLP, we turn text into tensors of numbers so neural networks can process it. The basic idea behind word embeddings is: two words are considered more related if their vectors are close in the embedding space.&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Name&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Dimensions&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Example Shape&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Scalar&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0D Tensor&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;() - a single number&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Vector&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1D Tensor&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;(128,)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Marix&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;2D tensor&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;(64,128)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3D tensor&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3D&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;(32,64,128)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4D tensor&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;4D&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;(batch, channels, height, width)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;5D tensor&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;5D&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;(batch, frames, channels, height, width)&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;Gradient descent is an optimization algorithm commonly used in machine learning models and neural networks. It trains ML models by minimizing errors between predicted and actual results. A loss function used with gradient descent acts as a barometer during training, gauging the accuracy with each iteration. The training algorithm continues to adjust the model parameters to yield the smallest possible error until the gradient is close to or equal to zero.&lt;/p&gt;&#10;&lt;p&gt;The goal of gradient descent is to minimize the loss function, or the error between predicted value and ground truth. In order to do this, it requires two data points - a direction and a learning rate. These factors determine the partial derivative calculations of future iterations, allowing it to gradually arrive at the local or global minimum (point of convergence).&lt;/p&gt;&#10;&lt;p&gt;There are three types of gradient descent learning algorithms: &lt;code&gt;batch gradient descent&lt;/code&gt;, &lt;code&gt;stochastic gradient descent&lt;/code&gt; and &lt;code&gt;mini-batch gradient descent&lt;/code&gt;.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Batch gradient descent sums the error for each point in a training set, updating the model (running an epoch) only after all training examples have been evaluated. While this batching provides computation efficiency, it can still have a long processing time for large training sets as it needs to store all of the data into memory.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;On the other hand, SGD (Stochastic Gradient Descent) runs a training epoch for each example within the dataset, and it updates each training example’s parameter one at a time. Since you only need to hold one training example, it is easier to store in memory. While these frequent updates can offer more detail and speed, they can result in losses in computational efficiency when compared to batch gradient descent.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;Mini-batch gradient descent combines concepts from both batch and stochastic gradient descent. It splits the training dataset into small batch sizes and performs updates on each of those batches, to strike a balance between computational efficiency and the speed.&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Although gradient descent is the most common approach for optimization problems, it does come with challenges. For non-convex problems, gradient descent can struggle to find the global minimum, where the model achieves the best results. In deeper neural networks, particularly in RNNs, we can also encounter two other problems when the model is trained with gradient descent and backpropagation: vanishing gradients, and exploding gradients.&lt;/p&gt;&#10;&lt;p&gt;In neural network training, &lt;code&gt;backpropagation&lt;/code&gt; is a remarkably fast, efficient algorithm to untangle the massive web of interconnected variables and equations in a neural network. It works backwards from the model’s output, and applies the &lt;code&gt;chain rule&lt;/code&gt;(链式法则) to calculate the influence of changes to each individual neural network parameter on the overall error of the model’s predictions.&lt;/p&gt;&#10;&lt;p&gt;We know that the goal of backpropagation is to determine how model weights and biases should be adjusted to minimize error as measured by a loss function. At a mathematical level, the goal of backpropagation is to calculate the gradient of the loss function, with respect to each of the individual parameters of the neural network. In simpler terms, backpropagation uses the chain rule to calculate the rate at which loss changes in response to any change to a specific weight or bias in the network. The steps include:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A forward pass, making predictions on training data&lt;/li&gt;&#10;&lt;li&gt;A loss function measures the error of the mode’s predictions during that forward pass&lt;/li&gt;&#10;&lt;li&gt;Backpropagation of error, or a backward pass, to calculate the partial derivatives of the loss function.&lt;/li&gt;&#10;&lt;li&gt;Gradient descent, to update the model weights.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Modern GPUs are optimized for these tasks.&lt;/p&gt;&#10;&lt;h2 id="llm-training-on-gpu"&gt;LLM Training on GPU&#10;&lt;/h2&gt;&lt;p&gt;To understand how LLM meets GPU, it helps to trace three things that came together at the right moment:&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;A unified architecture.&lt;/strong&gt; Before the Transformer, the NLP field was fragmented — RNNs, CNNs, and various attention mechanisms each had their advocates, but none scaled cleanly. The Transformer changed that by reducing the core computation to large-scale matrix multiplication, which maps almost perfectly onto how GPUs are designed to work. Overnight, the field had a single shared foundation to build on.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;A predictable scaling curve.&lt;/strong&gt; Researchers discovered that model parameters, training data, and compute don&amp;rsquo;t interact randomly — they follow a surprisingly orderly relationship now called the Scaling Law. Put simply: if you have enough compute, performance will improve in a predictable, reliable way. That finding had enormous consequences. It turned model development from an art into something closer to an engineering problem, and it gave organizations a clear rationale to keep investing: bigger models, trained on more data with more compute, would reliably get better.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;Engineering that kept pace with ambition.&lt;/strong&gt; Knowing that scale works is one thing; actually achieving it is another. A whole stack of optimizations emerged to make it practical — mixed precision training (FP16, BF16) to reduce memory pressure, ZeRO and distributed training strategies to spread computation across thousands of GPUs, and kernel-level innovations like Flash Attention to squeeze out throughput that would otherwise be left on the table. Meanwhile, hardware generations — &lt;code&gt;A100&lt;/code&gt; to &lt;code&gt;H100&lt;/code&gt; to &lt;code&gt;B200&lt;/code&gt; — kept raising the ceiling, and the engineering evolved in lockstep to exploit each new generation.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;The flywheel effect.&lt;/strong&gt; What makes this a &amp;ldquo;revolution&amp;rdquo; rather than just incremental progress is that these three elements formed a self-reinforcing loop. A common architecture made engineering investment worthwhile. Scaling laws made compute investment rational. And hardware-software co-optimization meant that algorithmic gains and hardware gains compounded each other rather than bottlenecking on one another. The result was an unusually fast and sustained improvement cycle — one where the next generation of models was almost always better, and often dramatically so.&lt;/p&gt;&#10;&lt;h2 id="distributed-training"&gt;Distributed Training&#10;&lt;/h2&gt;&lt;p&gt;Since deep learning requires massive amount (e.g. petabytes) of training data across text, images, and video, training with a single GPU would take months to years. To approach such scale of model training, distributed training is a necessity, especially for LLMs, computer vision and multimodal AI systems. There are a few approaches to parallelize training:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Data Parallelism (DP): split dataset (batches) across GPUs&lt;/li&gt;&#10;&lt;li&gt;Model Parallelism (MP): split layers across GPUs for massive models&lt;/li&gt;&#10;&lt;li&gt;Pipeline Parallelism: stage execution like an assembly line.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Model training frameworks can usually combine these approaches to enable scaling from 1 to 1,000+ GPUs, adaptable to different model architectures and hardware configurations. With multiple GPUs, it’s important to reduce communication overhead, where high-speed interconnects (NVLink, InfiniBand) and efficient algorithms (e.g. AllReduce, ZeRO) are critical.&lt;/p&gt;&#10;&lt;p&gt;DP has simple implementation and scales effectively to many GPUs linearly. However, the entire model must fit in single GPU memory. The slowest GPU determines overall training speed (straggler problem). The gradient synchronization creates network overhead that grows with model size. DP is best for medium-sized models with large dataset. Specifically, DP includes the following steps:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Split &amp;amp; distribute: divide mini-batch into N smaller batches and send to N different GPUs&lt;/li&gt;&#10;&lt;li&gt;Parallel Computation: each GPU performs forward and backward pass independently with its portion of data&lt;/li&gt;&#10;&lt;li&gt;Gradient Synchronization: AllReduce operation aggregates gradients across all GPUs&lt;/li&gt;&#10;&lt;li&gt;Model Update: update global model wights using synchronized gradients&lt;/li&gt;&#10;&lt;li&gt;Repeat 1 to 4.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;On the other hand, MP has powerful capability because it overcomes GPU memory limitations and can even combine with pipeline parallelism for ultra-large models (e.g. LLM). However, its implementation is quite complex, and is used for billion+ to trillion+ parameter models, which is impossible with DP alone. Specifically, MP includes the following steps:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Layer Distribution: partition model layers across available GPUs (e.g. layers 1-4 on GPU 1, layers 5-8 on GPU 2)&lt;/li&gt;&#10;&lt;li&gt;Sequential Forward Pass: input data flows through layers on GPU 1, then activates transfer to GPU 2 for next layers&lt;/li&gt;&#10;&lt;li&gt;Continue Chain (process continues sequentially until output from final layer on last GPU)&lt;/li&gt;&#10;&lt;li&gt;Backward Pass (backpropagation reverses the same sequence, flowing gradients backward through the chain)&lt;/li&gt;&#10;&lt;li&gt;Memory optimization (activation checkpointing reduces memory overhead by saving specific activation points)&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;In MP, the frequent transfer of activations between devices also creates bandwidth bottlenecks.&#10;PyTorch Distributed Architecture (including torch.distributed and DistributedDataParallel DDP and RPC Framework) works across single or multi-node clusters with flexible backend support. Supported backends include:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;NCCL: NVIDIA collective communication library&lt;/li&gt;&#10;&lt;li&gt;Gloo: general purpose collective communications&lt;/li&gt;&#10;&lt;li&gt;MPI: Message Passing Interface common in research-oriented HPC environment but now considered legacy system integration.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;With these libraries NCCL provides fastest for GPU-to-GPU communication and is the default choice for deep learning.&lt;/p&gt;&#10;&lt;h2 id="allreduce-and-nccl"&gt;AllReduce and NCCL&#10;&lt;/h2&gt;&lt;p&gt;One of the fundamental collection communication operation for distributed training is &lt;code&gt;AllReduce&lt;/code&gt;. This operation is implemented in many general/HPC collective libraries (e.g. Gloo, Horovod, OpenMPI) and vendor specific collective libraries (e.g. NCCL, AMD’s RCCL, etc). At a high level, AllReduce operation:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;sums gradients across all GPUs in the training cluster&lt;/li&gt;&#10;&lt;li&gt;distributes identical results back to every participating GPU&lt;/li&gt;&#10;&lt;li&gt;ensures all copies of the model across GPUs maintain synchronized weights&lt;/li&gt;&#10;&lt;li&gt;eliminates the bottleneck inherent in parameter server architectures&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;This operation forms the foundation of scalable deep learning training systems. AllReduce has variations such as Ring All Reduce.&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;NCCL&lt;/code&gt; is NVIDIA’s many-to-many communication library for collective operations, used by groups of GPUs to share data. NCCL underpins most multi-GPU training workloads in NVIDIA’s eco-system. When performing model training and inference across multiple GPUs, data such as model weights, gradients, and activations must be exchanged quickly to keep the GPUs busy. NCCL is the library that orchestrates these exchanges efficiently.&lt;/p&gt;&#10;&lt;p&gt;During distributed training, each GPU computes gradients on its portion of data. NCCL is then used to perform all-reduce of these gradients across all GPUs such that each GPU updates the model weights with the averaged gradients. During distributed inference, GPUs need to exchange activations and other intermediate results.&lt;/p&gt;&#10;&lt;p&gt;NCCL is optimized for NVIDIA GPUs and supports communication over various interconnects such as PCIe, NVLink, NVSwitch, InfiniBand and TCP sockets. It will automatically choose the fastest path available between any two GPUs.&lt;/p&gt;&#10;&lt;img src="https://www.digihunch.com/images/posts/deep-learning-101-2-of-2/distributed-training-frameworks.webp" alt="Diagram of distributed training components, including ProcessGroup, NCCL, Gloo, MPI, and RR" style="display: block; margin-left: auto; margin-right: auto;" /&gt;&#10;&lt;p&gt;&lt;code&gt;AllReduce&lt;/code&gt; operation is performed via training frameworks (e.g. PyTorch’s DDP, Horovod) on top of &lt;code&gt;NCCL&lt;/code&gt;. As an example, DDP maintains computational efficiency at scale and is significantly more efficient than legacy DataParallel. DDP replicates the entire model across all GPUs, with each GPU processing a subset of the mini-batch. It leverages AllReduce operation to synchronize gradients. The workflow involves:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Initialize Process Group: by calling dist.init_proccess_group() to establish communication between processes&lt;/li&gt;&#10;&lt;li&gt;Wrap Model in DDP: by creating DistributedDataParallel(model, device_ids=[rank])&lt;/li&gt;&#10;&lt;li&gt;Configure Data Sampling by using DistributedSampler to partition dataset across processes.&lt;/li&gt;&#10;&lt;li&gt;Train With Gradient Sync with forward/backward passes triggering automatic gradient synchronization&lt;/li&gt;&#10;&lt;li&gt;Save Distributed Checkpoints: coordinate checkpoint saving for multi-node compatibility.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;In this training paradigm, backpropagation produces gradients on each device, and All Reduce enables synchronous gradient descent, making it equivalent to training with a larger effective batch size. With multi-GPU, this operation operates on high-speed interconnects.&lt;/p&gt;&#10;&lt;h1 id="references"&gt;References&#10;&lt;/h1&gt;&lt;ul&gt;&#10;&lt;li&gt;&lt;a class="link" href="https://www.tensortonic.com/ml-math" target="_blank" rel="noopener"&#10; &gt;Machine Learning Math&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a class="link" href="https://www.ibm.com/think/machine-learning" target="_blank" rel="noopener"&#10; &gt;The 2025 Guide to Machine Learning&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a class="link" href="https://wentao.site/overview_of_distribution_training" target="_blank" rel="noopener"&#10; &gt;Overview of PyTorch Distributed Training&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;</description></item></channel></rss>