Skip to content

Conversation

lucylq
Copy link
Contributor

@lucylq lucylq commented 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

Copy link

pytorch-bot bot commented Sep 24, 2025

🔗 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
Contributor

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

Copy link

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
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
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
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
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

BackendDelegateInlineData(data=processed_bytes)
)

backend_delegate = BackendDelegate(
Copy link
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
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.

Instruction(DelegateCall(delegate_index=delegate_index, args=delegate_args))
Instruction(
DelegateCall(
delegate_index=len(self.emitter_state.delegates) - 1,
Copy link
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
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
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
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
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
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
128 of 132 checks passed
@lucylq
Copy link
Contributor Author

lucylq commented Sep 29, 2025

@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
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

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