Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions litert/vendors/qualcomm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ litert_cc_lib_with_qnn(
"//litert/core:dynamic_loading",
"//litert/core:filesystem",
"//litert/vendors/qualcomm/core:common",
"//litert/vendors/qualcomm/core:context_config_builder",
"//litert/vendors/qualcomm/core:op_code",
"//litert/vendors/qualcomm/core/backends:backend_utils",
"//litert/vendors/qualcomm/core/backends:dsp_backend",
Expand Down
1 change: 1 addition & 0 deletions litert/vendors/qualcomm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ target_link_libraries(qnn_manager
absl::strings
absl::str_format
qnn_backends
qnn_context_config_builder
qnn_wrappers
PRIVATE
qnn_saver_utils
Expand Down
36 changes: 23 additions & 13 deletions litert/vendors/qualcomm/compiler/qnn_compiler_plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "absl/strings/str_format.h" // from @com_google_absl
#include "absl/strings/str_split.h" // from @com_google_absl
#include "absl/strings/string_view.h" // from @com_google_absl
#include "absl/types/span.h" // from @com_google_absl
#include "litert/c/internal/litert_logging.h"
#include "litert/c/internal/litert_logging_helper_with_compiler_context.h"
#include "litert/c/litert_any.h"
Expand Down Expand Up @@ -65,6 +66,7 @@
#include "litert/vendors/qualcomm/core/backends/backend_factory.h"
#include "litert/vendors/qualcomm/core/backends/qnn_backend.h"
#include "litert/vendors/qualcomm/core/common.h"
#include "litert/vendors/qualcomm/core/context_config_builder.h"
#include "litert/vendors/qualcomm/core/schema/soc_table.h"
#include "litert/vendors/qualcomm/core/tensor_pool.h"
#include "litert/vendors/qualcomm/core/wrappers/op_wrapper.h"
Expand Down Expand Up @@ -181,6 +183,20 @@ LiteRtStatus MoveSchematic(absl::string_view graph_name,
return kLiteRtStatusOk;
}

::qnn::ContextConfigs BuildCompileContextConfigs(
const ::qnn::Options& options, bool enable_effective_weight_sharing) {
::qnn::ContextConfigsBuilder builder;
if (enable_effective_weight_sharing) {
builder.Emplace<::qnn::HtpWeightSharingConfig>(true);
LITERT_LOG(LITERT_INFO, "Enable weight sharing feature");
} else if (const auto performance_mode = options.GetGpuPerformanceMode();
options.GetBackendType() == ::qnn::BackendType::kGpuBackend &&
performance_mode != ::qnn::GpuPerformanceMode::kDefault) {
builder.Emplace<::qnn::GpuPerformanceConfig>(performance_mode);
}
return std::move(builder).Build();
}

} // namespace

LiteRtStatus LiteRtGetCompilerPluginVersion(LiteRtApiVersion* api_version) {
Expand Down Expand Up @@ -671,7 +687,7 @@ LiteRtStatus LiteRtCompilerPluginCompile(
if (context_handle_idx == next_context_handle_idx) {
// Initialize context.
LITERT_LOG(LITERT_INFO, "%s", "Creating context handle");
auto context_configs = QnnManager::DefaultContextConfigs();
bool enable_effective_weight_sharing = false;
if (backend_options.GetEnableWeightSharing()) {
if (backend_options.GetBackendType() !=
::qnn::BackendType::kHtpBackend) {
Expand All @@ -681,25 +697,19 @@ LiteRtStatus LiteRtCompilerPluginCompile(
}
if (num_partitions != kDefaultPartitionNum &&
IsWeightSharingSupported()) {
context_configs = QnnManager::WeightSharingContextConfigs();
LITERT_LOG(LITERT_INFO, "Enable weight sharing feature");
enable_effective_weight_sharing = true;
} else {
LITERT_LOG(LITERT_WARNING,
"Disable weight sharing feature. Only support with "
"multiple partitions and on x86-64 host");
}
} else if (backend_options.GetBackendType() ==
::qnn::BackendType::kGpuBackend) {
if (backend_options.GetGpuPerformanceMode() !=
::qnn::GpuPerformanceMode::kDefault) {
context_configs = QnnManager::GpuPerformanceContextConfigs(
backend_options.GetGpuPerformanceMode());
LITERT_LOG(LITERT_INFO, "Enable GPU performance mode: %d",
static_cast<int>(backend_options.GetGpuPerformanceMode()));
}
}
const auto context_configs = BuildCompileContextConfigs(
backend_options, enable_effective_weight_sharing);
auto context_handle = qnn_manager->CreateContextHandle(
*qnn_backend, context_configs, backend_options.GetProfiling());
*qnn_backend,
context_configs.NullTerminatedConfigs(),
backend_options.GetProfiling());
if (!context_handle) {
LITERT_LOG(LITERT_ERROR, "%s", context_handle.Error().Message().data());
return context_handle.Error().Status();
Expand Down
37 changes: 37 additions & 0 deletions litert/vendors/qualcomm/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,43 @@ cc_library(
],
)

cc_library(
name = "context_config_builder",
srcs = ["context_config_builder.cc"],
hdrs = ["context_config_builder.h"],
deps = [
":common",
"@com_google_absl//absl/types:span",
"@qairt//:qnn_lib_headers",
],
)

litert_test(
name = "context_config_builder_test",
srcs = [
"context_config_builder_test.cc",
],
no_main = True,
tags = [
"noasan",
"nomsan",
"nosan",
"notsan",
],
# This test can be run only on Android and Linux.
target_compatible_with = select({
"@platforms//os:android": [],
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
ungrte = True,
use_sys_malloc = True,
deps = [
":context_config_builder",
"@qairt//:qnn_lib_headers",
] + gtest_main_no_heapcheck_deps(),
)

litert_test(
name = "common_test",
srcs = [
Expand Down
11 changes: 11 additions & 0 deletions litert/vendors/qualcomm/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ target_link_libraries(qnn_common
absl::str_format
)

# context config builder
add_library(qnn_context_config_builder STATIC)

target_sources(qnn_context_config_builder PRIVATE context_config_builder.cc)

target_link_libraries(qnn_context_config_builder
PUBLIC
qnn_common
absl::span
)

# tensor pool
add_library(qnn_tensor_pool STATIC)

Expand Down
29 changes: 29 additions & 0 deletions litert/vendors/qualcomm/core/context_config_builder.cc
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
209 changes: 209 additions & 0 deletions litert/vendors/qualcomm/core/context_config_builder.h
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;
Comment thread
graham0824 marked this conversation as resolved.
Resolved
}

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_;
};

Comment thread
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 {
Comment thread
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_
Loading