Skip to content

Commit b3d2f63

Browse files
[OpenCL] Delete armv7 binary when running armv8, vice versa (#5804)
1 parent 5243127 commit b3d2f63

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

lite/backends/opencl/cl_runtime.cc

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,
216216
"from source.";
217217
} else {
218218
LOG(INFO) << "Load opencl kernel bin file: " << bin_file;
219-
ret = Deserialize(bin_file, &programs_precompiled_binary_);
220-
CHECK(ret) << "Deserialize failed.";
219+
bool success = Deserialize(bin_file, &programs_precompiled_binary_);
220+
CHECK(success) << "Deserialize failed!";
221221

222222
VLOG(3) << "sn_key: " << sn_key_;
223223
VLOG(3) << "map size: " << programs_precompiled_binary_.size();
@@ -237,9 +237,12 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,
237237
} else if (host::memcmp(((sn_iter->second)[0]).data(),
238238
GetSN(build_option).data(),
239239
GetSN(build_option).length())) {
240-
LOG(INFO) << "size of sn_info: " << ((sn_iter->second)[0]).size()
241-
<< "\nsize of GetSN: " << GetSN(build_option).length()
242-
<< "\nGetSN: " << GetSN(build_option);
240+
std::string sn_str(reinterpret_cast<char*>((sn_iter->second)[0].data()),
241+
(sn_iter->second)[0].size());
242+
LOG(INFO) << "\nSN required: " << GetSN(build_option)
243+
<< "\tsize: " << GetSN(build_option).length()
244+
<< "\nSN in bin file: " << sn_str
245+
<< "\tsize: " << ((sn_iter->second)[0]).size();
243246
LOG(WARNING) << "The precompiled OpenCL binary[" << bin_file
244247
<< "] is invalid!";
245248
delete_bin_flag = true;
@@ -250,13 +253,12 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,
250253
#endif
251254
// loop all programs of the binary file
252255
cl_int status{CL_SUCCESS};
253-
const std::vector<cl::Device> device{*device_};
254256
for (auto& ins : programs_precompiled_binary_) {
255257
std::string prog_key = ins.first;
256258
if (prog_key == sn_key_) continue; // skip sn_key
257259

258260
cl::Program program(
259-
*context_, {*device_}, ins.second, nullptr, &status);
261+
context(), {device()}, ins.second, nullptr, &status);
260262
CL_CHECK_FATAL_SOLID(status);
261263
auto pos_start = prog_key.find_first_of("-D");
262264
std::string options = prog_key.substr(pos_start);
@@ -281,6 +283,8 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,
281283

282284
if (delete_bin_flag) {
283285
remove_file(bin_file);
286+
programs_precompiled_binary_.clear();
287+
programs_.clear();
284288
}
285289
} else if (gotten_bin_flag_) {
286290
// This case happened when model has updated. Bin file should be updated
@@ -296,7 +300,7 @@ bool CLRuntime::CheckFromPrecompiledBinary(const std::string& program_key,
296300
bool CLRuntime::CheckFromSource(const std::string& file_name,
297301
const std::string& program_key,
298302
const std::string& build_option) {
299-
auto ptr = CreateProgramFromSource(*context_, file_name);
303+
auto ptr = CreateProgramFromSource(context(), file_name);
300304
auto program = ptr.get();
301305
#ifdef LITE_WITH_LOG
302306
VLOG(3) << " --- begin build program from source -> " << program_key
@@ -351,7 +355,7 @@ std::unique_ptr<cl::Program> CLRuntime::CreateProgramFromSource(
351355
}
352356

353357
bool CLRuntime::BuildProgram(cl::Program* program, const std::string& options) {
354-
status_ = program->build({*device_}, options.c_str());
358+
status_ = program->build({device()}, options.c_str());
355359
CL_CHECK_ERROR(status_);
356360

357361
if (status_ != CL_SUCCESS) {
@@ -376,6 +380,13 @@ void CLRuntime::SaveProgram() {
376380
bool ret = Serialize(binary_file, programs_precompiled_binary_);
377381
CHECK(ret) << "Serialize failed for opencl binary_file:" << binary_file;
378382
#ifdef LITE_WITH_LOG
383+
if (programs_precompiled_binary_.find(sn_key_) !=
384+
programs_precompiled_binary_.end()) {
385+
std::string sn_str(reinterpret_cast<char*>(
386+
programs_precompiled_binary_[sn_key_][0].data()),
387+
programs_precompiled_binary_[sn_key_][0].size());
388+
LOG(INFO) << "SN stored: " << sn_str;
389+
}
379390
LOG(INFO) << "Programs have been serialized to disk successfully. File: "
380391
<< binary_file;
381392
#endif
@@ -471,14 +482,31 @@ std::string CLRuntime::GetSN(const std::string options) {
471482
// Identifier info(Serial Number) for each binary file: lite version,
472483
// build options, platform info, device version, driver version
473484
STL::stringstream sn_ss;
474-
std::string lite_version = lite::version() + "; ";
475-
std::string platform_info = platform_->getInfo<CL_PLATFORM_NAME>() + ", " +
476-
platform_->getInfo<CL_PLATFORM_PROFILE>() + "; ";
477-
std::string device_version = device_->getInfo<CL_DEVICE_VERSION>() + "; ";
478-
std::string driver_version = device_->getInfo<CL_DRIVER_VERSION>() + "; ";
479-
std::string place_holder{"place_holder"};
480-
sn_ss << lite_version << options << platform_info << device_version
481-
<< driver_version << place_holder;
485+
486+
const std::string aarch =
487+
#if defined(__aarch64__)
488+
"android_armv8";
489+
#else
490+
"android_armv7";
491+
#endif
492+
#if defined(_WIN64)
493+
"win64";
494+
#elif defined(_WIN32)
495+
"win32";
496+
#endif
497+
498+
const std::string aarch_info = aarch + "; ";
499+
const std::string lite_version = lite::version() + "; ";
500+
const std::string platform_info =
501+
platform_->getInfo<CL_PLATFORM_NAME>() + ", " +
502+
platform_->getInfo<CL_PLATFORM_PROFILE>() + "; ";
503+
const std::string device_version =
504+
device_->getInfo<CL_DEVICE_VERSION>() + "; ";
505+
const std::string driver_version =
506+
device_->getInfo<CL_DRIVER_VERSION>() + "; ";
507+
const std::string place_holder{"place_holder"};
508+
sn_ss << aarch_info << lite_version << options << platform_info
509+
<< device_version << driver_version << place_holder;
482510
return sn_ss.str();
483511
}
484512

0 commit comments

Comments
 (0)