Skip to content

[QNN:Feature] Auto ADSP_LIBRARY_PATH setup and device creation fallback - #4584

Open
lanling-47 wants to merge 1 commit into
alibaba:masterfrom
lanling-47:qnn/backend-robustness
Open

[QNN:Feature] Auto ADSP_LIBRARY_PATH setup and device creation fallback#4584
lanling-47 wants to merge 1 commit into
alibaba:masterfrom
lanling-47:qnn/backend-robustness

Conversation

@lanling-47

Copy link
Copy Markdown
Contributor

Summary

Robustness improvements for QNN HTP backend initialization to handle diverse device configurations gracefully.

Changes

1. Auto ADSP_LIBRARY_PATH Setup

  • Detect libQnnHtp.so location at runtime via dladdr
  • Set ADSP_LIBRARY_PATH to prioritize app-bundled skel libraries over system ones
  • Prevents "Skel lib id mismatch" errors caused by stub/skel version conflicts between app-bundled QNN SDK and system-preinstalled libraries

2. Device Creation Fallback

  • When default deviceCreate fails, exhaustively try multiple SOC/Arch/SignedPD combinations:
    • SM8750 (Snapdragon 8 Elite) / HTP v79
    • SM8650 (Snapdragon 8 Gen 3) / HTP v75
    • Both signed and unsigned process domain variants
  • Continue without device handle rather than aborting entirely

3. Runtime Creation Safety

  • Check logHandle and backendHandle are non-null before creating QnnRuntime
  • Warn (but proceed) if deviceHandle is null

4. Minor Cleanup

  • Rename gContext.QnnInterfacegContext.interface for clarity
  • Use __fp16 directly instead of mnn_qnn_fp16_t wrapper
  • CMakeLists: fallback to bundled 3rdParty/ directory for QNN SDK headers when QNN_SDK_ROOT is not set

Testing

Tested on Snapdragon 8 Gen 3 and Snapdragon 8 Elite devices. Verified that ADSP path auto-detection resolves skel mismatch issues and device fallback works when default config fails.

Robustness improvements for QNN HTP backend initialization:

1. Auto ADSP_LIBRARY_PATH:
   - Detect libQnnHtp.so location at runtime via dladdr
   - Set ADSP_LIBRARY_PATH to prioritize app-bundled skel libraries
   - Prevents 'Skel lib id mismatch' errors from system/stub
     version conflicts

2. Device creation fallback:
   - When default deviceCreate fails, try multiple SOC/Arch/SignedPD
     combinations (SM8750/v79, SM8650/v75, signed/unsigned PD)
   - Continue without device handle rather than aborting entirely

3. Runtime creation safety:
   - Check logHandle and backendHandle are non-null before creating
     QnnRuntime
   - Warn (but proceed) if deviceHandle is null

4. Minor cleanup:
   - Rename gContext.QnnInterface -> gContext.interface for clarity
   - Use __fp16 directly instead of mnn_qnn_fp16_t wrapper
   - CMakeLists: fallback to bundled 3rdParty/ for QNN SDK headers
@wangzhaode

Copy link
Copy Markdown
Collaborator

Thanks for the contribution. The overall direction makes sense, but there are several blocking issues to address before merging:

  1. This change removes the existing Windows/MSVC compatibility path and unconditionally uses dlfcn.h, setenv, and __fp16. Please preserve the Windows implementation, including QnnHtp.dll loading and the RPC memory fallback.
  2. A null deviceHandle is now treated as a valid fallback, but QnnRuntime::create still uses deviceHandle == nullptr as the initialization check. This can repeatedly recreate and overwrite the global log/backend handles. Please introduce a separate initialization state, such as std::once_flag or an explicit state flag.
  3. Please do not overwrite an existing ADSP_LIBRARY_PATH or unconditionally prepend /data/local/tmp. Preserve the user-provided value, append/prepend only the detected Skel directory as needed, and check the setenv result.
  4. The CMake fallback path source/backend/qnn/3rdParty does not exist in this PR or the repository, so the bundled-SDK fallback is currently ineffective. Please remove it or provide a valid dependency path.
  5. Please clarify and validate the signed-PD fallback. Recent QAIRT versions default to unsigned PD and normally ship unsigned Skels, so the signed variants may not be usable without additional deployment.
  6. This branch currently conflicts with master and contains a large amount of unrelated formatting/renaming. Please rebase it and keep the functional diff focused.

Please also provide validation for Windows/MSVC compilation, the normal Android deviceCreate path, each fallback path, the all-deviceCreate-failed path for both online/offline context creation, and behavior when ADSP_LIBRARY_PATH is already set.

@wangzhaode wangzhaode added the awaiting contributor Waiting for contributor to address review comments or rebase label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting contributor Waiting for contributor to address review comments or rebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants