Repository navigation
Qualcomm AI Engine Direct - Refactor QNN context config construction. #11546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
copybara-service
merged 1 commit into
google-ai-edge:main
from
graham0824:dev/chuntl/refactor_context_configs
Dec 1, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Qualcomm Innovation Center, Inc. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include "litert/vendors/qualcomm/core/context_config_builder.h" | ||
|
|
||
| #include <utility> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| namespace qnn { | ||
|
|
||
| ContextConfigs::ContextConfigs(std::vector<ContextConfigVariant> entries) | ||
| : entries_(std::move(entries)) { | ||
| // `entries_` is final from here on, so the pointers that BuildQnnConfig() | ||
| // hands out into the wrappers stay valid for the lifetime of this object. | ||
| configs_.reserve(entries_.size()); | ||
| for (auto& entry : entries_) { | ||
| configs_.push_back( | ||
| std::visit([](auto& config) { return config.BuildQnnConfig(); }, entry)); | ||
| } | ||
|
|
||
| config_ptrs_.reserve(configs_.size() + 1); | ||
| for (const auto& config : configs_) { | ||
| config_ptrs_.push_back(&config); | ||
| } | ||
| config_ptrs_.push_back(nullptr); | ||
| } | ||
|
|
||
| } // namespace qnn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| // Copyright (c) Qualcomm Innovation Center, Inc. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #ifndef ODML_LITERT_LITERT_VENDORS_QUALCOMM_CORE_CONTEXT_CONFIG_BUILDER_H_ | ||
| #define ODML_LITERT_LITERT_VENDORS_QUALCOMM_CORE_CONTEXT_CONFIG_BUILDER_H_ | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| #include "QnnCommon.h" // from @qairt | ||
| #include "QnnContext.h" // from @qairt | ||
| #include "GPU/QnnGpuContext.h" // from @qairt | ||
| #include "HTP/QnnHtpContext.h" // from @qairt | ||
| #include "absl/types/span.h" // from @com_google_absl | ||
| #include "litert/vendors/qualcomm/core/common.h" | ||
|
|
||
| namespace qnn { | ||
|
|
||
| // Every wrapper below owns the payload that its QnnContext_Config_t points to | ||
| // and exposes `QnnContext_Config_t BuildQnnConfig()`. BuildQnnConfig() may | ||
| // return pointers into the wrapper itself, so it must only be called once the | ||
| // wrapper has reached its final address (see ContextConfigs). | ||
|
|
||
| class PriorityConfig { | ||
| public: | ||
| explicit PriorityConfig(Qnn_Priority_t priority) : priority_(priority) {} | ||
|
|
||
| QnnContext_Config_t BuildQnnConfig() { | ||
| QnnContext_Config_t config = QNN_CONTEXT_CONFIG_INIT; | ||
| config.option = QNN_CONTEXT_CONFIG_OPTION_PRIORITY; | ||
| config.priority = priority_; | ||
| return config; | ||
| } | ||
|
|
||
| private: | ||
| Qnn_Priority_t priority_; | ||
| }; | ||
|
|
||
| // Loads only the named graphs of a context binary. | ||
| class EnableGraphsConfig { | ||
| public: | ||
| explicit EnableGraphsConfig(std::vector<std::string> names) | ||
| : names_(std::move(names)) {} | ||
|
|
||
| QnnContext_Config_t BuildQnnConfig() { | ||
| name_ptrs_.clear(); | ||
| name_ptrs_.reserve(names_.size() + 1); | ||
| for (const std::string& name : names_) { | ||
| name_ptrs_.push_back(name.c_str()); | ||
| } | ||
| name_ptrs_.push_back(nullptr); | ||
|
|
||
| QnnContext_Config_t config = QNN_CONTEXT_CONFIG_INIT; | ||
| config.option = QNN_CONTEXT_CONFIG_ENABLE_GRAPHS; | ||
| config.enableGraphs = name_ptrs_.data(); | ||
| return config; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<std::string> names_; | ||
| std::vector<const char*> name_ptrs_; | ||
| }; | ||
|
|
||
| class GpuPerformanceConfig { | ||
| public: | ||
| explicit GpuPerformanceConfig(GpuPerformanceMode performance_mode) | ||
| : config_(QNN_GPU_CONTEXT_CUSTOM_CONFIG_INIT) { | ||
| config_.option = QNN_GPU_CONTEXT_CONFIG_OPTION_PERF_HINT; | ||
| config_.perfHint = ToQnnPerfHint(performance_mode); | ||
| } | ||
|
|
||
| QnnContext_Config_t BuildQnnConfig() { | ||
| QnnContext_Config_t config = QNN_CONTEXT_CONFIG_INIT; | ||
| config.option = QNN_CONTEXT_CONFIG_OPTION_CUSTOM; | ||
| config.customConfig = &config_; | ||
| return config; | ||
| } | ||
|
|
||
| private: | ||
| static QnnGpuContext_PerfHint_t ToQnnPerfHint(GpuPerformanceMode mode) { | ||
| switch (mode) { | ||
| case GpuPerformanceMode::kHigh: | ||
| return QNN_GPU_CONTEXT_PERF_HINT_HIGH; | ||
| case GpuPerformanceMode::kNormal: | ||
| return QNN_GPU_CONTEXT_PERF_HINT_NORMAL; | ||
| case GpuPerformanceMode::kLow: | ||
| return QNN_GPU_CONTEXT_PERF_HINT_LOW; | ||
| case GpuPerformanceMode::kDefault: | ||
| return QNN_GPU_CONTEXT_PERF_HINT_HIGH; | ||
| } | ||
| return QNN_GPU_CONTEXT_PERF_HINT_HIGH; | ||
| } | ||
|
|
||
| QnnGpuContext_CustomConfig_t config_; | ||
| }; | ||
|
|
||
| class HtpWeightSharingConfig { | ||
| public: | ||
| explicit HtpWeightSharingConfig(bool enabled) | ||
| : config_(QNN_HTP_CONTEXT_CUSTOM_CONFIG_INIT) { | ||
| config_.option = QNN_HTP_CONTEXT_CONFIG_OPTION_WEIGHT_SHARING_ENABLED; | ||
| config_.weightSharingEnabled = enabled; | ||
| } | ||
|
|
||
| QnnContext_Config_t BuildQnnConfig() { | ||
| QnnContext_Config_t config = QNN_CONTEXT_CONFIG_INIT; | ||
| config.option = QNN_CONTEXT_CONFIG_OPTION_CUSTOM; | ||
| config.customConfig = &config_; | ||
| return config; | ||
| } | ||
|
|
||
| private: | ||
| QnnHtpContext_CustomConfig_t config_; | ||
| }; | ||
|
|
||
| class HtpFileReadMemoryBudgetConfig { | ||
| public: | ||
| explicit HtpFileReadMemoryBudgetConfig(uint32_t budget_mb) | ||
| : config_(QNN_HTP_CONTEXT_CUSTOM_CONFIG_INIT) { | ||
| config_.option = QNN_HTP_CONTEXT_CONFIG_OPTION_FILE_READ_MEMORY_BUDGET; | ||
| config_.fileReadMemoryBudgetInMb = budget_mb; | ||
| } | ||
|
|
||
| QnnContext_Config_t BuildQnnConfig() { | ||
| QnnContext_Config_t config = QNN_CONTEXT_CONFIG_INIT; | ||
| config.option = QNN_CONTEXT_CONFIG_OPTION_CUSTOM; | ||
| config.customConfig = &config_; | ||
| return config; | ||
| } | ||
|
|
||
| private: | ||
| QnnHtpContext_CustomConfig_t config_; | ||
| }; | ||
|
|
||
| class HtpInitAccelerationConfig { | ||
| public: | ||
| explicit HtpInitAccelerationConfig(bool enabled) | ||
| : config_(QNN_HTP_CONTEXT_CUSTOM_CONFIG_INIT) { | ||
| config_.option = QNN_HTP_CONTEXT_CONFIG_OPTION_INIT_ACCELERATION; | ||
| config_.initAcceleration = enabled; | ||
| } | ||
|
|
||
| QnnContext_Config_t BuildQnnConfig() { | ||
| QnnContext_Config_t config = QNN_CONTEXT_CONFIG_INIT; | ||
| config.option = QNN_CONTEXT_CONFIG_OPTION_CUSTOM; | ||
| config.customConfig = &config_; | ||
| return config; | ||
| } | ||
|
|
||
| private: | ||
| QnnHtpContext_CustomConfig_t config_; | ||
| }; | ||
|
|
||
|
graham0824 marked this conversation as resolved.
Resolved
|
||
| using ContextConfigVariant = | ||
| std::variant<PriorityConfig, EnableGraphsConfig, GpuPerformanceConfig, | ||
| HtpWeightSharingConfig, HtpFileReadMemoryBudgetConfig, | ||
| HtpInitAccelerationConfig>; | ||
|
|
||
| class ContextConfigsBuilder; | ||
|
|
||
| // Immutable, null-terminated QNN context config list. The container owns all | ||
| // config payloads referenced by Data(). | ||
| class ContextConfigs { | ||
|
graham0824 marked this conversation as resolved.
Resolved
|
||
| public: | ||
| ContextConfigs(const ContextConfigs&) = delete; | ||
| ContextConfigs& operator=(const ContextConfigs&) = delete; | ||
| ContextConfigs(ContextConfigs&&) = delete; | ||
| ContextConfigs& operator=(ContextConfigs&&) = delete; | ||
|
|
||
| absl::Span<const QnnContext_Config_t* const> NullTerminatedConfigs() const { | ||
| return absl::MakeConstSpan(config_ptrs_); | ||
| } | ||
|
|
||
| private: | ||
| friend class ContextConfigsBuilder; | ||
|
|
||
| explicit ContextConfigs(std::vector<ContextConfigVariant> entries); | ||
|
|
||
| std::vector<ContextConfigVariant> entries_; | ||
| std::vector<QnnContext_Config_t> configs_; | ||
| std::vector<const QnnContext_Config_t*> config_ptrs_; | ||
| }; | ||
|
|
||
| class ContextConfigsBuilder { | ||
| public: | ||
| ContextConfigsBuilder() = default; | ||
| ContextConfigsBuilder(ContextConfigsBuilder&&) = default; | ||
| ContextConfigsBuilder& operator=(ContextConfigsBuilder&&) = default; | ||
| ContextConfigsBuilder(const ContextConfigsBuilder&) = delete; | ||
| ContextConfigsBuilder& operator=(const ContextConfigsBuilder&) = delete; | ||
|
|
||
| template <typename T, typename... Args> | ||
| ContextConfigsBuilder& Emplace(Args&&... args) { | ||
| entries_.emplace_back(std::in_place_type<T>, std::forward<Args>(args)...); | ||
| return *this; | ||
| } | ||
|
|
||
| ContextConfigs Build() && { return ContextConfigs(std::move(entries_)); } | ||
|
|
||
| private: | ||
| std::vector<ContextConfigVariant> entries_; | ||
| }; | ||
|
|
||
| } // namespace qnn | ||
|
|
||
| #endif // ODML_LITERT_LITERT_VENDORS_QUALCOMM_CORE_CONTEXT_CONFIG_BUILDER_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.