Repository navigation
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
Conversation
shoumikhin
force-pushed
the
engine-offload-dir
branch
3 times, most recently
from
November 18, 2026 19:18
1aa14f5 to
19804b5
Compare
shoumikhin
force-pushed
the
engine-offload-dir
branch
2 times, most recently
from
November 22, 2026 04:18
76733c5 to
6eb8fc0
Compare
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
force-pushed
the
engine-offload-dir
branch
from
November 23, 2026 14:53
6eb8fc0 to
0575088
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.
What is wrong today
The disk engine cache deletes folders it never wrote. To make room,
DiskEngineCachedeletes the oldest entries in its folder, and it treated every file and folder there as an entry. So engine files kept there, for example bycompile_and_savewithTMPDIRinside it, could be deleted mid-call.compile_and_saverefuses engines whose outputs have static shapes. An engine with a data-dependent operation inside, like the mask writex[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 forexecute_enginenodes.compile_and_saveemits placeholder nodes, so it refused programs thatdynamo.compileplussaveexport.Saving needs a lot of host memory, in a folder the caller cannot choose. An ExecuTorch
save()keeps each engine in a Pythonbytesobject until the end: about 3 times the weights in host memory.compile_and_savealways puts engine files inTMPDIR, 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, forsave()andcompile_and_save():In
save()it is opt-in: each engine goes to a file in a private directory there and is streamed into the.pteor.ptd, so the save no longer keeps every engine in memory.compile_and_saveuses the folder instead ofTMPDIR. It warns where it does nothing (other formats, Windows).What was tested
Linux x86_64, A100, TensorRT 11.4:
compile_and_savewith its engine files inside an evicting cache now saves.engine_offload_dirinto a.pteand a.ptd, with nobytescopy, on every path ofsave().save()for one engine with 4 GiB of bf16 weights: 13.7 GiB by default, 9.8 GiB withengine_offload_dir.