Skip to content

Qualcomm: stop an expired bundle from making the backend cache stale - #21944

Open
psiddh wants to merge 1 commit into
pytorch:mainfrom
psiddh:qnn-bundle-cache-insert-or-assign
Open

Qualcomm: stop an expired bundle from making the backend cache stale#21944
psiddh wants to merge 1 commit into
pytorch:mainfrom
psiddh:qnn-bundle-cache-insert-or-assign

Conversation

@psiddh

@psiddh psiddh commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

QnnBackendUnifiedRegistry caches backend bundles as weak_ptr keyed on backend type,
and CleanupExpired() only runs in the registry destructor. When a bundle's last
owner goes away, the dead weak_ptr stays in the map. The next request finds the
entry, fails to lock it, builds a fresh bundle, and then calls emplace() — which is
a no-op because the key already exists. The corpse is never replaced, so every
subsequent lowering rebuilds the backend and device instead of sharing them.

insert_or_assign replaces the expired entry. CleanupExpired() can't be used here
because it takes mutex_, which is already held.

Single-lowering processes never reach this, which is why it has been invisible. It
shows up once a process does two or more lowerings: with the fix the second
lowering reuses the cached bundle instead of creating a second one, which also
avoids repeated device configuration.

Test: test_backend_bundle_cache_survives_an_expired_entry asserts two creations
plus at least one reuse, and fails on unmodified main.

cc @cbilgin

QnnBackendUnifiedRegistry keeps backend bundles in an
unordered_map<backend_type, weak_ptr<QnnBackendBundle>>. When the last
QnnManager for a type goes away the bundle expires, leaving a dead
weak_ptr under that key. The insert on the create path used emplace,
which does not replace an existing key, so the dead entry stayed put:
every subsequent lookup failed to lock, built a new backend, and again
failed to record it. From the first expiry onward the cache never hit,
so a live bundle could sit unused while a second QnnBackend and
QnnDevice were created for the same backend type.

Reproduced with one compile spec: create a manager and destroy it, keep
a second alive, then ask for a third. Before this change the third
builds its own backend; after it reuses the second's bundle.

CleanupExpired() is not usable from GetOrCreateBackendBundle -- it takes
mutex_, which is already held there -- hence insert_or_assign.

Authored with Claude Code.
Copilot AI lite review requested due to automatic review settings August 19, 2026 15:45
@pytorch-bot

pytorch-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 7d4c42f with merge base b45e85d (image):
💚 Looks good so far! There are no failures yet. 💚

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 Aug 19, 2026
@psiddh
psiddh requested review from shewu-quic and removed request for abhinaykukkadapu and Copilot August 19, 2026 15:46
@psiddh psiddh added the module: qnn Issues related to Qualcomm's QNN delegate and code under backends/qualcomm/ label Aug 19, 2026
@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.

@psiddh psiddh changed the title Qualcomm: stop an expired bundle from poisoning the backend cache Qualcomm: stop an expired bundle from making the backend cache stale Aug 19, 2026
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. module: qnn Issues related to Qualcomm's QNN delegate and code under backends/qualcomm/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant