| 1 |
<!--Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 |
the License. You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 |
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 |
specific language governing permissions and limitations under the License. |
| 11 |
|
| 12 |
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 |
rendered properly in your Markdown viewer. |
| 14 |
--> |
| 15 |
|
| 16 |
# Low Precision Training Methods |
| 17 |
|
| 18 |
Accelerate provides integrations to train on lower precision methods using specified supported hardware through the `TransformersEngine`, `MS-AMP`, and `torchao` packages. This documentation will help guide you through what hardware is supported, how to configure your [`Accelerator`] to leverage the low precision methods, and what you can expect when training. |
| 19 |
|
| 20 |
## What training on FP8 means |
| 21 |
|
| 22 |
To explore more of the nitty-gritty in training in FP8 with PyTorch and Accelerate, check out the [concept_guide](../concept_guides/low_precision_training) on why this can be difficult. But essentially rather than training in BF16, some (or all) aspects of training a model can be performed using 8 bits instead of 16. The challenge is doing so without degrading final performance. |
| 23 |
|
| 24 |
This is only enabled on specific NVIDIA hardware, namely: |
| 25 |
|
| 26 |
* Anything after the 3000 series consumer graphics cards (such as the 4090) |
| 27 |
* Hopper-based GPU architectures (such as the `H100` and `H200`) |
| 28 |
|
| 29 |
What this will result in is some reduction in the memory used (as we've cut the needed memory in half for some parts of training) and an increase in throughput *should* be seen as well for larger models that can replace certain layers with FP8-enabled ones. |
| 30 |
|
| 31 |
## Configuring the Accelerator |
| 32 |
|
| 33 |
Currently two actively maintained backends for FP8 are supported (`TransformersEngine` and `torchao`), each with different capabilities and configurations. A legacy `MS-AMP` backend also exists but is no longer recommended (see [below](#configuring-ms-amp) for details). |
| 34 |
|
| 35 |
To use either, the same core API is used. Just pass `mixed_precision="fp8"` to either the [`Accelerator`], during `accelerate config` when prompted about mixed precision, or as part of your `config.yaml` file in the `mixed_precision` key: |
| 36 |
|
| 37 |
```{python} |
| 38 |
from accelerate import Accelerator |
| 39 |
accelerator = Accelerator(mixed_precision="fp8") |
| 40 |
``` |
| 41 |
|
| 42 |
To specify a backend (and customize other parts of the FP8 mixed precision setup), you can utilize one of the `RecipeKwargs` dataclasses such as [`utils.AORecipeKwargs`], [`utils.TERecipeKwargs`], or [`utils.MSAMPRecipeKwargs`]; you can also clarify it in your config `yaml`/during `accelerate launch`. We recommend using `TransformersEngine` or `torchao` for new projects: |
| 43 |
|
| 44 |
```{python} |
| 45 |
from accelerate import Accelerator |
| 46 |
from accelerate.utils import TERecipeKwargs, AORecipeKwargs |
| 47 |
# Use TransformersEngine |
| 48 |
kwargs = [TERecipeKwargs()] |
| 49 |
# Or to use torchao |
| 50 |
# kwargs = [AORecipeKwargs()] |
| 51 |
accelerator = Accelerator(mixed_precision="fp8", kwarg_handlers=kwargs) |
| 52 |
``` |
| 53 |
|
| 54 |
```{yaml} |
| 55 |
mixed_precision: fp8 |
| 56 |
fp8_config: |
| 57 |
amax_compute_algo: max |
| 58 |
amax_history_len: 1024 |
| 59 |
backend: TE |
| 60 |
fp8_format: HYBRID |
| 61 |
interval: 1 |
| 62 |
margin: 0 |
| 63 |
override_linear_precision: (false, false, false) |
| 64 |
use_autocast_during_eval: false |
| 65 |
``` |
| 66 |
|
| 67 |
## Configuring MS-AMP |
| 68 |
|
| 69 |
<Tip warning={true}> |
| 70 |
|
| 71 |
**⚠️ Deprecated / Unmaintained:** MS-AMP is no longer actively maintained by Microsoft. The [MS-AMP repository](https://github.com/Azure/MS-AMP) has not received updates since 2023 and has known compatibility issues: |
| 72 |
|
| 73 |
- Requires CUDA 11.x (does not support CUDA 12.x+) |
| 74 |
- Requires older NCCL versions incompatible with recent PyTorch releases |
| 75 |
- Does not support recent PyTorch versions (2.2+) |
| 76 |
|
| 77 |
**We strongly recommend using [`TransformersEngine`](#configuring-transformersengine) or [`torchao`](#configuring-torchao) instead for all new and existing FP8 training workflows.** Both are actively maintained and support modern CUDA/PyTorch versions. Native PyTorch FP8 support via `torchao` is particularly promising as a vendor-neutral solution. |
| 78 |
|
| 79 |
The MS-AMP backend is retained in Accelerate for legacy compatibility but may be removed in a future release. |
| 80 |
|
| 81 |
</Tip> |
| 82 |
|
| 83 |
`MS-AMP` has a single configuration argument: the optimization level. |
| 84 |
|
| 85 |
Currently two levels of optimization are supported in the Accelerate integration, `"O1"` and `"O2"` (using the letter 'o', not zero). |
| 86 |
|
| 87 |
* `"O1"` will cast the weight gradients and `all_reduce` communications to happen in 8-bit, while the rest are done in 16 bit. This reduces the general GPU memory usage and speeds up communication bandwidths. |
| 88 |
* `"O2"` will also cast first-order optimizer states into 8 bit, while the second order states are in FP16. (Currently just the `Adam` optimizer is supported). This tries its best to minimize final accuracy degradation and will save the highest potential memory. |
| 89 |
|
| 90 |
To specify an optimization level, pass it to the `FP8KwargsHandler` by setting the `optimization_level` argument: |
| 91 |
|
| 92 |
```{python} |
| 93 |
from accelerate import Accelerator |
| 94 |
from accelerate.utils import FP8RecipeKwargs |
| 95 |
kwargs = [FP8RecipeKwargs(backend="msamp", optimization_level="O2")] |
| 96 |
accelerator = Accelerator(mixed_precision="fp8", kwarg_handlers=kwargs) |
| 97 |
``` |
| 98 |
|
| 99 |
Or during `accelerate launch` via `--fp8_backend=msamp --fp8_opt_level=O2` |
| 100 |
|
| 101 |
Similarly this can be set in your `config.yaml`: |
| 102 |
|
| 103 |
```{yaml} |
| 104 |
mixed_precision: fp8 |
| 105 |
fp8_config: |
| 106 |
backend: MSAMP |
| 107 |
opt_level: O2 |
| 108 |
``` |
| 109 |
|
| 110 |
## Configuring TransformersEngine |
| 111 |
|
| 112 |
TransformersEngine has many options for customizing how and what FP8 calculations are performed. A full list of supported arguments and what they mean are available in [NVIDIA's documentation](https://docs.nvidia.com/deeplearning/transformer-engine/user-guide/api/common.html), however they are restated as part of [`FP8KwargsHandler`]'s docstring for your convenience. |
| 113 |
|
| 114 |
Accelerate tries to set sensible defaults, but exploring and tweaking the various parameters yourself can lead to better performance potentially. |
| 115 |
|
| 116 |
To use it, specify `backend="te"` and modify any of the arguments you want as part of your kwarg handler: |
| 117 |
|
| 118 |
```{python} |
| 119 |
from accelerate import Accelerator |
| 120 |
from accelerate.utils import FP8RecipeKwargs |
| 121 |
kwargs = [FP8RecipeKwargs(backend="te", ...)] |
| 122 |
accelerator = Accelerator(mixed_precision="fp8", kwarg_handlers=kwargs) |
| 123 |
``` |
| 124 |
|
| 125 |
Or during `accelerate launch` via `--fp8_backend=te ...`. Use `accelerate launch --fp8_backend=te -h` to see relevant arguments. |
| 126 |
|
| 127 |
Similarly this can be set in your `config.yaml`: |
| 128 |
|
| 129 |
```{yaml} |
| 130 |
mixed_precision: fp8 |
| 131 |
fp8_config: |
| 132 |
amax_compute_algo: max |
| 133 |
amax_history_len: 1024 |
| 134 |
backend: TE |
| 135 |
fp8_format: HYBRID |
| 136 |
interval: 1 |
| 137 |
margin: 0 |
| 138 |
override_linear_precision: (false, false, false) |
| 139 |
use_autocast_during_eval: false |
| 140 |
``` |
| 141 |
|
| 142 |
## Configuring `torchao` |
| 143 |
|
| 144 |
`torchao` is a [PyTorch-driven](https://github.com/pytorch/ao/tree/main/torchao/float8) hackable FP8 backend, aiming to be more approchable than the prior two engines. One of the core differences with `ao` compared to the prior two is that for numerical stability, it's found to be generally better off keeping the first *and* last layers in the model at the regular precision (be it FP32 or BF16), and then the other layers quantized down to FP8. As a result, a config for `ao` looks a bit differently: |
| 145 |
|
| 146 |
> Note: this API is experimental and is subject to change |
| 147 |
|
| 148 |
```{python} |
| 149 |
from accelerate import Accelerator |
| 150 |
from accelerate.utils import AORecipeKwargs, TorchDynamoPlugin, FullyShardedDataParallelPlugin |
| 151 |
from torchao.float8 import Float8LinearConfig |
| 152 |
|
| 153 |
fsdp2_plugin = FullyShardedDataParallelPlugin( |
| 154 |
fsdp_version=2, |
| 155 |
cpu_ram_efficient_loading=False, # CPU RAM efficient loading CANNOT work with fp8 torchao |
| 156 |
fsdp_auto_wrap_policy="TRANSFORMER_BASED_WRAP", |
| 157 |
) |
| 158 |
dynamo_plugin = TorchDynamoPlugin( |
| 159 |
backend="inductor", |
| 160 |
use_regional_compilation=True, |
| 161 |
) |
| 162 |
fp8_config = Float8LinearConfig( |
| 163 |
enable_fsdp_float8_all_gather=True, # Use FP8 all_gather in FSDP2 |
| 164 |
pad_inner_dim=True, |
| 165 |
) |
| 166 |
kwargs = [AORecipeKwargs( |
| 167 |
config=fp8_config |
| 168 |
)] |
| 169 |
accelerator = Accelerator( |
| 170 |
mixed_precision="fp8", |
| 171 |
fsdp_plugin=fsdp2_plugin, |
| 172 |
dynamo_plugin=dynamo_plugin, |
| 173 |
kwarg_handlers=kwargs, |
| 174 |
) |
| 175 |
``` |
| 176 |
|
| 177 |
Or during `accelerate launch` via `--fp8_backend=ao ...`. Use `accelerate launch --fp8_backend=ao -h` to see relevant arguments. |
| 178 |
|
| 179 |
Similarly, this can be set in `config.yaml`: |
| 180 |
|
| 181 |
```{yaml} |
| 182 |
mixed_precision: fp8 |
| 183 |
fsdp_config: |
| 184 |
fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP |
| 185 |
fsdp_cpu_ram_efficient_loading: false |
| 186 |
fsdp_version: 2 |
| 187 |
fp8_config: |
| 188 |
backend: AO |
| 189 |
pad_inner_dim: true |
| 190 |
enable_fsdp_float8_all_gather: true |
| 191 |
dynamo_config: |
| 192 |
dynamo_backend: INDUCTOR |
| 193 |
dynamo_use_regional_compilation: true |
| 194 |
``` |
| 195 |
|
| 196 |
To learn more about the specific parameters to be used, please see the official `torchao` repo. |
| 197 |
|
| 198 |
|
| 199 |
## Example Zoo |
| 200 |
|
| 201 |
We have examples showcasing training with FP8 both with accelerate and its underlying implementation available in the accelerate repo. |
| 202 |
Currently we support scripts showcasing: |
| 203 |
|
| 204 |
* Single GPU |
| 205 |
* Distributed Data Parallelism (Multi-GPU) |
| 206 |
* Fully Sharded Data Parallelism |
| 207 |
* DeepSpeed ZeRO 1 through 3 |
| 208 |
|
| 209 |
Find out more [here](https://github.com/huggingface/accelerate/tree/main/benchmarks/fp8) |
| 210 |
|
| 211 |
## Further Reading |
| 212 |
|
| 213 |
To learn more about training in FP8 please check out the following resources: |
| 214 |
|
| 215 |
* [Our concept guide](../concept_guides/low_precision_training) detailing into more about TransformersEngine, torchao, and MS-AMP |
| 216 |
* [The `transformers-engine` documentation](https://docs.nvidia.com/deeplearning/transformer-engine/user-guide/api/common.html) |
| 217 |
* [The `torchao` documentation](https://github.com/pytorch/ao/tree/main/torchao/float8) |
| 218 |
* [The `MS-AMP` documentation](https://azure.github.io/MS-AMP/docs/) (⚠️ no longer maintained) |