Repository navigation
fix(executorch): move compile_and_save's engines into the program instead of copying them - #5045
Draft
shoumikhin wants to merge 1 commit into
Draft
shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
shoumikhin
force-pushed
the
compile-and-save-consume
branch
from
November 15, 2026 01:40
03b81ce to
ba01aca
Compare
shoumikhin
force-pushed
the
compile-and-save-consume
branch
2 times, most recently
from
November 15, 2026 07:04
3cf1702 to
306a9de
Compare
shoumikhin
marked this pull request as draft
November 15, 2026 07:04
shoumikhin
force-pushed
the
compile-and-save-consume
branch
9 times, most recently
from
November 20, 2026 13:04
d53ffa2 to
bc9748e
Compare
…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
force-pushed
the
compile-and-save-consume
branch
from
November 23, 2026 22:56
bc9748e to
3b9c603
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_savecopies as before. Other saves are unchanged.What is wrong today
compile_and_savewrites 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
FileBackedData(path, cleanup=True)).compile_and_savedeletes it after its one save..ptdare written withconsume=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.savetakes only a.ptepath. With another name, the program is copied, with a warning when it holds engines.generate_etrecord, the save copies, because the ETRecord reads the engines afterwards. The docs now say whycompile_and_savecan write an ETRecord (it never sets up the engines).Tests
test_api.py, CPU, lowering stubbed: when the save moves or copies, the.ptename 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.pteor the.ptd, an ETRecord makes the save copy, and a disk that fills mid-move raises and says to export again.compile_and_savewith 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.