File: init_intrinsics_check.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (80 lines) | stat: -rw-r--r-- 2,399 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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"

C10_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 {

inline 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) {
#ifdef CAFFE2_NO_CROSS_ARCH_WARNING
    // When cross-compiling single binary for multiple archs - turns off the
    // annoying warning
    VLOG(1)
#else
    LOG(ERROR)
#endif
        << "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().avx(), "avx");
#else
  WarnIfFeatureUnused(GetCpuId().avx(), "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