jit_kernel_blas.cc 8.3 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 86 87 88 89 90 91 92 93 94 95
#define FOR_EACH_ISA(macro_, block) \
  macro_(jit::avx512f, block);      \
  macro_(jit::avx2, block);         \
  macro_(jit::avx, block);          \
  macro_(jit::isa_any, block)

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

#define FOR_EACH_ISA_BLOCK(macro_)      \
  FOR_EACH_BLOCK(macro_, jit::avx512f); \
  FOR_EACH_BLOCK(macro_, jit::avx2);    \
  FOR_EACH_BLOCK(macro_, jit::avx);     \
  FOR_EACH_BLOCK(macro_, jit::isa_any)
T
tensor-tang 已提交
96

T
tensor-tang 已提交
97
/* VMUL JitKernel */
T
tensor-tang 已提交
98
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
99 100 101 102 103 104
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 已提交
105
  }
T
tensor-tang 已提交
106
};
T
tensor-tang 已提交
107

T
tensor-tang 已提交
108
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
109 110 111 112 113
#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 已提交
114 115
  }

T
tensor-tang 已提交
116 117 118 119 120
#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 已提交
121 122
  }

T
tensor-tang 已提交
123 124
FOR_EACH_ISA(VMUL_MKL_FLOAT, kGT16);
FOR_EACH_ISA_BLOCK(VMUL_MKL_DOUBLE);
T
tensor-tang 已提交
125 126
#endif

T
tensor-tang 已提交
127 128 129 130 131 132 133 134 135
#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 已提交
136 137
  }

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

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

T
tensor-tang 已提交
154
/* VADD JitKernel */
T
tensor-tang 已提交
155
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
156 157 158 159 160 161
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 已提交
162
  }
T
tensor-tang 已提交
163
};
T
tensor-tang 已提交
164

T
tensor-tang 已提交
165
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
166 167 168 169 170
#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 已提交
171 172
  }

T
tensor-tang 已提交
173 174 175 176 177
#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 已提交
178 179
  }

T
tensor-tang 已提交
180 181
FOR_EACH_ISA(VADD_MKL_FLOAT, kGT16);
FOR_EACH_ISA_BLOCK(VADD_MKL_DOUBLE);
T
tensor-tang 已提交
182 183
#endif

T
tensor-tang 已提交
184 185 186 187 188 189 190 191 192
#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 已提交
193
  }
T
tensor-tang 已提交
194
#ifdef __AVX__
T
tensor-tang 已提交
195
VADD_INTRI8_FLOAT(jit::avx);
T
tensor-tang 已提交
196 197
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
198 199 200 201
VADD_INTRI8_FLOAT(jit::avx2);
#endif
#ifdef __AVX512F__
VADD_INTRI8_FLOAT(jit::avx512f);
T
tensor-tang 已提交
202
#endif
T
tensor-tang 已提交
203
// TODO(TJ): eq16 test and complete avx512
T
tensor-tang 已提交
204 205 206 207 208

#undef VADD_INTRI8_FLOAT
#undef VADD_MKL_FLOAT
#undef VADD_MKL_DOUBLE

T
tensor-tang 已提交
209 210
REGISTER_BLAS_JITKERNEL(vmul, VMulKernel);
REGISTER_BLAS_JITKERNEL(vadd, VAddKernel);
T
tensor-tang 已提交
211

T
tensor-tang 已提交
212 213 214
#undef FOR_EACH_ISA
#undef FOR_EACH_BLOCK
#undef FOR_EACH_ISA_BLOCK
T
tensor-tang 已提交
215 216
#undef REGISTER_BLAS_JITKERNEL
#undef DEFINE_WITH_DTYPE
T
tensor-tang 已提交
217 218
#undef SEARCH_ISA_BLOCK
#undef SEARCH_BLOCK
T
tensor-tang 已提交
219
#undef NEW_IMPL
T
tensor-tang 已提交
220 221 222 223 224

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