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