Repository navigation
fix(executorch): copy the engine once when building the delegate blob - #4774
Merged
lanluo-nvidia merged 1 commit intoOct 2, 2026
Merged
lanluo-nvidia merged 1 commit into
lanluo-nvidia merged 1 commit into
Conversation
TensorRTBackend.preprocess turned the engine tensor into bytes, and serialize_engine then concatenated those bytes into the blob. Both copies were alive at the same time, so saving a program held one extra copy of the engine. For a multi-gigabyte engine that is gigabytes of host memory. Pass a memoryview of the tensor to serialize_engine instead. The concatenation that builds the blob is now the only copy. serialize_engine reads the engine length from the buffer, so bytes, bytearray and memoryview all work.
shoumikhin
force-pushed
the
executorch-preprocess-single-engine-copy
branch
from
October 2, 2026 11:55
b9a9bcb to
dadfe05
Compare
3 of 7 tasks
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
When a compiled module is saved as an ExecuTorch program,
TensorRTBackend.preprocesspacks the TensorRT engine into one blob: a header, some JSON metadata, then the engine bytes.It makes two copies of the engine to do that:
bytesobject.serialize_enginethen joins header, metadata and those bytes into the blob.Both copies are alive at the same time. For a large model the engine is several gigabytes, so saving needs gigabytes of extra host memory. On a board where the CPU and GPU share memory, that can decide whether the export fits at all.
What this change does
preprocesspasses amemoryviewof the engine tensor toserialize_engine, instead of copying it intobytesfirst.serialize_enginereads the engine size withmemoryview(...).nbytes, so it acceptsbytes,bytearrayormemoryview.The join that builds the blob is now the only copy. The blob format does not change.
The
memoryviewstill covers only the tensor's own bytes, so a tensor that views part of a larger buffer still writes only its own extent. The existing test for that case passes.What was tested
preprocessgivesserialize_enginea buffer that shares memory with the engine tensor. It fails before this change (the engine arrives as a separatebytescopy) and passes after it.test_backend.pyandtest_serialization.pypass.