jit_kernel_blas.cc 8.0 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
class VMulKernelImpl : public VMulKernel<T> {
 public:
T
tensor-tang 已提交
37
  void Compute(const int n, const T* x, const T* y, T* z) const override {
T
tensor-tang 已提交
38 39 40
    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 46 47 48 49
#define MKL_FLOAT(isa, block)                                        \
  template <>                                                        \
  void VMulKernelImpl<float, isa, block>::Compute(                   \
      const int n, const float* x, const float* y, float* z) const { \
    platform::dynload::vsMul(n, x, y, z);                            \
T
tensor-tang 已提交
50 51
  }

T
tensor-tang 已提交
52 53 54 55 56
#define MKL_DOUBLE(isa, block)                                          \
  template <>                                                           \
  void VMulKernelImpl<double, isa, block>::Compute(                     \
      const int n, const double* x, const double* y, double* z) const { \
    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 64 65 66 67 68 69 70 71
#define INTRI8_FLOAT(isa)                                            \
  template <>                                                        \
  void VMulKernelImpl<float, isa, kEQ8>::Compute(                    \
      const int n, const float* x, const float* y, float* z) const { \
    __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
class VAddKernelImpl : public VAddKernel<T> {
 public:
T
tensor-tang 已提交
93
  void Compute(const int n, const T* x, const T* y, T* z) const override {
T
tensor-tang 已提交
94 95 96
    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 102 103 104 105
#define MKL_FLOAT(isa, block)                                        \
  template <>                                                        \
  void VAddKernelImpl<float, isa, block>::Compute(                   \
      const int n, const float* x, const float* y, float* z) const { \
    platform::dynload::vsAdd(n, x, y, z);                            \
T
tensor-tang 已提交
106 107
  }

T
tensor-tang 已提交
108 109 110 111 112
#define MKL_DOUBLE(isa, block)                                          \
  template <>                                                           \
  void VAddKernelImpl<double, isa, block>::Compute(                     \
      const int n, const double* x, const double* y, double* z) const { \
    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 120 121 122 123 124 125 126 127
#define INTRI8_FLOAT(isa)                                            \
  template <>                                                        \
  void VAddKernelImpl<float, isa, kEQ8>::Compute(                    \
      const int n, const float* x, const float* y, float* z) const { \
    __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
/* VSCAL JitKernel */
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
class VScalKernelImpl : public VScalKernel<T> {
 public:
T
tensor-tang 已提交
148
  void Compute(const int n, const T a, const T* x, T* y) const override {
T
tensor-tang 已提交
149 150 151 152
    for (int i = 0; i < n; ++i) {
      y[i] = a * x[i];
    }
  }
T
tensor-tang 已提交
153
  void Compute(const int n, const T a, T* x) const override {
T
tensor-tang 已提交
154 155 156 157 158 159 160
    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
  template <>                                                                  \
  void VScalKernelImpl<float, isa, block>::Compute(const int n, const float a, \
T
tensor-tang 已提交
164
                                                   float* x) const {           \
T
tensor-tang 已提交
165 166 167
    platform::dynload::cblas_sscal(n, a, x, 1);                                \
  }

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

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 180 181 182 183 184 185 186 187
#define INTRI8_FLOAT(isa)                                           \
  template <>                                                       \
  void VScalKernelImpl<float, isa, kEQ8>::Compute(                  \
      const int n, const float a, const float* x, float* y) const { \
    __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 已提交
188
  }
T
tensor-tang 已提交
189
#define INTRI8_INPLACE_FLOAT(isa)                                             \
T
tensor-tang 已提交
190 191
  template <>                                                                 \
  void VScalKernelImpl<float, isa, kEQ8>::Compute(const int n, const float a, \
T
tensor-tang 已提交
192
                                                  float* x) const {           \
T
tensor-tang 已提交
193 194 195 196 197 198 199 200
    __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