Skip to content

fix(executorch): copy the engine once when building the delegate blob - #4774

Merged
lanluo-nvidia merged 1 commit into
pytorch:mainfrom
shoumikhin:executorch-preprocess-single-engine-copy
Oct 2, 2026
Merged

lanluo-nvidia merged 1 commit into
pytorch:mainfrom
shoumikhin:executorch-preprocess-single-engine-copy

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

What is wrong today

When a compiled module is saved as an ExecuTorch program, TensorRTBackend.preprocess packs 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:

  1. It turns the engine tensor into a bytes object.
  2. serialize_engine then 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

  • preprocess passes a memoryview of the engine tensor to serialize_engine, instead of copying it into bytes first.
  • serialize_engine reads the engine size with memoryview(...).nbytes, so it accepts bytes, bytearray or memoryview.

The join that builds the blob is now the only copy. The blob format does not change.

engine = serialized_engine.cpu().contiguous().view(torch.uint8)
blob = serialize_engine(memoryview(engine.numpy()), metadata)  # one copy, into the blob

The memoryview still 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

  • New unit test: preprocess gives serialize_engine a buffer that shares memory with the engine tensor. It fails before this change (the engine arrives as a separate bytes copy) and passes after it.
  • test_backend.py and test_serialization.py pass.
  • Saved a large model with a 5.9 GiB engine on an aarch64 board with unified memory, with this change and the companion change that shares tensors in the preprocess copy. Both runs also freed the PyTorch model before saving and returned freed heap memory to the system. With that setup, the peak memory of the save fell from 40.9 GiB to 28.8 GiB, and the saved program was byte for byte the same as before.

@meta-cla meta-cla Bot added the cla signed label Oct 2, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: api [Python] Issues re: Python API labels Oct 2, 2026
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
shoumikhin force-pushed the executorch-preprocess-single-engine-copy branch from b9a9bcb to dadfe05 Compare October 2, 2026 11:55

@lanluo-nvidia lanluo-nvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants