Answers.Body: <p>I don’t think the problem lies in the gradient or related stuffs - the problem here is the <em>hardware limitation</em> of GPU VRAM.</p>
  <p>Sure, CLIP can fit in a single GPU, but what GPU are we talking about? I have done some experiments with CLIP and I am pretty sure that CLIP ViT-B/16 with 224x224 images and batch size above 40, it is very easy to get out-of-memory on RTX 3090.</p>
  <p>You can try it on Google Colab. Try with ViT-B/16 and input a random tensor with size 224x244x3, then gradually scale up the batch size to see which threshold it exceeds the VRAM of the given GPU.</p>
  <p>So in short, it is more about &quot;we cannot fit it in a single GPU&quot; rather than &quot;It is about training stability&quot;.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-03T09:32:16.310Z
Answers.LastActivityDate: 2023-03-05T01:54:39.327Z
Answers.LastEditDate: 2023-03-05T01:54:39.327Z
Answers.UserDisplayName: Minh-Long Luu
Answers.Body: <p>The large batch size is mostly to speed things up as you point out. You could in principle train these models with batch size 1 and just do gradient accumulation, but then training would take muuuuch longer.</p>
  <p>Aside for tokens/seconds, empirically, it has been found that larger batch sizes lead to faster convergence (<a href="https://arxiv.org/abs/2212.14034" rel="nofollow noreferrer">https://arxiv.org/abs/2212.14034</a>)</p>
  <p>The time and efficiency advantages provided by large batch sizes will often necessitate distributed training, as Minh points out, due to the VRAM limitations of individual GPUs. Good luck even just loading (no inference) anything larger than 15B on an A100 without optimization tricks.</p>
  <p>It also doesn’t help that transformer’s self-attention mechanism has memory requirements that are quadratic with the input sequence length. When you’re training these models, you tend to pack several examples into a single input sequence such that the maximum length is reached, minimizing padding tokens and maximizing GPU usage (which would otherwise remain underutilized, which is a waste of time/money). The result of that is that you’re training with fully maxed-out sequence lengths, and hence maxing-out memory requirements, once again requiring distributed training typically.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-05T01:26:03.170Z
Answers.LastActivityDate: 2023-03-05T01:26:03.170Z
Answers.UserDisplayName: thesofakillers
Body: <h1>Why do large language models (LLMs) need massive distributed training across nodes -- if the models fit in one GPU and larger batch only decreases the variance of gradients?</h1>
  <p>tldr: assuming for models that don't need sharding across nodes, why do we need (massive) distributed training if the models (e.g. CLIP, Chinchilla, even really large GPTs e.g. CLIP fits in a V100 32GB) fit in one GPU and larger batch only decreases the variance of gradients (but not expose ore tokens or param updates)? A larger batch doesn't necessarily mean we train on &quot;more data/tokens&quot; -- or at least that doesn't seem to be wrt SGD like optimizers.</p>
  <hr />
  <p>Intuitively, it feels that if we had a larger batch size then we have more tokens to learn about -- but knowing some theory of optimization and what SGD like algorithms actually do -- a larger batch size only actually decreases the variance of gradients. So to me it's not clear why massie distributed training is needed -- at all unless the model is so large that it has to be shared across nodes. In addition, even if the batch was &quot;huge&quot; -- we can only do a single gradient update.</p>
  <p>I feel I must be missing something obvious hence the question given how pervasive massive distributed training is.</p>
  <p>In addition some toy training curves with V100s &amp; T5's show me there is very little if any benefit in additional GPUs
  <a href="https://i.sstatic.net/8pZTP.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8pZTP.png" alt="enter image description here" /></a></p>
  <p>In addition, it seems from nonGPT we know small batch sizes are sufficient to train (reference <a href="https://github.com/karpathy/nanoGPT" rel="nofollow noreferrer">https://github.com/karpathy/nanoGPT</a> but I did ask Karpathy directly to confirm  <a href="https://github.com/karpathy/nanoGPT/issues/58" rel="nofollow noreferrer">https://github.com/karpathy/nanoGPT/issues/58</a>).</p>
  <p>I am missing something obvious, but I wanted to clear this up in my head since it seems to be a foundation thing in training foundation models.</p>
  <p>Related to the previous, I've also been unsure about the role of the batch size in training LLMs compared to traditional deep learning. In traditional deep learning when we used epochs to train, a model <strong>the larger the batch size the quicker we could go through an epoch</strong> -- so the advice I received (e.g. approximate advice by Ruslan Salakhutdinov's at the Simon's institute for deep learning tutorials) was to make the batch size large. Intuitively, the larger the batch size the more data the model sees per iteration. But mathematically this only really improves the variance of the gradient -- which isn't immediately obvious is what we want (I've done experiments and seen papers where noisy gradients lead to better models).
  It is clear too the that the larger the context size the better (for everything, but for the sake of this conv it's better for training) -- whenever possible.  But context size is totally different from batch size. So my question is, how does distributed training, especially at the node level help at all if batch size isn't really the helping factor (which might be a wrong assumption)? So the only role for distributed training I see is if the model is to large to fit in 1 node -- since I'm arguing there is no point to make the batch size too large (I'd guess 64-32 is fine due to the CLT).</p>
  <p>What am I missing? Empirical answers are fine! Or any answers are fine!</p>
  <hr />
  <p>Related:</p>
  <ul>
  <li>cross quora: <a href="https://www.quora.com/unanswered/Why-do-large-language-models-LLMs-need-massive-distributed-training-across-nodes-if-the-models-fit-in-one-GPU-and-larger-batch-only-decreases-the-variance-of-gradients" rel="nofollow noreferrer">https://www.quora.com/unanswered/Why-do-large-language-models-LLMs-need-massive-distributed-training-across-nodes-if-the-models-fit-in-one-GPU-and-larger-batch-only-decreases-the-variance-of-gradients</a></li>
  <li>cross reddit: <a href="https://www.reddit.com/r/learnmachinelearning/comments/113whxu/why_do_llms_need_massive_distributed_training/" rel="nofollow noreferrer">https://www.reddit.com/r/learnmachinelearning/comments/113whxu/why_do_llms_need_massive_distributed_training/</a></li>
  </ul>
  
Tags: <machine-learning><deep-learning><training><distributed-computing><large-language-models>
Title: Why do LLMs need massive distributed training across nodes -- if the models fit in one GPU while batch decreases the variance of gradients?
UserDisplayName: Charlie Parker
ThreadText: # Why do LLMs need massive distributed training across nodes -- if the models fit in one GPU while batch decreases the variance of gradients?
  
  (Asked by: username_0 on 2023-02-16)
  
  # Why do large language models (LLMs) need massive distributed training across nodes -- if the models fit in one GPU and larger batch only decreases the variance of gradients?
  
  tldr: assuming for models that don't need sharding across nodes, why do we need (massive) distributed training if the models (e.g. CLIP, Chinchilla, even really large GPTs e.g. CLIP fits in a V100 32GB) fit in one GPU and larger batch only decreases the variance of gradients (but not expose ore tokens or param updates)? A larger batch doesn't necessarily mean we train on "more data/tokens" -- or at least that doesn't seem to be wrt SGD like optimizers.
  
  ---
  
  Intuitively, it feels that if we had a larger batch size then we have more tokens to learn about -- but knowing some theory of optimization and what SGD like algorithms actually do -- a larger batch size only actually decreases the variance of gradients. So to me it's not clear why massie distributed training is needed -- at all unless the model is so large that it has to be shared across nodes. In addition, even if the batch was "huge" -- we can only do a single gradient update.
  
  I feel I must be missing something obvious hence the question given how pervasive massive distributed training is.
  
  In addition some toy training curves with V100s & T5's show me there is very little if any benefit in additional GPUs
  [![enter image description here](https://i.sstatic.net/8pZTP.png)](https://i.sstatic.net/8pZTP.png)
  
  In addition, it seems from nonGPT we know small batch sizes are sufficient to train (reference <https://github.com/karpathy/nanoGPT> but I did ask Karpathy directly to confirm <https://github.com/karpathy/nanoGPT/issues/58>).
  
  I am missing something obvious, but I wanted to clear this up in my head since it seems to be a foundation thing in training foundation models.
  
  Related to the previous, I've also been unsure about the role of the batch size in training LLMs compared to traditional deep learning. In traditional deep learning when we used epochs to train, a model **the larger the batch size the quicker we could go through an epoch** -- so the advice I received (e.g. approximate advice by Ruslan Salakhutdinov's at the Simon's institute for deep learning tutorials) was to make the batch size large. Intuitively, the larger the batch size the more data the model sees per iteration. But mathematically this only really improves the variance of the gradient -- which isn't immediately obvious is what we want (I've done experiments and seen papers where noisy gradients lead to better models).
  It is clear too the that the larger the context size the better (for everything, but for the sake of this conv it's better for training) -- whenever possible. But context size is totally different from batch size. So my question is, how does distributed training, especially at the node level help at all if batch size isn't really the helping factor (which might be a wrong assumption)? So the only role for distributed training I see is if the model is to large to fit in 1 node -- since I'm arguing there is no point to make the batch size too large (I'd guess 64-32 is fine due to the CLT).
  
  What am I missing? Empirical answers are fine! Or any answers are fine!
  
  ---
  
  Related:
  
  * cross quora: <https://www.quora.com/unanswered/Why-do-large-language-models-LLMs-need-massive-distributed-training-across-nodes-if-the-models-fit-in-one-GPU-and-larger-batch-only-decreases-the-variance-of-gradients>
  * cross reddit: <https://www.reddit.com/r/learnmachinelearning/comments/113whxu/why_do_llms_need_massive_distributed_training/>
  
  **Tags:** machine-learning, deep-learning, training, distributed-computing, large-language-models
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-03-03. Score: 3
  
  I don’t think the problem lies in the gradient or related stuffs - the problem here is the *hardware limitation* of GPU VRAM.
  
  Sure, CLIP can fit in a single GPU, but what GPU are we talking about? I have done some experiments with CLIP and I am pretty sure that CLIP ViT-B/16 with 224x224 images and batch size above 40, it is very easy to get out-of-memory on RTX 3090.
  
  You can try it on Google Colab. Try with ViT-B/16 and input a random tensor with size 224x244x3, then gradually scale up the batch size to see which threshold it exceeds the VRAM of the given GPU.
  
  So in short, it is more about "we cannot fit it in a single GPU" rather than "It is about training stability".
  
  
  ---
  
  ### Answer by username_2 on 2023-03-05. Score: 1
  
  The large batch size is mostly to speed things up as you point out. You could in principle train these models with batch size 1 and just do gradient accumulation, but then training would take muuuuch longer.
  
  Aside for tokens/seconds, empirically, it has been found that larger batch sizes lead to faster convergence (<https://arxiv.org/abs/2212.14034>)
  
  The time and efficiency advantages provided by large batch sizes will often necessitate distributed training, as Minh points out, due to the VRAM limitations of individual GPUs. Good luck even just loading (no inference) anything larger than 15B on an A100 without optimization tricks.
  
  It also doesn’t help that transformer’s self-attention mechanism has memory requirements that are quadratic with the input sequence length. When you’re training these models, you tend to pack several examples into a single input sequence such that the maximum length is reached, minimizing padding tokens and maximizing GPU usage (which would otherwise remain underutilized, which is a waste of time/money). The result of that is that you’re training with fully maxed-out sequence lengths, and hence maxing-out memory requirements, once again requiring distributed training typically.
  
  
  ---
  
  