Skip to content

Commit

Permalink
[OpenCL]Fix opencl save program binary (#8640)
Browse files Browse the repository at this point in the history
* fix opencl save program binary test=develop

* add sn precision option test=develop
  • Loading branch information
sprouteer authored Mar 17, 2022
1 parent 4b0ae24 commit b349c55
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions lite/backends/opencl/cl_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,

// find binary
std::string bin_file = path_name.at(0) + "/" + path_name.at(1);
std::string precision_option = (precision_ == lite_api::CL_PRECISION_FP16)
? "Precision: FP16; "
: "Precision: FP32; ";

if (programs_.empty()) {
// Check whether binary exist.
Expand Down Expand Up @@ -245,12 +248,12 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,
del_tune_bin_flag_ = true;
// Jump to build from source
} else if (host::memcmp(((sn_iter->second)[0]).data(),
GetSN(build_option).data(),
GetSN(build_option).length())) {
GetSN(precision_option).data(),
GetSN(precision_option).length())) {
std::string sn_str(reinterpret_cast<char*>((sn_iter->second)[0].data()),
(sn_iter->second)[0].size());
LOG(INFO) << "\nSN required: " << GetSN(build_option)
<< "\tsize: " << GetSN(build_option).length()
LOG(INFO) << "\nSN required: " << GetSN(precision_option)
<< "\tsize: " << GetSN(precision_option).length()
<< "\nSN in bin file: " << sn_str
<< "\tsize: " << ((sn_iter->second)[0]).size();
LOG(WARNING) << "The precompiled OpenCL binary[" << bin_file
Expand Down Expand Up @@ -320,34 +323,6 @@ bool CLRuntime::CheckFromSource(const std::string& file_name,
<< " --- ";
#endif
BuildProgram(program, build_option);

// Keep built program binary
if (binary_path_name_.size() == 2) {
cl_int status{CL_SUCCESS};
// 1. Query binary (PTX file) size
size_t bin_size;
status = program->getInfo(CL_PROGRAM_BINARY_SIZES, &bin_size);
CL_CHECK_FATAL_SOLID(status);
// 2. Read binary (PTX file) to memory buffer
cl::Program::Binaries binary;
binary.resize(1);
binary[0].resize(bin_size);
auto buf = binary[0].data();
status = program->getInfo(CL_PROGRAM_BINARIES, &buf);
CL_CHECK_FATAL_SOLID(status);
programs_precompiled_binary_[program_key] = binary;
#ifdef LITE_WITH_LOG
VLOG(3) << " --- binary size: " << bin_size << " ---";
#endif
if (programs_precompiled_binary_.find(sn_key_) ==
programs_precompiled_binary_.end()) {
// add identifier
std::string sn = GetSN(build_option);
std::vector<unsigned char> sn_info(sn.data(), sn.data() + sn.size());
programs_precompiled_binary_[sn_key_] = {sn_info};
}
}

programs_[program_key] = std::move(ptr);

return true;
Expand Down Expand Up @@ -392,6 +367,38 @@ void CLRuntime::SaveProgram() {
if (IsFileExists(binary_file)) {
LOG(INFO) << "OpenCL Program existed:" << binary_file;
} else {
for (auto& program_id : programs_) {
// Keep built program binary
if (binary_path_name_.size() == 2) {
cl_int status{CL_SUCCESS};
// 1. Query binary (PTX file) size
size_t bin_size;
cl::Program program = *(program_id.second);
status = program.getInfo(CL_PROGRAM_BINARY_SIZES, &bin_size);
CL_CHECK_FATAL_SOLID(status);
// 2. Read binary (PTX file) to memory buffer
cl::Program::Binaries binary;
binary.resize(1);
binary[0].resize(bin_size);
auto buf = binary[0].data();
status = program.getInfo(CL_PROGRAM_BINARIES, &buf);
CL_CHECK_FATAL_SOLID(status);
programs_precompiled_binary_[program_id.first] = binary;
#ifdef LITE_WITH_LOG
VLOG(3) << " --- binary size: " << bin_size << " ---";
#endif
if (programs_precompiled_binary_.find(sn_key_) ==
programs_precompiled_binary_.end()) {
// add identifier
std::string precision_option =
(precision_ == lite_api::CL_PRECISION_FP16) ? "Precision: FP16; "
: "Precision: FP32; ";
std::string sn = GetSN(precision_option);
std::vector<unsigned char> sn_info(sn.data(), sn.data() + sn.size());
programs_precompiled_binary_[sn_key_] = {sn_info};
}
}
}
bool ret = Serialize(binary_file, programs_precompiled_binary_);
if (!ret) {
LOG(WARNING) << "Serialize failed for opencl binary_file:" << binary_file;
Expand Down

0 comments on commit b349c55

Please sign in to comment.