提交 96f486a5 编写于 作者: L Liangliang He

Refactor tuning code as dev/prod library

上级 1b871466
......@@ -47,6 +47,7 @@ cc_library(
linkopts = if_android([
"-pie",
"-ldl",
"-lm",
]),
deps = [
":opencl_headers",
......@@ -55,8 +56,10 @@ cc_library(
"//mace/utils:utils_hdrs",
"//mace/codegen:generated_version",
] + if_production_mode([
"//mace/utils:tuner_prod",
"//mace/core:opencl_prod",
]) + if_not_production_mode([
"//mace/utils:tuner_dev",
"//mace/core:opencl_dev",
]),
)
......@@ -107,6 +110,5 @@ cc_library(
deps = [
":opencl_headers",
"//mace/codegen:generated_opencl_prod",
"//mace/codegen:generated_tuning_params",
],
)
......@@ -7,7 +7,6 @@
#include "mace/core/runtime/opencl/cl2_header.h"
#include "mace/utils/utils.h"
namespace mace {
bool GetSourceOrBinaryProgram(const std::string &program_name,
......@@ -30,34 +29,4 @@ bool GetSourceOrBinaryProgram(const std::string &program_name,
return true;
}
bool GetTuningParams(const char *path,
std::unordered_map<std::string, std::vector<unsigned int>> *param_table) {
if (path != nullptr) {
std::ifstream ifs(path, std::ios::binary | std::ios::in);
if (ifs.is_open()) {
int64_t num_params = 0;
ifs.read(reinterpret_cast<char *>(&num_params), sizeof(num_params));
while (num_params--) {
int32_t key_size = 0;
ifs.read(reinterpret_cast<char *>(&key_size), sizeof(key_size));
std::string key(key_size, ' ');
ifs.read(&key[0], key_size);
int32_t params_size = 0;
ifs.read(reinterpret_cast<char *>(&params_size), sizeof(params_size));
int32_t params_count = params_size / sizeof(unsigned int);
std::vector<unsigned int> params(params_count);
for (int i = 0; i < params_count; ++i) {
ifs.read(reinterpret_cast<char *>(&params[i]), sizeof(unsigned int));
}
param_table->emplace(key, params);
}
ifs.close();
} else {
return false;
}
}
return true;
}
} // namespace mace
......@@ -23,13 +23,4 @@ bool GetSourceOrBinaryProgram(const std::string &program_name,
return true;
}
bool GetTuningParams(const char *path,
std::unordered_map<std::string, std::vector<unsigned int>> *param_table) {
extern const std::map<std::string, std::vector<unsigned int>> kTuningParamsData;
for (auto it = kTuningParamsData.begin(); it != kTuningParamsData.end(); ++it) {
param_table->emplace(it->first, std::vector<unsigned int>(it->second.begin(), it->second.end()));
}
return true;
}
} // namespace mace
......@@ -47,6 +47,27 @@ cc_library(
],
)
cc_library(
name = "tuner_dev",
srcs = [
"tuner_development.cc",
],
deps = [
":tuner",
],
)
cc_library(
name = "tuner_prod",
srcs = [
"tuner_production.cc",
],
deps = [
":tuner",
"//mace/codegen:generated_tuning_params",
],
)
cc_test(
name = "tuner_test",
testonly = 1,
......@@ -55,11 +76,12 @@ cc_test(
],
linkopts = if_android([
"-pie",
"-lm",
"-lm", # Required by unordered_map
]),
linkstatic = 1,
deps = [
":tuner",
":tuner_dev",
"@gtest//:gtest",
"@gtest//:gtest_main",
],
......
......@@ -62,7 +62,9 @@ class Tuner {
<< internal::MakeString(param_table_[obfucated_param_key]);
return func(param_table_[obfucated_param_key]);
} else {
LOG(WARNING) << "Fallback to default parameter: " << param_key << ", table size: " << param_table_.size();
#ifndef MACE_DISABLE_NO_TUNING_WARNING
LOG(WARNING) << "Fallback to default parameter: " << param_key;
#endif
return func(default_param);
}
}
......
#include <fstream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
namespace mace {
bool GetTuningParams(
const char *path,
std::unordered_map<std::string, std::vector<unsigned int>> *param_table) {
if (path != nullptr) {
std::ifstream ifs(path, std::ios::binary | std::ios::in);
if (ifs.is_open()) {
int64_t num_params = 0;
ifs.read(reinterpret_cast<char *>(&num_params), sizeof(num_params));
while (num_params--) {
int32_t key_size = 0;
ifs.read(reinterpret_cast<char *>(&key_size), sizeof(key_size));
std::string key(key_size, ' ');
ifs.read(&key[0], key_size);
int32_t params_size = 0;
ifs.read(reinterpret_cast<char *>(&params_size), sizeof(params_size));
int32_t params_count = params_size / sizeof(unsigned int);
std::vector<unsigned int> params(params_count);
for (int i = 0; i < params_count; ++i) {
ifs.read(reinterpret_cast<char *>(&params[i]), sizeof(unsigned int));
}
param_table->emplace(key, params);
}
ifs.close();
} else {
return false;
}
}
return true;
}
} // namespace mace
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
namespace mace {
bool GetTuningParams(
const char *path,
std::unordered_map<std::string, std::vector<unsigned int>> *param_table) {
extern const std::map<std::string, std::vector<unsigned int>>
kTuningParamsData;
for (auto it = kTuningParamsData.begin(); it != kTuningParamsData.end();
++it) {
param_table->emplace(it->first, std::vector<unsigned int>(
it->second.begin(), it->second.end()));
}
return true;
}
} // namespace mace
......@@ -32,12 +32,15 @@ echo "Step 2: Generate version source"
bash mace/tools/git/gen_version_source.sh ${CODEGEN_DIR}/version/version.cc
echo "Step 3: Build target"
# -D_GLIBCXX_USE_C99_MATH_TR1 is used to solve include error instead
# of linking error which solved by -lm
bazel build -c opt $STRIP --verbose_failures $BAZEL_TARGET \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=$ANDROID_ABI \
--copt="-std=c++11" \
--copt="-D_GLIBCXX_USE_C99_MATH_TR1" \
--copt="-DMACE_DISABLE_NO_TUNING_WARNING" \
--copt="-Werror=return-type" \
--define neon=false
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册