Skip to content

Commit f5c21ee

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

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

backends/qualcomm/runtime/QnnManager.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#include <algorithm>
1818
#include <cstdlib>
1919
#include <cstring>
20+
#include <filesystem>
2021
#include <fstream>
22+
#include <iostream>
2123
#include <string>
2224

2325
namespace executorch {
@@ -681,6 +683,36 @@ Error QnnManager::Compile(
681683
return Error::Internal;
682684
}
683685

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

0 commit comments

Comments
 (0)