forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mem.html
549 lines (399 loc) · 28.6 KB
/
utils.mem.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
---
title: utils.mem
keywords:
sidebar: home_sidebar
---
<!--
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: docs_src/utils.mem.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="Memory-management-utils">Memory management utils<a class="anchor-link" href="#Memory-management-utils">¶</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>Utility functions for memory management. Currently primarily for GPU.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</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="gpu_mem_get" class="doc_header"><code>gpu_mem_get</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L25" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get</code>(<strong><code>id</code></strong>=<strong><em><code>None</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="gpu_mem_get-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>gpu_mem_get</code>:</p><ul><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_mem_by_id</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L25" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>get total, used and free memory (in MBs) for gpu <code>id</code>. if <code>id</code> is not passed, currently selected torch device is used</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><a href="/utils.mem.html#gpu_mem_get"><code>gpu_mem_get</code></a></p>
<ul>
<li>for gpu returns <code>GPUMemory(total, free, used)</code></li>
<li>for cpu returns <code>GPUMemory(0, 0, 0)</code></li>
<li>for invalid gpu id returns <code>GPUMemory(0, 0, 0)</code></li>
</ul>
</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="gpu_mem_get_all" class="doc_header"><code>gpu_mem_get_all</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L36" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get_all-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get_all</code>()</p>
</blockquote>
<div class="collapse" id="gpu_mem_get_all-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get_all-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>gpu_mem_get_all</code>:</p><ul><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_mem_all</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L35" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>get total, used and free memory (in MBs) for each available gpu</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><a href="/utils.mem.html#gpu_mem_get_all"><code>gpu_mem_get_all</code></a></p>
<ul>
<li>for gpu returns <code>[ GPUMemory(total_0, free_0, used_0), GPUMemory(total_1, free_1, used_1), .... ]</code></li>
<li>for cpu returns <code>[]</code></li>
</ul>
</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="gpu_mem_get_free" class="doc_header"><code>gpu_mem_get_free</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L41" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get_free-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get_free</code>()</p>
</blockquote>
<div class="collapse" id="gpu_mem_get_free-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get_free-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>gpu_mem_get_free</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>get free memory (in MBs) for the currently selected gpu id, w/o emptying the cache</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">
</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="gpu_mem_get_free_no_cache" class="doc_header"><code>gpu_mem_get_free_no_cache</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L45" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get_free_no_cache-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get_free_no_cache</code>()</p>
</blockquote>
<div class="collapse" id="gpu_mem_get_free_no_cache-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get_free_no_cache-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>gpu_mem_get_free_no_cache</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>get free memory (in MBs) for the currently selected gpu id, after emptying the cache</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">
</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="gpu_mem_get_used" class="doc_header"><code>gpu_mem_get_used</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L50" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get_used-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get_used</code>()</p>
</blockquote>
<div class="collapse" id="gpu_mem_get_used-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get_used-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>gpu_mem_get_used</code>:</p><ul><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_mem_measure_consumed_reclaimed</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L56" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>get used memory (in MBs) for the currently selected gpu id, w/o emptying the cache</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">
</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="gpu_mem_get_used_no_cache" class="doc_header"><code>gpu_mem_get_used_no_cache</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L59" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get_used_no_cache-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get_used_no_cache</code>()</p>
</blockquote>
<div class="collapse" id="gpu_mem_get_used_no_cache-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get_used_no_cache-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>gpu_mem_get_used_no_cache</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>get used memory (in MBs) for the currently selected gpu id, after emptying the cache</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><a href="/utils.mem.html#gpu_mem_get_used_no_cache"><code>gpu_mem_get_used_no_cache</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="gpu_mem_get_used_fast" class="doc_header"><code>gpu_mem_get_used_fast</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L54" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_get_used_fast-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_get_used_fast</code>(<strong><code>gpu_handle</code></strong>)</p>
</blockquote>
<div class="collapse" id="gpu_mem_get_used_fast-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_get_used_fast-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>gpu_mem_get_used_fast</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>get used memory (in MBs) for the currently selected gpu id, w/o emptying the cache, and needing the <code>gpu_handle</code> arg</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><a href="/utils.mem.html#gpu_mem_get_used_fast"><code>gpu_mem_get_used_fast</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="gpu_with_max_free_mem" class="doc_header"><code>gpu_with_max_free_mem</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L64" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_with_max_free_mem-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_with_max_free_mem</code>()</p>
</blockquote>
<div class="collapse" id="gpu_with_max_free_mem-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_with_max_free_mem-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>gpu_with_max_free_mem</code>:</p><ul><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_with_max_free_mem</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L44" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>get [gpu_id, its_free_ram] for the first gpu with highest available RAM</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><a href="/utils.mem.html#gpu_with_max_free_mem"><code>gpu_with_max_free_mem</code></a>:</p>
<ul>
<li>for gpu returns: <code>gpu_with_max_free_ram_id, its_free_ram</code></li>
<li>for cpu returns: <code>None, 0</code></li>
</ul>
</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="preload_pytorch" class="doc_header"><code>preload_pytorch</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L18" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#preload_pytorch-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>preload_pytorch</code>()</p>
</blockquote>
<div class="collapse" id="preload_pytorch-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#preload_pytorch-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>preload_pytorch</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>
</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><a href="/utils.mem.html#preload_pytorch"><code>preload_pytorch</code></a> is helpful when GPU memory is being measured, since the first time any operation on <code>cuda</code> is performed by pytorch, usually about 0.5GB gets used by CUDA context.</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="GPUMemory" class="doc_header"><code>class</code> <code>GPUMemory</code><a class="source_link" data-toggle="collapse" data-target="#GPUMemory-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>GPUMemory</code>(<strong><code>total</code></strong>, <strong><code>free</code></strong>, <strong><code>used</code></strong>) :: <code>tuple</code></p>
</blockquote>
<div class="collapse" id="GPUMemory-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GPUMemory-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>GPUMemory</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>GPUMemory(total, free, used)</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><a href="/utils.mem.html#GPUMemory"><code>GPUMemory</code></a> is a namedtuple that is returned by functions like <a href="/utils.mem.html#gpu_mem_get"><code>gpu_mem_get</code></a> and <a href="/utils.mem.html#gpu_mem_get_all"><code>gpu_mem_get_all</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="b2mb" class="doc_header"><code>b2mb</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L21" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#b2mb-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>b2mb</code>(<strong><code>num</code></strong>)</p>
</blockquote>
<div class="collapse" id="b2mb-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#b2mb-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>b2mb</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 Bs to MBs and round down</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><a href="/utils.mem.html#b2mb"><code>b2mb</code></a> is a helper utility that just does <code>int(bytes/2**20)</code></p>
</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">
<h2 id="Memory-Tracing-Utils">Memory Tracing Utils<a class="anchor-link" href="#Memory-Tracing-Utils">¶</a></h2>
</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="GPUMemTrace" class="doc_header"><code>class</code> <code>GPUMemTrace</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L72" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#GPUMemTrace-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>GPUMemTrace</code>(<strong><code>silent</code></strong>=<strong><em><code>False</code></em></strong>, <strong><code>ctx</code></strong>=<strong><em><code>None</code></em></strong>, <strong><code>on_exit_report</code></strong>=<strong><em><code>True</code></em></strong>)</p>
</blockquote>
<div class="collapse" id="GPUMemTrace-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#GPUMemTrace-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>GPUMemTrace</code>:</p><ul><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_mem_trace</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L76" class="source_link" style="float:right">[source]</a></li><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_mem_trace_ctx</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L137" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Trace allocated and peaked GPU memory usage (deltas).</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><strong>Arguments</strong>:</p>
<ul>
<li><code>silent</code>: a shortcut to make <code>report</code> and <code>report_n_reset</code> silent w/o needing to remove those calls - this can be done from the constructor, or alternatively you can call <code>silent</code> method anywhere to do the same.</li>
<li><code>ctx</code>: default context note in reports</li>
<li><code>on_exit_report</code>: auto-report on ctx manager exit (default <code>True</code>)</li>
</ul>
<p><strong>Definitions</strong>:</p>
<ul>
<li><p><strong>Delta Used</strong> is the difference between current used memory and used memory at the start of the counter.</p>
</li>
<li><p><strong>Delta Peaked</strong> is the memory overhead if any. It's calculated in two steps:</p>
<ol>
<li>The base measurement is the difference between the peak memory and the used memory at the start of the counter.</li>
<li><p>Then if delta used is positive it gets subtracted from the base value.</p>
<p>It indicates the size of the blip.</p>
<p><strong>Warning</strong>: currently the peak memory usage tracking is implemented using a python thread, which is very unreliable, since there is no guarantee the thread will get a chance at running at the moment the peak memory is occuring (or it might not get a chance to run at all). Therefore we need pytorch to implement multiple concurrent and resettable <a href="https://pytorch.org/docs/stable/cuda.html#torch.cuda.max_memory_allocated"><code>torch.cuda.max_memory_allocated</code></a> counters. Please vote for this <a href="https://github.com/pytorch/pytorch/issues/16266">feature request</a>.</p>
</li>
</ol>
</li>
</ul>
<p><strong>Usage Examples</strong>:</p>
<p>Setup:</p>
<pre><code>from fastai.utils.mem import GPUMemTrace
def some_code(): pass
mtrace = GPUMemTrace()</code></pre>
<p>Example 1: basic measurements via <code>report</code> (prints) and via <a href="/tabular.data.html#tabular.data"><code>data</code></a> (returns) accessors</p>
<pre><code>some_code()
mtrace.report()
delta_used, delta_peaked = mtrace.data()
some_code()
mtrace.report('2nd run of some_code()')
delta_used, delta_peaked = mtrace.data()</code></pre>
<p><code>report</code>'s optional <code>subctx</code> argument can be helpful if you have many <code>report</code> calls and you want to understand which is which in the outputs.</p>
<p>Example 2: measure in a loop, resetting the counter before each run</p>
<pre><code>for i in range(10):
mtrace.reset()
some_code()
mtrace.report(f'i={i}')</code></pre>
<p><code>reset</code> resets all the counters.</p>
<p>Example 3: like example 2, but having <code>report</code> automatically reset the counters</p>
<pre><code>mtrace.reset()
for i in range(10):
some_code()
mtrace.report_n_reset(f'i={i}')</code></pre>
<p>The tracing starts immediately upon the <a href="/utils.mem.html#GPUMemTrace"><code>GPUMemTrace</code></a> object creation, and stops when that object is deleted. But it can also be <code>stop</code>ed, <code>start</code>ed manually as well.</p>
<pre><code>mtrace.start()
mtrace.stop()</code></pre>
<p><code>stop</code> is in particular useful if you want to <strong>freeze</strong> the <a href="/utils.mem.html#GPUMemTrace"><code>GPUMemTrace</code></a> object and to be able to query its data on <code>stop</code> some time down the road.</p>
<p><strong>Reporting</strong>:</p>
<p>In reports you can print a main context passed via the constructor:</p>
<pre><code>mtrace = GPUMemTrace(ctx="foobar")
mtrace.report()</code></pre>
<p>prints:</p>
<pre><code>△Used Peaked MB: 0 0 (foobar)</code></pre>
<p>and then add subcontext notes as needed:</p>
<pre><code>mtrace = GPUMemTrace(ctx="foobar")
mtrace.report('1st try')
mtrace.report('2nd try')</code></pre>
<p>prints:</p>
<pre><code>△Used Peaked MB: 0 0 (foobar: 1st try)
△Used Peaked MB: 0 0 (foobar: 2nd try)</code></pre>
<p>Both context and sub-context are optional, and are very useful if you sprinkle <a href="/utils.mem.html#GPUMemTrace"><code>GPUMemTrace</code></a> in different places around the code.</p>
<p>You can silence report calls w/o needing to remove them via constructor or <code>silent</code>:</p>
<pre><code>mtrace = GPUMemTrace(silent=True)
mtrace.report() # nothing will be printed
mtrace.silent(silent=False)
mtrace.report() # printing resumed
mtrace.silent(silent=True)
mtrace.report() # nothing will be printed</code></pre>
<p><strong>Context Manager</strong>:</p>
<p><a href="/utils.mem.html#GPUMemTrace"><code>GPUMemTrace</code></a> can also be used as a context manager:</p>
<p>Report the used and peaked deltas automatically:</p>
<pre><code>with GPUMemTrace(): some_code()</code></pre>
<p>If you wish to add context:</p>
<pre><code>with GPUMemTrace(ctx='some context'): some_code()</code></pre>
<p>The context manager uses subcontext <code>exit</code> to indicate that the report comes after the context exited.</p>
<p>The reporting is done automatically, which is especially useful in functions due to return call:</p>
<pre><code>def some_func():
with GPUMemTrace(ctx='some_func'):
# some code
return 1
some_func()</code></pre>
<p>prints:</p>
<pre><code>△Used Peaked MB: 0 0 (some_func: exit)</code></pre>
<p>so you still get a perfect report despite the <code>return</code> call here. <code>ctx</code> is useful for specifying the <em>context</em> in case you have many of those calls through your code and you want to know which is which.</p>
<p>And, of course, instead of doing the above, you can use <a href="/utils.mem.html#gpu_mem_trace"><code>gpu_mem_trace</code></a> decorator to do it automatically, including using the function or method name as the context. Therefore, the example below does the same without modifying the function.</p>
<pre><code>@gpu_mem_trace
def some_func():
# some code
return 1
some_func()</code></pre>
<p>If you don't wish the automatic reporting, just pass <code>on_exit_report=False</code> in the constructor:</p>
<pre><code>with GPUMemTrace(ctx='some_func', on_exit_report=False) as mtrace:
some_code()
mtrace.report("measured in ctx")</code></pre>
<p>or the same w/o the context note:</p>
<pre><code>with GPUMemTrace(on_exit_report=False) as mtrace: some_code()
print(mtrace) # or mtrace.report()</code></pre>
<p>And, of course, you can get the numerical data (in rounded MBs):</p>
<pre><code>with GPUMemTrace() as mtrace: some_code()
delta_used, delta_peaked = mtrace.data()</code></pre>
</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="gpu_mem_trace" class="doc_header"><code>gpu_mem_trace</code><a href="https://github.com/fastai/fastai/blob/master/fastai/utils/mem.py#L169" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#gpu_mem_trace-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>gpu_mem_trace</code>(<strong><code>func</code></strong>)</p>
</blockquote>
<div class="collapse" id="gpu_mem_trace-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#gpu_mem_trace-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>gpu_mem_trace</code>:</p><ul><li><code>pytest -sv tests/test_utils_mem.py::test_gpu_mem_trace_decorator</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_utils_mem.py#L178" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>A decorator that runs <a href="/utils.mem.html#GPUMemTrace"><code>GPUMemTrace</code></a> w/ report on func</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>This allows you to decorate any function or method with:</p>
<pre><code>@gpu_mem_trace
def my_function(): pass
# run:
my_function()</code></pre>
<p>and it will automatically print the report including the function name as a context:</p>
<pre><code>△Used Peaked MB: 0 0 (my_function: exit)</code></pre>
<p>In the case of methods it'll print a fully qualified method, e.g.:</p>
<pre><code>△Used Peaked MB: 0 0 (Class.function: exit)</code></pre>
</div>
</div>
</div>
</div>