jit_kernel_blas.cc 10.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
#include "paddle/fluid/platform/enforce.h"

T
tensor-tang 已提交
20 21 22 23
#ifdef PADDLE_WITH_XBYAK
#include "paddle/fluid/operators/math/jit_code.h"
#endif

T
tensor-tang 已提交
24 25 26 27
#ifdef PADDLE_WITH_MKLML
#include "paddle/fluid/platform/dynload/mklml.h"
#endif

28 29 30 31
#ifdef __AVX__
#include <immintrin.h>
#endif

T
tensor-tang 已提交
32 33 34 35 36 37
namespace paddle {
namespace operators {
namespace math {
namespace jitkernel {
namespace jit = platform::jit;

T
tensor-tang 已提交
38 39 40 41
template <typename T>
void VMulRefer(const T* x, const T* y, T* z, int n) {
  for (int i = 0; i < n; ++i) {
    z[i] = x[i] * y[i];
T
tensor-tang 已提交
42
  }
T
tensor-tang 已提交
43
}
T
tensor-tang 已提交
44

T
tensor-tang 已提交
45 46 47 48 49 50 51
template <typename T>
void VAddRefer(const T* x, const T* y, T* z, int n) {
  for (int i = 0; i < n; ++i) {
    z[i] = x[i] + y[i];
  }
}

T
tensor-tang 已提交
52 53 54 55 56 57 58 59
template <typename T>
void VAddReluRefer(const T* x, const T* y, T* z, int n) {
  for (int i = 0; i < n; ++i) {
    z[i] = x[i] + y[i];
    z[i] = z[i] > 0 ? z[i] : 0;
  }
}

T
tensor-tang 已提交
60 61 62 63 64 65 66
template <typename T>
void VScalRefer(const T* a, const T* x, T* y, int n) {
  for (int i = 0; i < n; ++i) {
    y[i] = a[0] * x[i];
  }
}

T
tensor-tang 已提交
67 68 69 70 71 72 73
template <typename T>
void VAddBiasRefer(const T* a, const T* x, T* y, int n) {
  for (int i = 0; i < n; ++i) {
    y[i] = a[0] + x[i];
  }
}

T
tensor-tang 已提交
74 75 76 77 78 79 80
template <typename T>
void VReluRefer(const T* x, T* y, int n) {
  for (int i = 0; i < n; ++i) {
    y[i] = x[i] > 0 ? x[i] : 0;
  }
}

T
tensor-tang 已提交
81 82 83 84 85 86 87 88
#ifdef PADDLE_WITH_MKLML
template <typename T>
void VMulMKL(const T* x, const T* y, T* z, int n);

template <>
void VMulMKL<float>(const float* x, const float* y, float* z, int n) {
  platform::dynload::vsMul(n, x, y, z);
}
T
tensor-tang 已提交
89

T
tensor-tang 已提交
90 91 92 93
template <>
void VMulMKL<double>(const double* x, const double* y, double* z, int n) {
  platform::dynload::vdMul(n, x, y, z);
}
T
tensor-tang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106

template <typename T>
void VAddMKL(const T* x, const T* y, T* z, int n);

template <>
void VAddMKL<float>(const float* x, const float* y, float* z, int n) {
  platform::dynload::vsAdd(n, x, y, z);
}

template <>
void VAddMKL<double>(const double* x, const double* y, double* z, int n) {
  platform::dynload::vdAdd(n, x, y, z);
}
T
tensor-tang 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

template <typename T>
void VScalMKL(const T* a, const T* x, T* y, int n);

template <>
void VScalMKL<float>(const float* a, const float* x, float* y, int n) {
  if (x == y) {
    platform::dynload::cblas_sscal(n, *a, y, 1);
  } else {
    VScalRefer<float>(a, x, y, n);
  }
}

template <>
void VScalMKL<double>(const double* a, const double* x, double* y, int n) {
  if (x == y) {
    platform::dynload::cblas_dscal(n, *a, y, 1);
  } else {
    VScalRefer<double>(a, x, y, n);
  }
}

T
tensor-tang 已提交
129 130
#endif

T
tensor-tang 已提交
131 132 133 134 135 136 137
#define DECLARE_STATIC_FUNC                                 \
  static inline std::string name(int d) {                   \
    PADDLE_THROW("DType should be either float or double"); \
  }                                                         \
  static inline bool useJIT(int d) { return false; }        \
  static inline bool useMKL(int d) { return false; }

T
tensor-tang 已提交
138
/* VMUL JitKernel */
T
tensor-tang 已提交
139
template <typename T>
T
tensor-tang 已提交
140 141
class VMulKernelImpl : public VMulKernel<T> {
 public:
T
tensor-tang 已提交
142
  DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
143
  explicit VMulKernelImpl(int d) : VMulKernel<T>() {
T
tensor-tang 已提交
144
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
145
    if (useJIT(d)) {
T
tensor-tang 已提交
146 147
      // roughly estimate the size of code
      size_t sz = 96 + d / AVX_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
148
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::mul, 0, false,
T
tensor-tang 已提交
149
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
150 151 152 153
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
    }
T
tensor-tang 已提交
154
#endif
T
tensor-tang 已提交
155
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
156 157 158 159
    if (useMKL(d)) {
      this->Compute = VMulMKL<T>;
      return;
    }
T
tensor-tang 已提交
160
#endif
T
tensor-tang 已提交
161
    this->Compute = VMulRefer<T>;
T
tensor-tang 已提交
162 163
  }

T
tensor-tang 已提交
164
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
165

T
tensor-tang 已提交
166
 private:
T
tensor-tang 已提交
167
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
168
#endif
T
tensor-tang 已提交
169
};
T
tensor-tang 已提交
170

T
tensor-tang 已提交
171
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
172 173
template <>
bool VMulKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
174
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
175
}
T
tensor-tang 已提交
176
#endif
T
tensor-tang 已提交
177

T
tensor-tang 已提交
178
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
179 180 181 182 183 184 185 186 187
template <>
bool VMulKernelImpl<float>::useMKL(int d) {
  return jit::MayIUse(jit::avx512f) && d > 512;
}

template <>
bool VMulKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
188
#endif
T
tensor-tang 已提交
189

T
tensor-tang 已提交
190 191
/* VAdd JitKernel */
template <typename T>
T
tensor-tang 已提交
192 193
class VAddKernelImpl : public VAddKernel<T> {
 public:
T
tensor-tang 已提交
194 195
  DECLARE_STATIC_FUNC;
  explicit VAddKernelImpl(int d) : VAddKernel<T>() {
T
tensor-tang 已提交
196
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
197 198
    if (useJIT(d)) {
      size_t sz = 96 + d / AVX_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
199
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 0, false,
T
tensor-tang 已提交
200
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
201 202 203
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
204
    }
T
tensor-tang 已提交
205
#endif
T
tensor-tang 已提交
206 207 208 209
#ifdef PADDLE_WITH_MKLML
    if (useMKL(d)) {
      this->Compute = VAddMKL<T>;
      return;
T
tensor-tang 已提交
210
    }
T
tensor-tang 已提交
211 212
#endif
    this->Compute = VAddRefer<T>;
T
tensor-tang 已提交
213
  }
T
fix mac  
tensor-tang 已提交
214
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
215 216

 private:
T
tensor-tang 已提交
217
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
fix mac  
tensor-tang 已提交
218
#endif
T
tensor-tang 已提交
219
};
T
tensor-tang 已提交
220

T
tensor-tang 已提交
221
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
222 223
template <>
bool VAddKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
224
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
225
}
T
tensor-tang 已提交
226
#endif
T
tensor-tang 已提交
227

T
tensor-tang 已提交
228
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
229 230 231 232
template <>
bool VAddKernelImpl<float>::useMKL(int d) {
  return d > 512;
}
T
tensor-tang 已提交
233

T
tensor-tang 已提交
234 235 236 237
template <>
bool VAddKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
238 239
#endif

T
tensor-tang 已提交
240 241 242 243 244 245
/* VAddRelu JitKernel */
template <typename T>
class VAddReluKernelImpl : public VAddReluKernel<T> {
 public:
  DECLARE_STATIC_FUNC;
  explicit VAddReluKernelImpl(int d) : VAddReluKernel<T>() {
T
tensor-tang 已提交
246
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
247 248
    if (useJIT(d)) {
      size_t sz = 96 + d / AVX_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
249
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 0, true,
T
tensor-tang 已提交
250
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
251 252 253 254
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
    }
T
tensor-tang 已提交
255
#endif
T
tensor-tang 已提交
256 257
    this->Compute = VAddReluRefer<T>;
  }
T
fix mac  
tensor-tang 已提交
258
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
259 260

 private:
T
tensor-tang 已提交
261
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
262
#endif
T
tensor-tang 已提交
263 264
};

T
tensor-tang 已提交
265
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
266 267
template <>
bool VAddReluKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
268
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
269
}
T
tensor-tang 已提交
270 271
#endif

T
tensor-tang 已提交
272 273
/* VScal JitKernel */
template <typename T>
T
tensor-tang 已提交
274 275
class VScalKernelImpl : public VScalKernel<T> {
 public:
T
tensor-tang 已提交
276 277 278 279 280
  DECLARE_STATIC_FUNC;
  explicit VScalKernelImpl(int d) : VScalKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
      size_t sz = 96 + d / AVX_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
281 282
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::mul, 1, false,
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
283 284 285
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
286
    }
T
tensor-tang 已提交
287
#endif
T
tensor-tang 已提交
288
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
289 290 291 292
    if (useMKL(d)) {
      this->Compute = VScalMKL<T>;
      return;
    }
T
tensor-tang 已提交
293
#endif
T
tensor-tang 已提交
294
    this->Compute = VScalRefer<T>;
T
tensor-tang 已提交
295
  }
T
tensor-tang 已提交
296
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
297

T
tensor-tang 已提交
298
 private:
T
tensor-tang 已提交
299
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
300
#endif
T
tensor-tang 已提交
301 302 303 304 305
};

#ifdef PADDLE_WITH_XBYAK
template <>
bool VScalKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
306
  return gen::VXXJitCode::init(d, 1);
T
tensor-tang 已提交
307
}
T
tensor-tang 已提交
308 309
#endif

T
tensor-tang 已提交
310 311 312 313 314 315 316 317 318
#ifdef PADDLE_WITH_MKLML
template <>
bool VScalKernelImpl<float>::useMKL(int d) {
  return d > 512;
}
template <>
bool VScalKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
319
#endif
T
tensor-tang 已提交
320

T
tensor-tang 已提交
321
/* VAddBias JitKernel */
T
tensor-tang 已提交
322
template <typename T>
T
tensor-tang 已提交
323 324
class VAddBiasKernelImpl : public VAddBiasKernel<T> {
 public:
T
tensor-tang 已提交
325 326 327 328 329 330 331 332 333 334
  DECLARE_STATIC_FUNC;
  explicit VAddBiasKernelImpl(int d) : VAddBiasKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
      size_t sz = 96 + d / AVX_FLOAT_BLOCK * 4 * 8;
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 1, false,
                                         sz > 4096 ? sz : 4096));
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
335
    }
T
tensor-tang 已提交
336
#endif
T
tensor-tang 已提交
337

T
tensor-tang 已提交
338
    this->Compute = VAddBiasRefer<T>;
T
tensor-tang 已提交
339
  }
T
tensor-tang 已提交
340
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
341

T
tensor-tang 已提交
342 343
 private:
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
344
#endif
T
tensor-tang 已提交
345 346 347 348 349 350 351
};

#ifdef PADDLE_WITH_XBYAK
template <>
bool VAddBiasKernelImpl<float>::useJIT(int d) {
  return gen::VXXJitCode::init(d, 1);
}
T
tensor-tang 已提交
352 353
#endif

T
tensor-tang 已提交
354
/* VRelu JitKernel */
T
tensor-tang 已提交
355
template <typename T>
T
tensor-tang 已提交
356 357
class VReluKernelImpl : public VReluKernel<T> {
 public:
T
tensor-tang 已提交
358 359 360 361 362 363 364 365 366 367 368
  DECLARE_STATIC_FUNC;
  explicit VReluKernelImpl(int d) : VReluKernel<T>() {
    this->num_ = d;  // TODO(TJ): remove me when ComputeDeprecated done
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
      size_t sz = 96 /*init*/ +
                  d / AVX_FLOAT_BLOCK * 4 /* instructions*/ *
                      8 /*everage byte for each instruction*/;
      jitcode_.reset(new gen::ReluJitCode(d, sz > 4096 ? sz : 4096));
      this->Compute = jitcode_->getCode<void (*)(const T*, T*, int)>();
      return;
T
tensor-tang 已提交
369
    }
T
tensor-tang 已提交
370
#endif
T
tensor-tang 已提交
371

T
tensor-tang 已提交
372
    this->Compute = VReluRefer<T>;
T
tensor-tang 已提交
373
  }
T
tensor-tang 已提交
374 375
  void ComputeDeprecated(const T* x, T* y) const override {
    VReluRefer(x, y, this->num_);
T
tensor-tang 已提交
376
  }
T
tensor-tang 已提交
377
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
378

T
tensor-tang 已提交
379 380
 private:
  std::unique_ptr<gen::ReluJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
381
#endif
T
tensor-tang 已提交
382 383 384 385 386 387 388
};

#ifdef PADDLE_WITH_XBYAK
template <>
bool VReluKernelImpl<float>::useJIT(int d) {
  return gen::ReluJitCode::init(d);
}
T
tensor-tang 已提交
389 390
#endif

T
tensor-tang 已提交
391 392 393 394 395 396 397 398
#undef DECLARE_STATIC_FUNC

REGISTER_JITKERNEL(vmul, VMulKernel);
REGISTER_JITKERNEL(vadd, VAddKernel);
REGISTER_JITKERNEL(vaddrelu, VAddReluKernel);
REGISTER_JITKERNEL(vscal, VScalKernel);
REGISTER_JITKERNEL(vaddbias, VAddBiasKernel);
REGISTER_JITKERNEL(vrelu, VReluKernel);
T
tensor-tang 已提交
399

T
tensor-tang 已提交
400 401 402 403 404
/* An empty JitKernel */
template <typename T, platform::jit::cpu_isa_t isa, jit_block>
class VIdentityKernelImpl : public VIdentityKernel<T> {
 public:
  explicit VIdentityKernelImpl(int d) : VIdentityKernel<T>() { this->num_ = d; }
T
tensor-tang 已提交
405
  void ComputeDeprecated(const T* x, T* y) const override {}
T
tensor-tang 已提交
406 407
};

T
tensor-tang 已提交
408
REGISTER_JITKERNEL_DEPRECATED(videntity, VIdentityKernel);
T
tensor-tang 已提交
409 410 411 412 413

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