forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.fp16.html
276 lines (195 loc) · 16.5 KB
/
callbacks.fp16.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
---
title: callbacks.fp16
keywords: fastai
sidebar: home_sidebar
summary: "Training in mixed precision implementation"
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/callbacks.fp16.ipynb
# instructions: https://docs.fast.ai/gen_doc_main.html
-->
<div class="container" id="notebook-container">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Mixed-precision-training">Mixed precision training<a class="anchor-link" href="#Mixed-precision-training">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This module allows the forward and backward passes of your neural net to be done in fp16 (also known as <em>half precision</em>). This is particularly important if you have an NVIDIA GPU with <a href="https://www.nvidia.com/en-us/data-center/tensorcore/">tensor cores</a>, since it can speed up your training by 200% or more.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Overview">Overview<a class="anchor-link" href="#Overview">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To train your model in mixed precision you just have to call <a href="/train.html#to_fp16"><code>Learner.to_fp16</code></a>, which converts the model and modifies the existing <a href="/basic_train.html#Learner"><code>Learner</code></a> to add <a href="/callbacks.fp16.html#MixedPrecision"><code>MixedPrecision</code></a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="to_fp16" class="doc_header"><code>to_fp16</code><a href="https://github.com/fastai/fastai/blob/master/fastai/train.py#L34" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#to_fp16-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>to_fp16</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>loss_scale</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>max_noskip</code></strong>:<code>int</code>=<strong><em><code>1000</code></em></strong>, <strong><code>dynamic</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>flat_master</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>max_scale</code></strong>:<code>float</code>=<strong><em><code>16777216</code></em></strong>) → <a href="/basic_train.html#Learner"><code>Learner</code></a></p>
</blockquote>
<div class="collapse" id="to_fp16-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#to_fp16-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>to_fp16</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Put <code>learn</code> in FP16 precision mode.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>For example:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">path</span> <span class="o">=</span> <span class="n">untar_data</span><span class="p">(</span><span class="n">URLs</span><span class="o">.</span><span class="n">MNIST_SAMPLE</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">ImageDataBunch</span><span class="o">.</span><span class="n">from_folder</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">simple_cnn</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">16</span><span class="p">,</span><span class="mi">16</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="n">learn</span> <span class="o">=</span> <span class="n">Learner</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="n">accuracy</span><span class="p">])</span><span class="o">.</span><span class="n">to_fp16</span><span class="p">()</span>
<span class="n">learn</span><span class="o">.</span><span class="n">fit_one_cycle</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea ">
Total time: 00:03 <p><table style='width:300px; margin-bottom:10px'>
<tr>
<th>epoch</th>
<th>train_loss</th>
<th>valid_loss</th>
<th>accuracy</th>
</tr>
<tr>
<th>1</th>
<th>0.139469</th>
<th>0.115246</th>
<th>0.963199</th>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Details about mixed precision training are available in <a href="https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/index.html">NVIDIA's documentation</a>. We will just summarize the basics here.</p>
<p>The only parameter you may want to tweak is <code>loss_scale</code>. This is used to scale the loss up, so that it doesn't underflow fp16, leading to loss of accuracy (this is reversed for the final gradient calculation after converting back to fp32). Generally the default <code>512</code> works well, however. You can also enable or disable the flattening of the master parameter tensor with <code>flat_master=True</code>, however in our testing the different is negligible.</p>
<p>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 <code>master</code> 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.</p>
<p>All of this is implemented by the following Callback.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="MixedPrecision" class="doc_header"><code>class</code> <code>MixedPrecision</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/fp16.py#L64" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MixedPrecision-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>MixedPrecision</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>loss_scale</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>max_noskip</code></strong>:<code>int</code>=<strong><em><code>1000</code></em></strong>, <strong><code>dynamic</code></strong>:<code>bool</code>=<strong><em><code>True</code></em></strong>, <strong><code>clip</code></strong>:<code>float</code>=<strong><em><code>None</code></em></strong>, <strong><code>flat_master</code></strong>:<code>bool</code>=<strong><em><code>False</code></em></strong>, <strong><code>max_scale</code></strong>:<code>float</code>=<strong><em><code>16777216</code></em></strong>) :: <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a></p>
</blockquote>
<div class="collapse" id="MixedPrecision-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MixedPrecision-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>MixedPrecision</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Base class for creating callbacks for a <a href="/basic_train.html#Learner"><code>Learner</code></a>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Callback-methods">Callback methods<a class="anchor-link" href="#Callback-methods">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>You don't have to call the following functions yourself - they're called by fastai's <a href="/callback.html#Callback"><code>Callback</code></a> system automatically to enable the class's functionality.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="MixedPrecision.on_backward_begin" class="doc_header"><code>on_backward_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/fp16.py#L92" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MixedPrecision-on_backward_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_backward_begin</code>(<strong><code>last_loss</code></strong>:<code>Rank0Tensor</code>, <strong>**<code>kwargs</code></strong>:<code>Any</code>) → <code>Rank0Tensor</code></p>
</blockquote>
<div class="collapse" id="MixedPrecision-on_backward_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MixedPrecision-on_backward_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_backward_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Scale gradients up by <code>self.loss_scale</code> to prevent underflow.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="MixedPrecision.on_backward_end" class="doc_header"><code>on_backward_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/fp16.py#L98" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MixedPrecision-on_backward_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_backward_end</code>(<strong>**<code>kwargs</code></strong>:<code>Any</code>)</p>
</blockquote>
<div class="collapse" id="MixedPrecision-on_backward_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MixedPrecision-on_backward_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_backward_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Convert the gradients back to FP32 and divide them by the scale.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="MixedPrecision.on_loss_begin" class="doc_header"><code>on_loss_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/fp16.py#L88" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MixedPrecision-on_loss_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_loss_begin</code>(<strong><code>last_output</code></strong>:<code>Tensor</code>, <strong>**<code>kwargs</code></strong>:<code>Any</code>) → <code>Tensor</code></p>
</blockquote>
<div class="collapse" id="MixedPrecision-on_loss_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MixedPrecision-on_loss_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_loss_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Convert half precision output to FP32 to avoid reduction overflow.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="MixedPrecision.on_step_end" class="doc_header"><code>on_step_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/fp16.py#L117" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MixedPrecision-on_step_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_step_end</code>(<strong>**<code>kwargs</code></strong>:<code>Any</code>)</p>
</blockquote>
<div class="collapse" id="MixedPrecision-on_step_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MixedPrecision-on_step_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_step_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Update the params from master to model and zero grad.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="MixedPrecision.on_train_begin" class="doc_header"><code>on_train_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/fp16.py#L76" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#MixedPrecision-on_train_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_train_begin</code>(<strong>**<code>kwargs</code></strong>:<code>Any</code>)</p>
</blockquote>
<div class="collapse" id="MixedPrecision-on_train_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#MixedPrecision-on_train_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_train_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Prepare the master model.</p>
</div>
</div>
</div>
</div>
</div>
</div>