Skip to content

Qualcomm AI Engine Direct - Zero-initialize delegate-owned mutable buffers - #23404

Merged
psiddh merged 1 commit into
pytorch:mainfrom
XinyuXinyuGrocery:qnn-zero-init-mutable-buffers
Nov 17, 2026
Merged

psiddh merged 1 commit into
pytorch:mainfrom
XinyuXinyuGrocery:qnn-zero-init-mutable-buffers

Conversation

@XinyuXinyuGrocery

@XinyuXinyuGrocery XinyuXinyuGrocery commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

TensorWrapper::AllocateDataBuffer() allocates delegate-owned mutable buffers (mutbuf_*, used when skip_mutable_buffer=False) with new char[bytes_], leaving them uninitialized. This memory is owned by the delegate, not the runner, so the delegate has to initialize it. Otherwise the first execution reads stale data. A fresh adb shell process usually gets zeroed pages, so this goes unnoticed there, but inside an Android app process the memory can be recycled and the output is wrong. This change zero-initializes the allocation.

Test plan

SM8850 (HTP V81), ExecuTorch v1.3.1 with this change: a model with a delegated mutable buffer gave wrong output inside an Android app before the change and correct output after. Filling the buffer with a non-zero pattern right after allocation reproduces the wrong output deterministically.

cc @cccclai @winskuo-quic @shewu-quic @haowhsu-quic @DannyYuyang-quic @qti-horodnic @cbilgin @abhinaykukkadapu @psiddh @nil-is-all

@pytorch-bot

pytorch-bot Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit aed080b with merge base e2b7ee5 (image):

NEW FAILURE - The following job has failed:

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 Oct 4, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: XinyuXinyuGrocery / name: XinyuXinyuGrocery (d6f7243)

@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Oct 4, 2026
@nil-is-all nil-is-all added the module: qnn Issues related to Qualcomm's QNN delegate and code under backends/qualcomm/ label Oct 7, 2026
@nil-is-all

Copy link
Copy Markdown
Contributor

Hi @XinyuXinyuGrocery, thanks for the PR! Could you rebase and ping for review when ready? This would ensure the CI workflows focus on this PR's code changes post some fixes from our end.

@nil-is-all

Copy link
Copy Markdown
Contributor

Hi @XinyuXinyuGrocery, thanks for the PR! Could you rebase and ping for review when ready? This would ensure the CI workflows focus on this PR's code changes post some fixes from our end.

@XinyuXinyuGrocery pinging again, branch is out-of-date so please do update branch as well and give us a ping.

@XinyuXinyuGrocery
XinyuXinyuGrocery force-pushed the qnn-zero-init-mutable-buffers branch from b0b9cc1 to d6f7243 Compare October 13, 2026 10:06
@XinyuXinyuGrocery

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: qualcomm"

@pytorch-bot pytorch-bot Bot added the release notes: qualcomm Changes to the Qualcomm backend delegate label Oct 13, 2026
@nil-is-all

Copy link
Copy Markdown
Contributor

@XinyuXinyuGrocery as I run CI again, could you sign EasyCLA?

@XinyuXinyuGrocery

Copy link
Copy Markdown
Contributor Author

@nil-is-all Rebased onto the latest main and signed the EasyCLA. Ready for review, thanks!

@nil-is-all

Copy link
Copy Markdown
Contributor

@nil-is-all Rebased onto the latest main and signed the EasyCLA. Ready for review, thanks!

Thanks, could you resolve the CI failure and also update branch with main?

@nil-is-all nil-is-all added the need-user-input The issue needs more information from the reporter before moving forward label Oct 14, 2026
@XinyuXinyuGrocery
XinyuXinyuGrocery force-pushed the qnn-zero-init-mutable-buffers branch from d6f7243 to b1d4ebb Compare October 15, 2026 07:31
@XinyuXinyuGrocery

Copy link
Copy Markdown
Contributor Author

Updated the branch with main. The failing test (TextGenerationTest.OpaqueCancelAndResetReleaseBackingBeforeWaitReturns) looks unrelated: that job builds with QNN off, so the changed file is not compiled there. Could you re-run CI? Thanks!

@XinyuXinyuGrocery
XinyuXinyuGrocery force-pushed the qnn-zero-init-mutable-buffers branch from b1d4ebb to c147f00 Compare October 18, 2026 13:25
@XinyuXinyuGrocery

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest main. The only failure in the last run, test-webgpu / test-backend-linux (webgpu, operators), is a link error inside the prebuilt Dawn library (libwebgpu_dawn.a: undefined reference to std::__cxx11::basic_string::_M_replace_cold), so it looks like a toolchain mismatch in that job rather than this change (QNN is off there). The same job passes on main. Could you re-run CI? Thanks!

@nil-is-all nil-is-all removed the need-user-input The issue needs more information from the reporter before moving forward label Oct 21, 2026
@XinyuXinyuGrocery
XinyuXinyuGrocery force-pushed the qnn-zero-init-mutable-buffers branch from c147f00 to 1a5948f Compare October 22, 2026 11:09
@XinyuXinyuGrocery

Copy link
Copy Markdown
Contributor Author

@nil-is-all Thanks for re-running. All QNN jobs passed. The four failures are not from this change:

  • nxp-mcuxpresso-test, unittest-nxp-neutron: No module named 'model_explorer.pytorch_exported_program_adater_impl'. This is fixed on main by Support both old and new model_explorer adapter module names. #24020, which the old base did not include.
  • test-webgpu (operators, models): the setup step got 503 Service Unavailable from the toolchain PPA, so libstdc++ was not upgraded and the prebuilt Dawn library failed to link. The same failure happened on main at that time.

I rebased onto viable/strict, which includes #24020. Could you approve the CI run? Thanks!

@nil-is-all nil-is-all added the partner: qualcomm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Qualcomm label Nov 11, 2026

@qti-horodnic qti-horodnic left a comment

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.

Thanks @XinyuXinyuGrocery, lgtm.


Error TensorWrapper::AllocateDataBuffer() {
char* static_data_buffer = new (std::nothrow) char[bytes_]; // NOLINT
// Zero-initialize: this memory is owned by the delegate, not the runner.

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.

Minor: Ownership doesn't tell why zero is necessary, maybe "The graph reads mutable buffers before the first write; start from zero." would be more accurate.

…ffers

AllocateDataBuffer() left the memory of delegate-owned mutable buffers
uninitialized. The runner cannot initialize memory the delegate owns, so
the first execution could read stale data. Zero-initialize it.
@XinyuXinyuGrocery
XinyuXinyuGrocery force-pushed the qnn-zero-init-mutable-buffers branch from 1a5948f to aed080b Compare November 17, 2026 07:43
@XinyuXinyuGrocery

Copy link
Copy Markdown
Contributor Author

@qti-horodnic Thanks for the review! Updated the comment as suggested and rebased onto the latest viable/strict.
@nil-is-all @abhinaykukkadapu @psiddh Could you approve the CI run and review when you have time? Thanks!

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. community: contribution PRs coming from community (excluding hardware partners) module: qnn Issues related to Qualcomm's QNN delegate and code under backends/qualcomm/ partner: qualcomm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Qualcomm release notes: qualcomm Changes to the Qualcomm backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants