|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <ATen/Error.h> |
| 4 | +#include <ATen/ScalarType.h> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +// Implements instruction set specific function dispatch. |
| 8 | +// |
| 9 | +// Kernels that may make use of specialized instruction sets (e.g. AVX) are |
| 10 | +// compiled multiple times with different compiler flags (e.g. -mavx). A |
| 11 | +// DispatchStub contains a table of function pointers for a kernel. At runtime, |
| 12 | +// the fastest available kernel is chosen based on the features reported by |
| 13 | +// cpuinfo. |
| 14 | +// |
| 15 | +// Example: |
| 16 | +// |
| 17 | +// In native/MyKernel.h: |
| 18 | +// using fn_type = void(*)(const Tensor& x); |
| 19 | +// DECLARE_DISPATCH(fn_type, stub); |
| 20 | +// |
| 21 | +// In native/MyKernel.cpp |
| 22 | +// DEFINE_DISPATCH(stub); |
| 23 | +// |
| 24 | +// In native/cpu/MyKernel.cpp: |
| 25 | +// void kernel(const Tensor& x) { ... } |
| 26 | +// REGISTER_DISPATCH(stub, &kernel); |
| 27 | +// |
| 28 | +// To call: |
| 29 | +// stub(kCPU, tensor); |
| 30 | + |
| 31 | +// ignore warnings about DispatchStub::DEFAULT, AVX, AVX2 defined elsewhere |
| 32 | +#if defined(__clang__) |
| 33 | +#pragma clang diagnostic push |
| 34 | +#pragma clang diagnostic ignored "-Wundefined-var-template" |
| 35 | +#endif |
| 36 | + |
| 37 | +namespace at { namespace native { |
| 38 | + |
| 39 | +enum class CPUCapability { |
| 40 | + DEFAULT = 0, |
| 41 | + AVX = 1, |
| 42 | + AVX2 = 2, |
| 43 | + NUM_OPTIONS |
| 44 | +}; |
| 45 | + |
| 46 | +CPUCapability get_cpu_capability(); |
| 47 | + |
| 48 | +template <typename FnPtr, typename T> |
| 49 | +struct DispatchStub { |
| 50 | + static_assert(std::is_pointer<FnPtr>::value, "FnPtr should be a pointer type"); |
| 51 | + |
| 52 | + template <typename... ArgTypes> |
| 53 | + void operator()(Backend backend, ArgTypes... args) { |
| 54 | + if (backend == Backend::CPU) { |
| 55 | + if (!cpu_dispatch_ptr) { |
| 56 | + cpu_dispatch_ptr = choose_cpu_impl(); |
| 57 | + } |
| 58 | + (*cpu_dispatch_ptr)(args...); |
| 59 | + } else if (backend == Backend::CUDA) { |
| 60 | + AT_ASSERTM(cuda_dispatch_ptr, "DispatchStub: missing CUDA kernel"); |
| 61 | + (*cuda_dispatch_ptr)(args...); |
| 62 | + } else { |
| 63 | + AT_ERROR("DispatchStub: unsupported backend", backend); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + FnPtr choose_cpu_impl() { |
| 68 | + auto capability = static_cast<int>(get_cpu_capability()); |
| 69 | + (void)capability; |
| 70 | +#ifdef HAVE_AVX2_CPU_DEFINITION |
| 71 | + if (capability >= static_cast<int>(CPUCapability::AVX2)) { |
| 72 | + AT_ASSERTM(AVX2, "DispatchStub: missing AVX2 kernel"); |
| 73 | + return AVX2; |
| 74 | + } |
| 75 | +#endif |
| 76 | +#ifdef HAVE_AVX_CPU_DEFINITION |
| 77 | + if (capability >= static_cast<int>(CPUCapability::AVX)) { |
| 78 | + AT_ASSERTM(AVX, "DispatchStub: missing AVX kernel"); |
| 79 | + return AVX; |
| 80 | + } |
| 81 | +#endif |
| 82 | + AT_ASSERTM(DEFAULT, "DispatchStub: missing default kernel"); |
| 83 | + return DEFAULT; |
| 84 | + } |
| 85 | + |
| 86 | + FnPtr cpu_dispatch_ptr = nullptr; |
| 87 | + FnPtr cuda_dispatch_ptr = nullptr; |
| 88 | + static FnPtr DEFAULT; |
| 89 | +#ifdef HAVE_AVX_CPU_DEFINITION |
| 90 | + static FnPtr AVX; |
| 91 | +#endif |
| 92 | +#ifdef HAVE_AVX2_CPU_DEFINITION |
| 93 | + static FnPtr AVX2; |
| 94 | +#endif |
| 95 | +}; |
| 96 | + |
| 97 | +namespace { |
| 98 | +template <typename FnPtr, typename T> |
| 99 | +struct RegisterDispatch { |
| 100 | + RegisterDispatch(DispatchStub<FnPtr, T>& stub, FnPtr value) { |
| 101 | + stub.cuda_dispatch_ptr = value; |
| 102 | + } |
| 103 | +}; |
| 104 | +} // anonymous namespace |
| 105 | + |
| 106 | +#define DECLARE_DISPATCH(fn, name) \ |
| 107 | + extern struct name : DispatchStub<fn, name> {} name |
| 108 | + |
| 109 | +#define DEFINE_DISPATCH(name) struct name name |
| 110 | + |
| 111 | +#if defined(__CUDACC__) |
| 112 | +#define REGISTER_DISPATCH(name, fn) \ |
| 113 | + static RegisterDispatch<decltype(fn), struct name> name ## __register(name, fn); |
| 114 | +#elif defined(CPU_CAPABILITY) |
| 115 | +#define REGISTER_DISPATCH(name, fn) \ |
| 116 | + template <> decltype(fn) DispatchStub<decltype(fn), struct name>::CPU_CAPABILITY = fn; |
| 117 | +#endif |
| 118 | + |
| 119 | + |
| 120 | +}} // namespace at::native |
| 121 | + |
| 122 | + |
| 123 | +#if defined(__clang__) |
| 124 | +#pragma clang diagnostic pop |
| 125 | +#endif |
0 commit comments