Skip to content

fix(executorch): safer .ptd output from save() with external_engine_data - #4916

Closed
shoumikhin wants to merge 3 commits into
pytorch:mainfrom
shoumikhin:executorch-ptd-save-safety
Closed

shoumikhin wants to merge 3 commits into
pytorch:mainfrom
shoumikhin:executorch-ptd-save-safety

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

torch_tensorrt.save(..., output_format="executorch", external_engine_data="engines") writes the TensorRT engines to engines.ptd next to the .pte. Three things can go wrong with that file, and none of them is reported:

  1. If the program path is the same file, for example file_path="engines.ptd", save writes the program and then writes the engines over it. It returns normally, and there is no program left to load.
  2. The log line after saving says "point the runtime's data-files at this directory". But the loaders want the .ptd file itself. Runtime.load_program(pte, data_path=<directory>) fails with "Failed to load data map", and so does the C++ Module.
  3. The .ptd name does not depend on the .pte name, and save replaces an existing file. So two programs saved into one directory with the same name share one .ptd that holds only the second program's engines. The first program then fails to load. The user guides do not mention external_engine_data= at all.

What this change does

Three commits, one per problem:

  • save() checks the two names before it exports anything, and raises a ValueError when the program and the engine file would be the same file.

  • The log line lists the full path of every .ptd file it wrote, and says to pass the file, not the directory.

  • The ExecuTorch guide gets a short section on keeping engines in a .ptd file:

    torch_tensorrt.save(trt_gm, "model.pte", output_format="executorch", external_engine_data="model")
    program = Runtime.get().load_program(Path("model.pte"), data_path=Path("model.ptd"))

    It says the .ptd is trusted input, like the .pte, and warns about two programs sharing one name in a directory, next to the existing warning for the CUDA backend's file. The saving guide links to it, and the save() docstring says the file is replaced, so each program needs its own name.

What was tested

On Linux x86-64 with the pinned ExecuTorch nightly:

  • tests/py/dynamo/executorch/test_external_engine_data.py: 29 passed.
  • The two new tests fail without their fixes: the path check test reaches export and fails there, and the log test does not find the .ptd path in the message.
  • The docs were not built locally.

@meta-cla meta-cla Bot added the cla signed label Oct 22, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: api [Python] Issues re: Python API labels Oct 22, 2026
@github-actions
github-actions Bot requested a review from narendasan October 22, 2026 18:41
@shoumikhin
shoumikhin force-pushed the executorch-ptd-save-safety branch from ab6e3c5 to 956521e Compare October 22, 2026 18:58
save(file_path="engines.ptd", external_engine_data="engines") wrote the
program, then wrote the engines to the same file, and returned normally.
Only the engines were left, so there was no program to load. save() now
checks the two names before it exports anything and raises a ValueError
when they are the same file.
After writing a program's external data, save() logged "point the runtime's
data-files at this directory". The loaders want the .ptd file itself:
Runtime.load_program(pte, data_path=<directory>) fails with "Failed to load
data map", and so does the C++ Module. The log line now lists the full path
of every .ptd file it wrote and says to pass the file, not the directory.
The deployment and saving guides did not mention external_engine_data=, so a
reader learned neither that such a program needs its .ptd at load nor how to
pass it. And save() replaces an existing .ptd of the same name, so two
programs saved into one directory with the same name share one file, and the
first one then fails to load with no warning at save time.

Add a short section to the ExecuTorch guide with a save and load example, a
note that the .ptd is trusted input like the .pte, and a warning about the
shared name. Link it from the saving guide, and say in the save() docstring
that the file is replaced, so each program in a directory needs its own name.
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: 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