Skip to content
Open
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
2 changes: 2 additions & 0 deletions backends/qualcomm/runtime/QnnExecuTorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#define QNN_RUNTIME_LPAI_CLIENT_PERF_TYPE "qnn_runtime_lpai_client_perf_type"
#define QNN_RUNTIME_LPAI_AFFINITY "qnn_runtime_lpai_affinity"
#define QNN_RUNTIME_LPAI_CORE_SELECTION "qnn_runtime_lpai_core_selection"
// Load-time only: read from LoadBackendOptionsMap per session, not set_option.
#define QNN_RUNTIME_LPAI_ENABLE_ISLAND "qnn_runtime_lpai_enable_island"
#define QNN_RUNTIME_HEAP_PROFILING_PATH "qnn_runtime_heap_profiling_path"

#if defined(_MSC_VER)
Expand Down
18 changes: 16 additions & 2 deletions backends/qualcomm/runtime/QnnExecuTorchBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,26 @@ Result<DelegateHandle*> QnnExecuTorchBackend::init(
QNN_EXECUTORCH_LOG_WARN("unknown argument: %s", compile_spec.key);
}

QnnRuntimeSpecs runtime_specs;
auto enable_island =
context.get_runtime_spec<bool>(QNN_RUNTIME_LPAI_ENABLE_ISLAND);
if (enable_island.ok()) {
runtime_specs.lpai_enable_island = enable_island.get();
} else {
ET_CHECK_OR_RETURN_ERROR(
enable_island.error() == Error::NotFound,
InvalidArgument,
"%s must be a bool",
QNN_RUNTIME_LPAI_ENABLE_ISLAND);
}

MemoryAllocator* runtime_allocator = context.get_runtime_allocator();
// TODO: this is a temporary solution for multi-graph support, will be
// removed once framework starts to accept runtime configuration
// ---
// check if current context binary has already been initialized
// return cached one for reducing memory footprint
const DelegateKey key{signature, runtime_allocator};
const DelegateKey key{signature, runtime_allocator, runtime_specs};
if (auto cached_handle = get_cached_delegate(key)) {
QNN_EXECUTORCH_LOG_INFO(
"Use cached delegate handle for current method: %s",
Expand Down Expand Up @@ -106,7 +119,8 @@ Result<DelegateHandle*> QnnExecuTorchBackend::init(
}

// The runtime allocator owns the storage, so only invoke the destructor.
new (qnn_manager) QnnManager(qnn_executorch_options, qnn_context_blob);
new (qnn_manager)
QnnManager(qnn_executorch_options, qnn_context_blob, runtime_specs);
auto destroy_manager = [](QnnManager* manager) { manager->~QnnManager(); };
std::shared_ptr<QnnManager> manager_guard(qnn_manager, destroy_manager);

Expand Down
17 changes: 14 additions & 3 deletions backends/qualcomm/runtime/QnnExecuTorchBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <executorch/backends/qualcomm/runtime/QnnBackendOptions.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnRuntimeSpecs.h>
#include <executorch/runtime/backend/interface.h>
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/evalue.h>
Expand All @@ -16,6 +17,7 @@
#include <memory>
#include <mutex>
#include <optional>
#include <tuple>
#include <unordered_map>
#include <utility>

Expand Down Expand Up @@ -59,9 +61,18 @@ class QnnExecuTorchBackend final

// A context binary signature plus the runtime allocator that the methods of
// one Module share. Other Modules can be destroyed independently, so they
// must never receive this Module's QnnManager.
using DelegateKey =
std::pair<std::int64_t, const executorch::runtime::MemoryAllocator*>;
// must never receive this Module's QnnManager. Loads with different runtime
// specs need distinct QNN contexts, so the specs are part of the key.
struct DelegateKey {
std::int64_t signature;
const executorch::runtime::MemoryAllocator* allocator;
QnnRuntimeSpecs runtime_specs{};

bool operator<(const DelegateKey& other) const {
return std::tie(signature, allocator, runtime_specs) <
std::tie(other.signature, other.allocator, other.runtime_specs);
}
};

struct SpillFillGroup {
std::mutex mutex;
Expand Down
10 changes: 7 additions & 3 deletions backends/qualcomm/runtime/QnnManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ QnnManager::~QnnManager() {

QnnManager::QnnManager(
const QnnExecuTorchOptions* options,
const QnnExecuTorchContextBinary& qnn_executorch_context_binary)
: qnn_context_blob_(qnn_executorch_context_binary), options_(options) {
const QnnExecuTorchContextBinary& qnn_executorch_context_binary,
const QnnRuntimeSpecs& runtime_specs)
: qnn_context_blob_(qnn_executorch_context_binary),
options_(options),
runtime_specs_(runtime_specs) {
QnnExecuTorchBackendType backend_type =
options->backend_options()->backend_type();

Expand Down Expand Up @@ -354,7 +357,8 @@ Error QnnManager::InitContext(
qnn_context_blob_,
options_,
qnn_dlc_manager_.get(),
spill_fill_handle);
spill_fill_handle,
runtime_specs_);
ET_CHECK_OR_RETURN_ERROR(
backend_params_ptr_ != nullptr,
Internal,
Expand Down
4 changes: 3 additions & 1 deletion backends/qualcomm/runtime/QnnManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class QnnManager {
// Construct QnnManager
explicit QnnManager(
const QnnExecuTorchOptions* options,
const QnnExecuTorchContextBinary& qnn_executorch_context_binary);
const QnnExecuTorchContextBinary& qnn_executorch_context_binary,
const QnnRuntimeSpecs& runtime_specs = {});

~QnnManager();
// Initialize the shared backend bundle such as QnnBackend and QnnDevice
Expand Down Expand Up @@ -135,6 +136,7 @@ class QnnManager {
std::shared_ptr<QnnBackendBundle>
backend_bundle_ptr_; // New member to hold shared resources
const QnnExecuTorchOptions* options_;
QnnRuntimeSpecs runtime_specs_;
std::unordered_map<std::string, std::vector<std::shared_ptr<TensorWrapper>>>
input_tensors_;
std::unordered_map<std::string, std::vector<std::shared_ptr<TensorWrapper>>>
Expand Down
1 change: 1 addition & 0 deletions backends/qualcomm/runtime/backends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ target_sources(
target_sources(
qnn_factory
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/QnnBackendFactory.h
${CMAKE_CURRENT_LIST_DIR}/QnnRuntimeSpecs.h
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/QnnBackendFactory.cpp
)

Expand Down
9 changes: 7 additions & 2 deletions backends/qualcomm/runtime/backends/QnnBackendFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
const QnnExecuTorchContextBinary& qnn_context_blob,
const QnnExecuTorchOptions* options,
QnnDlcManager* qnn_dlc_manager,
Qnn_ContextHandle_t spill_fill_handle) {
Qnn_ContextHandle_t spill_fill_handle,
const QnnRuntimeSpecs& runtime_specs) {
auto backend_params = std::make_unique<BackendConfigParameters>();

switch (options->backend_options()->backend_type()) {
Expand Down Expand Up @@ -160,6 +161,9 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
QNN_RUNTIME_LPAI_CORE_SELECTION));
QNN_EXECUTORCH_LOG_INFO(
"target_env in lpai_options: %d", lpai_options->target_env());
QNN_EXECUTORCH_LOG_INFO(
"enable_island in runtime_specs: %d",
runtime_specs.lpai_enable_island);
}
backend_params->qnn_backend_cache_ptr_ =
std::make_unique<QnnBackendCache>(
Expand All @@ -171,7 +175,8 @@ std::unique_ptr<BackendConfigParameters> QnnBackendFactory::Create(
qnn_backend_ptr,
qnn_device_ptr,
backend_params->qnn_backend_cache_ptr_.get(),
qnn_dlc_manager);
qnn_dlc_manager,
runtime_specs.lpai_enable_island);

backend_params->qnn_graph_ptr_ = std::make_unique<LpaiGraph>(
implementation_ptr,
Expand Down
4 changes: 3 additions & 1 deletion backends/qualcomm/runtime/backends/QnnBackendFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <executorch/backends/qualcomm/runtime/backends/QnnImplementation.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnLogger.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnMemManager.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnRuntimeSpecs.h>
#include <executorch/backends/qualcomm/runtime/backends/gpu/GpuContext.h>
#include <executorch/backends/qualcomm/runtime/backends/gpu/GpuGraph.h>
#include <executorch/backends/qualcomm/runtime/backends/htp/HtpBackendCache.h>
Expand Down Expand Up @@ -69,7 +70,8 @@ class QnnBackendFactory {
const QnnExecuTorchContextBinary& qnn_context_blob,
const QnnExecuTorchOptions* options,
QnnDlcManager* qnn_dlc_manager,
Qnn_ContextHandle_t spill_fill_handle = nullptr);
Qnn_ContextHandle_t spill_fill_handle = nullptr,
const QnnRuntimeSpecs& runtime_specs = {});
};
} // namespace qnn
} // namespace backends
Expand Down
6 changes: 6 additions & 0 deletions backends/qualcomm/runtime/backends/QnnContextCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class QnnContext {
virtual executorch::runtime::Error AfterConfigure() {
return executorch::runtime::Error::Ok;
};
QnnImplementation* GetImplementation() const {
return implementation_;
}
QnnBackend* GetBackend() const {
return backend_;
}

private:
void WriteHeapProfile();
Expand Down
3 changes: 3 additions & 0 deletions backends/qualcomm/runtime/backends/QnnFunctionInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class QnnInterface {
backend_validate_op_config,
backendValidateOpConfig);
DEFINE_SHIM_FUNCTION_INTERFACE(backend_get_api_version, backendGetApiVersion);
#if QNN_EXECUTORCH_SUPPORTS_BACKEND_GET_PROPERTY
DEFINE_SHIM_FUNCTION_INTERFACE(backend_get_property, backendGetProperty);
#endif
// --------- QnnDevice ---------
DEFINE_SHIM_FUNCTION_INTERFACE(device_create, deviceCreate);
DEFINE_SHIM_FUNCTION_INTERFACE(device_free, deviceFree);
Expand Down
29 changes: 29 additions & 0 deletions backends/qualcomm/runtime/backends/QnnRuntimeSpecs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) Qualcomm Innovation Center, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once

#include <tuple>

namespace executorch {
namespace backends {
namespace qnn {

// Per-session options delivered at load time through BackendInitContext.
struct QnnRuntimeSpecs {
bool lpai_enable_island = false;

// Every field takes part, so loads with different specs never share a
// cached delegate.
bool operator<(const QnnRuntimeSpecs& other) const {
return std::tie(lpai_enable_island) < std::tie(other.lpai_enable_island);
}
};

} // namespace qnn
} // namespace backends
} // namespace executorch
7 changes: 7 additions & 0 deletions backends/qualcomm/runtime/backends/QnnSdkCompatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@
#else
#define QNN_EXECUTORCH_SUPPORTS_FCB 0
#endif

// backendGetProperty precedes contextAddToDlc in QNN_INTERFACE_VER_TYPE.
#if QNN_EXECUTORCH_QNN_API_VERSION_AT_LEAST(2, 37)
#define QNN_EXECUTORCH_SUPPORTS_BACKEND_GET_PROPERTY 1
#else
#define QNN_EXECUTORCH_SUPPORTS_BACKEND_GET_PROPERTY 0
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void* align_ptr(void* ptr) {
// Code logic below is similar to qnn_executor_runner to load pte file.
QnnExecuTorchIdlWrapper::QnnExecuTorchIdlWrapper(
const char* pte_path,
const int method_index) {
const int method_index,
const int load_flags) {
Result<FileDataLoader> loader =
FileDataLoader::from(pte_path, QNN_CTX_BIN_ALIGNMENT);
FARF(RUNTIME_HIGH, "Loading file %s.", pte_path);
Expand Down Expand Up @@ -108,8 +109,18 @@ QnnExecuTorchIdlWrapper::QnnExecuTorchIdlWrapper(
memory_allocator_.get(), planned_memory_.get());
}

Result<Method> method =
program_->load_method(*method_name, memory_manager_.get(), &etdump_gen_);
if (load_flags & LOAD_FLAG_LPAI_ENABLE_ISLAND) {
FARF(RUNTIME_HIGH, "Requesting LPAI island mode for %s", pte_path);
qnn_backend_options_.set_option(QNN_RUNTIME_LPAI_ENABLE_ISLAND, true);
backend_options_map_.set_options(QNN_BACKEND, qnn_backend_options_.view());
}

Result<Method> method = program_->load_method(
*method_name,
memory_manager_.get(),
&etdump_gen_,
/*named_data_map=*/nullptr,
&backend_options_map_);
if (!method.ok()) {
FARF(
RUNTIME_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

#include "HAP_farf.h"

#include <executorch/backends/qualcomm/runtime/QnnExecuTorch.h>
#include <executorch/backends/qualcomm/runtime/backends/QnnCustomProtocol.h>
#include <executorch/devtools/etdump/etdump_flatcc.h>
#include <executorch/extension/data_loader/file_data_loader.h>
#include <executorch/runtime/backend/backend_options_map.h>
#include <executorch/runtime/backend/options.h>
#include <executorch/runtime/core/memory_allocator.h>
#include <executorch/runtime/executor/method.h>
#include <executorch/runtime/executor/program.h>
Expand All @@ -25,7 +28,10 @@ namespace qnn {

class QnnExecuTorchIdlWrapper {
public:
QnnExecuTorchIdlWrapper(const char* pte_path, const int method_index);
QnnExecuTorchIdlWrapper(
const char* pte_path,
const int method_index,
const int load_flags = 0);
executorch::runtime::Error execute_all(
const char* input_list_path,
const char* output_folder_path,
Expand Down Expand Up @@ -54,6 +60,10 @@ class QnnExecuTorchIdlWrapper {
std::unique_ptr<executorch::runtime::MemoryAllocator> memory_allocator_;
std::unique_ptr<executorch::runtime::HierarchicalAllocator> planned_memory_;
std::unique_ptr<executorch::runtime::MemoryManager> memory_manager_;
// LoadBackendOptionsMap only references the options, so both are declared
// before method_ to outlive it.
executorch::runtime::BackendOptions<1> qnn_backend_options_;
executorch::runtime::LoadBackendOptionsMap backend_options_map_;
std::unique_ptr<executorch::runtime::Method> method_;

std::vector<std::unique_ptr<uint8_t[]>> planned_buffers_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

const string IDL_VERSION = "0.0.0";

// Bits of the load_flags argument of load_with_options.
const long LOAD_FLAG_LPAI_ENABLE_ISLAND = 1;

// qaic couldn't understand sequence<sequence<octet>>. It needs typedef for nested structure.
typedef sequence<octet> tensor;

Expand All @@ -20,6 +23,8 @@ interface qnn_executorch : remote_handle64 {

//TODO: Support loading multiple methods since qnn_executorch_imp.cpp only use pte_path as key
long load(in string pte_path, in long method_index);
// load_flags is a bitmask of LOAD_FLAG_* values.
long load_with_options(in string pte_path, in long method_index, in long load_flags);
long execute_all(in string pte_path, in string input_list_path, in string output_folder_path, rout long num_inferences_performed, rout double total_execute_interval, rout double total_read_file_interval, rout double total_save_file_interval);
long unload(in string pte_path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,30 @@ AEEResult qnn_executorch_close(remote_handle64 h) {
return 0;
}

AEEResult qnn_executorch_load(
AEEResult qnn_executorch_load_with_options(
remote_handle64 _h,
const char* pte_path,
const int method_index) {
FARF(RUNTIME_HIGH, __func__);
const int method_index,
const int load_flags) {
FARF(RUNTIME_HIGH, "%s load_flags=0x%x", __func__, load_flags);
std::string key(pte_path);
if (loaded_models.count(key)) {
FARF(RUNTIME_HIGH, "%s is already loaded.", pte_path);
} else {
loaded_models[key] =
std::make_unique<executorch::backends::qnn::QnnExecuTorchIdlWrapper>(
pte_path, method_index);
pte_path, method_index, load_flags);
}
return 0;
}

AEEResult qnn_executorch_load(
remote_handle64 _h,
const char* pte_path,
const int method_index) {
return qnn_executorch_load_with_options(_h, pte_path, method_index, 0);
}

AEEResult qnn_executorch_execute_all(
remote_handle64 _h,
const char* pte_path,
Expand Down
Loading