Accelerate documentation

AccelerateでのLocal SGDの使用

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.16.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

AccelerateでのLocal SGDの使用

Local SGDは、分散学習における手法であり、各ステップで勾配を同期しません。そのため、各プロセスは独自のモデル重みのバージョンを更新し、所定のステップ数後に、すべてのプロセス間で重みを平均化して同期します。これにより通信効率が向上し、NVLinkのような高速なインターコネクトを備えていないコンピュータでは、特に大幅な学習速度の向上が期待できます。 勾配の蓄積とは異なり(通信効率の向上には実効バッチサイズを増やす必要があります)、Local SGDではバッチサイズや学習率/スケジュールの変更は不要です。ただし、必要に応じて、Local SGDを勾配の蓄積と組み合わせて使用することもできます。

このチュートリアルでは、Local SGD Accelerateを素早くセットアップする方法を学びます。標準的なAccelerateのセットアップと比較して、追加で必要なのはわずか2行のコードだけです。

この例では、2バッチごとに勾配蓄積を実行する非常にシンプルなPyTorchの学習ループを使用します:

device = "cuda"
model.to(device)

gradient_accumulation_steps = 2

for index, batch in enumerate(training_dataloader):
    inputs, targets = batch
    inputs = inputs.to(device)
    targets = targets.to(device)
    outputs = model(inputs)
    loss = loss_function(outputs, targets)
    loss = loss / gradient_accumulation_steps
    loss.backward()
    if (index + 1) % gradient_accumulation_steps == 0:
        optimizer.step()
        scheduler.step()
        optimizer.zero_grad()

Accelerate への変換

まず、前述のコードを、LocalSGDも勾配蓄積ヘルパーも使用しないAccelerateで利用する形に変換します。

+ from accelerate import Accelerator
+ accelerator = Accelerator()

+ model, optimizer, training_dataloader, scheduler = accelerator.prepare(
+     model, optimizer, training_dataloader, scheduler
+ )

  for index, batch in enumerate(training_dataloader):
      inputs, targets = batch
-     inputs = inputs.to(device)
-     targets = targets.to(device)
      outputs = model(inputs)
      loss = loss_function(outputs, targets)
      loss = loss / gradient_accumulation_steps
+     accelerator.backward(loss)
      if (index+1) % gradient_accumulation_steps == 0:
          optimizer.step()
          scheduler.step()

Accelerateによるモデル同期の処理

残りの作業は、Accelerate にモデルパラメータの同期 と グラデーションの蓄積を処理させることです。簡略化のため、8 ステップごとに同期が必要であると仮定します。これは、1 つの with LocalSGD 文を追加し、オプティマイザのステップの後に local_sgd.step() を 1 回呼び出すことで実現できます。

+local_sgd_steps=8

+with LocalSGD(accelerator=accelerator, model=model, local_sgd_steps=8, enabled=True) as local_sgd:
    for batch in training_dataloader:
        with accelerator.accumulate(model):
            inputs, targets = batch
            outputs = model(inputs)
            loss = loss_function(outputs, targets)
            accelerator.backward(loss)
            optimizer.step()
            scheduler.step()
            optimizer.zero_grad()
+           local_sgd.step()

内部では、Local SGD のコードは自動的な勾配同期を無効にします(ただし、勾配の累積は引き続き期待通りに動作します)。代わりに、local_sgd_steps ステップごとに(およびトレーニングループの終了時に)モデルのパラメータを平均化します。

制限事項

現在の実装は、DeepSpeedなどを含まない基本的なマルチGPU(またはマルチCPU)トレーニングでのみ動作します。

参考文献

このシンプルなアプローチの真の起源については把握していませんが、ローカルSGDのアイデアは古く、少なくとも以下の文献に遡ります:

Zhang, J., De Sa, C., Mitliagkas, I., & Ré, C. (2016). Parallel SGD: When does averaging help?. arXiv preprint arXiv:1606.07365.

Local SGDという用語は、以下の論文に由来します(より古い参考文献が存在する可能性もありますが、我々はそれについて把握していません)。

Stich, Sebastian Urban. “Local SGD Converges Fast and Communicates Little.” ICLR 2019-International Conference on Learning Representations. No. CONF. 2019.

Update on GitHub