Skip to content

Commit 1cc62c2

Browse files
angelayipytorchmergebot
authored andcommitted
[export] Update docs (pytorch#157750)
Preview: https://docs-preview.pytorch.org/pytorch/pytorch/157750/export.html Changes: * Rename draft_export.md -> export.draft_export.md for consistency. * Removed non-strict section in export, instead pointed to programming model doc. * Extended "Expressing Dynamism" section to include Dim hints, ShapeCollection, and AdditionalInputs. * Removed Specialization section in favor of programming model doc * Added pt2 archive doc * Cleaned up sidebar Pull Request resolved: pytorch#157750 Approved by: https://github.com/pianpwk
1 parent f58a680 commit 1cc62c2

File tree

10 files changed

+410
-531
lines changed

10 files changed

+410
-531
lines changed

docs/source/export.md

Lines changed: 243 additions & 491 deletions
Large diffs are not rendered by default.

docs/source/draft_export.md renamed to docs/source/export/draft_export.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(draft-export)=
1+
(export.draft_export)=
22

33
# Draft Export
44

@@ -126,7 +126,7 @@ Running the `tlparse` command in the terminal will generate a
126126
[tlparse](https://github.com/pytorch/tlparse)
127127
HTML report. Here is an example of the `tlparse` report:
128128

129-
```{image} _static/img/export/draft_export_report.png
129+
```{image} ../_static/img/export/draft_export_report.png
130130
```
131131

132132
Clicking into the Data Dependent Error, we will see the following page which
@@ -136,7 +136,7 @@ contains information to help debug this error. Specifically, it contains:
136136
- A list of local variables and their shapes
137137
- Information for how this guard was created
138138

139-
```{image} _static/img/export/draft_export_report_dde.png
139+
```{image} ../_static/img/export/draft_export_report_dde.png
140140
```
141141

142142
## The returned Exported Program
@@ -251,12 +251,3 @@ and produce a runnable artifact. This optimized version can then be used for
251251
deployment. In parallel, we can utilize the report generated by draft-export to
252252
identify and fix `torch.export` errors that were encountered so that the
253253
original model can be directly traceable with `torch.export`.
254-
255-
```{toctree}
256-
:caption: Additional Links
257-
:maxdepth: 1
258-
259-
torch.compiler_fake_tensor
260-
torch.compiler_dynamic_shapes
261-
torch.compiler_aot_inductor
262-
```
File renamed without changes.

docs/source/export.programming_model.md renamed to docs/source/export/programming_model.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(export-programming-model)=
1+
(export.programming_model)=
22

33
# torch.export Programming Model
44

@@ -15,7 +15,9 @@ on different inputs as long as they satisfy the same conditions.
1515

1616
The basic output of {func}`torch.export.export` is a single graph of PyTorch
1717
operations, with associated metadata. The exact format of this output is
18-
covered in the {ref}`export.ir_spec`.
18+
covered in the {ref}`export IR spec <export.ir_spec>`.
19+
20+
(non-strict-export)=
1921

2022
### Strict vs. Non-Strict Tracing
2123

@@ -120,6 +122,9 @@ Whether a value is static or dynamic depends on its type:
120122

121123
- There are dynamic variants for some primitive types (`SymInt`,
122124
`SymFloat`, `SymBool`). Typically users do not have to deal with them.
125+
- Users can specify integer inputs as dynamic by specifying
126+
a [dynamic shape](https://pytorch.org/docs/main/export.html#expressing-dynamism)
127+
for it.
123128

124129
- For Python *standard containers* (`list`, `tuple`, `dict`, `namedtuple`):
125130

@@ -150,7 +155,7 @@ By default, the types of inputs you can use for your program are:
150155
- Python primitives (`int`, `float`, `bool`, `str`, `None`)
151156
- Python standard containers (`list`, `tuple`, `dict`, `namedtuple`)
152157

153-
### Custom Input Types
158+
### Custom Input Types (PyTree)
154159

155160
In addition, you can also define your own (custom) class and use it as an
156161
input type, but you will need to register such a class as a PyTree.
@@ -164,7 +169,8 @@ class Input:
164169
f: torch.Tensor
165170
p: torch.Tensor
166171

167-
torch.export.register_dataclass(Input)
172+
import torch.utils._pytree as pytree
173+
pytree.register_dataclass(Input)
168174

169175
class M(torch.nn.Module):
170176
def forward(self, x: Input):

docs/source/export/pt2_archive.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
(export.pt2_archive)=
2+
3+
# PT2 Archive Spec
4+
5+
The following specification defines the archive format which can be produced
6+
through the following methods:
7+
8+
* {ref}`torch.export <torch.export>` through calling {func}`torch.export.save`
9+
* {ref}`AOTInductor <torch.compiler_aot_inductor>` through calling {func}`torch._inductor.aoti_compile_and_package`
10+
11+
The archive is a zipfile, and can be manipulated using standard zipfile APIs.
12+
13+
The following is a sample archive. We will walk through the archive folder by folder.
14+
15+
```
16+
.
17+
├── archive_format
18+
├── byteorder
19+
├── .data
20+
│ ├── serialization_id
21+
│ └── version
22+
├── data
23+
│ ├── aotinductor
24+
│ │ └── model1
25+
│ │ ├── aotinductor_pickle_data.json
26+
│ │ ├── cf5ez6ifexr7i2hezzz4s7xfusj4wtisvu2gddeamh37bw6bghjw.cpp
27+
│ │ ├── cf5ez6ifexr7i2hezzz4s7xfusj4wtisvu2gddeamh37bw6bghjw.so
28+
│ │ ├── cg7domx3woam3nnliwud7yvtcencqctxkvvcafuriladwxw4nfiv.cubin
29+
│ │ └── cubaaxppb6xmuqdm4bej55h2pftbce3bjyyvljxbtdfuolmv45ex.cubin
30+
│ ├── weights
31+
│ │ ├── model1_model_param_config.json
32+
│ │ ├── weight_0
33+
│ │ ├── weight_1
34+
│ │ ├── weight_2
35+
│ └── constants
36+
│ │ ├── model1_model_constants_config.json
37+
│ │ ├── tensor_0
38+
│ │ ├── tensor_1
39+
│ │ ├── custom_obj_0
40+
│ │ ├── custom_obj_1
41+
│ └── sample_inputs
42+
│ ├── model1.pt
43+
│ └── model2.pt
44+
├── extra
45+
│ └── ....json
46+
└── models
47+
├── model1.json
48+
└── model2.json
49+
```
50+
51+
## Contents
52+
53+
### Archive Headers
54+
55+
* `archive_format` declares the format used by this archive. Currently, it can only be “pt2”.
56+
* `byteorder`. One of “little” or “big”, used by zip file reader
57+
* `/.data/version` contains the archive version. (Notice that this is neither export serialization’s schema version, nor Aten Opset Version).
58+
* `/.data/serialization_id` is a hash generated for the current archive, used for verification.
59+
60+
61+
### AOTInductor Compiled Artifact
62+
63+
Path: `/data/aotinductor/<model_name>-<backend>/`
64+
65+
AOTInductor compilation artifacts are saved for each model-backend pair. For
66+
example, compilation artifacts for the `model1` model on A100 and H100 will be
67+
saved in `model1-a100` and `model1-h100` folders separately.
68+
69+
The folder typically contains
70+
* `<uuid>.so`: Dynamic library compiled from <uuid>.cpp.
71+
* `<uuid>.cpp`: AOTInductor generated cpp wrapper file.
72+
* `*.cubin`: Triton kernels compiled from triton codegen kernels
73+
* (optional) `<uuid>.json`: External fallback nodes for custom ops to be executed by `ProxyExecutor`, serialized according to `ExternKernelNode` struct. If the model doesn’t use custom ops/ProxyExecutor, this file would be omitted.
74+
* `<uuid>_metadata.json`: Metadata which was passed in from the `aot_inductor.metadata` inductor config
75+
76+
### Weights
77+
78+
Path: `/data/weights/*`
79+
80+
Model parameters and buffers are saved in the `/data/weights/` folder. Each
81+
tensor is saved as a separated file. The file only contains the raw data blob,
82+
tensor metadata are saved separately in the
83+
`<model_name>_model_param_config.json`.
84+
85+
### Constants
86+
87+
Path: `/data/constants/*`
88+
89+
TensorConstants, non-persistent buffers and TorchBind objects are saved in the
90+
`/data/constants/` folder. Metadata is saved separately in the
91+
`<model_name>_model_constants_config.json`
92+
93+
### Sample Inputs
94+
95+
Path: `/data/sample_inputs/<model_name>.pt`
96+
97+
The `sample_input` used by `torch.export` could be included in the archive for
98+
downstream use. Typically, it’s a flattened list of Tensors, combining both args
99+
and kwargs of the forward() function.
100+
101+
The .pt file is produced by `torch.save(sample_input)`, and can be loaded by
102+
`torch.load()` in python and `torch::pickle_load()` in c++.
103+
104+
When the model has multiple copies of sample input, it would be packaged as
105+
`<model_name>_<index>.pt`.
106+
107+
### Models Definitions
108+
109+
Path: `/models/<model_name>.json`
110+
111+
Model definition is the serialized json of the ExportedProgram from
112+
`torch.export.save`, and other model-level metadata.
113+
114+
## Multiple Models
115+
116+
This archive spec supports multiple model definitions coexisting in the same
117+
file, with `<model_name>` serving as a unique identifier for the models, and
118+
will be used as reference in other folders of the archive.
119+
120+
Lower level APIs like {func}`torch.export.pt2_archive._package.package_pt2` and
121+
{func}`torch.export.pt2_archive._package.load_pt2` allow you to have
122+
finer-grained control over the packaging and loading process.

docs/source/torch.compiler_aot_inductor.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
(torch.compiler_aot_inductor)=
2+
13
# AOTInductor: Ahead-Of-Time Compilation for Torch.Export-ed Models
24

35
```{warning}
@@ -25,7 +27,7 @@ relies on.
2527

2628
We will then use {func}`torch._inductor.aoti_compile_and_package` to compile the
2729
exported program using TorchInductor, and save the compiled artifacts into one
28-
package.
30+
package. The package is in the format of a {ref}`PT2 Archive Spec <export.pt2_archive>`.
2931

3032
```{note}
3133
If you have a CUDA-enabled device on your machine and you installed PyTorch with CUDA support,

docs/source/torch.compiler_ir.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
(torch.compiler_ir)=
2+
13
# IRs
24

35
PyTorch 2.0 offers two set of IRs for backends to interface with: Core Aten IR and Prims IR.

torch/export/dynamic_shapes.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,19 @@ def __call__(self, min=None, max=None) -> "_DimHint":
8585

8686
class Dim:
8787
"""
88-
The `Dim` class allows users to specify dynamism in their exported programs. By marking a dimension with a `Dim`,
89-
the compiler associates the dimension with a symbolic integer containing a dynamic range.
88+
The ``Dim`` class allows users to specify dynamism in their exported
89+
programs. By marking a dimension with a ``Dim``, the compiler associates the
90+
dimension with a symbolic integer containing a dynamic range.
9091
91-
The API can be used in 2 ways: Dim hints (i.e. automatic dynamic shapes: `Dim.AUTO`, `Dim.DYNAMIC`, `Dim.STATIC`),
92-
or named Dims (i.e. `Dim("name", min=1, max=2)`).
92+
The API can be used in 2 ways: Dim hints (i.e. automatic dynamic shapes:
93+
``Dim.AUTO``, ``Dim.DYNAMIC``, ``Dim.STATIC``), or named Dims (i.e.
94+
``Dim("name", min=1, max=2)``).
9395
94-
Dim hints provide the lowest barrier to exportability, with the user only needing to specify if a dimension
95-
if dynamic, static, or left for the compiler to decide (`Dim.AUTO`). The export process will automatically
96-
infer the remaining constraints on min/max ranges and relationships between dimensions.
96+
Dim hints provide the lowest barrier to exportability, with the user only
97+
needing to specify if a dimension if dynamic, static, or left for the
98+
compiler to decide (``Dim.AUTO``). The export process will automatically
99+
infer the remaining constraints on min/max ranges and relationships between
100+
dimensions.
97101
98102
Example::
99103
@@ -112,19 +116,19 @@ def forward(self, x, y):
112116
}
113117
ep = torch.export(Foo(), (x, y), dynamic_shapes=dynamic_shapes)
114118
115-
Here, export would raise an exception if we replaced all uses of `Dim.AUTO` with `Dim.DYNAMIC`,
116-
as x.shape[0] is constrained to be static by the model.
119+
Here, export would raise an exception if we replaced all uses of ``Dim.AUTO`` with ``Dim.DYNAMIC``,
120+
as ``x.shape[0]`` is constrained to be static by the model.
117121
118122
More complex relations between dimensions may also be codegened as runtime assertion nodes by the compiler,
119-
e.g. (x.shape[0] + y.shape[1]) % 4 == 0, to be raised if runtime inputs do not satisfy such constraints.
123+
e.g. ``(x.shape[0] + y.shape[1]) % 4 == 0``, to be raised if runtime inputs do not satisfy such constraints.
120124
121-
You may also specify min-max bounds for Dim hints, e.g. `Dim.AUTO(min=16, max=32)`, `Dim.DYNAMIC(max=64)`,
125+
You may also specify min-max bounds for Dim hints, e.g. ``Dim.AUTO(min=16, max=32)``, ``Dim.DYNAMIC(max=64)``,
122126
with the compiler inferring the remaining constraints within the ranges. An exception will be raised if
123127
the valid range is entirely outside the user-specified range.
124128
125129
Named Dims provide a stricter way of specifying dynamism, where exceptions are raised if the compiler
126130
infers constraints that do not match the user specification. For example, exporting the previous
127-
model, the user would need the following `dynamic_shapes` argument::
131+
model, the user would need the following ``dynamic_shapes`` argument::
128132
129133
s0 = Dim("s0")
130134
s1 = Dim("s1", min=16)
@@ -134,8 +138,9 @@ def forward(self, x, y):
134138
}
135139
ep = torch.export(Foo(), (x, y), dynamic_shapes=dynamic_shapes)
136140
137-
Named Dims also allow specification of relationships between dimensions, up to univariate linear relations.
138-
For example, the following indicates one dimension is a multiple of another plus 4::
141+
Named Dims also allow specification of relationships between dimensions, up
142+
to univariate linear relations. For example, the following indicates one
143+
dimension is a multiple of another plus 4::
139144
140145
s0 = Dim("s0")
141146
s1 = 3 * s0 + 4

torch/export/pt2_archive/_package.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import torch
1313
import torch.utils._pytree as pytree
1414
from torch._export.serde.serialize import deserialize, serialize, SerializedArtifact
15+
from torch.export import ExportedProgram
1516
from torch.export._tree_utils import reorder_kwargs
16-
from torch.export.exported_program import ExportedProgram
1717
from torch.export.pt2_archive._package_weights import (
1818
get_complete,
1919
group_weights,
@@ -350,22 +350,21 @@ def package_pt2(
350350
opset_version: Optional[dict[str, int]] = None,
351351
pickle_protocol: int = DEFAULT_PICKLE_PROTOCOL,
352352
) -> FileLike:
353-
"""
354-
Saves the artifacts to a PT2Archive format
355-
(https://docs.google.com/document/d/1RQ4cmywilnFUT1VE-4oTGxwXdc8vowCSZsrRgo3wFA8/edit?tab=t.0#heading=h.v2y2jgnwc56a).
356-
The artifact can then be loaded using ``load_pt2``.
353+
r"""
354+
Saves the artifacts to a PT2Archive format. The artifact can then be loaded
355+
using ``load_pt2``.
357356
358357
Args:
359-
f (str | os.PathLike[str] | IO[bytes]) A file-like object (has to
358+
f (str | os.PathLike[str] | IO[bytes]): A file-like object (has to
360359
implement write and flush) or a string containing a file name.
361360
362361
exported_programs (Union[ExportedProgram, dict[str, ExportedProgram]]):
363362
The exported program to save, or a dictionary mapping model name to an
364363
exported program to save. The exported program will be saved under
365-
models/*.json. If only one ExportedProgram is specified, this will
364+
models/\*.json. If only one ExportedProgram is specified, this will
366365
automatically be named "model".
367366
368-
aoti_files (Union[list[str], dict[str, list[str]]): A list of files
367+
aoti_files (Union[list[str], dict[str, list[str]]]): A list of files
369368
generated by AOTInductor via
370369
``torch._inductor.aot_compile(..., {"aot_inductor.package": True})``,
371370
or a dictionary mapping model name to its AOTInductor generated files.

torch/export/unflatten.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import torch.fx._pytree as fx_pytree
1616
import torch.utils._pytree as pytree
1717
from torch._library.fake_class_registry import FakeScriptObject
18+
from torch.export import ExportedProgram
1819
from torch.export._tree_utils import reorder_kwargs
1920
from torch.export.exported_program import (
2021
ConstantArgument,
21-
ExportedProgram,
2222
ExportGraphSignature,
2323
InputKind,
2424
ModuleCallSignature,

0 commit comments

Comments
 (0)