forked from pytorch/pytorch.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.html
2091 lines (1838 loc) · 193 KB
/
package.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta name="robots" content="noindex">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>torch.package — PyTorch 1.12 documentation</title>
<link rel="canonical" href="https://pytorch.org/docs/stable/package.html"/>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> -->
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" type="text/css" />
<link rel="stylesheet" href="_static/katex-math.css" type="text/css" />
<link rel="stylesheet" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" type="text/css" />
<link rel="stylesheet" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" type="text/css" />
<link rel="stylesheet" href="_static/css/jit.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="torch.profiler" href="profiler.html" />
<link rel="prev" title="torch.overrides" href="torch.overrides.html" />
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117752657-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-117752657-2');
</script>
<!-- End Google Analytics -->
<script src="_static/js/modernizr.min.js"></script>
<!-- Preload the theme fonts -->
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Preload the katex fonts -->
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.2/css/all.css" integrity="sha384-vSIIfh2YWi9wW0r9iZe7RJPrKwp6bG+s9QZMoITbCckVJqGCCRhc+ccxNcdpHuYu" crossorigin="anonymous">
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
<div class="main-menu">
<ul>
<li>
<a href="https://pytorch.org/get-started">Get Started</a>
</li>
<li>
<a href="https://pytorch.org/ecosystem">Ecosystem</a>
</li>
<li>
<a href="https://pytorch.org/mobile">Mobile</a>
</li>
<li>
<a href="https://pytorch.org/blog/">Blog</a>
</li>
<li>
<a href="https://pytorch.org/tutorials">Tutorials</a>
</li>
<li class="active docs-active">
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="resource-option with-down-orange-arrow">
Docs
</a>
<div class="resources-dropdown-menu">
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/docs/stable/index.html">
<span class="dropdown-title">PyTorch</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/audio/stable/index.html">
<span class="dropdown-title">torchaudio</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/text/stable/index.html">
<span class="dropdown-title">torchtext</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/vision/stable/index.html">
<span class="dropdown-title">torchvision</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torchrec">
<span class="dropdown-title">TorchRec</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/data">
<span class="dropdown-title">TorchData</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/serve/">
<span class="dropdown-title">TorchServe</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/torchx/">
<span class="dropdown-title">TorchX</span>
<p></p>
</a>
<a class="doc-dropdown-option nav-dropdown-item" href="https://pytorch.org/xla">
<span class="dropdown-title">PyTorch on XLA Devices</span>
<p></p>
</a>
</div>
</li>
<li>
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="resource-option with-down-arrow">
Resources
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/features">
<span class="dropdown-title">About</span>
<p>Learn about PyTorch’s features and capabilities</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/#community-module">
<span class="dropdown-title">Community</span>
<p>Join the PyTorch developer community to contribute, learn, and get your questions answered.</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/resources">
<span class="dropdown-title">Developer Resources</span>
<p>Find resources and get questions answered</p>
</a>
<a class="nav-dropdown-item" href="https://discuss.pytorch.org/" target="_blank">
<span class="dropdown-title">Forums</span>
<p>A place to discuss PyTorch code, issues, install, research</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/hub">
<span class="dropdown-title">Models (Beta)</span>
<p>Discover, publish, and reuse pre-trained models</p>
</a>
</div>
</div>
</li>
<li>
<a href="https://github.com/pytorch/pytorch">GitHub</a>
</li>
</ul>
</div>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<body class="pytorch-body">
<div class="table-of-contents-link-wrapper">
<span>Table of Contents</span>
<a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
</div>
<nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
<div class="pytorch-side-scroll">
<div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<div class="pytorch-left-menu-search">
<div class="version">
<a href='https://pytorch.org/docs/versions.html'>1.12 ▼</a>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search Docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p class="caption" role="heading"><span class="caption-text">Community</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="community/build_ci_governance.html">PyTorch Governance | Build + CI</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/contribution_guide.html">PyTorch Contribution Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/design.html">PyTorch Design Philosophy</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/governance.html">PyTorch Governance | Mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="community/persons_of_interest.html">PyTorch Governance | Maintainers</a></li>
</ul>
<p class="caption"><span class="caption-text">Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="notes/amp_examples.html">CUDA Automatic Mixed Precision examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/autograd.html">Autograd mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/broadcasting.html">Broadcasting semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cpu_threading_torchscript_inference.html">CPU threading and TorchScript inference</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/cuda.html">CUDA semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/ddp.html">Distributed Data Parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/extending.html">Extending PyTorch</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/gradcheck.html">Gradcheck mechanics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/hip.html">HIP (ROCm) semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/large_scale_deployments.html">Features for large-scale deployments</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/modules.html">Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/mps.html">MPS backend</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/multiprocessing.html">Multiprocessing best practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/numerical_accuracy.html">Numerical accuracy</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/randomness.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/serialization.html">Serialization semantics</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes/windows.html">Windows FAQ</a></li>
</ul>
<p class="caption"><span class="caption-text">Language Bindings</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="cpp_index.html">C++</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/javadoc/">Javadoc</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">torch::deploy</a></li>
</ul>
<p class="caption"><span class="caption-text">Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="torch.html">torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.html">torch.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.functional.html">torch.nn.functional</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensors.html">torch.Tensor</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_attributes.html">Tensor Attributes</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensor_view.html">Tensor Views</a></li>
<li class="toctree-l1"><a class="reference internal" href="amp.html">torch.amp</a></li>
<li class="toctree-l1"><a class="reference internal" href="autograd.html">torch.autograd</a></li>
<li class="toctree-l1"><a class="reference internal" href="library.html">torch.library</a></li>
<li class="toctree-l1"><a class="reference internal" href="cuda.html">torch.cuda</a></li>
<li class="toctree-l1"><a class="reference internal" href="backends.html">torch.backends</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">torch.distributed</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.algorithms.join.html">torch.distributed.algorithms.join</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.elastic.html">torch.distributed.elastic</a></li>
<li class="toctree-l1"><a class="reference internal" href="fsdp.html">torch.distributed.fsdp</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributed.optim.html">torch.distributed.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="distributions.html">torch.distributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="fft.html">torch.fft</a></li>
<li class="toctree-l1"><a class="reference internal" href="futures.html">torch.futures</a></li>
<li class="toctree-l1"><a class="reference internal" href="fx.html">torch.fx</a></li>
<li class="toctree-l1"><a class="reference internal" href="hub.html">torch.hub</a></li>
<li class="toctree-l1"><a class="reference internal" href="jit.html">torch.jit</a></li>
<li class="toctree-l1"><a class="reference internal" href="linalg.html">torch.linalg</a></li>
<li class="toctree-l1"><a class="reference internal" href="monitor.html">torch.monitor</a></li>
<li class="toctree-l1"><a class="reference internal" href="special.html">torch.special</a></li>
<li class="toctree-l1"><a class="reference internal" href="torch.overrides.html">torch.overrides</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">torch.package</a></li>
<li class="toctree-l1"><a class="reference internal" href="profiler.html">torch.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="nn.init.html">torch.nn.init</a></li>
<li class="toctree-l1"><a class="reference internal" href="onnx.html">torch.onnx</a></li>
<li class="toctree-l1"><a class="reference internal" href="optim.html">torch.optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="complex_numbers.html">Complex Numbers</a></li>
<li class="toctree-l1"><a class="reference internal" href="ddp_comm_hooks.html">DDP Communication Hooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="pipeline.html">Pipeline Parallelism</a></li>
<li class="toctree-l1"><a class="reference internal" href="quantization.html">Quantization</a></li>
<li class="toctree-l1"><a class="reference internal" href="rpc.html">Distributed RPC Framework</a></li>
<li class="toctree-l1"><a class="reference internal" href="random.html">torch.random</a></li>
<li class="toctree-l1"><a class="reference internal" href="nested.html">torch.nested</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse.html">torch.sparse</a></li>
<li class="toctree-l1"><a class="reference internal" href="storage.html">torch.Storage</a></li>
<li class="toctree-l1"><a class="reference internal" href="testing.html">torch.testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmark_utils.html">torch.utils.benchmark</a></li>
<li class="toctree-l1"><a class="reference internal" href="bottleneck.html">torch.utils.bottleneck</a></li>
<li class="toctree-l1"><a class="reference internal" href="checkpoint.html">torch.utils.checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="cpp_extension.html">torch.utils.cpp_extension</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">torch.utils.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="dlpack.html">torch.utils.dlpack</a></li>
<li class="toctree-l1"><a class="reference internal" href="mobile_optimizer.html">torch.utils.mobile_optimizer</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">torch.utils.model_zoo</a></li>
<li class="toctree-l1"><a class="reference internal" href="tensorboard.html">torch.utils.tensorboard</a></li>
<li class="toctree-l1"><a class="reference internal" href="type_info.html">Type Info</a></li>
<li class="toctree-l1"><a class="reference internal" href="named_tensor.html">Named Tensors</a></li>
<li class="toctree-l1"><a class="reference internal" href="name_inference.html">Named Tensors operator coverage</a></li>
<li class="toctree-l1"><a class="reference internal" href="config_mod.html">torch.__config__</a></li>
</ul>
<p class="caption"><span class="caption-text">Libraries</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/audio/stable">torchaudio</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/data">TorchData</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/torchrec">TorchRec</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/serve">TorchServe</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/text/stable">torchtext</a></li>
<li class="toctree-l1"><a class="reference external" href="https://pytorch.org/vision/stable">torchvision</a></li>
<li class="toctree-l1"><a class="reference external" href="http://pytorch.org/xla/">PyTorch on XLA Devices</a></li>
</ul>
</div>
</div>
</nav>
<div class="pytorch-container">
<div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
<div class="pytorch-breadcrumbs-wrapper">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="pytorch-breadcrumbs">
<li>
<a href="index.html">
Docs
</a> >
</li>
<li>torch.package</li>
<li class="pytorch-breadcrumbs-aside">
<a href="_sources/package.rst.txt" rel="nofollow"><img src="_static/images/view-page-source-icon.svg"></a>
</li>
</ul>
</div>
</div>
<div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
Shortcuts
</div>
</div>
<section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
<div class="pytorch-content-left">
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<span class="target" id="module-torch.package"></span><span class="target" id="module-torch.package.analyze"></span><div class="section" id="torch-package">
<h1>torch.package<a class="headerlink" href="#torch-package" title="Permalink to this headline">¶</a></h1>
<p><code class="docutils literal notranslate"><span class="pre">torch.package</span></code> adds support for creating hermetic packages containing arbitrary
PyTorch code. These packages can be saved, shared, used to load and execute models
at a later date or on a different machine, and can even be deployed to production using
<code class="docutils literal notranslate"><span class="pre">torch::deploy</span></code>.</p>
<p>This document contains tutorials, how-to guides, explanations, and an API reference that
will help you learn more about <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> and how to use it.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This module depends on the <code class="docutils literal notranslate"><span class="pre">pickle</span></code> module which is not secure. Only unpackage data you trust.</p>
<p>It is possible to construct malicious pickle data which will <strong>execute arbitrary code during unpickling</strong>.
Never unpackage data that could have come from an untrusted source, or that could have been tampered with.</p>
<p>For more information, review the <a class="reference external" href="https://docs.python.org/3/library/pickle.html">documentation</a> for the <code class="docutils literal notranslate"><span class="pre">pickle</span></code> module.</p>
</div>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#tutorials" id="id1">Tutorials</a></p>
<ul>
<li><p><a class="reference internal" href="#packaging-your-first-model" id="id2">Packaging your first model</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#how-do-i" id="id3">How do I…</a></p>
<ul>
<li><p><a class="reference internal" href="#see-what-is-inside-a-package" id="id4">See what is inside a package?</a></p></li>
<li><p><a class="reference internal" href="#see-why-a-given-module-was-included-as-a-dependency" id="id5">See why a given module was included as a dependency?</a></p></li>
<li><p><a class="reference internal" href="#include-arbitrary-resources-with-my-package-and-access-them-later" id="id6">Include arbitrary resources with my package and access them later?</a></p></li>
<li><p><a class="reference internal" href="#customize-how-a-class-is-packaged" id="id7">Customize how a class is packaged?</a></p></li>
<li><p><a class="reference internal" href="#test-in-my-source-code-whether-or-not-it-is-executing-inside-a-package" id="id8">Test in my source code whether or not it is executing inside a package?</a></p></li>
<li><p><a class="reference internal" href="#patch-code-into-a-package" id="id9">Patch code into a package?</a></p></li>
<li><p><a class="reference internal" href="#access-package-contents-from-packaged-code" id="id10">Access package contents from packaged code?</a></p></li>
<li><p><a class="reference internal" href="#distinguish-between-packaged-code-and-non-packaged-code" id="id11">Distinguish between packaged code and non-packaged code?</a></p></li>
<li><p><a class="reference internal" href="#re-export-an-imported-object" id="id12">Re-export an imported object?</a></p></li>
<li><p><a class="reference internal" href="#package-a-torchscript-module" id="id13">Package a TorchScript module?</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#explanation" id="id14">Explanation</a></p>
<ul>
<li><p><a class="reference internal" href="#torch-package-format-overview" id="id15"><code class="docutils literal notranslate"><span class="pre">torch.package</span></code> Format Overview</a></p></li>
<li><p><a class="reference internal" href="#how-torch-package-finds-your-code-s-dependencies" id="id16">How <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> finds your code’s dependencies</a></p></li>
<li><p><a class="reference internal" href="#dependency-management" id="id17">Dependency Management</a></p></li>
<li><p><a class="reference internal" href="#torch-package-sharp-edges" id="id18"><code class="docutils literal notranslate"><span class="pre">torch.package</span></code> sharp edges</a></p></li>
<li><p><a class="reference internal" href="#how-torch-package-keeps-packages-isolated-from-each-other" id="id19">How <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> keeps packages isolated from each other</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#api-reference" id="id20">API Reference</a></p></li>
</ul>
</div>
<div class="section" id="tutorials">
<h2><a class="toc-backref" href="#id1">Tutorials</a><a class="headerlink" href="#tutorials" title="Permalink to this headline">¶</a></h2>
<div class="section" id="packaging-your-first-model">
<h3><a class="toc-backref" href="#id2">Packaging your first model</a><a class="headerlink" href="#packaging-your-first-model" title="Permalink to this headline">¶</a></h3>
<p>A tutorial that guides you through packaging and unpackaging a simple model is available
<a class="reference external" href="https://colab.research.google.com/drive/1lFZkLyViGfXxB-m3jqlyTQuYToo3XLo-">on Colab</a>.
After completing this exercise, you will be familiar with the basic API for creating and using
Torch packages.</p>
</div>
</div>
<div class="section" id="how-do-i">
<h2><a class="toc-backref" href="#id3">How do I…</a><a class="headerlink" href="#how-do-i" title="Permalink to this headline">¶</a></h2>
<div class="section" id="see-what-is-inside-a-package">
<h3><a class="toc-backref" href="#id4">See what is inside a package?</a><a class="headerlink" href="#see-what-is-inside-a-package" title="Permalink to this headline">¶</a></h3>
<div class="section" id="treat-the-package-like-a-zip-archive">
<h4>Treat the package like a ZIP archive<a class="headerlink" href="#treat-the-package-like-a-zip-archive" title="Permalink to this headline">¶</a></h4>
<p>The container format for a <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> is ZIP, so any tools that work with standard ZIP files should
work for exploring the contents. Some common ways to interact with ZIP files:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">unzip</span> <span class="pre">my_package.pt</span></code> will unzip the <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> archive to disk, where you can freely inspect its contents.</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ unzip my_package.pt && tree my_package
my_package
├── .data
│ ├── 94304870911616.storage
│ ├── 94304900784016.storage
│ ├── extern_modules
│ └── version
├── models
│ └── model_1.pkl
└── torchvision
└── models
├── resnet.py
└── utils.py
~ cd my_package && cat torchvision/models/resnet.py
...
</pre></div>
</div>
<ul class="simple">
<li><p>The Python <code class="docutils literal notranslate"><span class="pre">zipfile</span></code> module provides a standard way to read and write ZIP archive contents.</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">zipfile</span> <span class="kn">import</span> <span class="n">ZipFile</span>
<span class="k">with</span> <span class="n">ZipFile</span><span class="p">(</span><span class="s2">"my_package.pt"</span><span class="p">)</span> <span class="k">as</span> <span class="n">myzip</span><span class="p">:</span>
<span class="n">file_bytes</span> <span class="o">=</span> <span class="n">myzip</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="s2">"torchvision/models/resnet.py"</span><span class="p">)</span>
<span class="c1"># edit file_bytes in some way</span>
<span class="n">myzip</span><span class="o">.</span><span class="n">writestr</span><span class="p">(</span><span class="s2">"torchvision/models/resnet.py"</span><span class="p">,</span> <span class="n">new_file_bytes</span><span class="p">)</span>
</pre></div>
</div>
<ul class="simple">
<li><p>vim has the ability to natively read ZIP archives. You can even edit files and :<code class="docutils literal notranslate"><span class="pre">write</span></code> them back into the archive!</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># add this to your .vimrc to treat `*.pt` files as zip files</span>
<span class="n">au</span> <span class="n">BufReadCmd</span> <span class="o">*.</span><span class="n">pt</span> <span class="n">call</span> <span class="nb">zip</span><span class="c1">#Browse(expand("<amatch>"))</span>
<span class="o">~</span> <span class="n">vi</span> <span class="n">my_package</span><span class="o">.</span><span class="n">pt</span>
</pre></div>
</div>
</div>
<div class="section" id="use-the-file-structure-api">
<h4>Use the <code class="docutils literal notranslate"><span class="pre">file_structure()</span></code> API<a class="headerlink" href="#use-the-file-structure-api" title="Permalink to this headline">¶</a></h4>
<p><a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> and <a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a> provide a <code class="docutils literal notranslate"><span class="pre">file_structure()</span></code> method, which will return a printable
and queryable <code class="docutils literal notranslate"><span class="pre">Folder</span></code> object. The <code class="docutils literal notranslate"><span class="pre">Folder</span></code> object is a simple directory structure that you can use to explore the
current contents of a <code class="docutils literal notranslate"><span class="pre">torch.package</span></code>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">Folder</span></code> object itself is directly printable and will print out a file tree representation. To filter what is returned,
use the glob-style <code class="docutils literal notranslate"><span class="pre">include</span></code> and <code class="docutils literal notranslate"><span class="pre">exclude</span></code> filtering arguments.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">PackageExporter</span><span class="p">(</span><span class="s1">'my_package.pt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">pe</span><span class="p">:</span>
<span class="n">pe</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s1">'models'</span><span class="p">,</span> <span class="s1">'model_1.pkl'</span><span class="p">,</span> <span class="n">mod</span><span class="p">)</span>
<span class="c1"># can limit printed items with include/exclude args</span>
<span class="nb">print</span><span class="p">(</span><span class="n">pe</span><span class="o">.</span><span class="n">file_structure</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s2">"**/utils.py"</span><span class="p">,</span> <span class="s2">"**/*.pkl"</span><span class="p">],</span> <span class="n">exclude</span><span class="o">=</span><span class="s2">"**/*.storages"</span><span class="p">))</span>
<span class="n">importer</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="s1">'my_package.pt'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">importer</span><span class="o">.</span><span class="n">file_structure</span><span class="p">())</span> <span class="c1"># will print out all files</span>
</pre></div>
</div>
<p>Output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span># filtered with glob pattern:
# include=["**/utils.py", "**/*.pkl"], exclude="**/*.storages"
─── my_package.pt
├── models
│ └── model_1.pkl
└── torchvision
└── models
└── utils.py
# all files
─── my_package.pt
├── .data
│ ├── 94304870911616.storage
│ ├── 94304900784016.storage
│ ├── extern_modules
│ └── version
├── models
│ └── model_1.pkl
└── torchvision
└── models
├── resnet.py
└── utils.py
</pre></div>
</div>
<p>You can also query <code class="docutils literal notranslate"><span class="pre">Folder</span></code> objects with the <code class="docutils literal notranslate"><span class="pre">has_file()</span></code> method.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">exporter_file_structure</span> <span class="o">=</span> <span class="n">exporter</span><span class="o">.</span><span class="n">file_structure</span><span class="p">()</span>
<span class="n">found</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="n">exporter_file_structure</span><span class="o">.</span><span class="n">has_file</span><span class="p">(</span><span class="s2">"package_a/subpackage.py"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="see-why-a-given-module-was-included-as-a-dependency">
<h3><a class="toc-backref" href="#id5">See why a given module was included as a dependency?</a><a class="headerlink" href="#see-why-a-given-module-was-included-as-a-dependency" title="Permalink to this headline">¶</a></h3>
<p>Say there is a given module <code class="docutils literal notranslate"><span class="pre">foo</span></code>, and you want to know why your <a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a> is pulling in <code class="docutils literal notranslate"><span class="pre">foo</span></code> as a dependency.</p>
<p><a class="reference internal" href="#torch.package.PackageExporter.get_rdeps" title="torch.package.PackageExporter.get_rdeps"><code class="xref py py-meth docutils literal notranslate"><span class="pre">PackageExporter.get_rdeps()</span></code></a> will return all modules that directly depend on <code class="docutils literal notranslate"><span class="pre">foo</span></code>.</p>
<p>If you would like to see how a given module <code class="docutils literal notranslate"><span class="pre">src</span></code> depends on <code class="docutils literal notranslate"><span class="pre">foo</span></code>, the <a class="reference internal" href="#torch.package.PackageExporter.all_paths" title="torch.package.PackageExporter.all_paths"><code class="xref py py-meth docutils literal notranslate"><span class="pre">PackageExporter.all_paths()</span></code></a> method will
return a DOT-formatted graph showing all the dependency paths between <code class="docutils literal notranslate"><span class="pre">src</span></code> and <code class="docutils literal notranslate"><span class="pre">foo</span></code>.</p>
<p>If you would just like to see the whole dependency graph of your <a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a>, you can use <a class="reference internal" href="#torch.package.PackageExporter.dependency_graph_string" title="torch.package.PackageExporter.dependency_graph_string"><code class="xref py py-meth docutils literal notranslate"><span class="pre">PackageExporter.dependency_graph_string()</span></code></a>.</p>
</div>
<div class="section" id="include-arbitrary-resources-with-my-package-and-access-them-later">
<h3><a class="toc-backref" href="#id6">Include arbitrary resources with my package and access them later?</a><a class="headerlink" href="#include-arbitrary-resources-with-my-package-and-access-them-later" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a> exposes three methods, <code class="docutils literal notranslate"><span class="pre">save_pickle</span></code>, <code class="docutils literal notranslate"><span class="pre">save_text</span></code> and <code class="docutils literal notranslate"><span class="pre">save_binary</span></code> that allow you to save
Python objects, text, and binary data to a package.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">PackageExporter</span><span class="p">(</span><span class="s2">"package.pt"</span><span class="p">)</span> <span class="k">as</span> <span class="n">exporter</span><span class="p">:</span>
<span class="c1"># Pickles the object and saves to `my_resources/tens.pkl` in the archive.</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s2">"my_resources"</span><span class="p">,</span> <span class="s2">"tensor.pkl"</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_text</span><span class="p">(</span><span class="s2">"config_stuff"</span><span class="p">,</span> <span class="s2">"words.txt"</span><span class="p">,</span> <span class="s2">"a sample string"</span><span class="p">)</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_binary</span><span class="p">(</span><span class="s2">"raw_data"</span><span class="p">,</span> <span class="s2">"binary"</span><span class="p">,</span> <span class="n">my_bytes</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> exposes complementary methods named <code class="docutils literal notranslate"><span class="pre">load_pickle</span></code>, <code class="docutils literal notranslate"><span class="pre">load_text</span></code> and <code class="docutils literal notranslate"><span class="pre">load_binary</span></code> that allow you to load
Python objects, text and binary data from a package.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">importer</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">PackageImporter</span><span class="p">(</span><span class="s2">"package.pt"</span><span class="p">)</span>
<span class="n">my_tensor</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s2">"my_resources"</span><span class="p">,</span> <span class="s2">"tensor.pkl"</span><span class="p">)</span>
<span class="n">text</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_text</span><span class="p">(</span><span class="s2">"config_stuff"</span><span class="p">,</span> <span class="s2">"words.txt"</span><span class="p">)</span>
<span class="n">binary</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_binary</span><span class="p">(</span><span class="s2">"raw_data"</span><span class="p">,</span> <span class="s2">"binary"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="customize-how-a-class-is-packaged">
<h3><a class="toc-backref" href="#id7">Customize how a class is packaged?</a><a class="headerlink" href="#customize-how-a-class-is-packaged" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">torch.package</span></code> allows for the customization of how classes are packaged. This behavior is accessed through defining the method
<code class="docutils literal notranslate"><span class="pre">__reduce_package__</span></code> on a class and by defining a corresponding de-packaging function. This is similar to defining <code class="docutils literal notranslate"><span class="pre">__reduce__</span></code> for
Python’s normal pickling process.</p>
<p>Steps:</p>
<ol class="arabic simple">
<li><p>Define the method <code class="docutils literal notranslate"><span class="pre">__reduce_package__(self,</span> <span class="pre">exporter:</span> <span class="pre">PackageExporter)</span></code> on the target class. This method should do the work to save the class instance inside of the package, and should return a tuple of the corresponding de-packaging function with the arguments needed to invoke the de-packaging function. This method is called by the <code class="docutils literal notranslate"><span class="pre">PackageExporter</span></code> when it encounters an instance of the target class.</p></li>
<li><p>Define a de-packaging function for the class. This de-packaging function should do the work to reconstruct and return an instance of the class. The function signature’s first parameter should be a <code class="docutils literal notranslate"><span class="pre">PackageImporter</span></code> instance, and the rest of the parameters are user defined.</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># foo.py [Example of customizing how class Foo is packaged]</span>
<span class="kn">from</span> <span class="nn">torch.package</span> <span class="kn">import</span> <span class="n">PackageExporter</span><span class="p">,</span> <span class="n">PackageImporter</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="k">class</span> <span class="nc">Foo</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">my_string</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">my_string</span> <span class="o">=</span> <span class="n">my_string</span>
<span class="bp">self</span><span class="o">.</span><span class="n">time_imported</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">time_exported</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">__reduce_package__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exporter</span><span class="p">:</span> <span class="n">PackageExporter</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Called by ``torch.package.PackageExporter``'s Pickler's ``persistent_id`` when</span>
<span class="sd"> saving an instance of this object. This method should do the work to save this</span>
<span class="sd"> object inside of the ``torch.package`` archive.</span>
<span class="sd"> Returns function w/ arguments to load the object from a</span>
<span class="sd"> ``torch.package.PackageImporter``'s Pickler's ``persistent_load`` function.</span>
<span class="sd"> """</span>
<span class="c1"># use this pattern to ensure no naming conflicts with normal dependencies,</span>
<span class="c1"># anything saved under this module name shouldn't conflict with other</span>
<span class="c1"># items in the package</span>
<span class="n">generated_module_name</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"foo-generated._</span><span class="si">{</span><span class="n">exporter</span><span class="o">.</span><span class="n">get_unique_id</span><span class="p">()</span><span class="si">}</span><span class="s2">"</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_text</span><span class="p">(</span>
<span class="n">generated_module_name</span><span class="p">,</span>
<span class="s2">"foo.txt"</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">my_string</span> <span class="o">+</span> <span class="s2">", with exporter modification!"</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">time_exported</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">clock_gettime</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># returns de-packaging function w/ arguments to invoke with</span>
<span class="k">return</span> <span class="p">(</span><span class="n">unpackage_foo</span><span class="p">,</span> <span class="p">(</span><span class="n">generated_module_name</span><span class="p">,</span> <span class="n">time_exported</span><span class="p">,))</span>
<span class="k">def</span> <span class="nf">unpackage_foo</span><span class="p">(</span>
<span class="n">importer</span><span class="p">:</span> <span class="n">PackageImporter</span><span class="p">,</span> <span class="n">generated_module_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">time_exported</span><span class="p">:</span> <span class="nb">float</span>
<span class="p">)</span> <span class="o">-></span> <span class="n">Foo</span><span class="p">:</span>
<span class="sd">"""</span>
<span class="sd"> Called by ``torch.package.PackageImporter``'s Pickler's ``persistent_load`` function</span>
<span class="sd"> when depickling a Foo object.</span>
<span class="sd"> Performs work of loading and returning a Foo instance from a ``torch.package`` archive.</span>
<span class="sd"> """</span>
<span class="n">time_imported</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">clock_gettime</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">(</span><span class="n">importer</span><span class="o">.</span><span class="n">load_text</span><span class="p">(</span><span class="n">generated_module_name</span><span class="p">,</span> <span class="s2">"foo.txt"</span><span class="p">))</span>
<span class="n">foo</span><span class="o">.</span><span class="n">time_imported</span> <span class="o">=</span> <span class="n">time_imported</span>
<span class="n">foo</span><span class="o">.</span><span class="n">time_exported</span> <span class="o">=</span> <span class="n">time_exported</span>
<span class="k">return</span> <span class="n">foo</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># example of saving instances of class Foo</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch.package</span> <span class="kn">import</span> <span class="n">PackageImporter</span><span class="p">,</span> <span class="n">PackageExporter</span>
<span class="kn">import</span> <span class="nn">foo</span>
<span class="n">foo_1</span> <span class="o">=</span> <span class="n">foo</span><span class="o">.</span><span class="n">Foo</span><span class="p">(</span><span class="s2">"foo_1 initial string"</span><span class="p">)</span>
<span class="n">foo_2</span> <span class="o">=</span> <span class="n">foo</span><span class="o">.</span><span class="n">Foo</span><span class="p">(</span><span class="s2">"foo_2 initial string"</span><span class="p">)</span>
<span class="k">with</span> <span class="n">PackageExporter</span><span class="p">(</span><span class="s1">'foo_package.pt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">pe</span><span class="p">:</span>
<span class="c1"># save as normal, no extra work necessary</span>
<span class="n">pe</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s1">'foo_collection'</span><span class="p">,</span> <span class="s1">'foo1.pkl'</span><span class="p">,</span> <span class="n">foo_1</span><span class="p">)</span>
<span class="n">pe</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s1">'foo_collection'</span><span class="p">,</span> <span class="s1">'foo2.pkl'</span><span class="p">,</span> <span class="n">foo_2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">pe</span><span class="o">.</span><span class="n">file_structure</span><span class="p">())</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="s1">'foo_package.pt'</span><span class="p">)</span>
<span class="n">imported_foo</span> <span class="o">=</span> <span class="n">pi</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s1">'foo_collection'</span><span class="p">,</span> <span class="s1">'foo1.pkl'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"foo_1 string: '</span><span class="si">{</span><span class="n">imported_foo</span><span class="o">.</span><span class="n">my_string</span><span class="si">}</span><span class="s2">'"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"foo_1 export time: </span><span class="si">{</span><span class="n">imported_foo</span><span class="o">.</span><span class="n">time_exported</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"foo_1 import time: </span><span class="si">{</span><span class="n">imported_foo</span><span class="o">.</span><span class="n">time_imported</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span># output of running above script
─── foo_package
├── foo-generated
│ ├── _0
│ │ └── foo.txt
│ └── _1
│ └── foo.txt
├── foo_collection
│ ├── foo1.pkl
│ └── foo2.pkl
└── foo.py
foo_1 string: 'foo_1 initial string, with reduction modification!'
foo_1 export time: 9857706.650140837
foo_1 import time: 9857706.652698385
</pre></div>
</div>
</div>
<div class="section" id="test-in-my-source-code-whether-or-not-it-is-executing-inside-a-package">
<h3><a class="toc-backref" href="#id8">Test in my source code whether or not it is executing inside a package?</a><a class="headerlink" href="#test-in-my-source-code-whether-or-not-it-is-executing-inside-a-package" title="Permalink to this headline">¶</a></h3>
<p>A <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> will add the attribute <code class="docutils literal notranslate"><span class="pre">__torch_package__</span></code> to every module that it initializes. Your code can check for the
presence of this attribute to determine whether it is executing in a packaged context or not.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># In foo/bar.py:</span>
<span class="k">if</span> <span class="s2">"__torch_package__"</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">():</span> <span class="c1"># true if the code is being loaded from a package</span>
<span class="k">def</span> <span class="nf">is_in_package</span><span class="p">():</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="n">UserException</span> <span class="o">=</span> <span class="ne">Exception</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">is_in_package</span><span class="p">():</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="n">UserException</span> <span class="o">=</span> <span class="n">UnpackageableException</span>
</pre></div>
</div>
<p>Now, the code will behave differently depending on whether it’s imported normally through your Python environment or imported from a
<code class="docutils literal notranslate"><span class="pre">torch.package</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">foo.bar</span> <span class="kn">import</span> <span class="n">is_in_package</span>
<span class="nb">print</span><span class="p">(</span><span class="n">is_in_package</span><span class="p">())</span> <span class="c1"># False</span>
<span class="n">loaded_module</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="n">my_pacakge</span><span class="p">)</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s2">"foo.bar"</span><span class="p">)</span>
<span class="n">loaded_module</span><span class="o">.</span><span class="n">is_in_package</span><span class="p">()</span> <span class="c1"># True</span>
</pre></div>
</div>
<p><strong>Warning</strong>: in general, it’s bad practice to have code that behaves differently depending on whether it’s packaged or not. This can lead to
hard-to-debug issues that are sensitive to how you imported your code. If your package is intended to be heavily used, consider restructuring
your code so that it behaves the same way no matter how it was loaded.</p>
</div>
<div class="section" id="patch-code-into-a-package">
<h3><a class="toc-backref" href="#id9">Patch code into a package?</a><a class="headerlink" href="#patch-code-into-a-package" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a> offers a <code class="docutils literal notranslate"><span class="pre">save_source_string()</span></code> method that allows one to save arbitrary Python source code to a module of your choosing.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">PackageExporter</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="k">as</span> <span class="n">exporter</span><span class="p">:</span>
<span class="c1"># Save the my_module.foo available in your current Python environment.</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_module</span><span class="p">(</span><span class="s2">"my_module.foo"</span><span class="p">)</span>
<span class="c1"># This saves the provided string to my_module/foo.py in the package archive.</span>
<span class="c1"># It will override the my_module.foo that was previously saved.</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_source_string</span><span class="p">(</span><span class="s2">"my_module.foo"</span><span class="p">,</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">dedent</span><span class="p">(</span>
<span class="sd">"""\</span>
<span class="sd"> def my_function():</span>
<span class="sd"> print('hello world')</span>
<span class="sd"> """</span>
<span class="p">))</span>
<span class="c1"># If you want to treat my_module.bar as a package</span>
<span class="c1"># (e.g. save to `my_module/bar/__init__.py` instead of `my_module/bar.py)</span>
<span class="c1"># pass is_package=True,</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_source_string</span><span class="p">(</span><span class="s2">"my_module.bar"</span><span class="p">,</span>
<span class="s2">"def foo(): print('hello')</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span>
<span class="n">is_package</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="n">importer</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="n">importer</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s2">"my_module.foo"</span><span class="p">)</span><span class="o">.</span><span class="n">my_function</span><span class="p">()</span> <span class="c1"># prints 'hello world'</span>
</pre></div>
</div>
</div>
<div class="section" id="access-package-contents-from-packaged-code">
<h3><a class="toc-backref" href="#id10">Access package contents from packaged code?</a><a class="headerlink" href="#access-package-contents-from-packaged-code" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> implements the
<a class="reference external" href="https://docs.python.org/3/library/importlib.html#module-importlib.resources">importlib.resources</a>
API for accessing resources from inside a package.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">PackageExporter</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="k">as</span> <span class="n">exporter</span><span class="p">:</span>
<span class="c1"># saves text to my_resource/a.txt in the archive</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_text</span><span class="p">(</span><span class="s2">"my_resource"</span><span class="p">,</span> <span class="s2">"a.txt"</span><span class="p">,</span> <span class="s2">"hello world!"</span><span class="p">)</span>
<span class="c1"># saves the tensor to my_pickle/obj.pkl</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s2">"my_pickle"</span><span class="p">,</span> <span class="s2">"obj.pkl"</span><span class="p">,</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="c1"># see below for module contents</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_module</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">)</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_module</span><span class="p">(</span><span class="s2">"bar"</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">importlib.resources</span></code> API allows access to resources from within packaged code.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># foo.py:</span>
<span class="kn">import</span> <span class="nn">importlib.resources</span>
<span class="kn">import</span> <span class="nn">my_resource</span>
<span class="c1"># returns "hello world!"</span>
<span class="k">def</span> <span class="nf">get_my_resource</span><span class="p">():</span>
<span class="k">return</span> <span class="n">importlib</span><span class="o">.</span><span class="n">resources</span><span class="o">.</span><span class="n">read_text</span><span class="p">(</span><span class="n">my_resource</span><span class="p">,</span> <span class="s2">"a.txt"</span><span class="p">)</span>
</pre></div>
</div>
<p>Using <code class="docutils literal notranslate"><span class="pre">importlib.resources</span></code> is the recommended way to access package contents from within packaged code, since it complies
with the Python standard. However, it is also possible to access the parent <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> instance itself from within
packaged code.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># bar.py:</span>
<span class="kn">import</span> <span class="nn">torch_package_importer</span> <span class="c1"># this is the PackageImporter that imported this module.</span>
<span class="c1"># Prints "hello world!", equivalient to importlib.resources.read_text</span>
<span class="k">def</span> <span class="nf">get_my_resource</span><span class="p">():</span>
<span class="k">return</span> <span class="n">torch_package_importer</span><span class="o">.</span><span class="n">load_text</span><span class="p">(</span><span class="s2">"my_resource"</span><span class="p">,</span> <span class="s2">"a.txt"</span><span class="p">)</span>
<span class="c1"># You also do things that the importlib.resources API does not support, like loading</span>
<span class="c1"># a pickled object from the package.</span>
<span class="k">def</span> <span class="nf">get_my_pickle</span><span class="p">():</span>
<span class="k">return</span> <span class="n">torch_package_importer</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s2">"my_pickle"</span><span class="p">,</span> <span class="s2">"obj.pkl"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="distinguish-between-packaged-code-and-non-packaged-code">
<h3><a class="toc-backref" href="#id11">Distinguish between packaged code and non-packaged code?</a><a class="headerlink" href="#distinguish-between-packaged-code-and-non-packaged-code" title="Permalink to this headline">¶</a></h3>
<p>To tell if an object’s code is from a <code class="docutils literal notranslate"><span class="pre">torch.package</span></code>, use the <code class="docutils literal notranslate"><span class="pre">torch.package.is_from_package()</span></code> function.
Note: if an object is from a package but its definition is from a module marked <code class="docutils literal notranslate"><span class="pre">extern</span></code> or from <code class="docutils literal notranslate"><span class="pre">stdlib</span></code>,
this check will return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">importer</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="n">mod</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span>
<span class="n">obj</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s1">'model'</span><span class="p">,</span> <span class="s1">'model.pkl'</span><span class="p">)</span>
<span class="n">txt</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_text</span><span class="p">(</span><span class="s1">'text'</span><span class="p">,</span> <span class="s1">'my_test.txt'</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">is_from_package</span><span class="p">(</span><span class="n">mod</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">is_from_package</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
<span class="k">assert</span> <span class="ow">not</span> <span class="n">is_from_package</span><span class="p">(</span><span class="n">txt</span><span class="p">)</span> <span class="c1"># str is from stdlib, so this will return False</span>
</pre></div>
</div>
</div>
<div class="section" id="re-export-an-imported-object">
<h3><a class="toc-backref" href="#id12">Re-export an imported object?</a><a class="headerlink" href="#re-export-an-imported-object" title="Permalink to this headline">¶</a></h3>
<p>To re-export an object that was previously imported by a <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a>, you must make the new <a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a>
aware of the original <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> so that it can find source code for your object’s dependencies.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">importer</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="n">obj</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s2">"model"</span><span class="p">,</span> <span class="s2">"model.pkl"</span><span class="p">)</span>
<span class="c1"># re-export obj in a new package</span>
<span class="k">with</span> <span class="n">PackageExporter</span><span class="p">(</span><span class="n">f2</span><span class="p">,</span> <span class="n">importer</span><span class="o">=</span><span class="p">(</span><span class="n">importer</span><span class="p">,</span> <span class="n">sys_importer</span><span class="p">))</span> <span class="k">as</span> <span class="n">exporter</span><span class="p">:</span>
<span class="n">exporter</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s2">"model"</span><span class="p">,</span> <span class="s2">"model.pkl"</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="package-a-torchscript-module">
<h3><a class="toc-backref" href="#id13">Package a TorchScript module?</a><a class="headerlink" href="#package-a-torchscript-module" title="Permalink to this headline">¶</a></h3>
<p>To package a TorchScript model, use the same <code class="docutils literal notranslate"><span class="pre">save_pickle</span></code> and <code class="docutils literal notranslate"><span class="pre">load_pickle</span></code> APIs as you would with any other object.
Saving TorchScript objects that are attributes or submodules is supported as well with no extra work.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># save TorchScript just like any other object</span>
<span class="k">with</span> <span class="n">PackageExporter</span><span class="p">(</span><span class="n">file_name</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="n">e</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s2">"res"</span><span class="p">,</span> <span class="s2">"script_model.pkl"</span><span class="p">,</span> <span class="n">scripted_model</span><span class="p">)</span>
<span class="n">e</span><span class="o">.</span><span class="n">save_pickle</span><span class="p">(</span><span class="s2">"res"</span><span class="p">,</span> <span class="s2">"mixed_model.pkl"</span><span class="p">,</span> <span class="n">python_model_with_scripted_submodule</span><span class="p">)</span>
<span class="c1"># load as normal</span>
<span class="n">importer</span> <span class="o">=</span> <span class="n">PackageImporter</span><span class="p">(</span><span class="n">file_name</span><span class="p">)</span>
<span class="n">loaded_script</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s2">"res"</span><span class="p">,</span> <span class="s2">"script_model.pkl"</span><span class="p">)</span>
<span class="n">loaded_mixed</span> <span class="o">=</span> <span class="n">importer</span><span class="o">.</span><span class="n">load_pickle</span><span class="p">(</span><span class="s2">"res"</span><span class="p">,</span> <span class="s2">"mixed_model.pkl"</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="explanation">
<h2><a class="toc-backref" href="#id14">Explanation</a><a class="headerlink" href="#explanation" title="Permalink to this headline">¶</a></h2>
<div class="section" id="torch-package-format-overview">
<h3><a class="toc-backref" href="#id15"><code class="docutils literal notranslate"><span class="pre">torch.package</span></code> Format Overview</a><a class="headerlink" href="#torch-package-format-overview" title="Permalink to this headline">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> file is a ZIP archive which conventionally uses the <code class="docutils literal notranslate"><span class="pre">.pt</span></code> extension. Inside the ZIP archive, there are two kinds of files:</p>
<ul class="simple">
<li><p>Framework files, which are placed in the <code class="docutils literal notranslate"><span class="pre">.data/</span></code>.</p></li>
<li><p>User files, which is everything else.</p></li>
</ul>
<p>As an example, this is what a fully packaged ResNet model from <code class="docutils literal notranslate"><span class="pre">torchvision</span></code> looks like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>resnet
├── .data # All framework-specific data is stored here.
│ │ # It's named to avoid conflicts with user-serialized code.
│ ├── 94286146172688.storage # tensor data
│ ├── 94286146172784.storage
│ ├── extern_modules # text file with names of extern modules (e.g. 'torch')
│ ├── version # version metadata
│ ├── ...
├── model # the pickled model
│ └── model.pkl
└── torchvision # all code dependencies are captured as source files
└── models
├── resnet.py
└── utils.py
</pre></div>
</div>
<div class="section" id="framework-files">
<h4>Framework files<a class="headerlink" href="#framework-files" title="Permalink to this headline">¶</a></h4>
<p>The <code class="docutils literal notranslate"><span class="pre">.data/</span></code> directory is owned by torch.package, and its contents are considered to be a private implementation detail.
The <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> format makes no guarantees about the contents of <code class="docutils literal notranslate"><span class="pre">.data/</span></code>, but any changes made will be backward compatible
(that is, newer version of PyTorch will always be able to load older <code class="docutils literal notranslate"><span class="pre">torch.packages</span></code>).</p>
<p>Currently, the <code class="docutils literal notranslate"><span class="pre">.data/</span></code> directory contains the following items:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">version</span></code>: a version number for the serialized format, so that the <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> import infrastructures knows how to load this package.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">extern_modules</span></code>: a list of modules that are considered <code class="docutils literal notranslate"><span class="pre">extern:class:`PackageImporter`.</span> <span class="pre">``extern</span></code> modules will be imported using the loading environment’s system importer.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">*.storage</span></code>: serialized tensor data.</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>.data
├── 94286146172688.storage
├── 94286146172784.storage
├── extern_modules
├── version
├── ...
</pre></div>
</div>
</div>
<div class="section" id="user-files">
<h4>User files<a class="headerlink" href="#user-files" title="Permalink to this headline">¶</a></h4>
<p>All other files in the archive were put there by a user. The layout is identical to a Python
<a class="reference external" href="https://docs.python.org/3/reference/import.html#regular-packages">regular package</a>. For a deeper dive in how Python packaging works,
please consult <a class="reference external" href="https://www.python.org/doc/essays/packages/">this essay</a> (it’s slightly out of date, so double-check implementation details
with the <a class="reference external" href="https://docs.python.org/3/library/importlib.html">Python reference documentation</a>).</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><package root>
├── model # the pickled model
│ └── model.pkl
├── another_package
│ ├── __init__.py
│ ├── foo.txt # a resource file , see importlib.resources
│ └── ...
└── torchvision
└── models
├── resnet.py # torchvision.models.resnet
└── utils.py # torchvision.models.utils
</pre></div>
</div>
</div>
</div>
<div class="section" id="how-torch-package-finds-your-code-s-dependencies">
<h3><a class="toc-backref" href="#id16">How <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> finds your code’s dependencies</a><a class="headerlink" href="#how-torch-package-finds-your-code-s-dependencies" title="Permalink to this headline">¶</a></h3>
<div class="section" id="analyzing-an-object-s-dependencies">
<h4>Analyzing an object’s dependencies<a class="headerlink" href="#analyzing-an-object-s-dependencies" title="Permalink to this headline">¶</a></h4>
<p>When you issue a <code class="docutils literal notranslate"><span class="pre">save_pickle(obj,</span> <span class="pre">...)</span></code> call, <a class="reference internal" href="#torch.package.PackageExporter" title="torch.package.PackageExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageExporter</span></code></a> will pickle the object normally. Then, it uses the
<code class="docutils literal notranslate"><span class="pre">pickletools</span></code> standard library module to parse the pickle bytecode.</p>
<p>In a pickle, an object is saved along with a <code class="docutils literal notranslate"><span class="pre">GLOBAL</span></code> opcode that describes where to find the implementation of the object’s type, like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">GLOBAL</span> <span class="s1">'torchvision.models.resnet Resnet`</span>
</pre></div>
</div>
<p>The dependency resolver will gather up all <code class="docutils literal notranslate"><span class="pre">GLOBAL</span></code> ops and mark them as dependencies of your pickled object.
For more information about pickling and the pickle format, please consult <a class="reference external" href="https://docs.python.org/3/library/pickle.html">the Python docs</a>.</p>
</div>
<div class="section" id="analyzing-a-module-s-dependencies">
<h4>Analyzing a module’s dependencies<a class="headerlink" href="#analyzing-a-module-s-dependencies" title="Permalink to this headline">¶</a></h4>
<p>When a Python module is identified as a dependency, <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> walks the module’s python AST representation and looks for import statements with
full support for the standard forms: <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">x</span> <span class="pre">import</span> <span class="pre">y</span></code>, <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">z</span></code>, <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">w</span> <span class="pre">import</span> <span class="pre">v</span> <span class="pre">as</span> <span class="pre">u</span></code>, etc. When one of these import statements are
encountered, <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> registers the imported modules as dependencies that are then themselves parsed in the same AST walking way.</p>
<p><strong>Note</strong>: AST parsing has limited support for the <code class="docutils literal notranslate"><span class="pre">__import__(...)</span></code> syntax and does not support <code class="docutils literal notranslate"><span class="pre">importlib.import_module</span></code> calls. In general, you should
not expect dynamic imports to be detected by <code class="docutils literal notranslate"><span class="pre">torch.package</span></code>.</p>
</div>
</div>
<div class="section" id="dependency-management">
<h3><a class="toc-backref" href="#id17">Dependency Management</a><a class="headerlink" href="#dependency-management" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">torch.package</span></code> automatically finds the Python modules that your code and objects depend on. This process is called dependency resolution.
For each module that the dependency resolver finds, you must specify an <em>action</em> to take.</p>
<p>The allowed actions are:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">intern</span></code>: put this module into the package.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">extern</span></code>: declare this module as an external dependency of the package.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">mock</span></code>: stub out this module.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">deny</span></code>: depending on this module will raise an error during package export.</p></li>
</ul>
<p>Finally, there is one more important action that is not technically part of <code class="docutils literal notranslate"><span class="pre">torch.package</span></code>:</p>
<ul class="simple">
<li><p>Refactoring: remove or change the dependencies in your code.</p></li>
</ul>
<p>Note that actions are only defined on entire Python modules. There is no way to package “just” a function or class from module and leave the rest out.
This is by design. Python does not offer clean boundaries between objects defined in a module. The only defined unit of dependency organization is a
module, so that’s what <code class="docutils literal notranslate"><span class="pre">torch.package</span></code> uses.</p>
<p>Actions are applied to modules using patterns. Patterns can either be module names (<code class="docutils literal notranslate"><span class="pre">"foo.bar"</span></code>) or globs (like <code class="docutils literal notranslate"><span class="pre">"foo.**"</span></code>). You associate a pattern
with an action using methods on <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a>, e.g.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">my_exporter</span><span class="o">.</span><span class="n">intern</span><span class="p">(</span><span class="s2">"torchvision.**"</span><span class="p">)</span>
<span class="n">my_exporter</span><span class="o">.</span><span class="n">extern</span><span class="p">(</span><span class="s2">"numpy"</span><span class="p">)</span>
</pre></div>
</div>
<p>If a module matches a pattern, the corresponding action is applied to it. For a given module, patterns will be checked in the order that they were defined,
and the first action will be taken.</p>
<div class="section" id="intern">
<h4><code class="docutils literal notranslate"><span class="pre">intern</span></code><a class="headerlink" href="#intern" title="Permalink to this headline">¶</a></h4>
<p>If a module is <code class="docutils literal notranslate"><span class="pre">intern</span></code>-ed, it will be placed into the package.</p>
<p>This action is your model code, or any related code you want to package. For example, if you are trying to package a ResNet from <code class="docutils literal notranslate"><span class="pre">torchvision</span></code>,
you will need to <code class="docutils literal notranslate"><span class="pre">intern</span></code> the module torchvision.models.resnet.</p>
<p>On package import, when your packaged code tries to import an <code class="docutils literal notranslate"><span class="pre">intern</span></code>-ed module, PackageImporter will look inside your package for that module.
If it can’t find that module, an error will be raised. This ensures that each <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> is isolated from the loading environment—even
if you have <code class="docutils literal notranslate"><span class="pre">my_interned_module</span></code> available in both your package and the loading environment, <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> will only use the version in your
package.</p>
<p><strong>Note</strong>: Only Python source modules can be <code class="docutils literal notranslate"><span class="pre">intern</span></code>-ed. Other kinds of modules, like C extension modules and bytecode modules, will raise an error if
you attempt to <code class="docutils literal notranslate"><span class="pre">intern</span></code> them. These kinds of modules need to be <code class="docutils literal notranslate"><span class="pre">mock</span></code>-ed or <code class="docutils literal notranslate"><span class="pre">extern</span></code>-ed.</p>
</div>
<div class="section" id="extern">
<h4><code class="docutils literal notranslate"><span class="pre">extern</span></code><a class="headerlink" href="#extern" title="Permalink to this headline">¶</a></h4>
<p>If a module is <code class="docutils literal notranslate"><span class="pre">extern</span></code>-ed, it will not be packaged. Instead, it will be added to a list of external dependencies for this package. You can find this
list on <code class="docutils literal notranslate"><span class="pre">package_exporter.extern_modules</span></code>.</p>
<p>On package import, when time packaged code tries to import an <code class="docutils literal notranslate"><span class="pre">extern</span></code>-ed module, <a class="reference internal" href="#torch.package.PackageImporter" title="torch.package.PackageImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">PackageImporter</span></code></a> will use the default Python importer to find
that module, as if you did <code class="docutils literal notranslate"><span class="pre">importlib.import_module("my_externed_module")</span></code>. If it can’t find that module, an error will be raised.</p>
<p>In this way, you can depend on third-party libraries like <code class="docutils literal notranslate"><span class="pre">numpy</span></code> and <code class="docutils literal notranslate"><span class="pre">scipy</span></code> from within your package without having to package them too.</p>
<p><strong>Warning</strong>: If any external library changes in a backwards-incompatible way, your package may fail to load. If you need long-term reproducibility
for your package, try to limit your use of <code class="docutils literal notranslate"><span class="pre">extern</span></code>.</p>
</div>
<div class="section" id="mock">
<h4><code class="docutils literal notranslate"><span class="pre">mock</span></code><a class="headerlink" href="#mock" title="Permalink to this headline">¶</a></h4>
<p>If a module is <code class="docutils literal notranslate"><span class="pre">mock</span></code>-ed, it will not be packaged. Instead a stub module will be packaged in its place. The stub module will allow you to retrieve
objects from it (so that <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">my_mocked_module</span> <span class="pre">import</span> <span class="pre">foo</span></code> will not error), but any use of that object will raise a <code class="docutils literal notranslate"><span class="pre">NotImplementedError</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">mock</span></code> should be used for code that you “know” will not be needed in the loaded package, but you still want to available for use in non-packaged contents.
For example, initialization/configuration code, or code only used for debugging/training.</p>
<p><strong>Warning</strong>: In general, <code class="docutils literal notranslate"><span class="pre">mock</span></code> should be used as a last resort. It introduces behavioral differences between packaged code and non-packaged code,
which may lead to later confusion. Prefer instead to refactor your code to remove unwanted dependencies.</p>
</div>
<div class="section" id="refactoring">
<h4>Refactoring<a class="headerlink" href="#refactoring" title="Permalink to this headline">¶</a></h4>
<p>The best way to manage dependencies is to not have dependencies at all! Often, code can be refactored to remove unnecessary dependencies. Here are some
guidelines for writing code with clean dependencies (which are also generally good practices!):</p>
<p><strong>Include only what you use</strong>. Do not leave unused imports in our code. The dependency resolver is not smart enough to tell that they are indeed unused,
and will try to process them.</p>
<p><strong>Qualify your imports</strong>. For example, instead of writing import foo and later using <code class="docutils literal notranslate"><span class="pre">foo.bar.baz</span></code>, prefer to write <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">foo.bar</span> <span class="pre">import</span> <span class="pre">baz</span></code>. This more
precisely specifies your real dependency (<code class="docutils literal notranslate"><span class="pre">foo.bar</span></code>) and lets the dependency resolver know you don’t need all of <code class="docutils literal notranslate"><span class="pre">foo</span></code>.</p>