Commit d2bfd97
[export] Refactor pt2 save/load (pytorch#152495)
Refactor the pt2 archive saving to consolidate the format of torch.export.save and torch._inductor.package.package_aoti.
This PR adds the following functions, which torch.export.save and AOTI packaging calls into:
```python
package_pt2(
f: FileLike,
*,
exported_programs: Optional[Union[ExportedProgram, dict[str, ExportedProgram]]] = None,
aoti_files: Optional[Union[list[str], dict[str, list[str]]]] = None,
extra_files: Optional[dict[str, Any]] = None,
) -> FileLike
@DataClass
class PT2ArchiveContents:
exported_programs: dict[str, ExportedProgram]
aoti_runners: dict[str, AOTICompiledModel]
extra_files: dict[str, Any]
load_pt2(f: FileLike) -> PT2ArchiveContents
```
Power users directly call into these APIs if they want to bundle multiple exported programs, aoti files, or extra metadata.
This is how the pt2 archive looks like ([spec](https://docs.google.com/document/d/1RQ4cmywilnFUT1VE-4oTGxwXdc8vowCSZsrRgo3wFA8/edit?tab=t.0)):
```
├── archive_format
├── version
├── .data
├── data
│ ├── aotinductor
│ │ └── model1
│ │ ├── model1.cpp
│ │ ├── model1.so # currently AOTI automatically moves weights in here, TODO to move it out
│ │ ├── cg7domx3woam3nnliwud7yvtcencqctxkvvcafuriladwxw4nfiv.cubin
│ │ └── cubaaxppb6xmuqdm4bej55h2pftbce3bjyyvljxbtdfuolmv45ex.cubin
│ ├── weights
│ │ ├── model1.pt # TODO to dedup weights between model1/model2
│ │ └── model2.pt
│ └── constants
│ │ ├── model1.pt # TODO to dedup weights between model1/model2
│ │ └── model2.pt
│ └── sample_inputs
│ ├── model1.pt # TODO to dedup weights between model1/model2
│ └── model2.pt
├── extra
│ └── user_metadata.txt
└── models
├── model1.json
└── model2.json
```
Future todos:
- unbundle the weights -- instead of .pt, we can use bin files, which will also allow us to dedup weights if we store multiple models
- update aoti_compile_and_package to also save the exported program
- integrate TNR with this packaging flow
Pull Request resolved: pytorch#152495
Approved by: https://github.com/yushangdi1 parent 75b24c2 commit d2bfd97
File tree
5 files changed
+472
-167
lines changed- test/export
- torch
- _C
- _inductor/package
- export
- pt2_archive
5 files changed
+472
-167
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| 41 | + | |
| 42 | + | |
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
| |||
1491 | 1494 | | |
1492 | 1495 | | |
1493 | 1496 | | |
| 1497 | + | |
1494 | 1498 | | |
1495 | 1499 | | |
1496 | 1500 | | |
| |||
1501 | 1505 | | |
1502 | 1506 | | |
1503 | 1507 | | |
1504 | | - | |
1505 | | - | |
| 1508 | + | |
| 1509 | + | |
1506 | 1510 | | |
1507 | | - | |
| 1511 | + | |
1508 | 1512 | | |
1509 | 1513 | | |
1510 | 1514 | | |
| |||
1518 | 1522 | | |
1519 | 1523 | | |
1520 | 1524 | | |
1521 | | - | |
| 1525 | + | |
1522 | 1526 | | |
1523 | 1527 | | |
1524 | 1528 | | |
| |||
1545 | 1549 | | |
1546 | 1550 | | |
1547 | 1551 | | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
1548 | 1555 | | |
1549 | 1556 | | |
1550 | 1557 | | |
| |||
1555 | 1562 | | |
1556 | 1563 | | |
1557 | 1564 | | |
1558 | | - | |
| 1565 | + | |
1559 | 1566 | | |
1560 | | - | |
1561 | | - | |
| 1567 | + | |
| 1568 | + | |
1562 | 1569 | | |
| 1570 | + | |
1563 | 1571 | | |
1564 | 1572 | | |
1565 | 1573 | | |
1566 | | - | |
| 1574 | + | |
1567 | 1575 | | |
1568 | 1576 | | |
1569 | | - | |
| 1577 | + | |
1570 | 1578 | | |
1571 | 1579 | | |
1572 | 1580 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1561 | 1561 | | |
1562 | 1562 | | |
1563 | 1563 | | |
| 1564 | + | |
1564 | 1565 | | |
1565 | 1566 | | |
1566 | 1567 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
11 | 9 | | |
12 | 10 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 11 | + | |
20 | 12 | | |
21 | 13 | | |
22 | 14 | | |
| |||
95 | 87 | | |
96 | 88 | | |
97 | 89 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | 90 | | |
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 | | - | |
| 91 | + | |
214 | 92 | | |
215 | 93 | | |
216 | 94 | | |
| |||
220 | 98 | | |
221 | 99 | | |
222 | 100 | | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
228 | 113 | | |
229 | 114 | | |
230 | 115 | | |
231 | 116 | | |
232 | 117 | | |
233 | | - | |
234 | 118 | | |
| 119 | + | |
235 | 120 | | |
236 | 121 | | |
237 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
407 | 393 | | |
408 | 394 | | |
409 | 395 | | |
| |||
460 | 446 | | |
461 | 447 | | |
462 | 448 | | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
463 | 469 | | |
464 | 470 | | |
465 | 471 | | |
466 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
467 | 475 | | |
468 | 476 | | |
469 | 477 | | |
| |||
0 commit comments