Skip to content

Commit

Permalink
Backward compatible with old QNN version (#23095)
Browse files Browse the repository at this point in the history
### Description
Make QNN EP compliable with old QNN version
  • Loading branch information
HectorSVC authored Dec 13, 2024
1 parent 01539ee commit f43f40f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
30 changes: 22 additions & 8 deletions onnxruntime/core/providers/qnn/builder/qnn_backend_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ std::unique_ptr<unsigned char[]> QnnBackendManager::GetContextBinaryBuffer(uint6
Status QnnBackendManager::GetMaxSpillFillBufferSize(unsigned char* buffer,
uint64_t buffer_length,
uint64_t& max_spill_fill_buffer_size) {
max_spill_fill_buffer_size = 0;
// spill fill starts from 2.28
#if QNN_API_VERSION_MAJOR == 2 && (QNN_API_VERSION_MINOR >= 21)
bool result = nullptr == qnn_sys_interface_.systemContextCreate ||
nullptr == qnn_sys_interface_.systemContextGetBinaryInfo ||
nullptr == qnn_sys_interface_.systemContextFree;
Expand Down Expand Up @@ -672,6 +675,10 @@ Status QnnBackendManager::GetMaxSpillFillBufferSize(unsigned char* buffer,
LOGS(*logger_, VERBOSE) << "Unknown context binary graph info version.";
}
}
#else
ORT_UNUSED_PARAMETER(buffer);
ORT_UNUSED_PARAMETER(buffer_length);
#endif

LOGS(*logger_, VERBOSE) << "Get max spill fill buffer size completed.";
return Status::OK();
Expand Down Expand Up @@ -705,16 +712,23 @@ Status QnnBackendManager::LoadCachedQnnContextFromBuffer(char* buffer, uint64_t
ORT_RETURN_IF(nullptr == binary_info, "Qnn cached binary info is nullptr.");
uint32_t graph_count = 0;
QnnSystemContext_GraphInfo_t* graphs_info = nullptr;
if (binary_info->version == QNN_SYSTEM_CONTEXT_BINARY_INFO_VERSION_3) {
graph_count = binary_info->contextBinaryInfoV3.numGraphs;
graphs_info = binary_info->contextBinaryInfoV3.graphs;
} else if (binary_info->version == QNN_SYSTEM_CONTEXT_BINARY_INFO_VERSION_2) {
graph_count = binary_info->contextBinaryInfoV2.numGraphs;
graphs_info = binary_info->contextBinaryInfoV2.graphs;
} else if (binary_info->version == QNN_SYSTEM_CONTEXT_BINARY_INFO_VERSION_1) {
if (binary_info->version == QNN_SYSTEM_CONTEXT_BINARY_INFO_VERSION_1) {
graph_count = binary_info->contextBinaryInfoV1.numGraphs;
graphs_info = binary_info->contextBinaryInfoV1.graphs;
} else {
}
#if QNN_API_VERSION_MAJOR == 2 && (QNN_API_VERSION_MINOR >= 15) // starts from 2.22
else if (binary_info->version == QNN_SYSTEM_CONTEXT_BINARY_INFO_VERSION_2) {
graph_count = binary_info->contextBinaryInfoV2.numGraphs;
graphs_info = binary_info->contextBinaryInfoV2.graphs;
}
#endif
#if QNN_API_VERSION_MAJOR == 2 && (QNN_API_VERSION_MINOR >= 21) // starts from 2.28
else if (binary_info->version == QNN_SYSTEM_CONTEXT_BINARY_INFO_VERSION_3) {
graph_count = binary_info->contextBinaryInfoV3.numGraphs;
graphs_info = binary_info->contextBinaryInfoV3.graphs;
}
#endif
else {
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Unsupported context binary info version.");
}

Expand Down
35 changes: 21 additions & 14 deletions onnxruntime/core/providers/qnn/builder/qnn_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,28 +325,35 @@ Status QnnModel::DeserializeGraphInfoFromBinaryInfo(const QnnSystemContext_Graph
Qnn_Tensor_t* output_tensors = nullptr;
uint32_t graph_input_num = 0;
uint32_t graph_output_num = 0;
if (qnn_sys_ctx_graph_info.version == QNN_SYSTEM_CONTEXT_GRAPH_INFO_VERSION_3) {
graph_name.assign(qnn_sys_ctx_graph_info.graphInfoV3.graphName);
graph_input_num = qnn_sys_ctx_graph_info.graphInfoV3.numGraphInputs;
graph_output_num = qnn_sys_ctx_graph_info.graphInfoV3.numGraphOutputs;
if (qnn_sys_ctx_graph_info.version == QNN_SYSTEM_CONTEXT_GRAPH_INFO_VERSION_1) {
graph_name.assign(qnn_sys_ctx_graph_info.graphInfoV1.graphName);
graph_input_num = qnn_sys_ctx_graph_info.graphInfoV1.numGraphInputs;
graph_output_num = qnn_sys_ctx_graph_info.graphInfoV1.numGraphOutputs;

input_tensors = qnn_sys_ctx_graph_info.graphInfoV3.graphInputs;
output_tensors = qnn_sys_ctx_graph_info.graphInfoV3.graphOutputs;
} else if (qnn_sys_ctx_graph_info.version == QNN_SYSTEM_CONTEXT_GRAPH_INFO_VERSION_2) {
input_tensors = qnn_sys_ctx_graph_info.graphInfoV1.graphInputs;
output_tensors = qnn_sys_ctx_graph_info.graphInfoV1.graphOutputs;
}
#if QNN_API_VERSION_MAJOR == 2 && (QNN_API_VERSION_MINOR >= 18) // start from 2.25
else if (qnn_sys_ctx_graph_info.version == QNN_SYSTEM_CONTEXT_GRAPH_INFO_VERSION_2) {
graph_name.assign(qnn_sys_ctx_graph_info.graphInfoV2.graphName);
graph_input_num = qnn_sys_ctx_graph_info.graphInfoV2.numGraphInputs;
graph_output_num = qnn_sys_ctx_graph_info.graphInfoV2.numGraphOutputs;

input_tensors = qnn_sys_ctx_graph_info.graphInfoV2.graphInputs;
output_tensors = qnn_sys_ctx_graph_info.graphInfoV2.graphOutputs;
} else if (qnn_sys_ctx_graph_info.version == QNN_SYSTEM_CONTEXT_GRAPH_INFO_VERSION_1) {
graph_name.assign(qnn_sys_ctx_graph_info.graphInfoV1.graphName);
graph_input_num = qnn_sys_ctx_graph_info.graphInfoV1.numGraphInputs;
graph_output_num = qnn_sys_ctx_graph_info.graphInfoV1.numGraphOutputs;
}
#endif
#if QNN_API_VERSION_MAJOR == 2 && (QNN_API_VERSION_MINOR >= 21) // start from 2.28
else if (qnn_sys_ctx_graph_info.version == QNN_SYSTEM_CONTEXT_GRAPH_INFO_VERSION_3) {
graph_name.assign(qnn_sys_ctx_graph_info.graphInfoV3.graphName);
graph_input_num = qnn_sys_ctx_graph_info.graphInfoV3.numGraphInputs;
graph_output_num = qnn_sys_ctx_graph_info.graphInfoV3.numGraphOutputs;

input_tensors = qnn_sys_ctx_graph_info.graphInfoV1.graphInputs;
output_tensors = qnn_sys_ctx_graph_info.graphInfoV1.graphOutputs;
} else {
input_tensors = qnn_sys_ctx_graph_info.graphInfoV3.graphInputs;
output_tensors = qnn_sys_ctx_graph_info.graphInfoV3.graphOutputs;
}
#endif
else {
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Unsupported context graph info version.");
}
ORT_RETURN_IF(nullptr == input_tensors, "Graph from cached context doesn't have any inputs.");
Expand Down

0 comments on commit f43f40f

Please sign in to comment.