自动混合精度示例#
创建于:2020年2月13日 | 最后更新于:2024年9月13日
通常,“自动混合精度训练”意味着将 torch.autocast 和 torch.amp.GradScaler 结合使用进行训练。
torch.autocast 的实例允许在选定的区域内启用自动类型转换。自动类型转换会自动选择操作的精度,以提高性能同时保持准确性。
torch.amp.GradScaler 的实例有助于方便地执行梯度缩放的步骤。梯度缩放通过最小化梯度下溢来改善具有 float16(默认在 CUDA 和 XPU 上)梯度的网络的收敛性,具体解释请参阅 此处。
torch.autocast 和 torch.amp.GradScaler 是模块化的。在下面的示例中,每个都按照其单独的文档建议使用。
(这里的示例仅供说明。有关可运行的详细教程,请参阅 自动混合精度指南。)
典型的混合精度训练#
# Creates model and optimizer in default precision
model = Net().cuda()
optimizer = optim.SGD(model.parameters(), ...)
# Creates a GradScaler once at the beginning of training.
scaler = GradScaler()
for epoch in epochs:
for input, target in data:
optimizer.zero_grad()
# Runs the forward pass with autocasting.
with autocast(device_type='cuda', dtype=torch.float16):
output = model(input)
loss = loss_fn(output, target)
# Scales loss. Calls backward() on scaled loss to create scaled gradients.
# Backward passes under autocast are not recommended.
# Backward ops run in the same dtype autocast chose for corresponding forward ops.
scaler.scale(loss).backward()
# scaler.step() first unscales the gradients of the optimizer's assigned params.
# If these gradients do not contain infs or NaNs, optimizer.step() is then called,
# otherwise, optimizer.step() is skipped.
scaler.step(optimizer)
# Updates the scale for next iteration.
scaler.update()
使用未缩放的梯度#
由 scaler.scale(loss).backward() 生成的所有梯度都已缩放。如果您希望在 backward() 和 scaler.step(optimizer) 之间修改或检查参数的 .grad 属性,则应先取消缩放它们。例如,梯度裁剪会操纵一组梯度,使其全局范数(请参阅 torch.nn.utils.clip_grad_norm_())或最大幅度(请参阅 torch.nn.utils.clip_grad_value_())小于或等于用户设定的阈值。如果您在未取消缩放的情况下尝试裁剪,则梯度的范数/最大幅度也会被缩放,因此您请求的阈值(本来是针对*未缩放*梯度的阈值)将无效。
scaler.unscale_(optimizer) 会取消缩放由 optimizer 分配的参数所持有的梯度。如果您的模型或模型包含分配给另一个优化器(例如 optimizer2)的其他参数,您可以单独调用 scaler.unscale_(optimizer2) 来取消缩放这些参数的梯度。
梯度裁剪#
在裁剪之前调用 scaler.unscale_(optimizer) 可以让您像平常一样裁剪未缩放的梯度。
scaler = GradScaler()
for epoch in epochs:
for input, target in data:
optimizer.zero_grad()
with autocast(device_type='cuda', dtype=torch.float16):
output = model(input)
loss = loss_fn(output, target)
scaler.scale(loss).backward()
# Unscales the gradients of optimizer's assigned params in-place
scaler.unscale_(optimizer)
# Since the gradients of optimizer's assigned params are unscaled, clips as usual:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
# optimizer's gradients are already unscaled, so scaler.step does not unscale them,
# although it still skips optimizer.step() if the gradients contain infs or NaNs.
scaler.step(optimizer)
# Updates the scale for next iteration.
scaler.update()
scaler 会记录在本迭代中已为该优化器调用过 scaler.unscale_(optimizer),因此 scaler.step(optimizer) 会知道在(内部)调用 optimizer.step() 之前不要重复取消缩放梯度。
警告
unscale_ 应该每个优化器每 step 调用一次,并且仅在为该优化器分配的参数累积完所有梯度之后。在每次 step 调用之间,如果对给定优化器调用 unscale_ 两次,将触发 RuntimeError。
使用缩放的梯度#
梯度累积#
梯度累积会将梯度累加在一个有效批次中,其大小为 batch_per_iter * iters_to_accumulate(如果分布式,则为 * num_procs)。缩放因子应根据有效批次进行校准,这意味着在发现 inf/NaN 梯度时进行检查,在发现 inf/NaN 梯度时跳过步骤,并且在有效批次粒度上更新缩放因子。此外,在累积完给定有效批次的梯度之前,梯度应保持缩放状态,并且缩放因子应保持不变。如果在累积完成之前取消了梯度缩放(或更改了缩放因子),则下一次反向传播将在添加已缩放的梯度到未缩放的梯度(或按不同因子缩放的梯度)之后,此时将无法恢复 step 必须应用的累积未缩放梯度。
因此,如果您想 unscale_ 梯度(例如,允许裁剪未缩放的梯度),请在 step 调用之前调用 unscale_,此时将为即将进行的 step 调用累积完所有(已缩放的)梯度。另外,仅在您调用 step 来完成一个完整有效批次的迭代结束时调用 update。
scaler = GradScaler()
for epoch in epochs:
for i, (input, target) in enumerate(data):
with autocast(device_type='cuda', dtype=torch.float16):
output = model(input)
loss = loss_fn(output, target)
loss = loss / iters_to_accumulate
# Accumulates scaled gradients.
scaler.scale(loss).backward()
if (i + 1) % iters_to_accumulate == 0:
# may unscale_ here if desired (e.g., to allow clipping unscaled gradients)
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
梯度惩罚#
梯度惩罚的实现通常使用 torch.autograd.grad() 创建梯度,将它们组合起来创建惩罚值,并将惩罚值添加到损失中。
下面是一个不使用梯度缩放或自动类型转换的 L2 惩罚的普通示例。
for epoch in epochs:
for input, target in data:
optimizer.zero_grad()
output = model(input)
loss = loss_fn(output, target)
# Creates gradients
grad_params = torch.autograd.grad(outputs=loss,
inputs=model.parameters(),
create_graph=True)
# Computes the penalty term and adds it to the loss
grad_norm = 0
for grad in grad_params:
grad_norm += grad.pow(2).sum()
grad_norm = grad_norm.sqrt()
loss = loss + grad_norm
loss.backward()
# clip gradients here, if desired
optimizer.step()
要实现*带有*梯度缩放的梯度惩罚,传递给 torch.autograd.grad() 的 outputs 张量(Tensor)应该被缩放。因此,生成的梯度也将被缩放,在组合起来创建惩罚值之前应该先进行缩放。
此外,惩罚项的计算是前向传递的一部分,因此应该在 autocast 上下文中执行。
以下是相同的 L2 惩罚的实现方式。
scaler = GradScaler()
for epoch in epochs:
for input, target in data:
optimizer.zero_grad()
with autocast(device_type='cuda', dtype=torch.float16):
output = model(input)
loss = loss_fn(output, target)
# Scales the loss for autograd.grad's backward pass, producing scaled_grad_params
scaled_grad_params = torch.autograd.grad(outputs=scaler.scale(loss),
inputs=model.parameters(),
create_graph=True)
# Creates unscaled grad_params before computing the penalty. scaled_grad_params are
# not owned by any optimizer, so ordinary division is used instead of scaler.unscale_:
inv_scale = 1./scaler.get_scale()
grad_params = [p * inv_scale for p in scaled_grad_params]
# Computes the penalty term and adds it to the loss
with autocast(device_type='cuda', dtype=torch.float16):
grad_norm = 0
for grad in grad_params:
grad_norm += grad.pow(2).sum()
grad_norm = grad_norm.sqrt()
loss = loss + grad_norm
# Applies scaling to the backward call as usual.
# Accumulates leaf gradients that are correctly scaled.
scaler.scale(loss).backward()
# may unscale_ here if desired (e.g., to allow clipping unscaled gradients)
# step() and update() proceed as usual.
scaler.step(optimizer)
scaler.update()
处理多个模型、损失和优化器#
如果您的网络有多个损失,您必须分别对每个损失调用 scaler.scale。如果您的网络有多个优化器,您可以分别对任何一个优化器调用 scaler.unscale_,并且您必须分别对每个优化器调用 scaler.step。
但是,scaler.update 应该只调用一次,在所有本次迭代使用的优化器都已执行 `step` 之后。
scaler = torch.amp.GradScaler()
for epoch in epochs:
for input, target in data:
optimizer0.zero_grad()
optimizer1.zero_grad()
with autocast(device_type='cuda', dtype=torch.float16):
output0 = model0(input)
output1 = model1(input)
loss0 = loss_fn(2 * output0 + 3 * output1, target)
loss1 = loss_fn(3 * output0 - 5 * output1, target)
# (retain_graph here is unrelated to amp, it's present because in this
# example, both backward() calls share some sections of graph.)
scaler.scale(loss0).backward(retain_graph=True)
scaler.scale(loss1).backward()
# You can choose which optimizers receive explicit unscaling, if you
# want to inspect or modify the gradients of the params they own.
scaler.unscale_(optimizer0)
scaler.step(optimizer0)
scaler.step(optimizer1)
scaler.update()
每个优化器都会检查其梯度是否包含 inf/NaN,并独立决定是否跳过步骤。这可能导致一个优化器跳过步骤,而另一个则不跳过。由于跳过步骤的情况很少见(每几百次迭代一次),这不应该影响收敛。如果您在为多优化器模型添加梯度缩放后观察到收敛不佳,请报告 bug。
处理多 GPU#
此处描述的问题仅影响 autocast。 GradScaler 的使用保持不变。
单进程中的 DataParallel#
即使 torch.nn.DataParallel 启动线程在每个设备上运行前向传递。自动类型转换状态会在每个线程中传播,并且以下代码可以正常工作:
model = MyModel()
dp_model = nn.DataParallel(model)
# Sets autocast in the main thread
with autocast(device_type='cuda', dtype=torch.float16):
# dp_model's internal threads will autocast.
output = dp_model(input)
# loss_fn also autocast
loss = loss_fn(output)
DistributedDataParallel,每个进程一个 GPU#
torch.nn.parallel.DistributedDataParallel 的文档建议每个进程使用一个 GPU 以获得最佳性能。在这种情况下,DistributedDataParallel 不会在内部启动线程,因此 autocast 和 GradScaler 的使用不受影响。
DistributedDataParallel,每个进程多个 GPU#
在这种情况下,torch.nn.parallel.DistributedDataParallel 可能会启动一个辅助线程在每个设备上运行前向传递,类似于 torch.nn.DataParallel。 解决方法相同:将自动类型转换作为模型 forward 方法的一部分应用,以确保它在辅助线程中启用。
Autocast 和自定义 Autograd 函数#
如果您的网络使用 自定义 autograd 函数(torch.autograd.Function 的子类),则需要进行更改以实现自动类型转换兼容性,如果任何函数
接受多个浮点张量输入,
包装任何可自动类型转换的操作(请参阅 Autocast 操作参考),或
需要特定的
dtype(例如,如果它包装了仅为dtype编译的 CUDA 扩展)。
在所有情况下,如果您导入了函数且无法修改其定义,一个安全的折衷方法是在出现错误的使用点禁用自动类型转换,并强制以 float32(或 dtype)执行。
with autocast(device_type='cuda', dtype=torch.float16):
...
with autocast(device_type='cuda', dtype=torch.float16, enabled=False):
output = imported_function(input1.float(), input2.float())
如果您是函数的作者(或可以修改其定义),一个更好的解决方案是使用 torch.amp.custom_fwd() 和 torch.amp.custom_bwd() 装饰器,如下面的相关案例所示。
具有多个输入或可自动类型转换操作的函数#
将 custom_fwd 和 custom_bwd(无参数)分别应用于 forward 和 backward。这些确保 forward 在当前自动类型转换状态下执行,并且 backward 在与 forward 相同的自动类型转换状态下执行(这可以防止类型不匹配错误)。
class MyMM(torch.autograd.Function):
@staticmethod
@custom_fwd
def forward(ctx, a, b):
ctx.save_for_backward(a, b)
return a.mm(b)
@staticmethod
@custom_bwd
def backward(ctx, grad):
a, b = ctx.saved_tensors
return grad.mm(b.t()), a.t().mm(grad)
现在 MyMM 可以在任何地方调用,而无需禁用自动类型转换或手动转换输入。
mymm = MyMM.apply
with autocast(device_type='cuda', dtype=torch.float16):
output = mymm(input1, input2)
需要特定 dtype 的函数#
考虑一个需要 torch.float32 输入的自定义函数。将 custom_fwd(device_type='cuda', cast_inputs=torch.float32) 应用于 forward,并将 custom_bwd(device_type='cuda') 应用于 backward。如果 forward 在启用了自动类型转换的区域运行,这些装饰器会将浮点张量输入转换为 float32,在由 device_type 参数指定的设备上(在此示例中为 CUDA),并且在 forward 和 backward 执行期间本地禁用自动类型转换。
class MyFloat32Func(torch.autograd.Function):
@staticmethod
@custom_fwd(device_type='cuda', cast_inputs=torch.float32)
def forward(ctx, input):
ctx.save_for_backward(input)
...
return fwd_output
@staticmethod
@custom_bwd(device_type='cuda')
def backward(ctx, grad):
...
现在 MyFloat32Func 可以在任何地方调用,而无需手动禁用自动类型转换或转换输入。
func = MyFloat32Func.apply
with autocast(device_type='cuda', dtype=torch.float16):
# func will run in float32, regardless of the surrounding autocast state
output = func(input)