Skip to content

Commit 15cfbfc

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 Differential Revision: D65694854
1 parent b1e6617 commit 15cfbfc

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

backends/qualcomm/runtime/QnnManager.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,22 @@ Error QnnManager::Compile(
530530
qnn_executorch_context_binary) == Error::Ok,
531531
Internal,
532532
"Fail to get context binary.");
533+
534+
// Write context binary to file for debugging purpose
535+
auto qnn_profile_level = backend_params_ptr_->qnn_graph_ptr_->GetProfile()->GetQnnProfileLevel();
536+
if (qnn_profile_level == QNN_PROFILE_LEVEL_DETAILED) {
537+
const char* binary_file = "/tmp/qnn_context_binary.bin";
538+
std::ofstream fout(binary_file, std::ios::binary);
539+
fout.write(
540+
reinterpret_cast<const char*>(qnn_executorch_context_binary.buffer),
541+
qnn_executorch_context_binary.nbytes);
542+
543+
if (!fout) {
544+
QNN_EXECUTORCH_LOG_WARN(
545+
"Failed to write QNN context binary to file %s", binary_file);
546+
}
547+
fout.close();
548+
}
533549
}
534550

535551
return Error::Ok;

backends/qualcomm/runtime/backends/QnnProfiler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ QnnProfile::QnnProfile(
2020
if (profile_level != QnnExecuTorchProfileLevel::kProfileOff) {
2121
const QnnInterface& qnn_interface = implementation_.GetQnnInterface();
2222

23-
QnnProfile_Level_t qnnProfileLevel = 0;
2423
if (profile_level == QnnExecuTorchProfileLevel::kProfileBasic) {
25-
qnnProfileLevel = QNN_PROFILE_LEVEL_BASIC;
24+
qnnProfileLevel_ = QNN_PROFILE_LEVEL_BASIC;
2625
} else if (
2726
profile_level == QnnExecuTorchProfileLevel::kProfileDetailed ||
2827
profile_level == QnnExecuTorchProfileLevel::kProfileOptrace) {
29-
qnnProfileLevel = QNN_PROFILE_LEVEL_DETAILED;
28+
qnnProfileLevel_ = QNN_PROFILE_LEVEL_DETAILED;
3029
} else {
3130
QNN_EXECUTORCH_LOG_WARN("Invalid profile level");
3231
return;
3332
}
3433

3534
Qnn_ErrorHandle_t error = qnn_interface.qnn_profile_create(
36-
backend_->GetHandle(), qnnProfileLevel, &handle_);
35+
backend_->GetHandle(), qnnProfileLevel_, &handle_);
3736
if (error != QNN_SUCCESS) {
3837
QNN_EXECUTORCH_LOG_WARN(
3938
"Failed to create profile_handle for backend "

backends/qualcomm/runtime/backends/QnnProfiler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ class QnnProfile {
2929
return handle_;
3030
}
3131

32+
const QnnProfile_Level_t& GetQnnProfileLevel() const {
33+
return qnnProfileLevel_;
34+
}
35+
3236
private:
3337
Qnn_ProfileHandle_t handle_;
38+
QnnProfile_Level_t qnnProfileLevel_ = 0;
3439
const QnnImplementation& implementation_;
3540
QnnBackend* backend_;
3641
};

0 commit comments

Comments
 (0)