Skip to content

fix: disk engine cache evicts only its own entries, compile_and_save with static-shape outputs, and a caller-chosen engine folder - #5026

Open
shoumikhin wants to merge 3 commits into
pytorch:mainfrom
shoumikhin:engine-offload-dir
Open

shoumikhin wants to merge 3 commits into
pytorch:mainfrom
shoumikhin:engine-offload-dir

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

The disk engine cache deletes folders it never wrote. To make room, DiskEngineCache deletes the oldest entries in its folder, and it treated every file and folder there as an entry. So engine files kept there, for example by compile_and_save with TMPDIR inside it, could be deleted mid-call.

compile_and_save refuses engines whose outputs have static shapes. An engine with a data-dependent operation inside, like the mask write x[mask] = x[mask] + y, is marked as needing an output allocator, which the ExecuTorch runtime lacks. The export clears the mark when every output shape is known, but only for execute_engine nodes. compile_and_save emits placeholder nodes, so it refused programs that dynamo.compile plus save export.

Saving needs a lot of host memory, in a folder the caller cannot choose. An ExecuTorch save() keeps each engine in a Python bytes object until the end: about 3 times the weights in host memory. compile_and_save always puts engine files in TMPDIR, which may be small or RAM-backed.

What this change does

  • The cache counts and evicts only the entries it wrote (a folder holding blob.bin).

  • For a placeholder, validation loads a marked engine from its file, asks TensorRT with the same check, and drops it. Traced shapes are not enough: export can prove an output size that TensorRT computes at run time.

  • A new keyword, engine_offload_dir, for save() and compile_and_save():

    torch_tensorrt.save(trt_gm, "model.pte", output_format="executorch",
                        engine_offload_dir="/data/engines")

    In save() it is opt-in: each engine goes to a file in a private directory there and is streamed into the .pte or .ptd, so the save no longer keeps every engine in memory. compile_and_save uses the folder instead of TMPDIR. It warns where it does nothing (other formats, Windows).

What was tested

Linux x86_64, A100, TensorRT 11.4:

  • Eviction keeps a folder it did not write, and compile_and_save with its engine files inside an evicting cache now saves.
  • A mask write exports with static and dynamic shapes and runs in the ExecuTorch runtime, matching PyTorch. Data-dependent shapes are still refused, also on the Python-only runtime.
  • Engines stream from engine_offload_dir into a .pte and a .ptd, with no bytes copy, on every path of save().
  • Each new test fails when its fix is reverted, and the touched tests have no new failures.
  • Peak host memory of save() for one engine with 4 GiB of bf16 weights: 13.7 GiB by default, 9.8 GiB with engine_offload_dir.

@meta-cla meta-cla Bot added the cla signed label Nov 12, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 12, 2026
@github-actions
github-actions Bot requested a review from narendasan November 12, 2026 17:50
@shoumikhin
shoumikhin force-pushed the engine-offload-dir branch 3 times, most recently from 1aa14f5 to 19804b5 Compare November 18, 2026 19:18
@shoumikhin shoumikhin changed the title feat(executorch): let the caller choose the folder for engine files in save() and compile_and_save() fix(executorch): compile_and_save with static-shape outputs and a caller-chosen engine folder Nov 18, 2026
@shoumikhin
shoumikhin force-pushed the engine-offload-dir branch 2 times, most recently from 76733c5 to 6eb8fc0 Compare November 22, 2026 04:18
When DiskEngineCache needs room for a new engine, it deletes the oldest
entries in its folder. It listed every file and folder there, so it also
deleted folders it never wrote, for example a folder of engine files that
another part of the program, or the user, keeps next to the cache.

Each entry the cache writes is a folder named after the engine hash with a
blob.bin file inside. Eviction now only considers those entries, and leaves
anything else in the cache folder alone. The size a new cache starts from
counts the same entries, since anything else can no longer be freed and
would take up the cache size for good.

Test: a folder without blob.bin, older than every engine, survives an
eviction that removes the oldest engine, and a new cache on the folder
counts only the engine that is left.

Signed-off-by: Anthony Shoumikhin <[email protected]>
…ve static shapes

An engine is marked as needing an output allocator when an operation inside it
has a data-dependent shape, such as a boolean mask write. The ExecuTorch export
clears that mark when every engine output shape is known, by asking the live
engine. That check ran only for execute_engine nodes. compile_and_save keeps
engines in their files and emits no_op_placeholder_for_execute_engine nodes, so
the same program was refused there while dynamo.compile plus save exported it.

For a placeholder, validation now loads the engine from its file, asks it with
the same check, and drops it again. Only an engine with the mark is loaded, one
at a time. When TensorRT answers with an error, the refusal keeps that error as
its cause but clears its frames, so the loaded engine does not stay alive with
it. The shapes export traced are not enough: export can prove an output
size that TensorRT still computes at run time, as in
x[torch.nonzero(m)[:2, 0]] after torch._check(n >= 2), and the ExecuTorch
runtime then fails on that .pte. replace_execute_engine writes the cleared mark
into the staged placeholder's arguments, since the backend checks that record
again, and recompiles the staged module.

Tests: a boolean mask write exports with static and with dynamic input
shapes, and its program runs in the ExecuTorch runtime and matches the
eager model; programs whose output shape depends on the data are still
refused, and the engine loaded for the check is not kept alive.

Signed-off-by: Anthony Shoumikhin <[email protected]>
…n save() and compile_and_save()

save(output_format="executorch") serializes each TensorRT engine again
and copies it into a Python bytes object for ExecuTorch, which stays
alive until the save ends. For a model whose weights sit in one large
engine, the save needs about 3 times the weights in host memory.
compile_and_save already kept its engines in files, but always in a
shared folder under TMPDIR, which must hold every engine at once and may
be small or RAM-backed.

Add the keyword-only option engine_offload_dir to both. In save() it is
opt-in: with None the save is unchanged. With a folder, each engine is
written to a file in a private torch_tensorrt_engines_* directory
(tempfile.mkdtemp) inside it and streamed into the program. Each engine
still passes through host memory once while it is written, but the save
no longer keeps a copy of every engine.
compile_and_save uses the same private directory, in TMPDIR by default
or in the given folder, which it also accepts in save_options. The
folder is expanded ("~") and resolved; only its last level is created,
with mode 0o700, so a mistyped parent fails, and the private directory
is made before the first build. Only the private directory is removed at
the end; the folder and anything else in it stay. A failed create or
write names the directory and engine_offload_dir, and a failed write
also names the engine. Windows,
which cannot delete a mapped file, keeps the in-memory path.

Engine setup is deferred only in compile_and_save: a save leaves
anything it compiles unchanged. engine_offload_dir is ignored with a
warning for the other formats and on Windows. On a runtime without the
engine tensor accessor, the save writes the engine it decoded to a file
too. Who can write to the folder is not checked; the docs say that
anyone who can write there can swap an engine file, even for a link to
another file the caller can read, so use one only its owner can write to.

Tests: a save with a folder (given as "~/...") streams each engine from
one private directory there, into the .pte or into a .ptd, adds no
engine as bytes, and leaves the folder's other files alone; each of the
three paths of save() passes the folder on; a
save without a folder adds each engine as in-memory bytes;
compile_and_save streams from its private directory without setting
engines up, also with the folder in save_options, makes the folder
(owner only) and the directory before any build, fails before any build
on a missing parent, and rejects the folder given twice; engines
compiled inside a save are set up; a full disk, when the file is created
or partly written, points to engine_offload_dir and leaves no file; the
two warnings; the fallback writes its engine to a file.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin shoumikhin changed the title fix(executorch): compile_and_save with static-shape outputs and a caller-chosen engine folder fix: disk engine cache evicts only its own entries, compile_and_save with static-shape outputs, and a caller-chosen engine folder Nov 23, 2026

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.

2 participants