Checking performance flags during init.

Summary:
Adds 2 features:
(1) In cmake, allow the use of -march=native
(2) During initialization, check if Caffe2 is built with matching cpu
features of the current machine.

This helps us guarding performance claims in case the Caffe2 baseline is
built with limited computation capability.

Currently only added avx, avx2 and fma which are common.
Closes https://github.com/caffe2/caffe2/pull/1775

Reviewed By: ezyang

Differential Revision: D6772059

Pulled By: Yangqing

fbshipit-source-id: 884a3d7c7a71ed9631b7c6269ae95d842a09e1bd
This commit is contained in:
Yangqing Jia 2018-01-22 13:47:44 -08:00 committed by Facebook Github Bot
parent a82b3096ef
commit 27f4041738
3 changed files with 104 additions and 1 deletions

View file

@ -33,6 +33,7 @@ option(USE_LMDB "Use LMDB" ON)
option(USE_METAL "Use Metal for iOS build" ON)
option(USE_MOBILE_OPENGL "Use OpenGL for mobile code" ON)
option(USE_MPI "Use MPI" ON)
option(USE_NATIVE_ARCH "Use -march=native" OFF)
option(USE_NCCL "Use NCCL" ON)
option(USE_NERVANA_GPU "Use Nervana GPU backend" OFF)
option(USE_NNPACK "Use NNPACK" ON)

View file

@ -0,0 +1,89 @@
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "caffe2/core/common.h"
#include "caffe2/core/flags.h"
#include "caffe2/core/init.h"
#include "caffe2/core/logging.h"
#include "caffe2/utils/cpuid.h"
CAFFE2_DEFINE_bool(
caffe2_quit_on_unsupported_cpu_feature,
false,
"If set, when Caffe2 is built with a CPU feature (like avx2) but the "
"current CPU does not support it, quit early. If not set (by default), "
"log this as an error message and continue execution.");
namespace caffe2 {
static void QuitIfFeatureUnsupported(
const bool cpu_has_feature, const string& feature) {
VLOG(1) << "Caffe2 built with " << feature << ".";
if (!cpu_has_feature) {
string err_string =
"The Caffe2 binary is compiled with CPU feature " + feature +
", but your CPU does not support it. This will lead to segfaults "
"on your machine, such as SIGILL 'illegal instructions' on Linux. "
"As a result Caffe2 will preemptively quit. Please install or "
"build a Caffe2 binary with the feature turned off.";
if (FLAGS_caffe2_quit_on_unsupported_cpu_feature) {
LOG(FATAL) << err_string;
} else {
LOG(ERROR) << err_string;
}
}
}
static void WarnIfFeatureUnused(
const bool cpu_has_feature, const string& feature) {
VLOG(1) << "Caffe2 not built with " << feature << ".";
if (cpu_has_feature) {
LOG(ERROR)
<< "CPU feature " << feature << " is present on your machine, "
"but the Caffe2 binary is not compiled with it. It means you "
"may not get the full speed of your CPU.";
}
}
bool Caffe2CheckIntrinsicsFeatures(int*, char***) {
#ifdef __AVX__
QuitIfFeatureUnsupported(GetCpuId().avx2(), "avx");
#else
WarnIfFeatureUnused(GetCpuId().avx2(), "avx");
#endif
#ifdef __AVX2__
QuitIfFeatureUnsupported(GetCpuId().avx2(), "avx2");
#else
WarnIfFeatureUnused(GetCpuId().avx2(), "avx2");
#endif
#ifdef __FMA__
QuitIfFeatureUnsupported(GetCpuId().fma(), "fma");
#else
WarnIfFeatureUnused(GetCpuId().fma(), "fma");
#endif
return true;
}
REGISTER_CAFFE2_INIT_FUNCTION(
Caffe2CheckIntrinsicsFeatures,
&Caffe2CheckIntrinsicsFeatures,
"Check intrinsics compatibility between the CPU feature and the binary.");
} // namespace caffe2

View file

@ -1,4 +1,5 @@
include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)
include(CMakePushCheckState)
# ---[ If running on Ubuntu, check system version and compiler version.
@ -170,4 +171,16 @@ endif()
if (${CAFFE2_CMAKE_USE_LOCAL_FINDCUDA})
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules_CUDA_fix)
endif()
endif()
if (USE_NATIVE_ARCH)
check_cxx_compiler_flag("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
if (COMPILER_SUPPORTS_MARCH_NATIVE)
add_definitions("-march=native")
else()
message(
WARNING
"Your compiler does not support -march=native. Turn off this warning "
"by setting -DUSE_NATIVE_ARCH=OFF.")
endif()
endif()