评价此页

自动混合精度#

创建于:2020年9月15日 | 最后更新:2025年1月30日 | 最后验证:2024年11月05日

作者Michael Carilli

torch.cuda.amp 提供了混合精度的便捷方法,其中一些操作使用 torch.float32 (float) 数据类型,其他操作使用 torch.float16 (half)。一些操作(如线性层和卷积)在 float16bfloat16 中速度快得多。其他操作(如归约)通常需要 float32 的动态范围。混合精度尝试将每个操作与其适当的数据类型匹配,这可以减少网络的运行时间和内存占用。

通常,“自动混合精度训练”同时使用 torch.autocasttorch.cuda.amp.GradScaler

本秘籍测量了简单网络在默认精度下的性能,然后逐步添加 autocastGradScaler 以在混合精度下运行相同的网络,从而提高性能。

您可以下载并以独立 Python 脚本形式运行此秘籍。唯一的要求是 PyTorch 1.6 或更高版本以及支持 CUDA 的 GPU。

混合精度主要受益于启用 Tensor Core 的架构(Volta、Turing、Ampere)。此秘籍应在这些架构上显示显著的(2-3 倍)加速。在早期架构(Kepler、Maxwell、Pascal)上,您可能会观察到适度的加速。运行 nvidia-smi 以显示您的 GPU 架构。

import torch, time, gc

# Timing utilities
start_time = None

def start_timer():
    global start_time
    gc.collect()
    torch.cuda.empty_cache()
    torch.cuda.reset_max_memory_allocated()
    torch.cuda.synchronize()
    start_time = time.time()

def end_timer_and_print(local_msg):
    torch.cuda.synchronize()
    end_time = time.time()
    print("\n" + local_msg)
    print("Total execution time = {:.3f} sec".format(end_time - start_time))
    print("Max memory used by tensors = {} bytes".format(torch.cuda.max_memory_allocated()))

一个简单网络#

以下线性层和 ReLU 的序列应在混合精度下显示加速。

def make_model(in_size, out_size, num_layers):
    layers = []
    for _ in range(num_layers - 1):
        layers.append(torch.nn.Linear(in_size, in_size))
        layers.append(torch.nn.ReLU())
    layers.append(torch.nn.Linear(in_size, out_size))
    return torch.nn.Sequential(*tuple(layers)).cuda()

batch_sizein_sizeout_sizenum_layers 选择得足够大,以使 GPU 工作饱和。通常,当 GPU 饱和时,混合精度提供最大的加速。小型网络可能是 CPU 绑定的,在这种情况下,混合精度不会提高性能。大小也经过选择,以便线性层的参与维度是 8 的倍数,以便在支持 Tensor Core 的 GPU 上使用 Tensor Core(参见下面的故障排除)。

练习:改变参与大小,看看混合精度加速如何变化。

batch_size = 512 # Try, for example, 128, 256, 513.
in_size = 4096
out_size = 4096
num_layers = 3
num_batches = 50
epochs = 3

device = 'cuda' if torch.cuda.is_available() else 'cpu'
torch.set_default_device(device)

# Creates data in default precision.
# The same data is used for both default and mixed precision trials below.
# You don't need to manually change inputs' ``dtype`` when enabling mixed precision.
data = [torch.randn(batch_size, in_size) for _ in range(num_batches)]
targets = [torch.randn(batch_size, out_size) for _ in range(num_batches)]

loss_fn = torch.nn.MSELoss().cuda()

默认精度#

在没有 torch.cuda.amp 的情况下,以下简单网络以默认精度 (torch.float32) 执行所有操作。

net = make_model(in_size, out_size, num_layers)
opt = torch.optim.SGD(net.parameters(), lr=0.001)

start_timer()
for epoch in range(epochs):
    for input, target in zip(data, targets):
        output = net(input)
        loss = loss_fn(output, target)
        loss.backward()
        opt.step()
        opt.zero_grad() # set_to_none=True here can modestly improve performance
end_timer_and_print("Default precision:")

添加 torch.autocast#

torch.autocast 的实例作为上下文管理器,允许脚本的区域以混合精度运行。

在这些区域中,CUDA 操作以 autocast 选择的 dtype 运行,以提高性能同时保持精度。有关 autocast 为每个操作选择的精度以及在何种情况下的详细信息,请参阅Autocast 操作参考

for epoch in range(0): # 0 epochs, this section is for illustration only
    for input, target in zip(data, targets):
        # Runs the forward pass under ``autocast``.
        with torch.autocast(device_type=device, dtype=torch.float16):
            output = net(input)
            # output is float16 because linear layers ``autocast`` to float16.
            assert output.dtype is torch.float16

            loss = loss_fn(output, target)
            # loss is float32 because ``mse_loss`` layers ``autocast`` to float32.
            assert loss.dtype is torch.float32

        # Exits ``autocast`` before backward().
        # Backward passes under ``autocast`` are not recommended.
        # Backward ops run in the same ``dtype`` ``autocast`` chose for corresponding forward ops.
        loss.backward()
        opt.step()
        opt.zero_grad() # set_to_none=True here can modestly improve performance

添加 GradScaler#

梯度缩放有助于防止在混合精度训练时,梯度幅度较小导致梯度归零(“下溢”)。

torch.cuda.amp.GradScaler 方便地执行梯度缩放步骤。

# Constructs a ``scaler`` once, at the beginning of the convergence run, using default arguments.
# If your network fails to converge with default ``GradScaler`` arguments, please file an issue.
# The same ``GradScaler`` instance should be used for the entire convergence run.
# If you perform multiple convergence runs in the same script, each run should use
# a dedicated fresh ``GradScaler`` instance. ``GradScaler`` instances are lightweight.
scaler = torch.amp.GradScaler("cuda")

for epoch in range(0): # 0 epochs, this section is for illustration only
    for input, target in zip(data, targets):
        with torch.autocast(device_type=device, dtype=torch.float16):
            output = net(input)
            loss = loss_fn(output, target)

        # Scales loss. Calls ``backward()`` on scaled loss to create scaled gradients.
        scaler.scale(loss).backward()

        # ``scaler.step()`` first unscales the gradients of the optimizer's assigned parameters.
        # If these gradients do not contain ``inf``s or ``NaN``s, optimizer.step() is then called,
        # otherwise, optimizer.step() is skipped.
        scaler.step(opt)

        # Updates the scale for next iteration.
        scaler.update()

        opt.zero_grad() # set_to_none=True here can modestly improve performance

整合起来:“自动混合精度”#

(以下还演示了 enabled,一个可选的方便参数,用于 autocastGradScaler。如果为 False,autocastGradScaler 的调用将变为无操作。这允许在默认精度和混合精度之间切换而无需 if/else 语句。)

use_amp = True

net = make_model(in_size, out_size, num_layers)
opt = torch.optim.SGD(net.parameters(), lr=0.001)
scaler = torch.amp.GradScaler("cuda" ,enabled=use_amp)

start_timer()
for epoch in range(epochs):
    for input, target in zip(data, targets):
        with torch.autocast(device_type=device, dtype=torch.float16, enabled=use_amp):
            output = net(input)
            loss = loss_fn(output, target)
        scaler.scale(loss).backward()
        scaler.step(opt)
        scaler.update()
        opt.zero_grad() # set_to_none=True here can modestly improve performance
end_timer_and_print("Mixed precision:")

检查/修改梯度(例如,裁剪)#

scaler.scale(loss).backward() 生成的所有梯度都经过缩放。如果您希望在 backward()scaler.step(optimizer) 之间修改或检查参数的 .grad 属性,您应该首先使用 scaler.unscale_(optimizer) 取消缩放它们。

for epoch in range(0): # 0 epochs, this section is for illustration only
    for input, target in zip(data, targets):
        with torch.autocast(device_type=device, dtype=torch.float16):
            output = net(input)
            loss = loss_fn(output, target)
        scaler.scale(loss).backward()

        # Unscales the gradients of optimizer's assigned parameters in-place
        scaler.unscale_(opt)

        # Since the gradients of optimizer's assigned parameters are now unscaled, clips as usual.
        # You may use the same value for max_norm here as you would without gradient scaling.
        torch.nn.utils.clip_grad_norm_(net.parameters(), max_norm=0.1)

        scaler.step(opt)
        scaler.update()
        opt.zero_grad() # set_to_none=True here can modestly improve performance

保存/恢复#

要以比特级精度保存/恢复启用 Amp 的运行,请使用 scaler.state_dictscaler.load_state_dict

保存时,将 scaler 状态字典与通常的模型和优化器状态 dicts 一起保存。这可以在任何前向传播之前迭代开始时进行,或者在 scaler.update() 之后迭代结束时进行。

checkpoint = {"model": net.state_dict(),
              "optimizer": opt.state_dict(),
              "scaler": scaler.state_dict()}
# Write checkpoint as desired, e.g.,
# torch.save(checkpoint, "filename")

恢复时,将 scaler 状态字典与模型和优化器状态 dicts 一起加载。根据需要读取检查点,例如

dev = torch.cuda.current_device()
checkpoint = torch.load("filename",
                        map_location = lambda storage, loc: storage.cuda(dev))
net.load_state_dict(checkpoint["model"])
opt.load_state_dict(checkpoint["optimizer"])
scaler.load_state_dict(checkpoint["scaler"])

如果检查点是从未启用 Amp 的运行创建的,并且您想在启用 Amp 的情况下恢复训练,则照常从检查点加载模型和优化器状态。检查点将不包含已保存的 scaler 状态,因此请使用新的 GradScaler 实例。

如果检查点是从启用 Amp 的运行创建的,并且您想在未启用 Amp 的情况下恢复训练,则照常从检查点加载模型和优化器状态,并忽略已保存的 scaler 状态。

推断/评估#

autocast 可以单独用于包装推断或评估前向传播。GradScaler 没有必要。

高级主题#

有关高级用例,请参阅自动混合精度示例,包括:

  • 梯度累积

  • 梯度惩罚/二次反向传播

  • 具有多个模型、优化器或损失的网络

  • 多 GPU (torch.nn.DataParalleltorch.nn.parallel.DistributedDataParallel)

  • 自定义自动求导函数(torch.autograd.Function 的子类)

如果您在同一个脚本中执行多次收敛运行,每次运行都应使用专用的新 GradScaler 实例。GradScaler 实例是轻量级的。

如果您正在使用调度器注册自定义 C++ 操作,请参阅调度器教程的autocast 部分

故障排除#

使用 Amp 加速不明显#

  1. 您的网络可能无法使 GPU 饱和工作,因此受到 CPU 限制。Amp 对 GPU 性能的影响无关紧要。

    • 使 GPU 饱和的经验法则是尽可能增加批次和/或网络大小,而不会耗尽内存 (OOM)。

    • 尽量避免过多的 CPU-GPU 同步(.item() 调用,或从 CUDA 张量打印值)。

    • 尽量避免连续执行许多小的 CUDA 操作(如果可以,将它们合并成几个大的 CUDA 操作)。

  2. 您的网络可能受 GPU 计算限制(大量 matmuls/卷积),但您的 GPU 没有 Tensor Cores。在这种情况下,预期加速会降低。

  3. matmul 维度不适合 Tensor Core。请确保 matmuls 参与维度是 8 的倍数。(对于带有编码器/解码器的 NLP 模型,这可能很微妙。此外,用于 Tensor Core 的卷积过去有类似的大小限制,但对于 CuDNN 版本 7.3 及更高版本,不存在此类限制。请参阅此处以获取指导。)

损失为 inf/NaN#

首先,检查您的网络是否符合高级用例。另请参阅优先使用 binary_cross_entropy_with_logits 而非 binary_cross_entropy

如果您确信您的 Amp 用法是正确的,您可能需要提交问题,但在这样做之前,收集以下信息会有所帮助:

  1. 单独禁用 autocastGradScaler(通过向其构造函数传递 enabled=False),并查看 infs/NaNs 是否持续存在。

  2. 如果您怀疑网络的一部分(例如,一个复杂的损失函数)溢出,请在 float32 中运行该前向区域,并查看 infs/NaN``s 是否持续存在。 `autocast 文档字符串 <https://pytorch.ac.cn/docs/stable/amp.html#torch.autocast>`_ 的最后一个代码片段显示了如何强制子区域以 ``float32 运行(通过局部禁用 autocast 并转换子区域的输入)。

类型不匹配错误(可能表现为 CUDNN_STATUS_BAD_PARAM#

Autocast 尝试涵盖所有受益于或需要类型转换的操作。明确覆盖的操作是根据数值特性和经验选择的。如果您在启用 autocast 的前向区域或随后的反向传播中看到类型不匹配错误,则可能是 autocast 遗漏了某个操作。

请提交带有错误回溯的问题。在运行脚本之前 export TORCH_SHOW_CPP_STACKTRACES=1 以提供有关哪个后端操作失败的详细信息。