callbacks.fp16

This module allows the forward and backward passes of your neural net to be done in fp16 (also known as half precision). This is particularly important if you have an NVIDIA GPU with , since it can speed up your training by 200% or more.

To train your model in mixed precision you just have to call Learner.to_fp16, which converts the model and modifies the existing to add MixedPrecision.

to_fp16[source][test]

Put learn in FP16 precision mode.

For example:

Warning: Known issue: Learner.to_fp16 doesn’t work when training GANs.

Details about mixed precision training are available in . We will just summarize the basics here.

Internally, the callback ensures that all model parameters (except batchnorm layers, which require fp32) are converted to fp16, and an fp32 copy is also saved. The fp32 copy (the master parameters) is what is used for actually updating with the optimizer; the fp16 parameters are used for calculating gradients. This helps avoid underflow with small learning rates.

All of this is implemented by the following Callback.

MixedPrecision(learn:Learner, loss_scale:float=None, max_noskip:int=1000, dynamic:bool=True, clip:float=None, flat_master:bool=False, max_scale:float=16777216, loss_fp32:bool=True) :: No tests found for MixedPrecision. To contribute a test please refer to this guide and .

Base class for creating callbacks for a Learner.

You don’t have to call the following functions yourself - they’re called by fastai’s system automatically to enable the class’s functionality.

on_backward_begin[test]

Scale gradients up by self.loss_scale to prevent underflow.

on_backward_end[test]

Convert the gradients back to FP32 and divide them by the scale.

on_loss_begin[test]

Convert half precision output to FP32 to avoid reduction overflow.

on_step_end[test]

on_step_end(**kwargs:Any) No tests found for on_step_end. To contribute a test please refer to this guide and .

Update the params from master to model and zero grad.

on_train_begin[test]

Prepare the master model.


©2021 fast.ai. All rights reserved.
Site last generated: Jan 5, 2021