jit_kernel_blas.cc 8.2 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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>
T
tensor-tang 已提交
17
#include "paddle/fluid/operators/math/jit_kernel_macro.h"
T
tensor-tang 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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 已提交
33
/* VMUL JitKernel */
T
tensor-tang 已提交
34
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
35 36 37 38 39 40
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 已提交
41
  }
T
tensor-tang 已提交
42
};
T
tensor-tang 已提交
43

T
tensor-tang 已提交
44
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
45
#define MKL_FLOAT(isa, block)                                                  \
T
tensor-tang 已提交
46 47 48 49
  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 已提交
50 51
  }

T
tensor-tang 已提交
52
#define MKL_DOUBLE(isa, block)                                    \
T
tensor-tang 已提交
53 54 55 56
  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 已提交
57 58
  }

T
tensor-tang 已提交
59 60
FOR_EACH_ISA(MKL_FLOAT, kGT16);
FOR_EACH_ISA_BLOCK(MKL_DOUBLE);
T
tensor-tang 已提交
61 62
#endif

T
tensor-tang 已提交
63
#define INTRI8_FLOAT(isa)                                                     \
T
tensor-tang 已提交
64 65 66 67 68 69 70 71
  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 已提交
72 73
  }

T
tensor-tang 已提交
74 75
// avx > for > mkl
#ifdef __AVX__
T
tensor-tang 已提交
76
INTRI8_FLOAT(jit::avx);
T
tensor-tang 已提交
77 78
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
79
INTRI8_FLOAT(jit::avx2);
T
tensor-tang 已提交
80 81
#endif
#ifdef __AVX512F__
T
tensor-tang 已提交
82
INTRI8_FLOAT(jit::avx512f);
T
tensor-tang 已提交
83
#endif
T
tensor-tang 已提交
84
// TODO(TJ): eq16 test and complete avx512
T
tensor-tang 已提交
85 86 87
#undef INTRI8_FLOAT
#undef MKL_FLOAT
#undef MKL_DOUBLE
T
tensor-tang 已提交
88

T
tensor-tang 已提交
89
/* VADD JitKernel */
T
tensor-tang 已提交
90
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
T
tensor-tang 已提交
91 92 93 94 95 96
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 已提交
97
  }
T
tensor-tang 已提交
98
};
T
tensor-tang 已提交
99

T
tensor-tang 已提交
100
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
101
#define MKL_FLOAT(isa, block)                                                  \
T
tensor-tang 已提交
102 103 104 105
  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 已提交
106 107
  }

T
tensor-tang 已提交
108
#define MKL_DOUBLE(isa, block)                                    \
T
tensor-tang 已提交
109 110 111 112
  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 已提交
113 114
  }

T
tensor-tang 已提交
115 116
FOR_EACH_ISA(MKL_FLOAT, kGT16);
FOR_EACH_ISA_BLOCK(MKL_DOUBLE);
T
tensor-tang 已提交
117 118
#endif

T
tensor-tang 已提交
119
#define INTRI8_FLOAT(isa)                                                     \
T
tensor-tang 已提交
120 121 122 123 124 125 126 127
  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 已提交
128
  }
T
tensor-tang 已提交
129
#ifdef __AVX__
T
tensor-tang 已提交
130
INTRI8_FLOAT(jit::avx);
T
tensor-tang 已提交
131 132
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
133
INTRI8_FLOAT(jit::avx2);
T
tensor-tang 已提交
134 135
#endif
#ifdef __AVX512F__
T
tensor-tang 已提交
136
INTRI8_FLOAT(jit::avx512f);
T
tensor-tang 已提交
137
#endif
T
tensor-tang 已提交
138
// TODO(TJ): eq16 test and complete avx512
T
tensor-tang 已提交
139

T
tensor-tang 已提交
140 141 142
#undef INTRI8_FLOAT
#undef MKL_FLOAT
#undef MKL_DOUBLE
T
tensor-tang 已提交
143

T
tensor-tang 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/* VSCAL JitKernel */
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
class VScalKernelImpl : public VScalKernel<T> {
 public:
  void Compute(const int n, const T a, const T* x, T* y) override {
    for (int i = 0; i < n; ++i) {
      y[i] = a * x[i];
    }
  }
  void Compute(const int n, const T a, T* x) override {
    for (int i = 0; i < n; ++i) {
      x[i] = a * x[i];
    }
  }
};

#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
161
#define MKL_FLOAT(isa, block)                                                  \
T
tensor-tang 已提交
162 163 164 165 166 167
  template <>                                                                  \
  void VScalKernelImpl<float, isa, block>::Compute(const int n, const float a, \
                                                   float* x) {                 \
    platform::dynload::cblas_sscal(n, a, x, 1);                                \
  }

T
tensor-tang 已提交
168
#define MKL_DOUBLE(isa, block)                       \
T
tensor-tang 已提交
169 170 171 172 173 174
  template <>                                        \
  void VScalKernelImpl<double, isa, block>::Compute( \
      const int n, const double a, double* x) {      \
    platform::dynload::cblas_dscal(n, a, x, 1);      \
  }

T
tensor-tang 已提交
175 176
FOR_EACH_ISA(MKL_FLOAT, kGT16);
FOR_EACH_ISA_BLOCK(MKL_DOUBLE);
T
tensor-tang 已提交
177 178
#endif

T
tensor-tang 已提交
179
#define INTRI8_FLOAT(isa)                                                     \
T
tensor-tang 已提交
180 181 182 183 184 185 186 187 188
  template <>                                                                 \
  void VScalKernelImpl<float, isa, kEQ8>::Compute(const int n, const float a, \
                                                  const float* x, float* y) { \
    __m256 tmp;                                                               \
    __m256 scalar = _mm256_set1_ps(a);                                        \
    tmp = _mm256_loadu_ps(x);                                                 \
    tmp = _mm256_mul_ps(tmp, scalar);                                         \
    _mm256_storeu_ps(y, tmp);                                                 \
  }
T
tensor-tang 已提交
189
#define INTRI8_INPLACE_FLOAT(isa)                                             \
T
tensor-tang 已提交
190 191 192 193 194 195 196 197 198 199 200
  template <>                                                                 \
  void VScalKernelImpl<float, isa, kEQ8>::Compute(const int n, const float a, \
                                                  float* x) {                 \
    __m256 tmp;                                                               \
    __m256 scalar = _mm256_set1_ps(a);                                        \
    tmp = _mm256_loadu_ps(x);                                                 \
    tmp = _mm256_mul_ps(tmp, scalar);                                         \
    _mm256_storeu_ps(x, tmp);                                                 \
  }

#ifdef __AVX__
T
tensor-tang 已提交
201 202
INTRI8_FLOAT(jit::avx);
INTRI8_INPLACE_FLOAT(jit::avx);
T
tensor-tang 已提交
203 204
#endif
#ifdef __AVX2__
T
tensor-tang 已提交
205 206
INTRI8_FLOAT(jit::avx2);
INTRI8_INPLACE_FLOAT(jit::avx2);
T
tensor-tang 已提交
207 208
#endif
#ifdef __AVX512F__
T
tensor-tang 已提交
209 210
INTRI8_FLOAT(jit::avx512f);
INTRI8_INPLACE_FLOAT(jit::avx512f);
T
tensor-tang 已提交
211 212 213
#endif
// TODO(TJ): eq16 test and complete avx512

T
tensor-tang 已提交
214 215 216 217
#undef INTRI8_FLOAT
#undef INTRI8_INPLACE_FLOAT
#undef MKL_FLOAT
#undef MKL_DOUBLE
T
tensor-tang 已提交
218

T
tensor-tang 已提交
219 220 221
REGISTER_JITKERNEL(vmul, VMulKernel);
REGISTER_JITKERNEL(vadd, VAddKernel);
REGISTER_JITKERNEL(vscal, VScalKernel);
T
tensor-tang 已提交
222 223 224 225 226

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