Views
No views yet
apex.amp is a tool to enable mixed precision training by changing only 3 lines of your script.
Users can easily experiment with different pure and mixed precision training modes by supplying
different flags to amp.initialize.apex.parallel.DistributedDataParallel is deprecated. Use torch.nn.parallel.DistributedDataParallelapex.parallel.DistributedDataParallel is a module wrapper, similar to
torch.nn.parallel.DistributedDataParallel. It enables convenient multiprocess distributed training,
optimized for NVIDIA's NCCL communication library.torch.nn.SyncBatchNormapex.parallel.SyncBatchNorm extends torch.nn.modules.batchnorm._BatchNorm to
support synchronized BN.
It allreduces stats across processes during multiprocess (DistributedDataParallel) training.
Synchronous BN has been used in cases where only a small
local minibatch can fit on each GPU.
Allreduced stats increase the effective batch size for the BN layer to the
global batch size across all processes (which, technically, is the correct
formulation).
Synchronous BN has been observed to improve converged accuracy in some of our research models.amp training, we introduce the amp.state_dict(), which contains all loss_scalers and their corresponding unskipped steps,
as well as amp.load_state_dict() to restore these attributes.1# Initialization
2opt_level = 'O1'
3model, optimizer = amp.initialize(model, optimizer, opt_level=opt_level)
4
5# Train your model
6...
7with amp.scale_loss(loss, optimizer) as scaled_loss:
8 scaled_loss.backward()
9...
10
11# Save checkpoint
12checkpoint = {
13 'model': model.state_dict(),
14 'optimizer': optimizer.state_dict(),
15 'amp': amp.state_dict()
16}
17torch.save(checkpoint, 'amp_checkpoint.pt')
18...
19
20# Restore
21model = ...
22optimizer = ...
23checkpoint = torch.load('amp_checkpoint.pt')
24
25model, optimizer = amp.initialize(model, optimizer, opt_level=opt_level)
26model.load_state_dict(checkpoint['model'])
27optimizer.load_state_dict(checkpoint['optimizer'])
28amp.load_state_dict(checkpoint['amp'])
29
30# Continue training
31...opt_level. Also note that we recommend calling the load_state_dict methods after amp.initialize.apex.contrib module requires one or more install options other than --cpp_ext and --cuda_ext.
Note that contrib modules do not necessarily support stable PyTorch releases.1git clone https://github.com/NVIDIA/apex
2cd apex
3pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./pip install -v --disable-pip-version-check --no-cache-dir ./apex.optimizers.FusedAdam.apex.normalization.FusedLayerNorm and apex.normalization.FusedRMSNorm.apex.parallel.SyncBatchNorm.apex.parallel.DistributedDataParallel and apex.amp.
DistributedDataParallel, amp, and SyncBatchNorm will still be usable, but they may be slower.pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" . may work if you were able to build Pytorch from source
on your system. A Python-only build via pip install -v --no-cache-dir . is more likely to work.| Module Name | Install Option | Misc |
|---|---|---|
apex_C | --cpp_ext | |
amp_C | --cuda_ext | |
syncbn | --cuda_ext | |
fused_layer_norm_cuda | --cuda_ext | apex.normalization |
mlp_cuda | --cuda_ext | |
scaled_upper_triang_masked_softmax_cuda | --cuda_ext | |
generic_scaled_masked_softmax_cuda | --cuda_ext | |
scaled_masked_softmax_cuda | --cuda_ext | |
fused_weight_gradient_mlp_cuda | --cuda_ext | Requires CUDA>=11 |
permutation_search_cuda | --permutation_search | apex.contrib.sparsity |
bnp | --bnp | apex.contrib.groupbn |
xentropy | --xentropy | apex.contrib.xentropy |
focal_loss_cuda | --focal_loss | apex.contrib.focal_loss |
fused_index_mul_2d | --index_mul_2d | apex.contrib.index_mul_2d |
fused_adam_cuda | --deprecated_fused_adam | apex.contrib.optimizers |
fused_lamb_cuda | --deprecated_fused_lamb | apex.contrib.optimizers |
fast_layer_norm | --fast_layer_norm | apex.contrib.layer_norm. different from fused_layer_norm |
fmhalib | --fmha | apex.contrib.fmha |
fast_multihead_attn | --fast_multihead_attn | apex.contrib.multihead_attn |
transducer_joint_cuda | --transducer | apex.contrib.transducer |
transducer_loss_cuda | --transducer | apex.contrib.transducer |
cudnn_gbn_lib | --cudnn_gbn | Requires cuDNN>=8.5, apex.contrib.cudnn_gbn |
peer_memory_cuda | --peer_memory | apex.contrib.peer_memory |
nccl_p2p_cuda | --nccl_p2p | Requires NCCL >= 2.10, apex.contrib.nccl_p2p |
fast_bottleneck | --fast_bottleneck | Requires peer_memory_cuda and nccl_p2p_cuda, apex.contrib.bottleneck |
fused_conv_bias_relu | --fused_conv_bias_relu | Requires cuDNN>=8.4, apex.contrib.conv_bias_relu |