Skip to content

Dedup delegate blobs in emitter - #14564

Merged
facebook-github-bot merged 1 commit into
pytorch:mainfrom
lucylq:export-D83162107
Sep 27, 2025
Merged

facebook-github-bot merged 1 commit into
pytorch:mainfrom
lucylq:export-D83162107

Conversation

@lucylq

@lucylq lucylq commented Sep 24, 2025

Copy link
Copy Markdown
Contributor

Summary:
Previously we deduplicated entire 'BackendDelegate' blobs using the preprocessed blob. If two BackendDelegate fields have different id or compile specs, it would be disregarded.

This diff:

  1. Only deduplicates the preprocessed blob. BackendDelegate retains its own compile specs, etc.
  2. Removes the per-method 'delegate_cache', as we have a program-wide delegate cache.
  3. Adds a test to confirm we have one delegate segment but two BackendDelegate references pointing at it.

Differential Revision: D83162107

@pytorch-bot

pytorch-bot Bot commented Sep 24, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/14564

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 1 Cancelled Job, 1 Unrelated Failure

As of commit 50fb2d9 with merge base 684b5fd (image):

NEW FAILURE - The following job has failed:

CANCELLED JOB - The following job was cancelled. Please retry:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 24, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@lucylq has exported this pull request. If you are a Meta employee, you can view the originating diff in D83162107.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

lucylq added a commit to lucylq/executorch-1 that referenced this pull request Sep 24, 2025
Summary:

Previously we deduplicated entire 'BackendDelegate' blobs using the preprocessed blob. If two BackendDelegate fields have different id or compile specs, it would be disregarded.

This diff:
1. Only deduplicates the preprocessed blob. BackendDelegate retains its own compile specs, etc.
2. Removes the per-method 'delegate_cache', as we have a program-wide delegate cache.
3. Adds a test to confirm we have one delegate segment but two BackendDelegate references pointing at it.

Differential Revision: D83162107
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@lucylq has exported this pull request. If you are a Meta employee, you can view the originating diff in D83162107.

@lucylq
lucylq requested a review from cccclai September 26, 2025 17:01
Comment thread exir/emit/_emitter.py
processed_bytes = lowered_module.processed_bytes
hashed = hashlib.sha256(processed_bytes).hexdigest()
delegate_index = self.emitter_state.delegate_cache.get(hashed)
delegate_index = self.program_state.backend_delegate_data_cache.get(hashed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what's the difference between emitter state and program state? How is backend_delegate_data_cache different than delegate_cache

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

emitter state is per-method, program state covers all the methods in the program

Comment thread exir/emit/_emitter.py
BackendDelegateInlineData(data=processed_bytes)
)

backend_delegate = BackendDelegate(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not quite sure why the previous logic didn't work but the new one does. It seems the logic is the same here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The previous logic refers to the BackendDelegate created for the deduplicated processed blob. We may have a different compile specs, but the same processed blob, in which case the compile specs are lost. You can take a look at the test case as well, and try it on the old code.

Comment thread exir/emit/_emitter.py
Instruction(DelegateCall(delegate_index=delegate_index, args=delegate_args))
Instruction(
DelegateCall(
delegate_index=len(self.emitter_state.delegates) - 1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hmm why is the delegate index defined as len(self.emitter_state.delegates) - 1?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the new logic creates a separate BackendDelegate for each call_delegate, so it corresponds to the length self.emitter_state.delegates.


plan = program.execution_plan[0]
# Two delegates that point to the same blob.
self.assertEqual(len(plan.delegates), 2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it for checking the number of call_delegate instruction?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes

lucylq added a commit to lucylq/executorch-1 that referenced this pull request Sep 26, 2025
Summary:

Previously we deduplicated entire 'BackendDelegate' blobs using the preprocessed blob. If two BackendDelegate fields have different id or compile specs, but the same preprocessed blob, we would take the first one and use it in the execution plan. The id/compile specs of the second would be lost.


This diff:
1. Only deduplicates the preprocessed blob. BackendDelegate retains its own compile specs, etc.
2. Removes the per-method 'delegate_cache', as we have a program-wide delegate cache.
3. Adds a test to confirm we have one delegate segment but two BackendDelegate references pointing at it.

Reviewed By: JacobSzwejbka

Differential Revision: D83162107
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@lucylq has exported this pull request. If you are a Meta employee, you can view the originating diff in D83162107.

Summary:

Previously we deduplicated entire 'BackendDelegate' blobs using the preprocessed blob. If two BackendDelegate fields have different id or compile specs, but the same preprocessed blob, we would take the first one and use it in the execution plan. The id/compile specs of the second would be lost.


This diff:
1. Only deduplicates the preprocessed blob. BackendDelegate retains its own compile specs, etc.
2. Removes the per-method 'delegate_cache', as we have a program-wide delegate cache.
3. Adds a test to confirm we have one delegate segment but two BackendDelegate references pointing at it.

Reviewed By: JacobSzwejbka

Differential Revision: D83162107
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@lucylq has exported this pull request. If you are a Meta employee, you can view the originating diff in D83162107.

@facebook-github-bot
facebook-github-bot merged commit fd5f946 into pytorch:main Sep 27, 2025
@lucylq

lucylq commented Sep 29, 2025

Copy link
Copy Markdown
Contributor Author

@pytorchbot cherry-pick --onto release/1.0 -c critical

pytorchbot pushed a commit that referenced this pull request Sep 29, 2025
Differential Revision: D83162107

Pull Request resolved: #14564

(cherry picked from commit fd5f946)
@pytorchbot

Copy link
Copy Markdown
Collaborator

Cherry picking #14564

The cherry pick PR is at #14658 and it is recommended to link a critical cherry pick PR with an issue. The following tracker issues are updated:

Details for Dev Infra team Raised by workflow job

jirioc pushed a commit to nxp-upstream/executorch that referenced this pull request Dec 19, 2025
Differential Revision: D83162107

Pull Request resolved: pytorch#14564
shoumikhin added a commit that referenced this pull request Oct 27, 2026
…ule (#24127)

## What is wrong today

A method can call the same lowered module several times, for example a
denoising step that is lowered once and called 10 times in a loop. The
emitter then writes one `BackendDelegate` per call. So at load time the
runtime calls the backend's `init()` once per call and keeps every
handle alive, even though all of them come from the same compiled blob.
This wastes load time and memory.

## What this change does

A backend can now return `PreprocessResult(...,
share_handle_across_calls=True)`. Then every call of that lowered module
in a method uses one `BackendDelegate`, so `init()` runs once. Each call
still passes its own arguments.

- It is off by default, so existing backends behave as before. Some
backends keep argument pointers on the handle, so sharing is not safe
for all of them.
- A delegate that declares scratch memory is never shared, because
scratch is planned per call and the runtime keeps it on the delegate.
`Method::init` now rejects a program where such a delegate has more than
one call site. It also checks every delegate index at load, not only at
run time.
- Only calls of one lowered module share a handle. Separate partitions
keep separate delegates, even when their blobs are identical.
- The flag is saved and loaded like `scratch_specs`, including through
`exir.save` and `exir.load`, so the serde schema moves to version 5.6.
The `.pte` file format does not change.
- The demo test backend opts in.

Before #14564, the emitter shared one delegate between calls with the
same blob for every backend. Now it shares by lowered module, and only
for backends that ask for it.

## What was tested

- New tests cover one delegate for repeated calls with the same output
as separate calls, the partitioner path, separate modules keeping their
own delegates, save and load, the default, and scratch, including a
hand-made program that the runtime rejects. The runtime test for scratch
fails without the new runtime check. The load-time index check, calls
inside control flow, the multi-method lowering path and the number of
`init()` calls have no dedicated test.
- On an NVIDIA Jetson, a TensorRT step that a method calls 10 times went
from 10 delegates to 1 shared delegate: load time 435 ms to 74 ms,
memory 1060 MiB to 284 MiB, with the same outputs and speed.
Erik-Lundell pushed a commit to Erik-Lundell/executorch that referenced this pull request Oct 28, 2026
…ule (pytorch#24127)

## What is wrong today

A method can call the same lowered module several times, for example a
denoising step that is lowered once and called 10 times in a loop. The
emitter then writes one `BackendDelegate` per call. So at load time the
runtime calls the backend's `init()` once per call and keeps every
handle alive, even though all of them come from the same compiled blob.
This wastes load time and memory.

## What this change does

A backend can now return `PreprocessResult(...,
share_handle_across_calls=True)`. Then every call of that lowered module
in a method uses one `BackendDelegate`, so `init()` runs once. Each call
still passes its own arguments.

- It is off by default, so existing backends behave as before. Some
backends keep argument pointers on the handle, so sharing is not safe
for all of them.
- A delegate that declares scratch memory is never shared, because
scratch is planned per call and the runtime keeps it on the delegate.
`Method::init` now rejects a program where such a delegate has more than
one call site. It also checks every delegate index at load, not only at
run time.
- Only calls of one lowered module share a handle. Separate partitions
keep separate delegates, even when their blobs are identical.
- The flag is saved and loaded like `scratch_specs`, including through
`exir.save` and `exir.load`, so the serde schema moves to version 5.6.
The `.pte` file format does not change.
- The demo test backend opts in.

Before pytorch#14564, the emitter shared one delegate between calls with the
same blob for every backend. Now it shares by lowered module, and only
for backends that ask for it.

## What was tested

- New tests cover one delegate for repeated calls with the same output
as separate calls, the partitioner path, separate modules keeping their
own delegates, save and load, the default, and scratch, including a
hand-made program that the runtime rejects. The runtime test for scratch
fails without the new runtime check. The load-time index check, calls
inside control flow, the multi-method lowering path and the number of
`init()` calls have no dedicated test.
- On an NVIDIA Jetson, a TensorRT step that a method calls 10 times went
from 10 delegates to 1 shared delegate: load time 435 ms to 74 ms,
memory 1060 MiB to 284 MiB, with the same outputs and speed.
JacobSzwejbka pushed a commit that referenced this pull request Oct 29, 2026
…ule (#24127)

## What is wrong today

A method can call the same lowered module several times, for example a
denoising step that is lowered once and called 10 times in a loop. The
emitter then writes one `BackendDelegate` per call. So at load time the
runtime calls the backend's `init()` once per call and keeps every
handle alive, even though all of them come from the same compiled blob.
This wastes load time and memory.

## What this change does

A backend can now return `PreprocessResult(...,
share_handle_across_calls=True)`. Then every call of that lowered module
in a method uses one `BackendDelegate`, so `init()` runs once. Each call
still passes its own arguments.

- It is off by default, so existing backends behave as before. Some
backends keep argument pointers on the handle, so sharing is not safe
for all of them.
- A delegate that declares scratch memory is never shared, because
scratch is planned per call and the runtime keeps it on the delegate.
`Method::init` now rejects a program where such a delegate has more than
one call site. It also checks every delegate index at load, not only at
run time.
- Only calls of one lowered module share a handle. Separate partitions
keep separate delegates, even when their blobs are identical.
- The flag is saved and loaded like `scratch_specs`, including through
`exir.save` and `exir.load`, so the serde schema moves to version 5.6.
The `.pte` file format does not change.
- The demo test backend opts in.

Before #14564, the emitter shared one delegate between calls with the
same blob for every backend. Now it shares by lowered module, and only
for backends that ask for it.

## What was tested

- New tests cover one delegate for repeated calls with the same output
as separate calls, the partitioner path, separate modules keeping their
own delegates, save and load, the default, and scratch, including a
hand-made program that the runtime rejects. The runtime test for scratch
fails without the new runtime check. The load-time index check, calls
inside control flow, the multi-method lowering path and the number of
`init()` calls have no dedicated test.
- On an NVIDIA Jetson, a TensorRT step that a method calls 10 times went
from 10 delegates to 1 shared delegate: load time 435 ms to 74 ms,
memory 1060 MiB to 284 MiB, with the same outputs and speed.

(cherry picked from commit cef2de7)
JacobSzwejbka pushed a commit that referenced this pull request Oct 29, 2026
…ule (#24207)

## What is wrong today

A method can call the same lowered module several times, for example a
denoising step that is lowered once and called 10 times in a loop. The
emitter then writes one `BackendDelegate` per call. So at load time the
runtime calls the backend's `init()` once per call and keeps every
handle alive, even though all of them come from the same compiled blob.
This wastes load time and memory.

## What this change does

A backend can now return `PreprocessResult(...,
share_handle_across_calls=True)`. Then every call of that lowered module
in a method uses one `BackendDelegate`, so `init()` runs once. Each call
still passes its own arguments.

- It is off by default, so existing backends behave as before. Some
backends keep argument pointers on the handle, so sharing is not safe
for all of them.
- A delegate that declares scratch memory is never shared, because
scratch is planned per call and the runtime keeps it on the delegate.
`Method::init` now rejects a program where such a delegate has more than
one call site. It also checks every delegate index at load, not only at
run time.
- Only calls of one lowered module share a handle. Separate partitions
keep separate delegates, even when their blobs are identical.
- The flag is saved and loaded like `scratch_specs`, including through
`exir.save` and `exir.load`, so the serde schema moves to version 5.6.
The `.pte` file format does not change.
- The demo test backend opts in.

Before #14564, the emitter shared one delegate between calls with the
same blob for every backend. Now it shares by lowered module, and only
for backends that ask for it.

## What was tested

- New tests cover one delegate for repeated calls with the same output
as separate calls, the partitioner path, separate modules keeping their
own delegates, save and load, the default, and scratch, including a
hand-made program that the runtime rejects. The runtime test for scratch
fails without the new runtime check. The load-time index check, calls
inside control flow, the multi-method lowering path and the number of
`init()` calls have no dedicated test.
- On an NVIDIA Jetson, a TensorRT step that a method calls 10 times went
from 10 delegates to 1 shared delegate: load time 435 ms to 74 ms,
memory 1060 MiB to 284 MiB, with the same outputs and speed.

Co-authored-by: Anthony Shoumikhin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants