jit_kernel_blas.cc 9.9 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"
18
#include "paddle/fluid/operators/math/jit_kernel_refer.h"
T
tensor-tang 已提交
19 20
#include "paddle/fluid/platform/enforce.h"

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

T
tensor-tang 已提交
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 {

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

T
tensor-tang 已提交
43 44 45 46
template <>
void VMulMKL<double>(const double* x, const double* y, double* z, int n) {
  platform::dynload::vdMul(n, x, y, z);
}
T
tensor-tang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59

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 已提交
60 61 62 63 64 65 66 67 68

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 {
69
    refer::VScal<float>(a, x, y, n);
T
tensor-tang 已提交
70 71 72 73 74 75 76 77
  }
}

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 {
78
    refer::VScal<double>(a, x, y, n);
T
tensor-tang 已提交
79 80 81
  }
}

T
tensor-tang 已提交
82 83
#endif

T
tensor-tang 已提交
84
/* VMUL JitKernel */
T
tensor-tang 已提交
85
template <typename T>
T
tensor-tang 已提交
86 87
class VMulKernelImpl : public VMulKernel<T> {
 public:
T
tensor-tang 已提交
88
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
89
  explicit VMulKernelImpl(int d) : VMulKernel<T>() {
T
tensor-tang 已提交
90
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
91
    if (useJIT(d)) {
T
tensor-tang 已提交
92
      // roughly estimate the size of code
93
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
94
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::mul, 0, false,
T
tensor-tang 已提交
95
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
96 97 98 99
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
    }
T
tensor-tang 已提交
100
#endif
T
tensor-tang 已提交
101
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
102 103 104 105
    if (useMKL(d)) {
      this->Compute = VMulMKL<T>;
      return;
    }
T
tensor-tang 已提交
106
#endif
107
    this->Compute = refer::VMul<T>;
T
tensor-tang 已提交
108 109
  }

T
tensor-tang 已提交
110
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
111

T
tensor-tang 已提交
112
 private:
T
tensor-tang 已提交
113
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
114
#endif
T
tensor-tang 已提交
115
};
T
tensor-tang 已提交
116

T
tensor-tang 已提交
117
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
118 119
template <>
bool VMulKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
120
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
121
}
T
tensor-tang 已提交
122
#endif
T
tensor-tang 已提交
123

T
tensor-tang 已提交
124
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
125 126
template <>
bool VMulKernelImpl<float>::useMKL(int d) {
T
tensor-tang 已提交
127
  return platform::MayIUse(platform::avx512f) && d > 512;
T
tensor-tang 已提交
128 129 130 131 132 133
}

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

T
tensor-tang 已提交
136 137
/* VAdd JitKernel */
template <typename T>
T
tensor-tang 已提交
138 139
class VAddKernelImpl : public VAddKernel<T> {
 public:
T
tensor-tang 已提交
140
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
141
  explicit VAddKernelImpl(int d) : VAddKernel<T>() {
T
tensor-tang 已提交
142
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
143
    if (useJIT(d)) {
144
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
145
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 0, false,
T
tensor-tang 已提交
146
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
147 148 149
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
150
    }
T
tensor-tang 已提交
151
#endif
T
tensor-tang 已提交
152 153 154 155
#ifdef PADDLE_WITH_MKLML
    if (useMKL(d)) {
      this->Compute = VAddMKL<T>;
      return;
T
tensor-tang 已提交
156
    }
T
tensor-tang 已提交
157
#endif
158
    this->Compute = refer::VAdd<T>;
T
tensor-tang 已提交
159
  }
T
fix mac  
tensor-tang 已提交
160
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
161 162

 private:
T
tensor-tang 已提交
163
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
fix mac  
tensor-tang 已提交
164
#endif
T
tensor-tang 已提交
165
};
T
tensor-tang 已提交
166

T
tensor-tang 已提交
167
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
168 169
template <>
bool VAddKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
170
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
171
}
T
tensor-tang 已提交
172
#endif
T
tensor-tang 已提交
173

T
tensor-tang 已提交
174
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
175 176 177 178
template <>
bool VAddKernelImpl<float>::useMKL(int d) {
  return d > 512;
}
T
tensor-tang 已提交
179

T
tensor-tang 已提交
180 181 182 183
template <>
bool VAddKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
184 185
#endif

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
#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};
P
peizhilin 已提交
215
#endif
216 217
};

P
peizhilin 已提交
218
#ifdef PADDLE_WITH_XBYAK
219 220 221 222 223 224 225
template <>
bool EltwiseMulnChw16cNCKernelImpl<float>::useJIT(int d) {
  return true;
}
#endif
#endif

T
tensor-tang 已提交
226 227 228 229
/* VAddRelu JitKernel */
template <typename T>
class VAddReluKernelImpl : public VAddReluKernel<T> {
 public:
T
tensor-tang 已提交
230
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
231
  explicit VAddReluKernelImpl(int d) : VAddReluKernel<T>() {
T
tensor-tang 已提交
232
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
233
    if (useJIT(d)) {
234
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
235
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::add, 0, true,
T
tensor-tang 已提交
236
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
237 238 239 240
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
    }
T
tensor-tang 已提交
241
#endif
242
    this->Compute = refer::VAddRelu<T>;
T
tensor-tang 已提交
243
  }
T
fix mac  
tensor-tang 已提交
244
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
245 246

 private:
T
tensor-tang 已提交
247
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
248
#endif
T
tensor-tang 已提交
249 250
};

T
tensor-tang 已提交
251
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
252 253
template <>
bool VAddReluKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
254
  return gen::VXXJitCode::init(d);
T
tensor-tang 已提交
255
}
T
tensor-tang 已提交
256 257
#endif

T
tensor-tang 已提交
258 259
/* VScal JitKernel */
template <typename T>
T
tensor-tang 已提交
260 261
class VScalKernelImpl : public VScalKernel<T> {
 public:
T
tensor-tang 已提交
262
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
263 264 265
  explicit VScalKernelImpl(int d) : VScalKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
266
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
267 268
      jitcode_.reset(new gen::VXXJitCode(d, gen::operand_type::mul, 1, false,
                                         sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
269 270 271
      this->Compute =
          jitcode_->getCode<void (*)(const T*, const T*, T*, int)>();
      return;
T
tensor-tang 已提交
272
    }
T
tensor-tang 已提交
273
#endif
T
tensor-tang 已提交
274
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
275 276 277 278
    if (useMKL(d)) {
      this->Compute = VScalMKL<T>;
      return;
    }
T
tensor-tang 已提交
279
#endif
280
    this->Compute = refer::VScal<T>;
T
tensor-tang 已提交
281
  }
T
tensor-tang 已提交
282
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
283

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

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

T
tensor-tang 已提交
296 297 298 299 300 301 302 303 304
#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 已提交
305
#endif
T
tensor-tang 已提交
306

T
tensor-tang 已提交
307
/* VAddBias JitKernel */
T
tensor-tang 已提交
308
template <typename T>
T
tensor-tang 已提交
309 310
class VAddBiasKernelImpl : public VAddBiasKernel<T> {
 public:
T
tensor-tang 已提交
311
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
312 313 314
  explicit VAddBiasKernelImpl(int d) : VAddBiasKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
315
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 4 * 8;
T
tensor-tang 已提交
316 317 318 319 320
      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 已提交
321
    }
T
tensor-tang 已提交
322
#endif
T
tensor-tang 已提交
323

324
    this->Compute = refer::VAddBias<T>;
T
tensor-tang 已提交
325
  }
T
tensor-tang 已提交
326
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
327

T
tensor-tang 已提交
328 329
 private:
  std::unique_ptr<gen::VXXJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
330
#endif
T
tensor-tang 已提交
331 332 333 334 335 336 337
};

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

T
tensor-tang 已提交
340
/* VRelu JitKernel */
T
tensor-tang 已提交
341
template <typename T>
T
tensor-tang 已提交
342 343
class VReluKernelImpl : public VReluKernel<T> {
 public:
T
tensor-tang 已提交
344
  JITKERNEL_DECLARE_STATIC_FUNC;
T
tensor-tang 已提交
345 346 347
  explicit VReluKernelImpl(int d) : VReluKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
T
tensor-tang 已提交
348
      size_t sz = 96 /* init size */ +
349
                  d / YMM_FLOAT_BLOCK * 4 /* instructions */ *
T
tensor-tang 已提交
350
                      8 /* average bytes for each instruction */;
351 352
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::relu,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
353 354
      this->Compute = jitcode_->getCode<void (*)(const T*, T*, int)>();
      return;
T
tensor-tang 已提交
355
    }
T
tensor-tang 已提交
356
#endif
T
tensor-tang 已提交
357

358
    this->Compute = refer::VRelu<T>;
T
tensor-tang 已提交
359
  }
T
tensor-tang 已提交
360
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
361

T
tensor-tang 已提交
362
 private:
363
  std::unique_ptr<gen::VActJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
364
#endif
T
tensor-tang 已提交
365 366 367 368 369
};

#ifdef PADDLE_WITH_XBYAK
template <>
bool VReluKernelImpl<float>::useJIT(int d) {
370
  return gen::VActJitCode::init(d, gen::operand_type::relu);
T
tensor-tang 已提交
371
}
T
tensor-tang 已提交
372 373 374
#endif

/* An empty JitKernel */
T
tensor-tang 已提交
375
template <typename T>
T
tensor-tang 已提交
376 377
class VIdentityKernelImpl : public VIdentityKernel<T> {
 public:
T
tensor-tang 已提交
378 379
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit VIdentityKernelImpl(int d) : VIdentityKernel<T>() {
380
    this->Compute = refer::VIdentity<T>;
T
tensor-tang 已提交
381
  }
T
tensor-tang 已提交
382 383
};

T
tensor-tang 已提交
384 385 386 387 388 389 390
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);
391 392 393
#ifdef PADDLE_WITH_MKLDNN
REGISTER_JITKERNEL(eltwise_mul_nchw16c, EltwiseMulnChw16cNCKernel);
#endif
T
tensor-tang 已提交
394 395 396 397 398

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