Repository navigation
Make save raise on failure; let save move file-backed data into the output instead of copying it - #25099
Make save raise on failure; let save move file-backed data into the output instead of copying it#25099shoumikhin wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25099
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 1 PendingAs of commit 9077adc with merge base a4df8b7 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 28m 14s —— View job Request changes: the first write to reach a shared PR Review: #25099SummaryThe PR adds a The main problem is the design:
How this was checked: by reading the code and the CPython stdlib sources. The job wasn't allowed to run Python, so nothing was executed. pytorch/TensorRT#5045 was not reviewed. Backward Compatibility
Code Quality
Testing
Performance
Specific Comments
RecommendationRequest Changes What blocks approval:
|
9fab878 to
fcfc152
Compare
|
@claude review effort=max |
Review of PR #25099Request changes: after a consuming save, a second or retried PR Review: #25099SummaryThe PR adds an opt-in The main problems:
Python commands aren't permitted in this job, so the tests weren't run. Everything below comes from reading the code. Code Quality
API Design
Backward Compatibility
Testing
Performance
Specific Comments
RecommendationRequest Changes. Four findings can each lose data that only a full re-export brings back, and each has a small fix:
The Windows test failures will also block CI.
|
fcfc152 to
a97f7bf
Compare
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 40m 34s —— View job PR Review: #25099
Request Changes: a write after a consuming write is only caught after the output has been opened with Detailed reviewSummaryThe PR adds
Code Quality
Testing
API Design
Performance
Specific Comments
RecommendationRequest Changes Two things block this PR:
The silent copy and the sync per chunk are open to discussion. But on a large model they decide whether |
a97f7bf to
4c8247d
Compare
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 37m 13s —— View job Review of PR #25099Request Changes: if a consuming Detailed reviewPR Review: #25099SummaryThe PR adds Request Changes, for two reasons:
Points the previous review raised that are still open are marked (still open). I couldn't run the tests because running Python in this job needs approval, so every finding comes from reading the code. Code Quality
Testing
API Design
Performance
Specific Comments
Fix this → (all three) RecommendationRequest Changes. The killed-save problem under Code Quality blocks approval. Because the move writes in place, an interrupted consuming save becomes a Review steps
|
…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. For a model whose engine is tens of gigabytes, the copy needs that much more free disk, 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)`); it is deleted after the save anyway. Inside `compile_and_save`, the save then writes the program and the `.ptd` with `consume=True`, so ExecuTorch moves each engine into the program: it copies it back to front and truncates the file as it goes, and each engine is on disk once. ExecuTorch does this only when the temporary directory, where the engine files are, is on the same file system as the program; otherwise it copies. With `generate_etrecord`, the save copies the engines instead. The ETRecord reads the engines after the program is written, so the save order stays the same as before, and the program is kept when the ETRecord can't be written. Every ExecuTorch save now passes `consume` (as `False` outside `compile_and_save`), so this needs the `consume` argument from pytorch/executorch#25099. It lands after that change, together with an ExecuTorch pin that has it, which also raises the minimum ExecuTorch version. Test plan: - `tests/py/dynamo/executorch/test_api.py`, CPU, with the lowering stubbed: - New `test_compile_and_save_moves_the_engines`: inside `compile_and_save`'s offload block, the program and the `.ptd` are written with `consume=True`. It fails without this change. - New `test_compile_and_save_keeps_the_program_when_the_etrecord_fails`: in the same block with an ETRecord, the program and the `.ptd` are written first, without `consume`, and the `.pte` is kept when the ETRecord save raises. - New `test_save_executorch_keeps_the_program_when_the_etrecord_fails`: the same for a plain save. - New `test_save_executorch_does_not_consume`: a plain save writes without `consume`. - `tests/py/dynamo/executorch/test_compile_and_save.py` on an A100: - The streaming test now also checks that each engine reaches ExecuTorch as data that owns its file. - New `test_engines_move_into_the_program_without_a_second_copy` checks that each engine file is empty after the program is written. - New `test_an_etrecord_makes_the_save_copy_the_engines`: with an ETRecord, the engine files keep their bytes, and both the `.pte` and the ETRecord are written.
|
@claude review |
|
Claude finished @shoumikhin's task in 4m 53s —— View job Needs Discussion: I found no data-loss bug in the move at 905c4bc+2 (HEAD), but the PR Review: #25099SummaryThe PR makes API Design
Backward Compatibility
Code Quality
Testing
Performance
RecommendationNeeds Discussion. Nothing blocks on correctness of the move itself. Fix the ETRecord docstring before merge, since it tells users to do something that still fails. Decide whether replacing the output's inode (and dropping its other hard links) is acceptable for
|
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 53m 28s —— View job Request Changes: a consuming PR Review: #25099SummaryThis PR makes Code Quality
Testing
API Design
Thread Safety
Performance
Specific Comments
RecommendationRequest Changes The partial-failure state is the blocker. The missing |
…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. For a model whose engine is tens of gigabytes, the copy needs that much more free disk, 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)`); it is deleted after the save anyway. Inside `compile_and_save`, when the outputs would not fit in the free space of their file system (with a tenth to spare, and an output already there counted as free, since both a copy and a move replace it), the program is then written with ExecuTorch's `save(file_path, consume=True)` and the `.ptd` with `write_tensor_data_to_file(..., consume=True)`, so ExecuTorch moves each engine into the program: it copies it back to front and truncates the file as it goes, so the engines are on disk once (plus one chunk) instead of twice, on a file system with sparse files. ExecuTorch moves other data that owns its file the same way, such as the CUDA backend's weights, and copies data it can't move, with a warning. Its `save` takes only a `.pte` path, so a program saved under another name is written as before, with a copy; the `.ptd` still moves. ExecuTorch is an optional extra, and releases before pytorch/executorch#25099, such as 1.6.0, have no `consume` argument. Both writes check whether ExecuTorch's method takes it, through one small helper, and copy as before when it doesn't. That fallback writes the program through `open` and `write_to_file`, as before this change, so a failed write still raises. The move is slower than a copy, because ExecuTorch syncs each chunk to disk before the engine file gives it up. Measured 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 `compile_and_save` moves only when a copy would not fit, where the save failed before, and otherwise copies exactly as before. The docstring says so. If a move fails after some data left its engine file, ExecuTorch keeps its partial output, `<name>.<random>.partial`, which holds that data. The engine files are temporary and are deleted when `compile_and_save` returns, so that output can never be finished, and it can be as large as the engines on a disk that just ran out. So the consuming writes now run inside `moving_engines_into`: when the save fails, it deletes each partial output this save left, and a note on the error says to export again. A partial output that was there before the save is never touched. ExecuTorch can also leave other data it moved in part, such as a file of CUDA weights whose rest is in the partial output; that file is deleted too, since nothing can finish it either. Afterwards it empties the plan of each engine the save moved, in part or in full: that plan maps an emptied file, so a later read of it through its module, such as a second save in the block, killed the process with a bus error. Now the plan is empty and that read raises. An engine file the cleanup can't delete is logged as a warning, so it can't hide the error that ended the block, and a save that succeeded doesn't fail over it. The program path goes through `os.fspath` first, so a `pathlib.Path` still works. With `generate_etrecord`, the save copies the engines instead. The ETRecord is written after the program and reads the engines, so a move would leave it nothing to read. The program is still written first, as before. The docs now say why `compile_and_save` can write an ETRecord when a plain save of a program with TensorRT engines can't: it keeps each engine as a byte buffer read from its file. The record then holds copies of every engine, and writing it reads them into memory, which the docs also say: for a 4 GiB engine the record took 8 GiB, and the save 3 GiB more host memory. The move needs the `consume` argument of `save` and `write_tensor_data_to_file` from pytorch/executorch#25099; with an ExecuTorch without it, `compile_and_save` copies as before. Only the move passes `consume`, so every other save calls ExecuTorch exactly as before. Test plan: - `tests/py/dynamo/executorch/test_api.py`, CPU, with the lowering stubbed: - New `test_save_in_the_offload_block_moves_the_engines_when_a_copy_does_not_fit`: inside `compile_and_save`'s offload block, the program and the `.ptd` are written with `consume=True` when the disk has no room for a copy, and without it when it has, counting an old output it replaces as free. The path is a `pathlib.Path`. - New `test_save_in_the_offload_block_moves_only_the_ptd_into_a_path_without_pte`: the same block with a `.bin` path writes the program without `consume` and the `.ptd` with it. - New `test_save_in_the_offload_block_copies_with_an_executorch_without_consume`: with an ExecuTorch whose writes take no `consume`, the program goes through `write_to_file` and the `.ptd` is written without `consume`. - New `test_a_failed_move_in_the_offload_block_deletes_its_partial_output`: a consuming `.pte` or `.ptd` write that stops inside an engine leaves no partial file, and the error says to export again. - New `test_a_failed_move_deletes_what_it_moved_in_part`: a failed save deletes the partial output it left and a data file it moved in part, and says to export again; an untouched data file and a partial output from before the save are kept; the engine files are deleted in every case. - New `test_a_save_empties_the_plans_of_the_engines_it_moved`: after a save, a moved engine's plan is empty and no longer found as an engine file, the other one is untouched, and a second save passes over the moved one. - New tests for engine files the cleanup can't check or delete: a warning, not an error. - New `test_save_in_the_offload_block_keeps_the_program_when_the_etrecord_fails` and `test_save_executorch_does_not_consume`. - `tests/py/dynamo/executorch/test_compile_and_save.py` on an A100 with TensorRT 11.4, with ExecuTorch 1.6 and with ExecuTorch from pytorch/executorch#25099: - The streaming test also checks that each engine reaches ExecuTorch as data that owns its file. - New `test_engines_move_into_the_program_without_a_second_copy`, new `test_an_etrecord_makes_the_save_copy_the_engines`, and new `test_a_failed_move_leaves_no_partial_output` (the disk fills after the first engine moved; no partial file is left). They make the disk look full, so the save moves, and they need an ExecuTorch that moves data. The skip checks for `Cord.file_backed_buffers` as well as `consume`, so an ExecuTorch that renames `consume` fails them instead of skipping them. Signed-off-by: Anthony Shoumikhin <[email protected]>
e689a97 to
9fca900
Compare
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 47m 29s —— View job Request Changes: on Windows, PR Review: #25099SummaryThe PR makes
I could not run the tests, because running Python needs approval in this job, so everything below comes from reading the code at 9fca900. All 7 changed files were covered. "(n/3)" says how many of the three independent reviewers reported a finding. "Still open" marks a point an earlier review raised that this head doesn't address. Code Quality
TestingEach of these was reported by one or two reviewers. Together they contradict the PR description's claim that each test fails when the code it covers is removed.
API Design
Thread Safety
Performance
Specific Comments
RecommendationRequest Changes Two small changes block approval:
The Review progress
|
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 31m 24s —— View job Request Changes: the default Detailed reviewPR Review: #25099SummaryThe PR makes Code Quality1. A full disk before any data moves still triggers a full copy that cannot fit (
2. A save can fail after all of its data has moved if
3. Replacing the output loses its metadata (
4. The consuming save's file-system code is in the wrong module and uses private APIs across modules (
5. The copy warning doesn't say what stopped the move (
6. Whether data moved is inferred from the file size instead of being tracked (
7.
8. Nothing in the repo passes
Testing
Performance
Specific Comments
RecommendationRequest Changes
Review progress |
|
@claude review effort=max |
|
Claude finished @shoumikhin's task in 52m 48s —— View job No blockers remain; the points worth fixing first are zero-length buffers that always "move", the missing test that moves two buffers into one output, and the open question of who will pass Detailed reviewPR Review: #25099SummaryThe PR adds an opt-in All 7 changed files were reviewed. The tests could not be run, because running Python needs approval in this job, so everything below comes from reading the code at f1dab57. "(still open)" marks a point raised in an earlier review. Code Quality
Testing
API Design
Thread Safety
Specific Comments
RecommendationApprove. Nothing above blocks an opt-in flag whose default path only gains the intended errors. Worth doing before merging: the two-buffer test and skipping zero-length buffers. The caller question can be settled here or in a follow-up. Review progress
Done: three independent reviews, then every posted finding re-read against the code at f1dab57 and the two commit messages. Several reviewer claims were dropped because the code does not support them or the trigger was too contrived. One of them was that a consuming save breaks the program's own ETRecord. It doesn't: the ETRecord's edge programs are copied before Not done: running the tests (needs approval in this job), and anything outside the diff. |
`ExecutorchProgramManager.save` caught any error from the write, logged it and returned normally. Both `save_pte_program` helpers (the shared one in `extension/export_util` and the Cadence one) did the same, and the shared one then returned the path of a file that might be empty or cut short. A caller had no way to notice, short of reading the log. Now the error reaches the caller. `save` still logs the path when it succeeds. Both `save_pte_program` helpers write the `.pte` of an `ExecutorchProgramManager` through `save` instead of repeating its code, so `save` and both helpers behave the same way. The legacy `ExecutorchProgram` from the deprecated `exir.capture(...).to_edge().to_executorch()` path has no `save`, so the helpers still write it with its own `write_to_file`, as before. Behavior change for callers: `save` and `save_pte_program` raise on a failed write, where they used to log and return. Test plan: `exir/program/test/test_program.py` and the new `backends/cadence/aot/tests/test_utils.py`. New tests check that `save` raises when the write fails, that the shared `save_pte_program` writes the `.pte` and `.ptd` files, returns the path, and raises when either write fails, and that the Cadence one saves through `save` and raises when it fails. These fail on the code before this change. Both files also check that the helpers still write a program that has no `save`; that behavior did not change, so those tests guard it rather than fail before.
`FileBackedData` keeps a large blob, such as a delegate's compiled engine, in a file until the program is written. Writing it copies that file into the `.pte` or `.ptd`, so the blob is on disk twice until the caller deletes the file. For an engine of tens of gigabytes, that copy alone can make an export run out of disk. This adds a keyword-only `consume` flag, off by default, to `ExecutorchProgramManager.save`, `write_tensor_data_to_file` and both `save_pte_program` helpers. With it, data that owns its file (`cleanup=True`, as from `move_from`) moves into the output. The save opens `path` the same way a plain save does, so an existing output keeps its owner, group, permissions, other names and links. The directory is synced first, so the new file survives a crash. Everything except the moved data is written next, so a disk without room for it fails before any data moves. Each moved buffer is then copied from its end back to its start, 64 MiB at a time, and each chunk is cut off the source file after it is synced. On file systems with sparse files, peak disk use is the data once plus one chunk. On others, such as FAT32, the data still moves but saves no space. The cost is time: one sync per chunk. Because the output fills out of order, its first 16 bytes stay zero until everything else is synced, and are written and synced last. A save that stops partway leaves a file that the runtime refuses to load, instead of one that loads with holes in it. On macOS a sync uses `F_FULLFSYNC`, and falls back to `fsync` only where the file system does not support it. Data is copied instead, with a warning, when it does not own its file, the file is a link, has another name, can't be written or is on another file system than the output, or the program uses the file more than once. Everything is copied on Windows: NTFS fills the hole that a move leaves in a file that is not sparse, so a move would save no space there. The moved data is gone from every program that shares it, for example the `EdgeProgramManager` it was lowered from and other programs made from it. If a save fails after data moved, a note on the error says that `path` holds the only copy of the data moved so far. Unless that file loads, the program has to be exported again. A save that fails before any data moves raises like a plain save and leaves the data where it was. Other behavior changes: reading a `FileBackedData` whose file changed size, before or during the read, for example because it was moved, raises instead of returning short data, and so does writing a program again after a consuming save. `save` and `write_tensor_data_to_file` raise `ValueError` when the output is the file that holds the data to write, instead of emptying it. `copy.deepcopy` of a `FileBackedData` returns the same object, so a program made from a deep copy of a lowered `EdgeProgramManager` can still move its data. Before, the copy could not be read once the original was collected. `write_to_file`, which takes a file the caller opened, always copies, as before. The deprecated `ExecutorchProgram` can't move data, so `save_pte_program` raises `TypeError` when it gets one with `consume=True`. Test plan: `exir/_serialize/test/test_cord.py`, `exir/program/test/test_program.py` and `backends/cadence/aot/tests/test_utils.py` on Linux x86_64, including a save through `to_edge_transform_and_lower` that moves backend data into a `.pte` and a `.ptd`, several buffers in one output, a full disk before and during the move, the order of the syncs and what is on disk at each of them, a failure after the first buffer moved, a rerun into an existing output that is private, a link or has another name, each copy case, the copy on Windows (by setting `sys.platform`), and the macOS sync fallback (by mocking `fcntl`). Each new test fails when the code it covers is removed.
|
@claude review effort=max |
Reviewing PR #25099
|

1. Make
saveandsave_pte_programraise when a write failsExecutorchProgramManager.saveand bothsave_pte_programhelpers logged a failed write and returned. Now they raise. Both helpers write the.ptethroughsave. The deprecatedExecutorchProgramhas nosave, so it is still written with its ownwrite_to_file.2. Let
savemove file-backed data into the output instead of copying itFileBackedDatakeeps a large blob, such as a delegate's engine, in a file until the program is written. Writing copies it into the.pteor.ptd, so the blob is on disk twice.This adds a keyword-only
consumeflag, off by default, tosave,write_tensor_data_to_fileand bothsave_pte_programhelpers:Data that owns its file (as from
FileBackedData.move_from) then moves. The output is opened like in a plain save, so an existing file keeps its owner, permissions and links. Everything else is written first, so a disk without room for it fails before any data moves. Each buffer is copied end to start in 64 MiB chunks, and each synced chunk is cut off the source. On file systems with sparse files, the data is on disk once plus one chunk. The first 16 bytes of the output are written last, so a save that stops partway leaves a file that does not load.Data that can't move safely, for example data on another file system or any data on Windows (where a move saves no space), is copied with a warning.
The moved data is gone from every program that shares it, such as the
EdgeProgramManagerit came from. If a save fails after data moved, a note on the error says so; unless the output loads, the program must be exported again. A deprecatedExecutorchProgramwithconsume=TrueraisesTypeError.For all callers, reading a
FileBackedDatawhose file changed size raises instead of returning short data, saving onto the data's own file raisesValueErrorinstead of emptying it, andcopy.deepcopyof aFileBackedDatareturns the same object.Test plan
test_cord.py,test_program.pyand the Cadencetest_utils.pyon Linux x86_64. New tests cover a real lowering that moves backend data, several buffers in one output, a full disk, what is on disk at each sync, the failure note, a rerun into an existing output, each copy case, and the Windows and macOS paths (mocked). Each fails when the code it covers is removed.