Skip to content

fix(executorch): move compile_and_save's engines into the program instead of copying them - #5045

Draft
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:compile-and-save-consume
Draft

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:compile-and-save-consume

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Depends on pytorch/executorch#25099; lands after it with the ExecuTorch pin moved, so CI runs the move tests. With an ExecuTorch without consume, such as 1.6.0, compile_and_save copies as before. Other saves are unchanged.

What is wrong today

compile_and_save writes each engine to a temporary file as soon as it is built. The ExecuTorch save copies it into the .pte (or .ptd), and the file is deleted only at the end. So every engine is on disk twice, and a large export can fail on a device with room for the result.

What this change does

  • The backend hands each engine file to ExecuTorch as data that owns it (FileBackedData(path, cleanup=True)). compile_and_save deletes it after its one save.
  • When the outputs would not fit in the free space of their file system (with a tenth to spare), the program and the .ptd are written with consume=True. ExecuTorch then moves each engine into them, so on a file system with sparse files the engines are on disk once. Data on another file system is copied, since a move saves no space there.
  • When a copy fits, it copies as before, because the move is slower: on btrfs, engines of 0.5, 2 and 8 GiB took 1.3, 8 and 37 seconds to move and 0.5, 1.9 and 6.7 seconds to copy.
  • ExecuTorch's save takes only a .pte path. With another name, the program is copied, with a warning when it holds engines.
  • If a move fails, its output can't be finished, since the engine files holding the rest are deleted. The error says to export again, and a data file the move cut short is deleted.
  • With generate_etrecord, the save copies, because the ETRecord reads the engines afterwards. The docs now say why compile_and_save can write an ETRecord (it never sets up the engines).

Tests

  • test_api.py, CPU, lowering stubbed: when the save moves or copies, the .pte name warning, and the cleanup after a failed move.
  • test_compile_and_save.py, A100, TensorRT 11.4, ExecuTorch 1.6 and Make save raise on failure; let save move file-backed data into the output instead of copying it executorch#25099: engines move with no copy into the .pte or the .ptd, an ETRecord makes the save copy, and a disk that fills mid-move raises and says to export again.
  • A real compile_and_save with 1 GiB of engines: the copy peaked at 2 GiB on disk, the move at 1.06 GiB. Both programs load and match eager PyTorch.

@meta-cla meta-cla Bot added the cla signed label Nov 14, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: api [Python] Issues re: Python API labels Nov 14, 2026
@github-actions
github-actions Bot requested a review from zewenli98 November 14, 2026 22:41
@shoumikhin
shoumikhin force-pushed the compile-and-save-consume branch from 03b81ce to ba01aca Compare November 15, 2026 01:40
@github-actions github-actions Bot added component: core Issues re: The core compiler component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 15, 2026
@shoumikhin
shoumikhin force-pushed the compile-and-save-consume branch 2 times, most recently from 3cf1702 to 306a9de Compare November 15, 2026 07:04
@shoumikhin
shoumikhin marked this pull request as draft November 15, 2026 07:04
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 15, 2026
@shoumikhin
shoumikhin force-pushed the compile-and-save-consume branch 9 times, most recently from d53ffa2 to bc9748e Compare November 20, 2026 13:04
…tead of copying them

`compile_and_save` writes each engine to a temporary file as soon as it is built. The ExecuTorch
save then copies that file into the `.pte` (or the `.ptd`), and the file is deleted only when the
call returns. So while the program is written, every engine is on disk twice, and an export can
fail on a device that has room for the result.

The backend now hands each of these engine files to ExecuTorch as data that owns the file
(`FileBackedData(path, cleanup=True)`); `compile_and_save` deletes it after the save anyway, and
it saves only once. When the outputs would not fit in the free space of their file system (with a
tenth to spare, counting an output already there as free), `compile_and_save` writes the program
with `save(file_path, consume=True)` and the `.ptd` with `write_tensor_data_to_file(...,
consume=True)`. ExecuTorch then moves each engine into the output and truncates its file as it
goes, so on a file system with sparse files the engines are on disk once (plus one chunk). It
copies data on another file system than the output, since a move saves no space there.

The move syncs each chunk to disk, so it is slower than a copy: on btrfs, engines of 0.5, 2 and
8 GiB took 1.3, 8 and 37 seconds to move and 0.5, 1.9 and 6.7 seconds to copy. So it is used only
when a copy would not fit, where the save failed before. Otherwise the save copies as before.

ExecuTorch releases before pytorch/executorch#25099, such as 1.6.0, have no `consume`. With them,
`compile_and_save` copies as before. Outside `compile_and_save`, every save calls ExecuTorch exactly
as before.

ExecuTorch's `save` takes only a `.pte` path. For a program saved under another name, the program
is copied (with a warning when it holds engines) and the `.ptd` still moves.

If a move fails, ExecuTorch leaves an output that does not load, and notes that it holds the only
copy of the data moved so far. The rest of that data is in engine files that `compile_and_save`
deletes, so a note now says to export again. ExecuTorch keeps a file it moved only in part, such as
a file of CUDA weights; that file is deleted too, since nothing can finish it.

With `generate_etrecord`, the save copies, because the ETRecord reads the engines after the program
is written. The docs now say that `compile_and_save` can write an ETRecord, because it never sets
up the engines, and that the record holds a copy of every engine.

Test plan:

- `tests/py/dynamo/executorch/test_api.py`, CPU, lowering stubbed: the move happens only inside
  `compile_and_save` and only without room for a copy; a non-`.pte` name moves only the `.ptd` and
  warns; an ExecuTorch without `consume` copies through `write_to_file`; an ETRecord makes the save
  copy; a failed move deletes the data file it cut short, keeps the others, and says to export
  again.
- `tests/py/dynamo/executorch/test_compile_and_save.py`, A100, TensorRT 11.4, with ExecuTorch 1.6
  and with pytorch/executorch#25099: engines reach ExecuTorch as data that owns its file; with the
  disk made to look full, both engines move with no copy, into the `.pte` or the `.ptd`; an
  ETRecord makes the save copy; a disk that fills after the first engine moved raises, says to
  export again, and leaves no engine file.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the compile-and-save-consume branch from bc9748e to 3b9c603 Compare November 23, 2026 22:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: runtime component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant