Accelerate documentation
FP8
FP8
Below are functions and classes relative to the underlying FP8 implementation
TERecipeKwargs
class accelerate.utils.TERecipeKwargs
< source >( use_autocast_during_eval: bool | None = Nonemargin: int | None = Noneinterval: int | None = Nonefp8_format: typing.Literal['HYBRID', 'E4M3', 'E5M2'] = Noneamax_history_len: int | None = Noneamax_compute_algo: typing.Literal['max', 'most_recent'] = Noneoverride_linear_precision: tuple = Noneuse_mxfp8_block_scaling: bool | None = None )
Parameters
- use_autocast_during_eval (
bool, optional, default toFalse) — Whether to use FP8 autocast during eval mode. Generally better metrics are found when this isFalse. - margin (
int, optional, default to 0) — The margin to use for the gradient scaling. - interval (
int, optional, default to 1) — The interval to use for how often the scaling factor is recomputed. - fp8_format (
str, optional, default to “HYBRID”) — The format to use for the FP8 recipe. Must be one ofHYBRID,E4M3orE5M2. (GenerallyHYBRIDfor training,E4M3orE5M2for evaluation) - amax_history_len (
int, optional, default to 1024) — The length of the history to use for the scaling factor computation - amax_compute_algo (
str, optional, default to “most_recent”) — The algorithm to use for the scaling factor computation. Must be one ofmaxormost_recent. - override_linear_precision (
tupleof threebool, optional, default to(False, False, False)) — Whether or not to executefprop,dgrad, andwgradGEMMS in higher precision.
Use this object in your Accelerator to customize the initialization of the recipe for FP8 mixed precision
training with transformer-engine.
For more information on the args, please refer to the API documentation.
AORecipeKwargs
class accelerate.utils.AORecipeKwargs
< source >( config: ForwardRef('Float8LinearConfig') | None = Nonemodule_filter_func: typing.Callable | None = Nonepad_inner_dim: bool | None = Noneenable_fsdp_float8_all_gather: bool | None = None )
Parameters
- config (
torchao.float8.Float8LinearConfig, optional, default toNone) — The configuration for the FP8 training. IfNone, a default config will be created with sensible defaults for most use cases:pad_inner_dim=True: Pads matrix dimensions to be divisible by 16, required fortorch._scaled_mmoperations to prevent runtime errors.enable_fsdp_float8_all_gather=True: Enables FP8 all-gather for FSDP2. This provides memory bandwidth savings by casting parameters before the all-gather operation, saving 50% bandwidth compared to BF16.
You can override these defaults by providing your own
Float8LinearConfiginstance. - module_filter_func (
Callable, optional, default toNone) — Optional function that must take in a module and layer name, and returns a boolean indicating whether the module should be converted to FP8. Defaults toaccelerate.utils.ao.filter_linear_layers. See it for an example.
Use this object in your Accelerator to customize the initialization of the recipe for FP8 mixed precision
training with torchao FP8.
convert_model
accelerate.utils.convert_model
< source >( modelto_transformer_engine = True_convert_linear = True_convert_ln = True )
Recursively converts the linear and layernorm layers of a model to their transformers_engine counterpart.
has_transformer_engine_layers
Returns whether a given model has some transformer_engine layer or not.
contextual_fp8_autocast
accelerate.utils.contextual_fp8_autocast
< source >( model_forwardfp8_recipeuse_during_eval = False )
Wrapper for a model’s forward method to apply FP8 autocast. Is context aware, meaning that by default it will disable FP8 autocast during eval mode, which is generally better for more accurate metrics.
apply_fp8_autowrap
Applies FP8 context manager to the model’s forward method