jit_kernel_blas.cc 10.7 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 28 29 30 31 32 33
#ifdef PADDLE_WITH_MKLML
#include "paddle/fluid/platform/dynload/mklml.h"
#endif

namespace paddle {
namespace operators {
namespace math {
namespace jitkernel {
namespace jit = platform::jit;

T
tensor-tang 已提交
34 35 36 37
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 已提交
38
  }
T
tensor-tang 已提交
39
}
T
tensor-tang 已提交
40

T
tensor-tang 已提交
41 42 43 44 45 46 47
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 已提交
48 49 50 51 52 53 54 55
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 已提交
56 57 58 59 60 61 62
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 已提交
63 64 65 66 67 68 69
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 已提交
70 71 72 73 74 75 76
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 已提交
77 78 79 80 81 82 83 84
#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 已提交
85

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

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 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

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 已提交
125 126
#endif

T
tensor-tang 已提交
127
/* VMUL JitKernel */
T
tensor-tang 已提交
128
template <typename T>
T
tensor-tang 已提交
129 130
class VMulKernelImpl : public VMulKernel<T> {
 public:
T
tensor-tang 已提交
131
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
132
  explicit VMulKernelImpl(int d) : VMulKernel<T>() {
T
tensor-tang 已提交
133
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
134
    if (useJIT(d)) {
T
tensor-tang 已提交
135
      // roughly estimate the size of code
136
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
137
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::mul, 0, false,
T
tensor-tang 已提交
138
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
139 140 141 142
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
    }
T
tensor-tang 已提交
143
#endif
T
tensor-tang 已提交
144
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
145 146 147 148
    if (useMKL(d)) {
      this->Compute = VMulMKL<T>;
      return;
    }
T
tensor-tang 已提交
149
#endif
T
tensor-tang 已提交
150
    this->Compute = VMulRefer<T>;
T
tensor-tang 已提交
151 152
  }

T
tensor-tang 已提交
153
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
154

T
tensor-tang 已提交
155
 private:
T
tensor-tang 已提交
156
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
157
#endif
T
tensor-tang 已提交
158
};
T
tensor-tang 已提交
159

T
tensor-tang 已提交
160
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
161 162
template <>
bool VMulKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
163
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
164
}
T
tensor-tang 已提交
165
#endif
T
tensor-tang 已提交
166

T
tensor-tang 已提交
167
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
168 169 170 171 172 173 174 175 176
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 已提交
177
#endif
T
tensor-tang 已提交
178

T
tensor-tang 已提交
179 180
/* VAdd JitKernel */
template <typename T>
T
tensor-tang 已提交
181 182
class VAddKernelImpl : public VAddKernel<T> {
 public:
T
tensor-tang 已提交
183
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
184
  explicit VAddKernelImpl(int d) : VAddKernel<T>() {
T
tensor-tang 已提交
185
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
186
    if (useJIT(d)) {
187
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
188
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 0, false,
T
tensor-tang 已提交
189
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
190 191 192
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
193
    }
T
tensor-tang 已提交
194
#endif
T
tensor-tang 已提交
195 196 197 198
#ifdef PADDLE_WITH_MKLML
    if (useMKL(d)) {
      this->Compute = VAddMKL<T>;
      return;
T
tensor-tang 已提交
199
    }
T
tensor-tang 已提交
200 201
#endif
    this->Compute = VAddRefer<T>;
T
tensor-tang 已提交
202
  }
T
fix mac  
tensor-tang 已提交
203
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
204 205

 private:
T
tensor-tang 已提交
206
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
fix mac  
tensor-tang 已提交
207
#endif
T
tensor-tang 已提交
208
};
T
tensor-tang 已提交
209

T
tensor-tang 已提交
210
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
211 212
template <>
bool VAddKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
213
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
214
}
T
tensor-tang 已提交
215
#endif
T
tensor-tang 已提交
216

T
tensor-tang 已提交
217
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
218 219 220 221
template <>
bool VAddKernelImpl<float>::useMKL(int d) {
  return d > 512;
}
T
tensor-tang 已提交
222

T
tensor-tang 已提交
223 224 225 226
template <>
bool VAddKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
227 228
#endif

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
#ifdef PADDLE_WITH_MKLDNN
/* EltwiseMul for nChw16c & NC inputs JitKernel */
template <typename T>
class EltwiseMulnChw16cNCKernelImpl
    : public math::jitkernel::EltwiseMulnChw16cNCKernel<T> {
 public:
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit EltwiseMulnChw16cNCKernelImpl(int d)
      : EltwiseMulnChw16cNCKernel<T>() {
    using mul_func_t = void (*)(const float*, const float*, float*, int, int);
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
      // roughly estimate the size of code
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
      sz = sz > 4096 ? sz : 4096;
      jitcode_.reset(new gen::EltwiseMulnChw16cNC(sz));
      this->Compute = (mul_func_t)jitcode_->getCode();
      return;
    }
#endif
    PADDLE_THROW(
        "This kernel shouldn't be used in Non-Xbyak, Non-MKL-DNN "
        "environemnt");
  }

#ifdef PADDLE_WITH_XBYAK

 private:
  std::unique_ptr<gen::EltwiseMulnChw16cNC> jitcode_{nullptr};
};

template <>
bool EltwiseMulnChw16cNCKernelImpl<float>::useJIT(int d) {
  return true;
}
#endif
#endif

T
tensor-tang 已提交
267 268 269 270
/* VAddRelu JitKernel */
template <typename T>
class VAddReluKernelImpl : public VAddReluKernel<T> {
 public:
T
tensor-tang 已提交
271
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
272
  explicit VAddReluKernelImpl(int d) : VAddReluKernel<T>() {
T
tensor-tang 已提交
273
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
274
    if (useJIT(d)) {
275
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
276
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 0, true,
T
tensor-tang 已提交
277
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
278 279 280 281
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
    }
T
tensor-tang 已提交
282
#endif
T
tensor-tang 已提交
283 284
    this->Compute = VAddReluRefer<T>;
  }
T
fix mac  
tensor-tang 已提交
285
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
286 287

 private:
T
tensor-tang 已提交
288
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
289
#endif
T
tensor-tang 已提交
290 291
};

T
tensor-tang 已提交
292
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
293 294
template <>
bool VAddReluKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
295
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
296
}
T
tensor-tang 已提交
297 298
#endif

T
tensor-tang 已提交
299 300
/* VScal JitKernel */
template <typename T>
T
tensor-tang 已提交
301 302
class VScalKernelImpl : public VScalKernel<T> {
 public:
T
tensor-tang 已提交
303
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
304 305 306
  explicit VScalKernelImpl(int d) : VScalKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
307
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
308 309
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::mul, 1, false,
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
310 311 312
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
313
    }
T
tensor-tang 已提交
314
#endif
T
tensor-tang 已提交
315
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
316 317 318 319
    if (useMKL(d)) {
      this->Compute = VScalMKL<T>;
      return;
    }
T
tensor-tang 已提交
320
#endif
T
tensor-tang 已提交
321
    this->Compute = VScalRefer<T>;
T
tensor-tang 已提交
322
  }
T
tensor-tang 已提交
323
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
324

T
tensor-tang 已提交
325
 private:
T
tensor-tang 已提交
326
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
327
#endif
T
tensor-tang 已提交
328 329 330 331 332
};

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

T
tensor-tang 已提交
337 338 339 340 341 342 343 344 345
#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 已提交
346
#endif
T
tensor-tang 已提交
347

T
tensor-tang 已提交
348
/* VAddBias JitKernel */
T
tensor-tang 已提交
349
template <typename T>
T
tensor-tang 已提交
350 351
class VAddBiasKernelImpl : public VAddBiasKernel<T> {
 public:
T
tensor-tang 已提交
352
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
353 354 355
  explicit VAddBiasKernelImpl(int d) : VAddBiasKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
356
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
357 358 359 360 361
      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 已提交
362
    }
T
tensor-tang 已提交
363
#endif
T
tensor-tang 已提交
364

T
tensor-tang 已提交
365
    this->Compute = VAddBiasRefer<T>;
T
tensor-tang 已提交
366
  }
T
tensor-tang 已提交
367
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
368

T
tensor-tang 已提交
369 370
 private:
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
371
#endif
T
tensor-tang 已提交
372 373 374 375 376 377 378
};

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

T
tensor-tang 已提交
381
/* VRelu JitKernel */
T
tensor-tang 已提交
382
template <typename T>
T
tensor-tang 已提交
383 384
class VReluKernelImpl : public VReluKernel<T> {
 public:
T
tensor-tang 已提交
385
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
386 387 388
  explicit VReluKernelImpl(int d) : VReluKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
T
tensor-tang 已提交
389
      size_t sz = 96 /* init size */ +
390
                  d / YMM_FLOAT_BLOCK * 4 /* instructions */ *
T
tensor-tang 已提交
391
                      8 /* average bytes for each instruction */;
392 393
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::relu,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
394 395
      this->Compute = jitcode_->getCode<void (*)(const T*, T*, int)>();
      return;
T
tensor-tang 已提交
396
    }
T
tensor-tang 已提交
397
#endif
T
tensor-tang 已提交
398

T
tensor-tang 已提交
399
    this->Compute = VReluRefer<T>;
T
tensor-tang 已提交
400
  }
T
tensor-tang 已提交
401
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
402

T
tensor-tang 已提交
403
 private:
404
  std::unique_ptr<gen::VActJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
405
#endif
T
tensor-tang 已提交
406 407 408 409 410
};

#ifdef PADDLE_WITH_XBYAK
template <>
bool VReluKernelImpl<float>::useJIT(int d) {
411
  return gen::VActJitCode::init(d, gen::operand_type::relu);
T
tensor-tang 已提交
412
}
T
tensor-tang 已提交
413 414
#endif

T
tensor-tang 已提交
415 416
template <typename T>
inline void VIdentityRefer(const T* x, T* y, int n) {}
T
tensor-tang 已提交
417

T
tensor-tang 已提交
418
/* An empty JitKernel */
T
tensor-tang 已提交
419
template <typename T>
T
tensor-tang 已提交
420 421
class VIdentityKernelImpl : public VIdentityKernel<T> {
 public:
T
tensor-tang 已提交
422 423 424 425
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit VIdentityKernelImpl(int d) : VIdentityKernel<T>() {
    this->Compute = VIdentityRefer<T>;
  }
T
tensor-tang 已提交
426 427
};

T
tensor-tang 已提交
428 429 430 431 432 433 434
REGISTER_JITKERNEL(vmul, VMulKernel);
REGISTER_JITKERNEL(vadd, VAddKernel);
REGISTER_JITKERNEL(vaddrelu, VAddReluKernel);
REGISTER_JITKERNEL(vscal, VScalKernel);
REGISTER_JITKERNEL(vaddbias, VAddBiasKernel);
REGISTER_JITKERNEL(vrelu, VReluKernel);
REGISTER_JITKERNEL(videntity, VIdentityKernel);
435 436 437
#ifdef PADDLE_WITH_MKLDNN
REGISTER_JITKERNEL(eltwise_mul_nchw16c, EltwiseMulnChw16cNCKernel);
#endif
T
tensor-tang 已提交
438 439 440 441 442

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