Skip to content

Android: allow building Vulkan/XNNPACK backends as separate shared libraries - #21849

Open
msluszniak wants to merge 1 commit into
pytorch:mainfrom
msluszniak:ms/android-shared-backend-libs
Open

Android: allow building Vulkan/XNNPACK backends as separate shared libraries#21849
msluszniak wants to merge 1 commit into
pytorch:mainfrom
msluszniak:ms/android-shared-backend-libs

Conversation

@msluszniak

@msluszniak msluszniak commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

The Android AAR links every enabled backend into libexecutorch_jni.so, so an
app pays for all of them even when it uses one. Vulkan and XNNPACK are the
largest contributors, and an app shipping only the CPU path still carries the
GPU backend.

This PR adds EXECUTORCH_BUILD_VULKAN_BACKEND_SHARED and
EXECUTORCH_BUILD_XNNPACK_BACKEND_SHARED, both defaulting to OFF, so
existing builds are byte-for-byte unchanged. When set, the backend is emitted as
its own .so that whole-archives the corresponding static archive, and the JNI
library no longer links it — consumers load only what they need.

Two details worth calling out for review:

  • extension/android/jni/jni_noop.cpp is an empty translation unit. A SHARED
    target whose only content is a whole-archived static library still needs one
    source file for CMake to invoke the linker on.
  • custom_ops links xnnpack_backend directly, which would pull the backend
    back into the JNI library and defeat the split, so that link is dropped when
    the shared XNNPACK build is selected.

build_android_library.sh forwards both variables so they can be driven from
the environment.

Test plan

  • Default build (both flags OFF) produces the same single libexecutorch.so as
    before.
  • With EXECUTORCH_BUILD_VULKAN=ON EXECUTORCH_BUILD_VULKAN_BACKEND_SHARED=ON,
    the Vulkan backend builds as a separate .so and an app loading it runs
    Vulkan-delegated models on device (Galaxy S26 Ultra, Adreno).
  • This split is what react-native-executorch ships on Android today, so it has
    had real-world exercise across both backends.

cc @SS-JIA @manuelcandales @digantdesai @cbilgin

…braries

The Android AAR links every enabled backend into libexecutorch_jni.so, so an
app pays for all of them even when it uses one. Vulkan and XNNPACK are the
largest contributors, and an app that ships only the CPU path still carries the
GPU backend.

Add EXECUTORCH_BUILD_VULKAN_BACKEND_SHARED and
EXECUTORCH_BUILD_XNNPACK_BACKEND_SHARED, both defaulting to OFF so existing
builds are unchanged. When set, the backend is emitted as its own .so that
whole-archives the corresponding static archive, and the JNI library no longer
links it. Consumers then load only the backends they need.

Notes:

  * jni_noop.cpp is an empty translation unit. A SHARED target whose only
    content is a whole-archived static library still needs one source file for
    CMake to invoke the linker.
  * custom_ops links xnnpack_backend directly, which would pull the backend
    back into the JNI library and defeat the split, so that link is dropped
    when the shared XNNPACK build is selected.
  * build_android_library.sh forwards both variables so they can be driven
    from the environment.
@pytorch-bot pytorch-bot Bot added the module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ label Aug 14, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 12 Awaiting Approval

As of commit 8167cb1 with merge base 33f68d1 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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 14, 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.

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: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants