Skip to content

Commit 0849df3

Browse files
limintangfacebook-github-bot
authored andcommitted
Write QNN context binary to file when profiling level is detailed (#6745)
Summary: Pull Request resolved: #6745 Reviewed By: billmguo Differential Revision: D65694854
1 parent d679ad7 commit 0849df3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

backends/qualcomm/runtime/QnnManager.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <algorithm>
1818
#include <cstdlib>
1919
#include <cstring>
20+
#include <filesystem>
2021
#include <fstream>
2122
#include <string>
2223

@@ -681,6 +682,36 @@ 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+
702+
std::string binary_file = std::string(et_qnn_debug_dir) + "/qnn_context_binary.bin";
703+
std::ofstream fout(binary_file, std::ios::binary);
704+
fout.write(
705+
reinterpret_cast<const char*>(qnn_executorch_context_binary.buffer),
706+
qnn_executorch_context_binary.nbytes);
707+
708+
if (!fout) {
709+
QNN_EXECUTORCH_LOG_WARN(
710+
"Failed to write QNN context binary to file %s", binary_file.c_str());
711+
}
712+
fout.close();
713+
}
714+
684715
return Error::Ok;
685716
}
686717

0 commit comments

Comments
 (0)