Skip to content

Qualcomm AI Engine Direct - Refactor QNN context config construction. - #11546

Draft
graham0824 wants to merge 1 commit into
google-ai-edge:mainfrom
graham0824:dev/chuntl/refactor_context_configs
Draft

graham0824 wants to merge 1 commit into
google-ai-edge:mainfrom
graham0824:dev/chuntl/refactor_context_configs

Conversation

@graham0824

Copy link
Copy Markdown
Contributor

What

  • Add context config builder.
  • Refactor qnn_manager to use context config.
  • Refactor how we enable context config via compiler plugin.

Verification

  • Developer-Verified
  • Verified all context configs are workable.

Test

  • CI-Verified

@graham0824
graham0824 force-pushed the dev/chuntl/refactor_context_configs branch 4 times, most recently from 52c194e to c32b6cb Compare November 4, 2026 09:26
Comment thread litert/vendors/qualcomm/qnn_manager.h Outdated
Comment on lines +91 to +94
static ::qnn::ContextConfigBuilder BuildCompileContextConfigs(
const ::qnn::Options& options, bool enable_effective_weight_sharing);
static ::qnn::ContextConfigBuilder BuildDispatchContextConfigs(
const ::qnn::Options& options);

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.

I think these should be handle by builder, or have util functions provide by builder.

And it is strange to have compile / dispatch difference in QnnManager.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I moved the compile/dispatch config construction out of QnnManager into context-config builder utilities. QnnManager now only consumes the finalized configs when creating a QNN context.

}

private:
std::vector<std::unique_ptr<void, void (*)(void*)>> custom_config_storage_;

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.

I have some concerns about this:

  1. it is not good practice to store void pointer. (miss type information and also need to store a deleter).
  2. User may pass type which are not QNN custom config types.
  3. If type T is QNN structure and it has raw pointer point to a dynamic resource, it may not be release properly.

We can offline discuss about how to address these issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I removed the void* storage and the generic AddCustomConfig() API. Custom configs are now represented by typed backend-specific wrappers in a std::variant, so only supported QNN config types can be added.
The built config container owns these wrappers, allowing each wrapper to own and manage any nested dynamic resources when needed.

context_configs_.push_back(config);
}

std::vector<const QnnContext_Config_t*> GetNullTerminatedConfigs() const {

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.

After someone call GetNullTerminatedConfigs(), AddCustomConfig and AddContextConfig should not be called. I think we can use c++ "builder" pattern to prevent this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I replaced GetNullTerminatedConfigs() with a consuming Build() API. Configs can now only be added to the mutable builder.

std::move(builder).Build() returns an immutable config container that owns the payloads and exposes the null-terminated Data() list.

QnnContext_Config_t custom_config = QNN_CONTEXT_CONFIG_INIT;
custom_config.option = QNN_CONTEXT_CONFIG_OPTION_PRIORITY;
custom_config.priority = QNN_PRIORITY_HIGH;
config_builder.AddCustomConfig(custom_config);

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.

QnnContext_Config_t is not a custom config type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I separated QnnContext_Config_t from custom config payloads, priority now uses AddContextConfig(), while custom configs use typed HTP/GPU wrappers.

The old test was corrected, and passing QnnContext_Config_t as a custom config now fails at compile time.

@graham0824
graham0824 force-pushed the dev/chuntl/refactor_context_configs branch 2 times, most recently from 43c051d to 7a5b520 Compare November 6, 2026 09:49
@graham0824
graham0824 force-pushed the dev/chuntl/refactor_context_configs branch 2 times, most recently from ab6ecf1 to 29ab57a Compare November 30, 2026 07:01
@graham0824
graham0824 force-pushed the dev/chuntl/refactor_context_configs branch 3 times, most recently from f97aba9 to 1ee54f7 Compare December 1, 2026 06:21
@graham0824
graham0824 force-pushed the dev/chuntl/refactor_context_configs branch 2 times, most recently from afe91dc to cb80f3e Compare December 1, 2026 09:12
Summary:
- Add context config builder.
- Refactor qnn_manager to use context config.
- Refactor how we enable context config via compiler plugin.
@graham0824
graham0824 force-pushed the dev/chuntl/refactor_context_configs branch from cb80f3e to ffc81d6 Compare December 1, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants