{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mixed precision training" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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 [tensor cores](https://www.nvidia.com/en-us/data-center/tensorcore/), since it can speed up your training by 200% or more." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.callbacks.fp16 import *\n", "from fastai import *\n", "from fastai.vision import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To train your model in mixed precision you just have to call [`Learner.to_fp16`](/train.html#to_fp16), which converts the model and modifies the existing [`Learner`](/basic_train.html#Learner) to add [`MixedPrecision`](/callbacks.fp16.html#MixedPrecision)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hide_input": true }, "outputs": [ { "data": { "text/markdown": [ "
to_fp16
[source]to_fp16
(`learn`:[`Learner`](/basic_train.html#Learner), `loss_scale`:`float`=`512.0`, `flat_master`:`bool`=`False`) → [`Learner`](/basic_train.html#Learner)\n",
"\n",
"Transform `learn` in FP16 precision. "
],
"text/plain": [
"class
MixedPrecision
[source]MixedPrecision
(`learn`:[`Learner`](/basic_train.html#Learner), `loss_scale`:`float`=`512.0`, `flat_master`:`bool`=`False`) :: [`Callback`](/callback.html#Callback)\n",
"\n",
"Callback that handles mixed-precision training. "
],
"text/plain": [
"on_backward_begin
[source]on_backward_begin
(`last_loss`:`Rank0Tensor`, `kwargs`:`Any`) → `Rank0Tensor`\n",
"\n",
"Scale gradients up by `loss_scale` to prevent underflow. "
],
"text/plain": [
"on_backward_end
[source]on_backward_end
(`kwargs`:`Any`)\n",
"\n",
"Convert the gradients back to FP32 and divide them by the scale. "
],
"text/plain": [
"on_loss_begin
[source]on_loss_begin
(`last_output`:`Tensor`, `kwargs`:`Any`) → `Tensor`\n",
"\n",
"Convert half precision output to FP32 to avoid reduction overflow. "
],
"text/plain": [
"on_step_end
[source]on_step_end
(`kwargs`:`Any`)\n",
"\n",
"Update the params from master to model and zero grad. "
],
"text/plain": [
"on_train_begin
[source]on_train_begin
(`kwargs`:`Any`)\n",
"\n",
"Ensure everything is in half precision mode. "
],
"text/plain": [
"on_train_end
[source]on_train_end
(`kwargs`:`Any`)\n",
"\n",
"Remove half precision transforms added at `on_train_begin`. "
],
"text/plain": [
"