Caffe2 - C++ API
A deep learning, cross platform ML framework
init_intrinsics_check.cc
1 #include "caffe2/core/common.h"
2 #include "caffe2/core/flags.h"
3 #include "caffe2/core/init.h"
4 #include "caffe2/core/logging.h"
5 #include "caffe2/utils/cpuid.h"
6 
7 C10_DEFINE_bool(
8  caffe2_quit_on_unsupported_cpu_feature,
9  false,
10  "If set, when Caffe2 is built with a CPU feature (like avx2) but the "
11  "current CPU does not support it, quit early. If not set (by default), "
12  "log this as an error message and continue execution.");
13 
14 namespace caffe2 {
15 
16 static void QuitIfFeatureUnsupported(
17  const bool cpu_has_feature, const string& feature) {
18  VLOG(1) << "Caffe2 built with " << feature << ".";
19  if (!cpu_has_feature) {
20  string err_string =
21  "The Caffe2 binary is compiled with CPU feature " + feature +
22  ", but your CPU does not support it. This will lead to segfaults "
23  "on your machine, such as SIGILL 'illegal instructions' on Linux. "
24  "As a result Caffe2 will preemptively quit. Please install or "
25  "build a Caffe2 binary with the feature turned off.";
26  if (FLAGS_caffe2_quit_on_unsupported_cpu_feature) {
27  LOG(FATAL) << err_string;
28  } else {
29  LOG(ERROR) << err_string;
30  }
31  }
32 }
33 
34 static void WarnIfFeatureUnused(
35  const bool cpu_has_feature, const string& feature) {
36  VLOG(1) << "Caffe2 not built with " << feature << ".";
37  if (cpu_has_feature) {
38 #ifdef CAFFE2_NO_CROSS_ARCH_WARNING
39  // When cross-compiling single binary for multiple archs - turns off the
40  // annoying warning
41  VLOG(1)
42 #else
43  LOG(ERROR)
44 #endif
45  << "CPU feature " << feature
46  << " is present on your machine, "
47  "but the Caffe2 binary is not compiled with it. It means you "
48  "may not get the full speed of your CPU.";
49  }
50 }
51 
52 bool Caffe2CheckIntrinsicsFeatures(int*, char***) {
53 
54 #ifdef __AVX__
55  QuitIfFeatureUnsupported(GetCpuId().avx(), "avx");
56 #else
57  WarnIfFeatureUnused(GetCpuId().avx(), "avx");
58 #endif
59 
60 #ifdef __AVX2__
61  QuitIfFeatureUnsupported(GetCpuId().avx2(), "avx2");
62 #else
63  WarnIfFeatureUnused(GetCpuId().avx2(), "avx2");
64 #endif
65 
66 #ifdef __FMA__
67  QuitIfFeatureUnsupported(GetCpuId().fma(), "fma");
68 #else
69  WarnIfFeatureUnused(GetCpuId().fma(), "fma");
70 #endif
71 
72  return true;
73 }
74 
75 REGISTER_CAFFE2_INIT_FUNCTION(
76  Caffe2CheckIntrinsicsFeatures,
77  &Caffe2CheckIntrinsicsFeatures,
78  "Check intrinsics compatibility between the CPU feature and the binary.");
79 
80 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13