You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/amp.md
+42-8Lines changed: 42 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
# AMP (Automatic Mixed Precision) with Pytorch/XLA
2
2
3
-
Pytorch/XLA's AMP extends [Pytorch's AMP package](https://pytorch.org/docs/stable/amp.html) with support for automatic mixed precision on XLA:GPU and XLA:TPU devices. This document describes how to use AMP on XLA devices and best practices.
4
-
5
-
TODO: talk about how we follow PYtorch's rules and blah blah.
3
+
Pytorch/XLA's AMP extends [Pytorch's AMP package](https://pytorch.org/docs/stable/amp.html) with support for automatic mixed precision on XLA:GPU and XLA:TPU devices.
4
+
AMP is used to accelerate training and inference by executing certain operations in `float32` and other operations in a lower precision datatype (`float16` or `bfloat16` depending on hardware support).
5
+
This document describes how to use AMP on XLA devices and best practices.
6
6
7
7
## AMP for XLA:TPU
8
-
TPUs natively support [bfloat16](https://cloud.google.com/tpu/docs/bfloat16) and float32 datatypes.
8
+
AMP on TPUs automatically casts operations to run in either `float32` or `bfloat16` because TPUs natively support bfloat16. A simple TPU AMP example is below:
9
9
10
10
```
11
11
# Creates model and optimizer in default precision
@@ -25,14 +25,40 @@ for input, target in data:
25
25
loss.backward()
26
26
xm.optimizer_step.(optimizer)
27
27
```
28
+
`autocast(xm.xla_device())` aliases `torch.amp.autocast('xla')` when the XLA Device is a TPU. Alternatively, if a script is only used with TPUs, then `torch.amp.autocast('xla')` can be directly used.
29
+
30
+
Please file an issue or submit a pull request if there is an operator that should be autocasted that is not included.
31
+
28
32
29
33
### Best Practices
30
-
1. Do not set `XLA_USE_BF16` flag when using AMP on TPUs. This will override the per-operator precision settings provided by AMP and cause all operators to execute in bfloat16.
31
-
2. Since TPU's use bfloat16 mixed precision, gradient scaling is not required.
32
-
TODO: What is the error message?
33
-
3. Pytorch/XLA provides modified version of [optimizers](https://github.com/pytorch/xla/tree/master/torch_xla/amp/syncfree) that avoid the additional sync between device and host.
34
+
1.`autocast` should wrap only the forward pass(es) and loss computation(s) of the network. Backward ops run in the same type that autocast used for the corresponding forward ops.
35
+
2. Do not set `XLA_USE_BF16` flag when using AMP on TPUs. This will override the per-operator precision settings provided by AMP and cause all operators to execute in bfloat16.
36
+
3. Since TPU's use bfloat16 mixed precision, gradient scaling is not necessary.
37
+
4. Pytorch/XLA provides modified version of [optimizers](https://github.com/pytorch/xla/tree/master/torch_xla/amp/syncfree) that avoid the additional sync between device and host.
38
+
39
+
### Supported Operators
40
+
AMP on TPUs operates like Pytorch's AMP. Rules for how autocasting is applied is summarized below:
41
+
42
+
Only out-of-place ops and Tensor methods are eligible to be autocasted. In-place variants and calls that explicitly supply an out=... Tensor are allowed in autocast-enabled regions, but won’t go through autocasting. For example, in an autocast-enabled region a.addmm(b, c) can autocast, but a.addmm_(b, c) and a.addmm(b, c, out=d) cannot. For best performance and stability, prefer out-of-place ops in autocast-enabled regions.
43
+
44
+
Ops that run in float64 or non-floating-point dtypes are not eligible, and will run in these types whether or not autocast is enabled. Additionally, Ops called with an explicit dtype=... argument are not eligible, and will produce output that respects the dtype argument.
45
+
46
+
Ops not listed below do not go through autocasting. They run in the type defined by their inputs. Autocasting may still change the type in which unlisted ops run if they’re downstream from autocasted ops.
AMP on XLA:GPU devices reuse Pytorch's AMP rules. See [Pytorch's AMP documentation](https://pytorch.org/docs/stable/amp.html) for CUDA specific behavior. A simple CUDA AMP example is below:
36
62
37
63
```
38
64
# Creates model and optimizer in default precision
@@ -57,6 +83,14 @@ for input, target in data:
57
83
scaler.update()
58
84
```
59
85
86
+
`autocast(xm.xla_device())` aliases `torch.cuda.amp.autocast()` when the XLA Device is a CUDA device. Alternatively, if a script is only used with CUDA devices, then `torch.cuda.amp.autocast` can be directly used.
87
+
88
+
### Best Practices
89
+
1.`autocast` should wrap only the forward pass(es) and loss computation(s) of the network. Backward ops run in the same type that autocast used for the corresponding forward ops.
90
+
2. Do not set `XLA_USE_F16` flag when using AMP on Cuda devices. This will override the per-operator precision settings provided by AMP and cause all operators to execute in float16.
91
+
3. Use gradient scaling to prevent float16 gradients from underflowing.
92
+
4. Pytorch/XLA provides modified version of [optimizers](https://github.com/pytorch/xla/tree/master/torch_xla/amp/syncfree) that avoid the additional sync between device and host.
93
+
60
94
## Examples
61
95
Our [mnist training script](https://github.com/pytorch/xla/blob/master/test/test_train_mp_mnist_amp.py) and [imagenet training script](https://github.com/pytorch/xla/blob/master/test/test_train_mp_imagenet_amp.py) demonstrate how AMP is used on both TPUs and GPUs.
0 commit comments