Accelerate documentation

使用 Accelerate 进行本地 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 进行本地 SGD

Local SGD 是一种分布式训练技术,其特点是不在每一步都同步梯度。因此,每个进程都会更新自己版本的模型权重,并在经过指定数量的步骤后,通过对所有进程进行平均来同步这些权重。这提高了通信效率,并可能带来显著的训练加速,尤其是在计算机缺乏 NVLink 等更快互连的情况下。 与梯度累积不同(后者通过增大有效批量大小来提高通信效率),Local SGD 无需更改批量大小或学习率/调度。不过,如有必要,Local SGD 也可以与梯度累积结合使用。

在本教程中,您将了解如何快速设置 Local SGD Accelerate。与标准的 Accelerate 设置相比,这只需要额外两行代码。

本示例将使用一个非常简单的 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

首先,前面展示的代码将被转换为使用 Accelerate,且不使用 LocalSGD 或梯度累积辅助工具:

+ 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 步同步一次。这可以通过添加一个 with LocalSGD 语句,并在每次优化器步骤之后调用一次 local_sgd.step() 来实现:

+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 步对模型参数进行一次平均(训练循环结束时也会执行一次)。

局限性

当前实现仅支持基本的多 GPU(或多 CPU)训练,例如不支持 DeepSpeed。

参考

虽然我们不清楚这种简单方法的真正起源,但局部 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