diff --git a/mace/core/BUILD b/mace/core/BUILD index 48d746169832be86f5b477a88aca2d0377ce1723..1fbeed2f2da0ba71dc8c763b88f4802420941424 100644 --- a/mace/core/BUILD +++ b/mace/core/BUILD @@ -96,6 +96,7 @@ cc_library( deps = [ ":opencl_headers", "//mace/codegen:generated_opencl_dev", + "//mace/utils:utils_hdrs", ], ) diff --git a/mace/core/runtime/opencl/opencl_development.cc b/mace/core/runtime/opencl/opencl_development.cc index a13bb9a34b078d6deaebd29e555bb454e7cf92ea..2b0fd27e13b5315ddc3ae70f60e50e1f1796dbab 100644 --- a/mace/core/runtime/opencl/opencl_development.cc +++ b/mace/core/runtime/opencl/opencl_development.cc @@ -5,24 +5,11 @@ #include #include "mace/core/runtime/opencl/cl2_header.h" +#include "mace/utils/utils.h" namespace mace { -namespace { -inline void DecryptOpenCLSource(const std::vector &src, - std::vector *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,8 @@ bool GetSourceOrBinaryProgram(const std::string &program_name, return false; } cl::Program::Sources sources; - std::vector 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()); + sources.push_back(ObfuscateString(kernel_source)); *program = cl::Program(context, sources); return true; diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index 8e761a3e16867813b6fb514df07daa033c39485d..4d799cacff445a795406c53c0efe4b68655a6115 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -124,6 +124,9 @@ cl::CommandQueue &OpenCLRuntime::command_queue() { return *command_queue_; } std::string OpenCLRuntime::GenerateCLBinaryFilenamePrefix( const std::string &filename_msg) { +#ifdef MACE_OBFUSCATE_LITERALS + return ObfuscateSymbolWithCollision(filename_msg); +#else std::string filename_prefix = filename_msg; for (auto it = filename_prefix.begin(); it != filename_prefix.end(); ++it) { if (*it == ' ' || *it == '-' || *it == '=') { @@ -131,6 +134,7 @@ std::string OpenCLRuntime::GenerateCLBinaryFilenamePrefix( } } return filename_prefix; +#endif } extern bool GetSourceOrBinaryProgram(const std::string &program_name, @@ -219,7 +223,7 @@ cl::Kernel OpenCLRuntime::BuildKernel( program = built_program_it->second; } else { std::string binary_file_name_prefix = - GenerateCLBinaryFilenamePrefix(built_program_key); + GenerateCLBinaryFilenamePrefix(built_program_key); this->BuildProgram(program_name, binary_file_name_prefix, build_options_str, &program); built_program_map_.emplace(built_program_key, program); diff --git a/mace/kernels/opencl/addn.cc b/mace/kernels/opencl/addn.cc index 360e2ba9e5a504927c072cf4b106c5ba65022172..ce09002c4282dbc44e3429e63cb64c5966f276ec 100644 --- a/mace/kernels/opencl/addn.cc +++ b/mace/kernels/opencl/addn.cc @@ -31,10 +31,12 @@ static void AddN(const std::vector &input_tensors, auto runtime = OpenCLRuntime::Global(); std::set built_options; auto dt = DataTypeToEnum::value; + std::string kernel_name = MACE_KERNRL_NAME("addn"); + built_options.emplace("-Daddn=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); built_options.emplace("-DINPUT_NUM=" + ToString(input_tensors.size())); - auto addn_kernel = runtime->BuildKernel("addn", "addn", built_options); + auto addn_kernel = runtime->BuildKernel("addn", kernel_name, built_options); uint32_t idx = 0; for (auto input : input_tensors) { diff --git a/mace/kernels/opencl/batch_norm_opencl.cc b/mace/kernels/opencl/batch_norm_opencl.cc index 76b74906b72601dc613827ef8e88d0d5e1a135f8..2fc8f79f4afc9c2866c8b654025a507207e085a1 100644 --- a/mace/kernels/opencl/batch_norm_opencl.cc +++ b/mace/kernels/opencl/batch_norm_opencl.cc @@ -34,6 +34,8 @@ void BatchNormFunctor::operator()( auto runtime = OpenCLRuntime::Global(); std::set built_options; auto dt = DataTypeToEnum::value; + std::string kernel_name = MACE_KERNRL_NAME("batch_norm"); + built_options.emplace("-Dbatch_norm=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); if (folded_constant_) { @@ -42,7 +44,7 @@ void BatchNormFunctor::operator()( if (fused_relu_) { built_options.emplace("-DFUSED_RELU"); } - auto bm_kernel = runtime->BuildKernel("batch_norm", "batch_norm", built_options); + auto bm_kernel = runtime->BuildKernel("batch_norm", kernel_name, built_options); uint32_t idx = 0; bm_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/bias_add_opencl.cc b/mace/kernels/opencl/bias_add_opencl.cc index 6d9aa4a95c57489dc639e0df11356ab5ec6f6083..ce896a642e6713f8b339c630861bea4ede34c47c 100644 --- a/mace/kernels/opencl/bias_add_opencl.cc +++ b/mace/kernels/opencl/bias_add_opencl.cc @@ -31,9 +31,11 @@ void BiasAddFunctor::operator()( auto runtime = OpenCLRuntime::Global(); std::set built_options; auto dt = DataTypeToEnum::value; + std::string kernel_name = MACE_KERNRL_NAME("bias_add"); + built_options.emplace("-Dbias_add=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); - auto bias_kernel = runtime->BuildKernel("bias_add", "bias_add", built_options); + auto bias_kernel = runtime->BuildKernel("bias_add", kernel_name, built_options); const uint32_t kwg_size = runtime->GetKernelMaxWorkGroupSize(bias_kernel); const std::vector lws = {8, 16, 8}; diff --git a/mace/kernels/opencl/buffer_to_image.cc b/mace/kernels/opencl/buffer_to_image.cc index cfd92d3f083ebf318c7c8bff9884d77e2725686c..d69cce54e482b728ec3b24212f535a027f6227a1 100644 --- a/mace/kernels/opencl/buffer_to_image.cc +++ b/mace/kernels/opencl/buffer_to_image.cc @@ -25,15 +25,6 @@ void BufferToImageFunctor::operator()(Tensor *buffer, buffer->Resize(image->shape()); } - std::set built_options; - if (buffer->dtype() == image->dtype()) { - built_options.emplace("-DDATA_TYPE=" + DtToCLDt(DataTypeToEnum::value)); - built_options.emplace("-DCMD_DATA_TYPE=" + DtToCLCMDDt(DataTypeToEnum::value)); - } else { - built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(DataTypeToEnum::value)); - built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(DataTypeToEnum::value)); - } - auto runtime = OpenCLRuntime::Global(); string kernel_name; switch (type) { case FILTER: @@ -46,8 +37,21 @@ void BufferToImageFunctor::operator()(Tensor *buffer, kernel_name = i2b_ ? "arg_image_to_buffer" : "arg_buffer_to_image"; break; } + string obfuscated_kernel_name = MACE_KERNRL_NAME(kernel_name); + std::set built_options; + std::stringstream kernel_name_ss; + kernel_name_ss << "-D" << kernel_name << "=" << obfuscated_kernel_name; + built_options.emplace(kernel_name_ss.str()); + if (buffer->dtype() == image->dtype()) { + built_options.emplace("-DDATA_TYPE=" + DtToCLDt(DataTypeToEnum::value)); + built_options.emplace("-DCMD_DATA_TYPE=" + DtToCLCMDDt(DataTypeToEnum::value)); + } else { + built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(DataTypeToEnum::value)); + built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(DataTypeToEnum::value)); + } + auto runtime = OpenCLRuntime::Global(); auto b2f_kernel = runtime->BuildKernel("buffer_to_image", - kernel_name, + obfuscated_kernel_name, built_options); uint32_t idx = 0; diff --git a/mace/kernels/opencl/concat.cc b/mace/kernels/opencl/concat.cc index 8f8d38801200c862b433291bf280942222679753..6c557c04d56cbd2520b95ee48d699c7b592974fe 100644 --- a/mace/kernels/opencl/concat.cc +++ b/mace/kernels/opencl/concat.cc @@ -25,6 +25,8 @@ static void Concat2(const Tensor *input0, auto runtime = OpenCLRuntime::Global(); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("concat_channel"); + built_options.emplace("-Dconcat_channel=" + kernel_name); if (input0->dtype() == output->dtype()) { built_options.emplace("-DDATA_TYPE=" + DtToCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToCLCMDDt(dt)); @@ -35,7 +37,7 @@ static void Concat2(const Tensor *input0, if (input0->dim(3) % 4 == 0) { built_options.emplace("-DDIVISIBLE_FOUR"); } - auto concat_kernel = runtime->BuildKernel("concat", "concat_channel", built_options); + auto concat_kernel = runtime->BuildKernel("concat", kernel_name, built_options); uint32_t idx = 0; concat_kernel.setArg(idx++, *(static_cast(input0->buffer()))); diff --git a/mace/kernels/opencl/conv_2d_opencl_1x1.cc b/mace/kernels/opencl/conv_2d_opencl_1x1.cc index e160ce6142be20a21922075b01996e2d369abb6a..525731eaa317b8973ca2676ff84cae446d208022 100644 --- a/mace/kernels/opencl/conv_2d_opencl_1x1.cc +++ b/mace/kernels/opencl/conv_2d_opencl_1x1.cc @@ -36,6 +36,8 @@ void Conv1x1(const Tensor *input, MACE_CHECK(input_batch == batch); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("conv_2d_1x1"); + built_options.emplace("-Dconv_2d_1x1=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); built_options.emplace("-DSTRIDE=" + ToString(stride)); @@ -47,7 +49,7 @@ void Conv1x1(const Tensor *input, } auto runtime = OpenCLRuntime::Global(); - auto conv_2d_kernel = runtime->BuildKernel("conv_2d_1x1", "conv_2d_1x1", built_options); + auto conv_2d_kernel = runtime->BuildKernel("conv_2d_1x1", kernel_name, built_options); uint32_t idx = 0; conv_2d_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/conv_2d_opencl_3x3.cc b/mace/kernels/opencl/conv_2d_opencl_3x3.cc index e42060527d44700e47b58be0f2b63a23b3a6d990..93156cb608c21eca7074a9d1eee26f6a7bfadcea 100644 --- a/mace/kernels/opencl/conv_2d_opencl_3x3.cc +++ b/mace/kernels/opencl/conv_2d_opencl_3x3.cc @@ -28,6 +28,8 @@ static void Conv2d3x3S12(const Tensor *input, const Tensor *filter, const index_t width_blocks = RoundUpDiv(width); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("conv_2d_3x3"); + built_options.emplace("-Dconv_2d_3x3=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); built_options.emplace(bias != nullptr ? "-DBIAS" : ""); @@ -37,7 +39,7 @@ static void Conv2d3x3S12(const Tensor *input, const Tensor *filter, } auto runtime = OpenCLRuntime::Global(); - auto conv_2d_kernel = runtime->BuildKernel("conv_2d_3x3", "conv_2d_3x3", built_options); + auto conv_2d_kernel = runtime->BuildKernel("conv_2d_3x3", kernel_name, built_options); uint32_t idx = 0; conv_2d_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/conv_2d_opencl_general.cc b/mace/kernels/opencl/conv_2d_opencl_general.cc index 6b6746f37cefd1c10800d300621de08ddf3dedf5..7d42ab948b6e0fc4658ff5f6d9f3a58d31c8fcd8 100644 --- a/mace/kernels/opencl/conv_2d_opencl_general.cc +++ b/mace/kernels/opencl/conv_2d_opencl_general.cc @@ -28,6 +28,8 @@ void Conv2dOpencl(const Tensor *input, const Tensor *filter, const index_t width_blocks = RoundUpDiv4(width); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("conv_2d"); + built_options.emplace("-Dconv_2d=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); built_options.emplace(bias != nullptr ? "-DBIAS" : ""); @@ -37,7 +39,7 @@ void Conv2dOpencl(const Tensor *input, const Tensor *filter, } auto runtime = OpenCLRuntime::Global(); - auto conv_2d_kernel = runtime->BuildKernel("conv_2d", "conv_2d", built_options); + auto conv_2d_kernel = runtime->BuildKernel("conv_2d", kernel_name, built_options); uint32_t idx = 0; conv_2d_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/depthwise_conv_opencl_3x3.cc b/mace/kernels/opencl/depthwise_conv_opencl_3x3.cc index 2300e0f04c7fa2022464d2ee8cfe8e12f696a219..cc9289217b2c66d03396e1daf160a479c56ebe80 100644 --- a/mace/kernels/opencl/depthwise_conv_opencl_3x3.cc +++ b/mace/kernels/opencl/depthwise_conv_opencl_3x3.cc @@ -33,10 +33,12 @@ static void InnerDepthwiseConvOpenclK3x3S12(const Tensor *input, auto runtime = OpenCLRuntime::Global(); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("depthwise_conv_3x3"); + built_options.emplace("-Ddepthwise_conv_3x3=" + kernel_name); built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(input->dtype())); built_options.emplace(stride == 1 ? "-DSTRIDE_1" : ""); built_options.emplace(bias != nullptr ? "-DBIAS" : ""); - auto conv_kernel = runtime->BuildKernel("depthwise_conv_3x3", "depthwise_conv_3x3", built_options); + auto conv_kernel = runtime->BuildKernel("depthwise_conv_3x3", kernel_name, built_options); uint32_t idx = 0; conv_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/helper.h b/mace/kernels/opencl/helper.h index 70d74e5886c61a50c0a5fb684d02ecc6e00403cd..c0752f4587ad3fe6ba4bf8e021d2bce161a3bf5f 100644 --- a/mace/kernels/opencl/helper.h +++ b/mace/kernels/opencl/helper.h @@ -5,6 +5,7 @@ #ifndef MACE_KERNELS_OPENCL_HELPER_H_ #define MACE_KERNELS_OPENCL_HELPER_H_ #include "mace/core/types.h" +#include "mace/utils/utils.h" namespace mace { namespace kernels { diff --git a/mace/kernels/opencl/pooling_opencl.cc b/mace/kernels/opencl/pooling_opencl.cc index 0d1676337e26897b3e574085bb64212525edd66f..42b62fa2e731e5193131234ddf6c37f278cd0750 100644 --- a/mace/kernels/opencl/pooling_opencl.cc +++ b/mace/kernels/opencl/pooling_opencl.cc @@ -28,6 +28,8 @@ static void Pooling(const Tensor *input, auto runtime = OpenCLRuntime::Global(); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("pooling"); + built_options.emplace("-Dpooling=" + kernel_name); if (type == MAX && input->dtype() == output->dtype()) { built_options.emplace("-DDATA_TYPE=" + DtToCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToCLCMDDt(dt)); @@ -39,7 +41,7 @@ static void Pooling(const Tensor *input, if (type == AVG) { built_options.emplace("-DPOOL_AVG"); } - auto pooling_kernel = runtime->BuildKernel("pooling", "pooling", built_options); + auto pooling_kernel = runtime->BuildKernel("pooling", kernel_name, built_options); uint32_t idx = 0; pooling_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/relu_opencl.cc b/mace/kernels/opencl/relu_opencl.cc index 180b7317b01a6217c0b37264aa8d3ecc757a1592..2a993b90278e4d6014779e8f72245cf4dbb10a92 100644 --- a/mace/kernels/opencl/relu_opencl.cc +++ b/mace/kernels/opencl/relu_opencl.cc @@ -32,13 +32,17 @@ void ReluFunctor::operator()(const Tensor *input, built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); cl::Kernel relu_kernel; if (max_limit_ < 0) { - relu_kernel = runtime->BuildKernel("relu", "relu", built_options); + std::string kernel_name = MACE_KERNRL_NAME("relu"); + built_options.emplace("-Drelu=" + kernel_name); + relu_kernel = runtime->BuildKernel("relu", kernel_name, built_options); uint32_t idx = 0; relu_kernel.setArg(idx++, *(static_cast(input->buffer()))); relu_kernel.setArg(idx++, *(static_cast(output->buffer()))); } else { - relu_kernel = runtime->BuildKernel("relu", "relux", built_options); + std::string kernel_name = MACE_KERNRL_NAME("relux"); + built_options.emplace("-Drelux=" + kernel_name); + relu_kernel = runtime->BuildKernel("relu", kernel_name, built_options); uint32_t idx = 0; relu_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/resize_bilinear_opencl.cc b/mace/kernels/opencl/resize_bilinear_opencl.cc index 3496a56332c02eb205eff48b14a4a3060d2c1f94..3180416a9c844523bd146ee535bafce7ea06d7c0 100644 --- a/mace/kernels/opencl/resize_bilinear_opencl.cc +++ b/mace/kernels/opencl/resize_bilinear_opencl.cc @@ -40,10 +40,12 @@ void ResizeBilinearFunctor::operator()( auto runtime = OpenCLRuntime::Global(); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("resize_bilinear_nocache"); + built_options.emplace("-Dresize_bilinear_nocache=" + kernel_name); auto dt = DataTypeToEnum::value; built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); - auto rb_kernel = runtime->BuildKernel("resize_bilinear", "resize_bilinear_nocache", built_options); + auto rb_kernel = runtime->BuildKernel("resize_bilinear", kernel_name, built_options); uint32_t idx = 0; rb_kernel.setArg(idx++, *(static_cast(input->buffer()))); diff --git a/mace/kernels/opencl/softmax_opencl.cc b/mace/kernels/opencl/softmax_opencl.cc index 407de210abde1bfd38c3bde06d9c294a0d98f587..506f9b810d73cb7714e4374c24185e6dc63feea3 100644 --- a/mace/kernels/opencl/softmax_opencl.cc +++ b/mace/kernels/opencl/softmax_opencl.cc @@ -26,10 +26,12 @@ void SoftmaxFunctor::operator()(const Tensor *logits, auto runtime = OpenCLRuntime::Global(); std::set built_options; + std::string kernel_name = MACE_KERNRL_NAME("softmax"); + built_options.emplace("-Dsoftmax=" + kernel_name); auto dt = DataTypeToEnum::value; built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); - cl::Kernel softmax_kernel = runtime->BuildKernel("softmax", "softmax", built_options); + cl::Kernel softmax_kernel = runtime->BuildKernel("softmax", kernel_name, built_options); uint32_t idx = 0; softmax_kernel.setArg(idx++, *(static_cast(logits->buffer()))); diff --git a/mace/kernels/opencl/space_to_batch_opencl.cc b/mace/kernels/opencl/space_to_batch_opencl.cc index 4a8fb2b8810bb620e768d5d8364b0f6b2206a2d6..0b53afeb89451ba3b9e496e9ad0fe57d2c145c2b 100644 --- a/mace/kernels/opencl/space_to_batch_opencl.cc +++ b/mace/kernels/opencl/space_to_batch_opencl.cc @@ -30,8 +30,12 @@ void SpaceToBatchFunctor::operator()(Tensor *space_tensor batch_tensor->ResizeImage(output_shape, output_image_shape); kernel_name = "space_to_batch"; } + std::string obfuscated_kernel_name = MACE_KERNRL_NAME(kernel_name); auto runtime = OpenCLRuntime::Global(); std::set built_options; + std::stringstream kernel_name_ss; + kernel_name_ss << "-D" << kernel_name << "=" << obfuscated_kernel_name; + built_options.emplace(kernel_name_ss.str()); built_options.emplace("-DDATA_TYPE=" + DtToCLDt(DataTypeToEnum::value)); built_options.emplace("-DCMD_DATA_TYPE=" + DtToCLCMDDt(DataTypeToEnum::value)); auto s2b_kernel = runtime->BuildKernel("space_to_batch", kernel_name, built_options); diff --git a/mace/python/tools/binary_codegen.py b/mace/python/tools/binary_codegen.py index cb1b1280df96b9974c967659afb929f04d537b9e..ed516f8c4edecce3e97cb33d0925abe934bce5f4 100644 --- a/mace/python/tools/binary_codegen.py +++ b/mace/python/tools/binary_codegen.py @@ -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([ "\\%03o" % ord(c) if not c.isalnum() else c for c in key]) idx += key_size params_size, = struct.unpack("i", binary_array[idx:idx+4]) idx += 4 diff --git a/mace/utils/BUILD b/mace/utils/BUILD index a9ba154d21534d20ec3d21e3f9bb77a62c34953b..7f5079e209615a027fb8b2ea5fe69f128bce22fd 100644 --- a/mace/utils/BUILD +++ b/mace/utils/BUILD @@ -82,3 +82,21 @@ cc_library( ":utils_hdrs", ], ) + +cc_test( + name = "utils_test", + testonly = 1, + srcs = [ + "utils_test.cc", + ], + linkopts = if_android([ + "-pie", + "-lm", + ]), + linkstatic = 1, + deps = [ + ":utils_hdrs", + "@gtest//:gtest", + "@gtest//:gtest_main", + ], +) diff --git a/mace/utils/tuner.h b/mace/utils/tuner.h index 6296934dbe310fec2baa4f79da468bd5f187a40e..b11affd324aa46f78e460547bbc60a24e7d96c82 100644 --- a/mace/utils/tuner.h +++ b/mace/utils/tuner.h @@ -15,6 +15,10 @@ #include "mace/utils/logging.h" #include "mace/utils/timer.h" +#include "mace/utils/utils.h" + +namespace { +} // namespace namespace mace { @@ -42,22 +46,23 @@ class Tuner { ¶m_generator, const std::function &)> &func, Timer *timer) { + std::string obfucated_param_key = MACE_OBFUSCATE_SYMBOLS(param_key); if (IsTuning() && param_generator != nullptr) { // tune std::vector opt_param = default_param; RetType res = Tune(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]); + << internal::MakeString(param_table_[obfucated_param_key]); + return func(param_table_[obfucated_param_key]); } else { - LOG(WARNING) << "Fallback to default parameter: " << param_key; + LOG(WARNING) << "Fallback to default parameter: " << param_key << ", table size: " << param_table_.size(); return func(default_param); } } @@ -85,7 +90,8 @@ class Tuner { int32_t key_size = kp.first.size(); ofs.write(reinterpret_cast(&key_size), sizeof(key_size)); ofs.write(kp.first.c_str(), key_size); - VLOG(1) << kp.first.c_str(); + VLOG(1) << "Write tuning param: " + << MACE_OBFUSCATE_SYMBOLS(kp.first.c_str()); auto ¶ms = kp.second; int32_t params_size = params.size() * sizeof(param_type); diff --git a/mace/utils/utils.h b/mace/utils/utils.h index a8b13828de5208047292218a27d76e3f328923b7..606f21f21ce02d24d200113812303a5b47a1922c 100644 --- a/mace/utils/utils.h +++ b/mace/utils/utils.h @@ -48,5 +48,44 @@ inline std::string ToString(T v) { return ss.str(); } +inline std::string ObfuscateString(const std::string &src, + const std::string &lookup_table) { + std::string dest; + dest.resize(src.size()); + for (int i = 0; i < src.size(); i++) { + dest[i] = src[i] ^ lookup_table[i % lookup_table.size()]; + } + return std::move(dest); +} + +// ObfuscateString(ObfuscateString(str)) ==> str +inline std::string ObfuscateString(const std::string &src) { + // Keep consistent with obfuscation in python tools + return ObfuscateString(src, "Xiaomi-AI-Platform-Mace"); +} + +// Obfuscate synbol or path string +inline std::string ObfuscateSymbolWithCollision(const std::string &src) { + std::string dest = ObfuscateString(src); + const std::string encode_dict = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + for (int i = 0; i < src.size(); i++) { + dest[i] = encode_dict[dest[i] % encode_dict.size()]; + } + return std::move(dest); +} + +#ifdef MACE_OBFUSCATE_LITERALS +#define MACE_OBFUSCATE_STRING(str) ObfuscateString(str) +// This table is delibratedly selected to avoid '\0' in genereated literal +#define MACE_OBFUSCATE_SYMBOLS(str) ObfuscateString(str, "!@#$%^&*()+?") +// OpenCL will report error if there is name collision +#define MACE_KERNRL_NAME(name) ObfuscateSymbolWithCollision(name) +#else +#define MACE_OBFUSCATE_STRING(str) (str) +#define MACE_OBFUSCATE_SYMBOLS(str) (str) +#define MACE_KERNRL_NAME(name) (name) +#endif + } // namespace mace #endif // MACE_UTILS_UTILS_H_ diff --git a/mace/utils/utils_test.cc b/mace/utils/utils_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..0e57e929759aca6f23642ab96c158ef48baf87d1 --- /dev/null +++ b/mace/utils/utils_test.cc @@ -0,0 +1,84 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// +#include + +#include "gtest/gtest.h" + +#include "mace/utils/tuner.h" + +namespace mace { + +class TunerTest : public ::testing::Test { + protected: + virtual void SetUp() { + remove("/data/local/tmp/mace.config"); + setenv("MACE_RUN_PARAMETER_PATH", "/data/local/tmp/mace.config", 1); + setenv("MACE_TUNING", "1", 1); + } +}; + +TEST_F(TunerTest, SimpleRun) { + int expect = 1; + auto TunerFunc = [&](const std::vector ¶ms) -> int { + if (params.front() == 1) { + return expect; + } else { + return expect + 1; + } + }; + + WallClockTimer timer; + std::vector default_params(1, 1); + int res = Tuner::Get()->template TuneOrRun("SimpleRun", + default_params, + nullptr, + TunerFunc, + &timer); + + EXPECT_EQ(expect, res); + + default_params[0] = 2; + res = Tuner::Get()->template TuneOrRun("SimpleRun", + default_params, + nullptr, + TunerFunc, + &timer); + EXPECT_EQ(expect + 1, res); +} + +TEST_F(TunerTest, SimpleTune) { + int expect = 3; + auto TunerFunc = [&](const std::vector ¶ms) -> int { + if (params.front() == expect) { + return expect; + } else { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + return params.front(); + } + }; + + std::vector default_params(1, 1); + auto params_generator = []() -> std::vector> { + return {{1}, {2}, {3}, {4}}; + }; + // tune + WallClockTimer timer; + int res = + Tuner::Get()->template TuneOrRun("SimpleRun", + default_params, + *params_generator, + TunerFunc, + &timer); + EXPECT_EQ(expect, res); + + // run + res = Tuner::Get()->template TuneOrRun("SimpleRun", + default_params, + nullptr, + TunerFunc, + &timer); + EXPECT_EQ(expect, res); +} + +} // namespace mace diff --git a/tools/validate_gcn.sh b/tools/validate_gcn.sh index 8e54739dd0b063b950fb8ac1b2ace78bd6b27d79..d6dec6417b94e8d30d804d6f3c127936464b8b31 100755 --- a/tools/validate_gcn.sh +++ b/tools/validate_gcn.sh @@ -45,7 +45,7 @@ build_and_run() round=0 # only warm up else tuning_flag=0 - round=100 + round=2 fi bazel build --verbose_failures -c opt --strip always mace/examples:mace_run \ @@ -56,6 +56,7 @@ build_and_run() --copt="-D_GLIBCXX_USE_C99_MATH_TR1" \ --copt="-Werror=return-type" \ --copt="-DMACE_MODEL_TAG=${MODEL_TAG}" \ + --copt="-DMACE_OBFUSCATE_LITERALS" \ $PRODUCTION_MODE_BUILD_FLAGS \ $TUNING_MODE_BUILD_FLAGS || exit -1