Skip to content

Commit 655b391

Browse files
limintangfacebook-github-bot
authored andcommitted
Write QNN context binary to file when env variable ET_QNN_DEBUG_DIR is set (#6745)
Summary: Pull Request resolved: #6745 Reviewed By: billmguo Differential Revision: D65694854
1 parent c726a9b commit 655b391

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

backends/qualcomm/runtime/QnnManager.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#include <executorch/extension/tensor/tensor.h>
1717
#include <algorithm>
1818
#include <cstdlib>
19-
#include <cstring>
19+
20+
#include <filesystem>
2021
#include <fstream>
2122
#include <string>
2223

@@ -681,6 +682,39 @@ Error QnnManager::Compile(
681682
return Error::Internal;
682683
}
683684

685+
// Write context binary to file for debugging purpose
686+
const char* et_qnn_debug_dir = getenv("ET_QNN_DEBUG_DIR");
687+
if (et_qnn_debug_dir != nullptr) {
688+
namespace fs = std::filesystem;
689+
690+
fs::path debug_dir = et_qnn_debug_dir;
691+
if (!fs::exists(debug_dir) || !fs::is_directory(debug_dir)) {
692+
QNN_EXECUTORCH_LOG_WARN(
693+
"ET_QNN_DEBUG_DIR %s is not a valid directory", et_qnn_debug_dir);
694+
return Error::Ok;
695+
}
696+
697+
QnnExecuTorchContextBinary qnn_executorch_context_binary{nullptr, 0};
698+
if (GetContextBinary(qnn_executorch_context_binary) != Error::Ok) {
699+
return Error::Ok;
700+
}
701+
auto binary_info = GetBinaryInfo(qnn_executorch_context_binary.buffer);
702+
703+
std::string binary_file =
704+
std::string(et_qnn_debug_dir) + "/qnn_context_binary.bin";
705+
std::ofstream fout(binary_file, std::ios::binary);
706+
707+
fout.write(
708+
reinterpret_cast<const char*>(binary_info->data()->data()),
709+
binary_info->data()->size());
710+
711+
if (!fout) {
712+
QNN_EXECUTORCH_LOG_WARN(
713+
"Failed to write QNN context binary to file %s", binary_file.c_str());
714+
}
715+
fout.close();
716+
}
717+
684718
return Error::Ok;
685719
}
686720

0 commit comments

Comments
 (0)