-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathptq.html
763 lines (748 loc) · 35.4 KB
/
ptq.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<meta content="width=device-width,initial-scale=1" name="viewport"/>
<meta content="ie=edge" http-equiv="x-ua-compatible"/>
<meta content="Copy to clipboard" name="lang:clipboard.copy"/>
<meta content="Copied to clipboard" name="lang:clipboard.copied"/>
<meta content="en" name="lang:search.language"/>
<meta content="True" name="lang:search.pipeline.stopwords"/>
<meta content="True" name="lang:search.pipeline.trimmer"/>
<meta content="No matching documents" name="lang:search.result.none"/>
<meta content="1 matching document" name="lang:search.result.one"/>
<meta content="# matching documents" name="lang:search.result.other"/>
<meta content="[\s\-]+" name="lang:search.tokenizer"/>
<link crossorigin="" href="https://fonts.gstatic.com/" rel="preconnect"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,500,700|Roboto:300,400,400i,700&display=fallback" rel="stylesheet"/>
<style>
body,
input {
font-family: "Roboto", "Helvetica Neue", Helvetica, Arial, sans-serif
}
code,
kbd,
pre {
font-family: "Roboto Mono", "Courier New", Courier, monospace
}
</style>
<link href="../_static/stylesheets/application.css" rel="stylesheet"/>
<link href="../_static/stylesheets/application-palette.css" rel="stylesheet"/>
<link href="../_static/stylesheets/application-fixes.css" rel="stylesheet"/>
<link href="../_static/fonts/material-icons.css" rel="stylesheet"/>
<meta content="84bd00" name="theme-color"/>
<script src="../_static/javascripts/modernizr.js">
</script>
<title>
Post Training Quantization (PTQ) — TRTorch 0.0.3 documentation
</title>
<link href="../_static/material.css" rel="stylesheet" type="text/css"/>
<link href="../_static/pygments.css" rel="stylesheet" type="text/css"/>
<link href="../_static/collapsible-lists/css/tree_view.css" rel="stylesheet" type="text/css"/>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js">
</script>
<script src="../_static/jquery.js">
</script>
<script src="../_static/underscore.js">
</script>
<script src="../_static/doctools.js">
</script>
<script src="../_static/language_data.js">
</script>
<script src="../_static/collapsible-lists/js/CollapsibleLists.compressed.js">
</script>
<script src="../_static/collapsible-lists/js/apply-collapsible-lists.js">
</script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js">
</script>
<link href="../genindex.html" rel="index" title="Index"/>
<link href="../search.html" rel="search" title="Search"/>
<link href="trtorchc.html" rel="next" title="trtorchc"/>
<link href="getting_started.html" rel="prev" title="Getting Started"/>
</head>
<body data-md-color-accent="light-green" data-md-color-primary="light-green" dir="ltr">
<svg class="md-svg">
<defs data-children-count="0">
<svg height="448" id="__github" viewbox="0 0 416 448" width="416" xmlns="http://www.w3.org/2000/svg">
<path d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z" fill="currentColor">
</path>
</svg>
</defs>
</svg>
<input class="md-toggle" data-md-toggle="drawer" id="__drawer" type="checkbox"/>
<input class="md-toggle" data-md-toggle="search" id="__search" type="checkbox"/>
<label class="md-overlay" data-md-component="overlay" for="__drawer">
</label>
<a class="md-skip" href="#tutorials/ptq" tabindex="1">
Skip to content
</a>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex navheader">
<div class="md-flex__cell md-flex__cell--shrink">
<a class="md-header-nav__button md-logo" href="../index.html" title="TRTorch 0.0.3 documentation">
<i class="md-icon">
</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer">
</label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
TRTorch
</span>
<span class="md-header-nav__topic">
Post Training Quantization (PTQ)
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search">
</label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search">
</label>
<div class="md-search__inner" role="search">
<form action="../search.html" class="md-search__form" method="GET" name="search">
<input autocapitalize="off" autocomplete="off" class="md-search__input" data-md-component="query" data-md-state="active" name="q" placeholder="Search" spellcheck="false" type="text"/>
<label class="md-icon md-search__icon" for="__search">
</label>
<button class="md-icon md-search__icon" data-md-component="reset" tabindex="-1" type="reset">
</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix="">
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list">
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a class="md-source" data-md-source="github" href="https://github.com/nvidia/TRTorch/" title="Go to repository">
<div class="md-source__icon">
<svg height="28" viewbox="0 0 24 24" width="28" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<use height="24" width="24" xlink:href="#__github">
</use>
</svg>
</div>
<div class="md-source__repository">
TRTorch
</div>
</a>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink dropdown">
<button class="dropdownbutton">
Versions
</button>
<div class="dropdown-content md-hero">
<a href="https://nvidia.github.io/TRTorch/" title="master">
master
</a>
<a href="https://nvidia.github.io/TRTorch/v0.0.3/" title="v0.0.3">
v0.0.3
</a>
<a href="https://nvidia.github.io/TRTorch/v0.0.2/" title="v0.0.2">
v0.0.2
</a>
<a href="https://nvidia.github.io/TRTorch/v0.0.1/" title="v0.0.1">
v0.0.1
</a>
</div>
</div>
</div>
</nav>
</header>
<div class="md-container">
<nav class="md-tabs" data-md-component="tabs">
<div class="md-tabs__inner md-grid">
<ul class="md-tabs__list">
<li class="md-tabs__item">
<a class="md-tabs__link" href="../index.html">
TRTorch 0.0.3 documentation
</a>
</li>
</ul>
</div>
</nav>
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0">
<label class="md-nav__title md-nav__title--site" for="__drawer">
<a class="md-nav__button md-logo" href="../index.html" title="TRTorch 0.0.3 documentation">
<i class="md-icon">
</i>
</a>
<a href="../index.html" title="TRTorch 0.0.3 documentation">
TRTorch
</a>
</label>
<div class="md-nav__source">
<a class="md-source" data-md-source="github" href="https://github.com/nvidia/TRTorch/" title="Go to repository">
<div class="md-source__icon">
<svg height="28" viewbox="0 0 24 24" width="28" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<use height="24" width="24" xlink:href="#__github">
</use>
</svg>
</div>
<div class="md-source__repository">
TRTorch
</div>
</a>
</div>
<ul class="md-nav__list">
<li class="md-nav__item">
<span class="md-nav__link caption">
<span class="caption-text">
Getting Started
</span>
</span>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="installation.html">
Installation
</a>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="getting_started.html">
Getting Started
</a>
</li>
<li class="md-nav__item">
<input class="md-toggle md-nav__toggle" data-md-toggle="toc" id="__toc" type="checkbox"/>
<label class="md-nav__link md-nav__link--active" for="__toc">
Post Training Quantization (PTQ)
</label>
<a class="md-nav__link md-nav__link--active" href="#">
Post Training Quantization (PTQ)
</a>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">
Contents
</label>
<ul class="md-nav__list" data-md-scrollfix="">
<li class="md-nav__item">
<a class="md-nav__link" href="#tutorials-ptq--page-root">
Post Training Quantization (PTQ)
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a class="md-nav__link" href="#how-to-create-your-own-ptq-application">
How to create your own PTQ application
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a class="md-nav__link" href="#citations">
Citations
</a>
</li>
</ul>
</nav>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a class="md-nav__extra_link" href="../_sources/tutorials/ptq.rst.txt">
Show Source
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="trtorchc.html">
trtorchc
</a>
</li>
<li class="md-nav__item">
<span class="md-nav__link caption">
<span class="caption-text">
Notebooks
</span>
</span>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../_notebooks/lenet-getting-started.html">
TRTorch Getting Started - LeNet
</a>
</li>
<li class="md-nav__item">
<span class="md-nav__link caption">
<span class="caption-text">
Python API Documenation
</span>
</span>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../py_api/trtorch.html">
trtorch
</a>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../py_api/logging.html">
trtorch.logging
</a>
</li>
<li class="md-nav__item">
<span class="md-nav__link caption">
<span class="caption-text">
C++ API Documenation
</span>
</span>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../_cpp_api/trtorch_cpp.html">
TRTorch C++ API
</a>
</li>
<li class="md-nav__item">
<span class="md-nav__link caption">
<span class="caption-text">
Contributor Documentation
</span>
</span>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../contributors/system_overview.html">
System Overview
</a>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../contributors/writing_converters.html">
Writing Converters
</a>
</li>
<li class="md-nav__item">
<a class="md-nav__link" href="../contributors/useful_links.html">
Useful Links for TRTorch Development
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">
Contents
</label>
<ul class="md-nav__list" data-md-scrollfix="">
<li class="md-nav__item">
<a class="md-nav__link" href="#tutorials-ptq--page-root">
Post Training Quantization (PTQ)
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a class="md-nav__link" href="#how-to-create-your-own-ptq-application">
How to create your own PTQ application
</a>
<nav class="md-nav">
<ul class="md-nav__list">
<li class="md-nav__item">
<a class="md-nav__link" href="#citations">
Citations
</a>
</li>
</ul>
</nav>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a class="md-nav__extra_link" href="../_sources/tutorials/ptq.rst.txt">
Show Source
</a>
</li>
<li class="md-nav__item" id="searchbox">
</li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset" role="main">
<span id="ptq">
</span>
<h1 id="tutorials-ptq--page-root">
Post Training Quantization (PTQ)
<a class="headerlink" href="#tutorials-ptq--page-root" title="Permalink to this headline">
¶
</a>
</h1>
<p>
Post Training Quantization (PTQ) is a technique to reduce the required computational resources for inference
while still preserving the accuracy of your model by mapping the traditional FP32 activation space to a reduced
INT8 space. TensorRT uses a calibration step which executes your model with sample data from the target domain
and track the activations in FP32 to calibrate a mapping to INT8 that minimizes the information loss between
FP32 inference and INT8 inference.
</p>
<p>
Users writing TensorRT applications are required to setup a calibrator class which will provide sample data to
the TensorRT calibrator. With TRTorch we look to leverage existing infrastructure in PyTorch to make implementing
calibrators easier.
</p>
<p>
LibTorch provides a
<code class="docutils literal notranslate">
<span class="pre">
DataLoader
</span>
</code>
and
<code class="docutils literal notranslate">
<span class="pre">
Dataset
</span>
</code>
API which steamlines preprocessing and batching input data.
This section of the PyTorch documentation has more information
<a class="reference external" href="https://pytorch.org/tutorials/advanced/cpp_frontend.html#loading-data">
https://pytorch.org/tutorials/advanced/cpp_frontend.html#loading-data
</a>
.
TRTorch uses Dataloaders as the base of a generic calibrator implementation. So you will be able to reuse or quickly
implement a
<code class="docutils literal notranslate">
<span class="pre">
torch::Dataset
</span>
</code>
for your target domain, place it in a DataLoader and create a INT8 Calibrator
which you can provide to TRTorch to run INT8 Calibration during compliation of your module.
</p>
<span id="writing-ptq">
</span>
<h2 id="how-to-create-your-own-ptq-application">
How to create your own PTQ application
<a class="headerlink" href="#how-to-create-your-own-ptq-application" title="Permalink to this headline">
¶
</a>
</h2>
<p>
Here is an example interface of a
<code class="docutils literal notranslate">
<span class="pre">
torch::Dataset
</span>
</code>
class for CIFAR10:
</p>
<div class="highlight-c++ notranslate">
<table class="highlighttable">
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 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</pre>
</div>
</td>
<td class="code">
<div class="highlight">
<pre><span></span><span class="c1">//cpp/ptq/datasets/cifar10.h</span>
<span class="cp">#pragma once</span>
<span class="cp">#include</span> <span class="cpf">"torch/data/datasets/base.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"torch/data/example.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">"torch/types.h"</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><cstddef></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><string></span><span class="cp"></span>
<span class="k">namespace</span> <span class="n">datasets</span> <span class="p">{</span>
<span class="c1">// The CIFAR10 Dataset</span>
<span class="k">class</span> <span class="nc">CIFAR10</span> <span class="o">:</span> <span class="k">public</span> <span class="n">torch</span><span class="o">::</span><span class="n">data</span><span class="o">::</span><span class="n">datasets</span><span class="o">::</span><span class="n">Dataset</span><span class="o"><</span><span class="n">CIFAR10</span><span class="o">></span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="c1">// The mode in which the dataset is loaded</span>
<span class="k">enum</span> <span class="k">class</span> <span class="nc">Mode</span> <span class="p">{</span> <span class="n">kTrain</span><span class="p">,</span> <span class="n">kTest</span> <span class="p">};</span>
<span class="c1">// Loads CIFAR10 from un-tarred file</span>
<span class="c1">// Dataset can be found https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz</span>
<span class="c1">// Root path should be the directory that contains the content of tarball</span>
<span class="k">explicit</span> <span class="nf">CIFAR10</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&</span> <span class="n">root</span><span class="p">,</span> <span class="n">Mode</span> <span class="n">mode</span> <span class="o">=</span> <span class="n">Mode</span><span class="o">::</span><span class="n">kTrain</span><span class="p">);</span>
<span class="c1">// Returns the pair at index in the dataset</span>
<span class="n">torch</span><span class="o">::</span><span class="n">data</span><span class="o">::</span><span class="n">Example</span><span class="o"><></span> <span class="n">get</span><span class="p">(</span><span class="kt">size_t</span> <span class="n">index</span><span class="p">)</span> <span class="k">override</span><span class="p">;</span>
<span class="c1">// The size of the dataset</span>
<span class="n">c10</span><span class="o">::</span><span class="n">optional</span><span class="o"><</span><span class="kt">size_t</span><span class="o">></span> <span class="n">size</span><span class="p">()</span> <span class="k">const</span> <span class="k">override</span><span class="p">;</span>
<span class="c1">// The mode the dataset is in</span>
<span class="kt">bool</span> <span class="nf">is_train</span><span class="p">()</span> <span class="k">const</span> <span class="k">noexcept</span><span class="p">;</span>
<span class="c1">// Returns all images stacked into a single tensor</span>
<span class="k">const</span> <span class="n">torch</span><span class="o">::</span><span class="n">Tensor</span><span class="o">&</span> <span class="n">images</span><span class="p">()</span> <span class="k">const</span><span class="p">;</span>
<span class="c1">// Returns all targets stacked into a single tensor</span>
<span class="k">const</span> <span class="n">torch</span><span class="o">::</span><span class="n">Tensor</span><span class="o">&</span> <span class="n">targets</span><span class="p">()</span> <span class="k">const</span><span class="p">;</span>
<span class="c1">// Trims the dataset to the first n pairs</span>
<span class="n">CIFAR10</span><span class="o">&&</span> <span class="n">use_subset</span><span class="p">(</span><span class="kt">int64_t</span> <span class="n">new_size</span><span class="p">);</span>
<span class="k">private</span><span class="o">:</span>
<span class="n">Mode</span> <span class="n">mode_</span><span class="p">;</span>
<span class="n">torch</span><span class="o">::</span><span class="n">Tensor</span> <span class="n">images_</span><span class="p">,</span> <span class="n">targets_</span><span class="p">;</span>
<span class="p">};</span>
<span class="p">}</span> <span class="c1">// namespace datasets</span>
</pre>
</div>
</td>
</tr>
</table>
</div>
<p>
This class’s implementation reads from the binary distribution of the CIFAR10 dataset and builds two tensors which hold the images and labels.
</p>
<p>
We use a subset of the dataset to use for calibration, since we don’t need the the full dataset for effective calibration and calibration does
some take time, then define the preprocessing to apply to the images in the dataset and create a DataLoader from the dataset which will batch the data:
</p>
<div class="highlight-c++ notranslate">
<div class="highlight">
<pre><span></span><span class="k">auto</span> <span class="n">calibration_dataset</span> <span class="o">=</span> <span class="n">datasets</span><span class="o">::</span><span class="n">CIFAR10</span><span class="p">(</span><span class="n">data_dir</span><span class="p">,</span> <span class="n">datasets</span><span class="o">::</span><span class="n">CIFAR10</span><span class="o">::</span><span class="n">Mode</span><span class="o">::</span><span class="n">kTest</span><span class="p">)</span>
<span class="p">.</span><span class="n">use_subset</span><span class="p">(</span><span class="mi">320</span><span class="p">)</span>
<span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="n">torch</span><span class="o">::</span><span class="n">data</span><span class="o">::</span><span class="n">transforms</span><span class="o">::</span><span class="n">Normalize</span><span class="o"><></span><span class="p">({</span><span class="mf">0.4914</span><span class="p">,</span> <span class="mf">0.4822</span><span class="p">,</span> <span class="mf">0.4465</span><span class="p">},</span>
<span class="p">{</span><span class="mf">0.2023</span><span class="p">,</span> <span class="mf">0.1994</span><span class="p">,</span> <span class="mf">0.2010</span><span class="p">}))</span>
<span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="n">torch</span><span class="o">::</span><span class="n">data</span><span class="o">::</span><span class="n">transforms</span><span class="o">::</span><span class="n">Stack</span><span class="o"><></span><span class="p">());</span>
<span class="k">auto</span> <span class="n">calibration_dataloader</span> <span class="o">=</span> <span class="n">torch</span><span class="o">::</span><span class="n">data</span><span class="o">::</span><span class="n">make_data_loader</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">calibration_dataset</span><span class="p">),</span>
<span class="n">torch</span><span class="o">::</span><span class="n">data</span><span class="o">::</span><span class="n">DataLoaderOptions</span><span class="p">().</span><span class="n">batch_size</span><span class="p">(</span><span class="mi">32</span><span class="p">)</span>
<span class="p">.</span><span class="n">workers</span><span class="p">(</span><span class="mi">2</span><span class="p">));</span>
</pre>
</div>
</div>
<p>
Next we create a calibrator from the
<code class="docutils literal notranslate">
<span class="pre">
calibration_dataloader
</span>
</code>
using the calibrator factory (found in
<code class="docutils literal notranslate">
<span class="pre">
trtorch/ptq.h
</span>
</code>
):
</p>
<div class="highlight-c++ notranslate">
<div class="highlight">
<pre><span></span><span class="cp">#include</span> <span class="cpf">"trtorch/ptq.h"</span><span class="cp"></span>
<span class="p">...</span>
<span class="k">auto</span> <span class="n">calibrator</span> <span class="o">=</span> <span class="n">trtorch</span><span class="o">::</span><span class="n">ptq</span><span class="o">::</span><span class="n">make_int8_calibrator</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">calibration_dataloader</span><span class="p">),</span> <span class="n">calibration_cache_file</span><span class="p">,</span> <span class="nb">true</span><span class="p">);</span>
</pre>
</div>
</div>
<p>
Here we also define a location to write a calibration cache file to which we can use to reuse the calibration data without needing the dataset and whether or not
we should use the cache file if it exists. There also exists a
<code class="docutils literal notranslate">
<span class="pre">
trtorch::ptq::make_int8_cache_calibrator
</span>
</code>
factory which creates a calibrator that uses the cache
only for cases where you may do engine building on a machine that has limited storage (i.e. no space for a full dataset) or to have a simpiler deployment application.
</p>
<p>
The calibrator factories create a calibrator that inherits from a
<code class="docutils literal notranslate">
<span class="pre">
nvinfer1::IInt8Calibrator
</span>
</code>
virtual class (
<code class="docutils literal notranslate">
<span class="pre">
nvinfer1::IInt8EntropyCalibrator2
</span>
</code>
by default) which
defines the calibration algorithm used when calibrating. You can explicitly make the selection of calibration algorithm like this:
</p>
<div class="highlight-c++ notranslate">
<div class="highlight">
<pre><span></span><span class="c1">// MinMax Calibrator is geared more towards NLP tasks</span>
<span class="k">auto</span> <span class="n">calibrator</span> <span class="o">=</span> <span class="n">trtorch</span><span class="o">::</span><span class="n">ptq</span><span class="o">::</span><span class="n">make_int8_calibrator</span><span class="o"><</span><span class="n">nvinfer1</span><span class="o">::</span><span class="n">IInt8MinMaxCalibrator</span><span class="o">></span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">calibration_dataloader</span><span class="p">),</span> <span class="n">calibration_cache_file</span><span class="p">,</span> <span class="nb">true</span><span class="p">);</span>
</pre>
</div>
</div>
<p>
Then all thats required to setup the module for INT8 calibration is to set the following compile settings in the
<cite>
trtorch::ExtraInfo
</cite>
struct and compiling the module:
</p>
<div class="highlight-c++ notranslate">
<div class="highlight">
<pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o"><</span><span class="kt">int64_t</span><span class="o">>></span> <span class="n">input_shape</span> <span class="o">=</span> <span class="p">{{</span><span class="mi">32</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">32</span><span class="p">}};</span>
<span class="c1">/// Configure settings for compilation</span>
<span class="k">auto</span> <span class="n">extra_info</span> <span class="o">=</span> <span class="n">trtorch</span><span class="o">::</span><span class="n">ExtraInfo</span><span class="p">({</span><span class="n">input_shape</span><span class="p">});</span>
<span class="c1">/// Set operating precision to INT8</span>
<span class="n">extra_info</span><span class="p">.</span><span class="n">op_precision</span> <span class="o">=</span> <span class="n">torch</span><span class="o">::</span><span class="n">kI8</span><span class="p">;</span>
<span class="c1">/// Use the TensorRT Entropy Calibrator</span>
<span class="n">extra_info</span><span class="p">.</span><span class="n">ptq_calibrator</span> <span class="o">=</span> <span class="n">calibrator</span><span class="p">;</span>
<span class="c1">/// Set a larger workspace (you may get better performace from doing so)</span>
<span class="n">extra_info</span><span class="p">.</span><span class="n">workspace_size</span> <span class="o">=</span> <span class="mi">1</span> <span class="o"><<</span> <span class="mi">28</span><span class="p">;</span>
<span class="k">auto</span> <span class="n">trt_mod</span> <span class="o">=</span> <span class="n">trtorch</span><span class="o">::</span><span class="n">CompileGraph</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">extra_info</span><span class="p">);</span>
</pre>
</div>
</div>
<p>
If you have an existing Calibrator implementation for TensorRT you may directly set the
<code class="docutils literal notranslate">
<span class="pre">
ptq_calibrator
</span>
</code>
field with a pointer to your calibrator and it will work as well.
</p>
<p>
From here not much changes in terms of how to execution works. You are still able to fully use LibTorch as the sole interface for inference. Data should remain
in FP32 precision when it’s passed into
<cite>
trt_mod.forward
</cite>
. There exists an example application in the TRTorch demo that takes you from training a VGG16 network on
CIFAR10 to deploying in INT8 with TRTorch here:
<a class="reference external" href="https://github.com/NVIDIA/TRTorch/tree/master/cpp/ptq">
https://github.com/NVIDIA/TRTorch/tree/master/cpp/ptq
</a>
</p>
<h3 id="citations">
Citations
<a class="headerlink" href="#citations" title="Permalink to this headline">
¶
</a>
</h3>
<p>
Krizhevsky, A., & Hinton, G. (2009). Learning multiple layers of features from tiny images.
</p>
<p>
Simonyan, K., & Zisserman, A. (2014). Very deep convolutional networks for large-scale image recognition. arXiv preprint arXiv:1409.1556.
</p>
</article>
</div>
</div>
</main>
</div>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a class="md-flex md-footer-nav__link md-footer-nav__link--prev" href="getting_started.html" rel="prev" title="Getting Started">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button">
</i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
Getting Started
</span>
</div>
</a>
<a class="md-flex md-footer-nav__link md-footer-nav__link--next" href="trtorchc.html" rel="next" title="trtorchc">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
trtorchc
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button">
</i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
<div class="md-footer-copyright__highlight">
© Copyright 2020, NVIDIA Corporation.
</div>
Created using
<a href="http://www.sphinx-doc.org/">
Sphinx
</a>
3.1.2.
and
<a href="https://github.com/bashtage/sphinx-material/">
Material for
Sphinx
</a>
</div>
</div>
</div>
</footer>
<script src="../_static/javascripts/application.js">
</script>
<script>
app.initialize({version: "1.0.4", url: {base: ".."}})
</script>
</body>
</html>