Skip to content

Commit

Permalink
Write QNN context binary to file when profiling level is detailed (py…
Browse files Browse the repository at this point in the history
…torch#6745)

Summary: Pull Request resolved: pytorch#6745

Differential Revision: D65694854
  • Loading branch information
limintang authored and facebook-github-bot committed Nov 9, 2024
1 parent b1e6617 commit 15cfbfc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 16 additions & 0 deletions backends/qualcomm/runtime/QnnManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,22 @@ Error QnnManager::Compile(
qnn_executorch_context_binary) == Error::Ok,
Internal,
"Fail to get context binary.");

// Write context binary to file for debugging purpose
auto qnn_profile_level = backend_params_ptr_->qnn_graph_ptr_->GetProfile()->GetQnnProfileLevel();
if (qnn_profile_level == QNN_PROFILE_LEVEL_DETAILED) {
const char* binary_file = "/tmp/qnn_context_binary.bin";
std::ofstream fout(binary_file, std::ios::binary);
fout.write(
reinterpret_cast<const char*>(qnn_executorch_context_binary.buffer),
qnn_executorch_context_binary.nbytes);

if (!fout) {
QNN_EXECUTORCH_LOG_WARN(
"Failed to write QNN context binary to file %s", binary_file);
}
fout.close();
}
}

return Error::Ok;
Expand Down
7 changes: 3 additions & 4 deletions backends/qualcomm/runtime/backends/QnnProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ QnnProfile::QnnProfile(
if (profile_level != QnnExecuTorchProfileLevel::kProfileOff) {
const QnnInterface& qnn_interface = implementation_.GetQnnInterface();

QnnProfile_Level_t qnnProfileLevel = 0;
if (profile_level == QnnExecuTorchProfileLevel::kProfileBasic) {
qnnProfileLevel = QNN_PROFILE_LEVEL_BASIC;
qnnProfileLevel_ = QNN_PROFILE_LEVEL_BASIC;
} else if (
profile_level == QnnExecuTorchProfileLevel::kProfileDetailed ||
profile_level == QnnExecuTorchProfileLevel::kProfileOptrace) {
qnnProfileLevel = QNN_PROFILE_LEVEL_DETAILED;
qnnProfileLevel_ = QNN_PROFILE_LEVEL_DETAILED;
} else {
QNN_EXECUTORCH_LOG_WARN("Invalid profile level");
return;
}

Qnn_ErrorHandle_t error = qnn_interface.qnn_profile_create(
backend_->GetHandle(), qnnProfileLevel, &handle_);
backend_->GetHandle(), qnnProfileLevel_, &handle_);
if (error != QNN_SUCCESS) {
QNN_EXECUTORCH_LOG_WARN(
"Failed to create profile_handle for backend "
Expand Down
5 changes: 5 additions & 0 deletions backends/qualcomm/runtime/backends/QnnProfiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ class QnnProfile {
return handle_;
}

const QnnProfile_Level_t& GetQnnProfileLevel() const {
return qnnProfileLevel_;
}

private:
Qnn_ProfileHandle_t handle_;
QnnProfile_Level_t qnnProfileLevel_ = 0;
const QnnImplementation& implementation_;
QnnBackend* backend_;
};
Expand Down

0 comments on commit 15cfbfc

Please sign in to comment.