提交 4b9893ca 编写于 作者: L Liangliang He

Obfuscate tuning parameters

上级 6e946c75
......@@ -96,6 +96,7 @@ cc_library(
deps = [
":opencl_headers",
"//mace/codegen:generated_opencl_dev",
"//mace/utils:utils_hdrs",
],
)
......
......@@ -5,24 +5,11 @@
#include <vector>
#include "mace/core/runtime/opencl/cl2_header.h"
#include "mace/utils/utils.h"
namespace mace {
namespace {
inline void DecryptOpenCLSource(const std::vector<unsigned char> &src,
std::vector<unsigned char> *dst) {
dst->reserve(src.size());
// Keep consistent with encrypt in python tool
const std::string decrypt_lookup_table = "Xiaomi-AI-Platform-Mace";
size_t lookup_table_size = decrypt_lookup_table.size();
for (int i = 0; i < src.size(); i++) {
dst->push_back(src[i] ^ decrypt_lookup_table[i % lookup_table_size]);
}
}
} // namespace
bool GetSourceOrBinaryProgram(const std::string &program_name,
const std::string &binary_file_name_prefix,
cl::Context &context,
......@@ -36,9 +23,9 @@ bool GetSourceOrBinaryProgram(const std::string &program_name,
return false;
}
cl::Program::Sources sources;
std::vector<unsigned char> decrypt_source;
DecryptOpenCLSource(it_source->second, &decrypt_source);
sources.push_back(std::string(decrypt_source.begin(), decrypt_source.end()));
std::string kernel_source(it_source->second.begin(), it_source->second.end());
ObfuscateString(&kernel_source);
sources.push_back(kernel_source);
*program = cl::Program(context, sources);
return true;
......
......@@ -27,6 +27,7 @@ def generate_cpp_source():
key_size, = struct.unpack("i", binary_array[idx:idx+4])
idx += 4
key, = struct.unpack(str(key_size) + "s", binary_array[idx:idx+key_size])
key = ''.join(['\\x{:02x}'.format(ord(c)) for c in key])
idx += key_size
params_size, = struct.unpack("i", binary_array[idx:idx+4])
idx += 4
......
......@@ -15,6 +15,7 @@
#include "mace/utils/logging.h"
#include "mace/utils/timer.h"
#include "mace/utils/utils.h"
namespace mace {
......@@ -42,17 +43,19 @@ class Tuner {
&param_generator,
const std::function<RetType(const std::vector<param_type> &)> &func,
Timer *timer) {
std::string obfucated_param_key = param_key;
ObfuscateString(&obfucated_param_key);
if (IsTuning() && param_generator != nullptr) {
// tune
std::vector<param_type> opt_param = default_param;
RetType res = Tune<RetType>(param_generator, func, timer, &opt_param);
VLOG(1) << "Tuning result. "
<< param_key << ": " << internal::MakeString(opt_param);
param_table_[param_key] = opt_param;
param_table_[obfucated_param_key] = opt_param;
return res;
} else {
// run
if (param_table_.find(param_key) != param_table_.end()) {
if (param_table_.find(obfucated_param_key) != param_table_.end()) {
VLOG(1) << param_key << ": "
<< internal::MakeString(param_table_[param_key]);
return func(param_table_[param_key]);
......
......@@ -48,5 +48,15 @@ inline std::string ToString(T v) {
return ss.str();
}
// ObfuscateString(ObfuscateString(str)) ==> str
inline void ObfuscateString(std::string *str) {
// Keep consistent with obfuscation in python tools
const std::string kLookupTable = "Xiaomi-AI-Platform-Mace";
size_t lookup_table_size = kLookupTable.size();
for (int i = 0; i < str->size(); i++) {
(*str)[i] = (*str)[i] ^ kLookupTable[i % lookup_table_size];
}
}
} // namespace mace
#endif // MACE_UTILS_UTILS_H_
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册