jit_kernel_blas.cc 8.5 KB
Newer Older
T
tensor-tang 已提交
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
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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 "paddle/fluid/operators/math/jit_kernel.h"
#include <string>
#ifdef PADDLE_WITH_MKLML
#include "paddle/fluid/platform/dynload/mklml.h"
#endif

#ifdef __AVX__
#include <immintrin.h>
#endif

namespace paddle {
namespace operators {
namespace math {
namespace jitkernel {

namespace jit = platform::jit;

T
tensor-tang 已提交
32 33 34 35
#define NEW_IMPL(src, t, isa, k)         \
  p = std::dynamic_pointer_cast<src<t>>( \
      std::make_shared<src##Impl<t, isa, k>>())

T
tensor-tang 已提交
36 37
#define SEARCH_BLOCK(src, t, isa)                             \
  if (d < AVX_FLOAT_BLOCK) {                                  \
T
tensor-tang 已提交
38
    NEW_IMPL(src, t, isa, kLT8);                              \
T
tensor-tang 已提交
39
  } else if (d == AVX_FLOAT_BLOCK) {                          \
T
tensor-tang 已提交
40
    NEW_IMPL(src, t, isa, kEQ8);                              \
T
tensor-tang 已提交
41
  } else if (d > AVX_FLOAT_BLOCK && d < AVX512_FLOAT_BLOCK) { \
T
tensor-tang 已提交
42
    NEW_IMPL(src, t, isa, kGT8LT16);                          \
T
tensor-tang 已提交
43
  } else if (d == AVX512_FLOAT_BLOCK) {                       \
T
tensor-tang 已提交
44
    NEW_IMPL(src, t, isa, kEQ16);                             \
T
tensor-tang 已提交
45
  } else {                                                    \
T
tensor-tang 已提交
46
    NEW_IMPL(src, t, isa, kGT16);                             \
T
tensor-tang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
  }

#define SEARCH_ISA_BLOCK(src, t)        \
  if (jit::MayIUse(jit::avx512f)) {     \
    SEARCH_BLOCK(src, t, jit::avx512f); \
  } else if (jit::MayIUse(jit::avx2)) { \
    SEARCH_BLOCK(src, t, jit::avx2);    \
  } else if (jit::MayIUse(jit::avx)) {  \
    SEARCH_BLOCK(src, t, jit::avx);     \
  } else {                              \
    SEARCH_BLOCK(src, t, jit::isa_any); \
  }

T
tensor-tang 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#define DEFINE_WITH_DTYPE(ker_key, ker_class, ker_dtype, dtype_key)        \
  template <>                                                              \
  const std::shared_ptr<ker_class<ker_dtype>>                              \
  KernelPool::Get<ker_class<ker_dtype>>(int d) {                           \
    std::string key = #ker_key #dtype_key + std::to_string(d);             \
    if (kers_.find(key) == kers_.end()) {                                  \
      std::shared_ptr<ker_class<ker_dtype>> p;                             \
      SEARCH_ISA_BLOCK(ker_class, ker_dtype);                              \
      kers_.insert({key, std::dynamic_pointer_cast<Kernel>(p)});           \
      return p;                                                            \
    }                                                                      \
    return std::dynamic_pointer_cast<ker_class<ker_dtype>>(kers_.at(key)); \
  }

#define REGISTER_BLAS_JITKERNEL(ker_key, ker_class) \
  DEFINE_WITH_DTYPE(ker_key, ker_class, float, f);  \
  DEFINE_WITH_DTYPE(ker_key, ker_class, double, d)

T
tensor-tang 已提交
78 79 80 81 82 83 84 85
// do not include lt8, eq8, eq16
#define FOR_EACH_COMMON_BLOCK(macro_, isa) \
  macro_(isa, kGT8LT16) macro_(isa, kGT16)

#define FOR_EACH_ISA_COMMON_BLOCK(macro_)     \
  FOR_EACH_COMMON_BLOCK(macro_, jit::avx512f) \
  FOR_EACH_COMMON_BLOCK(macro_, jit::avx2)    \
  FOR_EACH_COMMON_BLOCK(macro_, jit::avx)     \
T
tensor-tang 已提交
86
  FOR_EACH_COMMON_BLOCK(macro_, jit::isa_any)
T
tensor-tang 已提交
87 88 89 90 91 92 93 94 95

#define FOR_EACH_ALL_BLOCK(macro_, isa)                                        \
  macro_(isa, kLT8) macro_(isa, kEQ8) macro_(isa, kGT8LT16) macro_(isa, kEQ16) \
      macro_(isa, kGT16)

#define FOR_EACH_ISA_ALL_BLOCK(macro_)     \
  FOR_EACH_ALL_BLOCK(macro_, jit::avx512f) \
  FOR_EACH_ALL_BLOCK(macro_, jit::avx2)    \
  FOR_EACH_ALL_BLOCK(macro_, jit::avx)     \
T
tensor-tang 已提交
96
  FOR_EACH_ALL_BLOCK(macro_, jit::isa_any)
T
tensor-tang 已提交
97

T
tensor-tang 已提交
98
/* VMUL JitKernel */
T
tensor-tang 已提交
99
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
100 101 102 103 104 105
class VMulKernelImpl : public VMulKernel<T> {
 public:
  void Compute(const int n, const T* x, const T* y, T* z) override {
    for (int i = 0; i < n; ++i) {
      z[i] = x[i] * y[i];
    }
T
tensor-tang 已提交
106
  }
T
tensor-tang 已提交
107
};
T
tensor-tang 已提交
108

T
tensor-tang 已提交
109
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
110 111 112 113 114
#define VMUL_MKL_FLOAT(isa, block)                                             \
  template <>                                                                  \
  void VMulKernelImpl<float, isa, block>::Compute(const int n, const float* x, \
                                                  const float* y, float* z) {  \
    platform::dynload::vsMul(n, x, y, z);                                      \
T
tensor-tang 已提交
115 116
  }

T
tensor-tang 已提交
117 118 119 120 121
#define VMUL_MKL_DOUBLE(isa, block)                               \
  template <>                                                     \
  void VMulKernelImpl<double, isa, block>::Compute(               \
      const int n, const double* x, const double* y, double* z) { \
    platform::dynload::vdMul(n, x, y, z);                         \
T
tensor-tang 已提交
122 123
  }

T
tensor-tang 已提交
124 125
FOR_EACH_ISA_COMMON_BLOCK(VMUL_MKL_FLOAT);
FOR_EACH_ISA_ALL_BLOCK(VMUL_MKL_DOUBLE);
T
tensor-tang 已提交
126 127
#endif

T
tensor-tang 已提交
128 129 130 131 132 133 134 135 136
#define VMUL_INTRI8_FLOAT(isa)                                                \
  template <>                                                                 \
  void VMulKernelImpl<float, isa, kEQ8>::Compute(const int n, const float* x, \
                                                 const float* y, float* z) {  \
    __m256 tmpx, tmpy;                                                        \
    tmpx = _mm256_loadu_ps(x);                                                \
    tmpy = _mm256_loadu_ps(y);                                                \
    tmpx = _mm256_mul_ps(tmpx, tmpy);                                         \
    _mm256_storeu_ps(z, tmpx);                                                \
T
tensor-tang 已提交
137 138
  }

T
tensor-tang 已提交
139 140
// avx > for > mkl
#ifdef __AVX__
T
tensor-tang 已提交
141
VMUL_INTRI8_FLOAT(jit::avx);
T
tensor-tang 已提交
142 143
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
144 145 146 147
VMUL_INTRI8_FLOAT(jit::avx2);
#endif
#ifdef __AVX512F__
VMUL_INTRI8_FLOAT(jit::avx512f);
T
tensor-tang 已提交
148 149
#endif

T
tensor-tang 已提交
150
// TODO(TJ): eq16 test and complete avx512
T
tensor-tang 已提交
151 152 153
#undef VMUL_INTRI8_FLOAT
#undef VMUL_MKL_FLOAT
#undef VMUL_MKL_DOUBLE
T
tensor-tang 已提交
154

T
tensor-tang 已提交
155
/* VADD JitKernel */
T
tensor-tang 已提交
156
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
157 158 159 160 161 162
class VAddKernelImpl : public VAddKernel<T> {
 public:
  void Compute(const int n, const T* x, const T* y, T* z) override {
    for (int i = 0; i < n; ++i) {
      z[i] = x[i] + y[i];
    }
T
tensor-tang 已提交
163
  }
T
tensor-tang 已提交
164
};
T
tensor-tang 已提交
165

T
tensor-tang 已提交
166
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
167 168 169 170 171
#define VADD_MKL_FLOAT(isa, block)                                             \
  template <>                                                                  \
  void VAddKernelImpl<float, isa, block>::Compute(const int n, const float* x, \
                                                  const float* y, float* z) {  \
    platform::dynload::vsAdd(n, x, y, z);                                      \
T
tensor-tang 已提交
172 173
  }

T
tensor-tang 已提交
174 175 176 177 178
#define VADD_MKL_DOUBLE(isa, block)                               \
  template <>                                                     \
  void VAddKernelImpl<double, isa, block>::Compute(               \
      const int n, const double* x, const double* y, double* z) { \
    platform::dynload::vdAdd(n, x, y, z);                         \
T
tensor-tang 已提交
179 180
  }

T
tensor-tang 已提交
181 182
FOR_EACH_ISA_COMMON_BLOCK(VADD_MKL_FLOAT);
FOR_EACH_ISA_ALL_BLOCK(VADD_MKL_DOUBLE);
T
tensor-tang 已提交
183 184
#endif

T
tensor-tang 已提交
185 186 187 188 189 190 191 192 193
#define VADD_INTRI8_FLOAT(isa)                                                \
  template <>                                                                 \
  void VAddKernelImpl<float, isa, kEQ8>::Compute(const int n, const float* x, \
                                                 const float* y, float* z) {  \
    __m256 tmpx, tmpy;                                                        \
    tmpx = _mm256_loadu_ps(x);                                                \
    tmpy = _mm256_loadu_ps(y);                                                \
    tmpx = _mm256_add_ps(tmpx, tmpy);                                         \
    _mm256_storeu_ps(z, tmpx);                                                \
T
tensor-tang 已提交
194
  }
T
tensor-tang 已提交
195
#ifdef __AVX__
T
tensor-tang 已提交
196
VADD_INTRI8_FLOAT(jit::avx);
T
tensor-tang 已提交
197 198
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
199 200 201 202
VADD_INTRI8_FLOAT(jit::avx2);
#endif
#ifdef __AVX512F__
VADD_INTRI8_FLOAT(jit::avx512f);
T
tensor-tang 已提交
203
#endif
T
tensor-tang 已提交
204
// TODO(TJ): eq16 test and complete avx512
T
tensor-tang 已提交
205 206 207 208 209

#undef VADD_INTRI8_FLOAT
#undef VADD_MKL_FLOAT
#undef VADD_MKL_DOUBLE

T
tensor-tang 已提交
210 211
REGISTER_BLAS_JITKERNEL(vmul, VMulKernel);
REGISTER_BLAS_JITKERNEL(vadd, VAddKernel);
T
tensor-tang 已提交
212 213 214 215 216

#undef FOR_EACH_ISA_ALL_BLOCK
#undef FOR_EACH_ALL_BLOCK
#undef FOR_EACH_ISA_COMMON_BLOCK
#undef FOR_EACH_COMMON_BLOCK
T
tensor-tang 已提交
217 218
#undef REGISTER_BLAS_JITKERNEL
#undef DEFINE_WITH_DTYPE
T
tensor-tang 已提交
219 220
#undef SEARCH_ISA_BLOCK
#undef SEARCH_BLOCK
T
tensor-tang 已提交
221
#undef NEW_IMPL
T
tensor-tang 已提交
222 223 224 225 226

}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle