add config simd_type (#2179)

* add config simd_type

Signed-off-by: feisiyicl <7764126@qq.com>

* update changelog

Signed-off-by: feisiyicl <7764126@qq.com>
This commit is contained in:
feisiyicl 2020-04-29 17:46:36 +08:00 committed by GitHub
parent e881ccbd30
commit b3aa06fce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 43 deletions

View File

@ -31,7 +31,7 @@ Please mark all change in change log and use the issue from GitHub
- \#2039 Support Milvus run on SSE CPUs
- \#2149 Merge server_cpu_config.template and server_gpu_config.template
- \#2153 Upgrade thirdparty oatpp to v1.0.0
- \#2167 Merge config file
- \#2167 Merge log_config.conf with server_config.yaml
## Task

View File

@ -113,8 +113,8 @@ const char* CONFIG_ENGINE_USE_BLAS_THRESHOLD = "use_blas_threshold";
const char* CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT = "1100";
const char* CONFIG_ENGINE_OMP_THREAD_NUM = "omp_thread_num";
const char* CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT = "0";
const char* CONFIG_ENGINE_USE_AVX512 = "use_avx512";
const char* CONFIG_ENGINE_USE_AVX512_DEFAULT = "true";
const char* CONFIG_ENGINE_SIMD_TYPE = "simd_type";
const char* CONFIG_ENGINE_SIMD_TYPE_DEFAULT = "auto";
const char* CONFIG_ENGINE_GPU_SEARCH_THRESHOLD = "gpu_search_threshold";
const char* CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT = "1000";
@ -338,8 +338,8 @@ Config::ValidateConfig() {
int64_t engine_omp_thread_num;
CONFIG_CHECK(GetEngineConfigOmpThreadNum(engine_omp_thread_num));
bool engine_use_avx512;
CONFIG_CHECK(GetEngineConfigUseAVX512(engine_use_avx512));
std::string engine_simd_type;
CONFIG_CHECK(GetEngineConfigSimdType(engine_simd_type));
#ifdef MILVUS_GPU_VERSION
int64_t engine_gpu_search_threshold;
@ -450,7 +450,7 @@ Config::ResetDefaultConfig() {
/* engine config */
CONFIG_CHECK(SetEngineConfigUseBlasThreshold(CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT));
CONFIG_CHECK(SetEngineConfigOmpThreadNum(CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT));
CONFIG_CHECK(SetEngineConfigUseAVX512(CONFIG_ENGINE_USE_AVX512_DEFAULT));
CONFIG_CHECK(SetEngineConfigSimdType(CONFIG_ENGINE_SIMD_TYPE_DEFAULT));
/* wal config */
CONFIG_CHECK(SetWalConfigEnable(CONFIG_WAL_ENABLE_DEFAULT));
@ -577,8 +577,8 @@ Config::SetConfigCli(const std::string& parent_key, const std::string& child_key
status = SetEngineConfigUseBlasThreshold(value);
} else if (child_key == CONFIG_ENGINE_OMP_THREAD_NUM) {
status = SetEngineConfigOmpThreadNum(value);
} else if (child_key == CONFIG_ENGINE_USE_AVX512) {
status = SetEngineConfigUseAVX512(value);
} else if (child_key == CONFIG_ENGINE_SIMD_TYPE) {
status = SetEngineConfigSimdType(value);
#ifdef MILVUS_GPU_VERSION
} else if (child_key == CONFIG_ENGINE_GPU_SEARCH_THRESHOLD) {
status = SetEngineConfigGpuSearchThreshold(value);
@ -1315,11 +1315,12 @@ Config::CheckEngineConfigOmpThreadNum(const std::string& value) {
}
Status
Config::CheckEngineConfigUseAVX512(const std::string& value) {
if (!ValidationUtil::ValidateStringIsBool(value).ok()) {
std::string msg =
"Invalid engine config: " + value + ". Possible reason: engine_config.use_avx512 is not a boolean.";
return Status(SERVER_INVALID_ARGUMENT, msg);
Config::CheckEngineConfigSimdType(const std::string& value) {
fiu_return_on("check_config_simd_type_fail",
Status(SERVER_INVALID_ARGUMENT, "engine_config.simd_type is not one of avx512, avx2, sse and auto."));
if (value != "avx512" && value != "avx2" && value != "sse" && value != "auto") {
return Status(SERVER_INVALID_ARGUMENT, "engine_config.simd_type is not one of avx512, avx2, sse and auto.");
}
return Status::OK();
}
@ -1927,12 +1928,9 @@ Config::GetEngineConfigOmpThreadNum(int64_t& value) {
}
Status
Config::GetEngineConfigUseAVX512(bool& value) {
std::string str = GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_USE_AVX512, CONFIG_ENGINE_USE_AVX512_DEFAULT);
CONFIG_CHECK(CheckEngineConfigUseAVX512(str));
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
value = (str == "true" || str == "on" || str == "yes" || str == "1");
return Status::OK();
Config::GetEngineConfigSimdType(std::string& value) {
value = GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_SIMD_TYPE, CONFIG_ENGINE_SIMD_TYPE_DEFAULT);
return CheckEngineConfigSimdType(value);
}
#ifdef MILVUS_GPU_VERSION
@ -2333,9 +2331,9 @@ Config::SetEngineConfigOmpThreadNum(const std::string& value) {
}
Status
Config::SetEngineConfigUseAVX512(const std::string& value) {
CONFIG_CHECK(CheckEngineConfigUseAVX512(value));
return SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_USE_AVX512, value);
Config::SetEngineConfigSimdType(const std::string& value) {
CONFIG_CHECK(CheckEngineConfigSimdType(value));
return SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_SIMD_TYPE, value);
}
/* tracing config */

View File

@ -109,8 +109,8 @@ extern const char* CONFIG_ENGINE_USE_BLAS_THRESHOLD;
extern const char* CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT;
extern const char* CONFIG_ENGINE_OMP_THREAD_NUM;
extern const char* CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT;
extern const char* CONFIG_ENGINE_USE_AVX512;
extern const char* CONFIG_ENGINE_USE_AVX512_DEFAULT;
extern const char* CONFIG_ENGINE_SIMD_TYPE;
extern const char* CONFIG_ENGINE_SIMD_TYPE_DEFAULT;
extern const char* CONFIG_ENGINE_GPU_SEARCH_THRESHOLD;
extern const char* CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT;
@ -284,7 +284,7 @@ class Config {
Status
CheckEngineConfigOmpThreadNum(const std::string& value);
Status
CheckEngineConfigUseAVX512(const std::string& value);
CheckEngineConfigSimdType(const std::string& value);
#ifdef MILVUS_GPU_VERSION
Status
@ -413,7 +413,7 @@ class Config {
Status
GetEngineConfigOmpThreadNum(int64_t& value);
Status
GetEngineConfigUseAVX512(bool& value);
GetEngineConfigSimdType(std::string& value);
#ifdef MILVUS_GPU_VERSION
Status
@ -534,7 +534,7 @@ class Config {
Status
SetEngineConfigOmpThreadNum(const std::string& value);
Status
SetEngineConfigUseAVX512(const std::string& value);
SetEngineConfigSimdType(const std::string& value);
/* tracing config */
Status

View File

@ -35,11 +35,28 @@ constexpr int64_t M_BYTE = 1024 * 1024;
Status
KnowhereResource::Initialize() {
server::Config& config = server::Config::GetInstance();
bool use_avx512 = true;
CONFIG_CHECK(config.GetEngineConfigUseAVX512(use_avx512));
faiss::faiss_use_avx512 = use_avx512;
std::string simd_type;
CONFIG_CHECK(config.GetEngineConfigSimdType(simd_type));
if (simd_type == "avx512") {
faiss::faiss_use_avx512 = true;
faiss::faiss_use_avx2 = false;
faiss::faiss_use_sse = false;
} else if (simd_type == "avx2") {
faiss::faiss_use_avx512 = false;
faiss::faiss_use_avx2 = true;
faiss::faiss_use_sse = false;
} else if (simd_type == "sse") {
faiss::faiss_use_avx512 = false;
faiss::faiss_use_avx2 = false;
faiss::faiss_use_sse = true;
} else {
faiss::faiss_use_avx512 = true;
faiss::faiss_use_avx2 = true;
faiss::faiss_use_sse = true;
}
std::string cpu_flag;
if (faiss::hook_init(cpu_flag)) {
std::cout << "FAISS hook " << cpu_flag << std::endl;
LOG_ENGINE_DEBUG_ << "FAISS hook " << cpu_flag;
} else {
return Status(KNOWHERE_UNEXPECTED_ERROR, "FAISS hook fail, CPU not supported!");

View File

@ -17,6 +17,8 @@
namespace faiss {
bool faiss_use_avx512 = true;
bool faiss_use_avx2 = true;
bool faiss_use_sse = true;
/* set default to AVX */
fvec_func_ptr fvec_inner_product = fvec_inner_product_avx;
@ -41,11 +43,15 @@ bool support_avx512() {
}
bool support_avx2() {
if (!faiss_use_avx2) return false;
InstructionSet& instruction_set_inst = InstructionSet::GetInstance();
return (instruction_set_inst.AVX2());
}
bool support_sse42() {
bool support_sse() {
if (!faiss_use_sse) return false;
InstructionSet& instruction_set_inst = InstructionSet::GetInstance();
return (instruction_set_inst.SSE42());
}
@ -80,7 +86,7 @@ bool hook_init(std::string& cpu_flag) {
sq_sel_quantizer = sq_select_quantizer_avx;
cpu_flag = "AVX2";
} else if (support_sse42()) {
} else if (support_sse()) {
/* for IVFFLAT */
fvec_inner_product = fvec_inner_product_sse;
fvec_L2sqr = fvec_L2sqr_sse;

View File

@ -17,6 +17,8 @@ typedef Quantizer* (*sq_sel_func_ptr)(QuantizerType, size_t, const std::vector<f
extern bool faiss_use_avx512;
extern bool faiss_use_avx2;
extern bool faiss_use_sse;
extern fvec_func_ptr fvec_inner_product;
extern fvec_func_ptr fvec_L2sqr;
@ -28,6 +30,8 @@ extern sq_get_func_ptr sq_get_distance_computer_IP;
extern sq_sel_func_ptr sq_sel_quantizer;
extern bool support_avx512();
extern bool support_avx2();
extern bool support_sse();
extern bool hook_init(std::string& cpu_flag);

View File

@ -37,13 +37,13 @@ CpuChecker::CheckCpuInstructionSet() {
instruction_sets.emplace_back("avx512");
}
bool support_axv2 = instruction_set_inst.AVX2();
bool support_axv2 = faiss::support_avx2();
fiu_do_on("CpuChecker.CheckCpuInstructionSet.not_support_avx2", support_axv2 = false);
if (support_axv2) {
instruction_sets.emplace_back("avx2");
}
bool support_sse4_2 = instruction_set_inst.SSE42();
bool support_sse4_2 = faiss::support_sse();
fiu_do_on("CpuChecker.CheckCpuInstructionSet.not_support_sse4_2", support_sse4_2 = false);
if (support_sse4_2) {
instruction_sets.emplace_back("sse4_2");

View File

@ -287,10 +287,10 @@ TEST_F(ConfigTest, SERVER_CONFIG_VALID_TEST) {
ASSERT_TRUE(config.GetEngineConfigOmpThreadNum(int64_val).ok());
ASSERT_TRUE(int64_val == engine_omp_thread_num);
bool engine_use_avx512 = false;
ASSERT_TRUE(config.SetEngineConfigUseAVX512(std::to_string(engine_use_avx512)).ok());
ASSERT_TRUE(config.GetEngineConfigUseAVX512(bool_val).ok());
ASSERT_TRUE(bool_val == engine_use_avx512);
std::string engine_simd_type = "sse";
ASSERT_TRUE(config.SetEngineConfigSimdType(engine_simd_type).ok());
ASSERT_TRUE(config.GetEngineConfigSimdType(str_val).ok());
ASSERT_TRUE(str_val == engine_simd_type);
#ifdef MILVUS_GPU_VERSION
int64_t engine_gpu_search_threshold = 800;
@ -481,14 +481,14 @@ TEST_F(ConfigTest, SERVER_CONFIG_CLI_TEST) {
ASSERT_TRUE(s.ok());
ASSERT_TRUE(result == engine_omp_thread_num);
std::string engine_use_avx512 = "true";
get_cmd = gen_get_command(ms::CONFIG_ENGINE, ms::CONFIG_ENGINE_USE_AVX512);
set_cmd = gen_set_command(ms::CONFIG_ENGINE, ms::CONFIG_ENGINE_USE_AVX512, engine_use_avx512);
std::string engine_simd_type = "sse";
get_cmd = gen_get_command(ms::CONFIG_ENGINE, ms::CONFIG_ENGINE_SIMD_TYPE);
set_cmd = gen_set_command(ms::CONFIG_ENGINE, ms::CONFIG_ENGINE_SIMD_TYPE, engine_simd_type);
s = config.ProcessConfigCli(dummy, set_cmd);
ASSERT_TRUE(s.ok());
s = config.ProcessConfigCli(result, get_cmd);
ASSERT_TRUE(s.ok());
ASSERT_TRUE(result == engine_use_avx512);
ASSERT_TRUE(result == engine_simd_type);
#ifdef MILVUS_GPU_VERSION
std::string engine_gpu_search_threshold = "800";
@ -643,7 +643,7 @@ TEST_F(ConfigTest, SERVER_CONFIG_INVALID_TEST) {
ASSERT_FALSE(config.SetEngineConfigOmpThreadNum("10000").ok());
ASSERT_FALSE(config.SetEngineConfigOmpThreadNum("-10").ok());
ASSERT_FALSE(config.SetEngineConfigUseAVX512("N").ok());
ASSERT_FALSE(config.SetEngineConfigSimdType("None").ok());
#ifdef MILVUS_GPU_VERSION
ASSERT_FALSE(config.SetEngineConfigGpuSearchThreshold("-1").ok());
@ -790,6 +790,11 @@ TEST_F(ConfigTest, SERVER_CONFIG_VALID_FAIL_TEST) {
ASSERT_FALSE(s.ok());
fiu_disable("check_config_omp_thread_num_fail");
fiu_enable("check_config_simd_type_fail", 1, NULL, 0);
s = config.ValidateConfig();
ASSERT_FALSE(s.ok());
fiu_disable("check_config_simd_type_fail");
#ifdef MILVUS_GPU_VERSION
fiu_enable("check_config_gpu_search_threshold_fail", 1, NULL, 0);
s = config.ValidateConfig();
@ -1023,6 +1028,11 @@ TEST_F(ConfigTest, SERVER_CONFIG_RESET_DEFAULT_CONFIG_FAIL_TEST) {
ASSERT_FALSE(s.ok());
fiu_disable("check_config_omp_thread_num_fail");
fiu_enable("check_config_simd_type_fail", 1, NULL, 0);
s = config.ResetDefaultConfig();
ASSERT_FALSE(s.ok());
fiu_disable("check_config_simd_type_fail");
#ifdef MILVUS_GPU_VERSION
fiu_enable("check_config_gpu_search_threshold_fail", 1, NULL, 0);
s = config.ResetDefaultConfig();