jit_kernel_exp.cc 5.6 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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>
#include "paddle/fluid/operators/math/jit_kernel_macro.h"
18
#include "paddle/fluid/operators/math/jit_kernel_refer.h"
T
tensor-tang 已提交
19 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
#ifdef PADDLE_WITH_MKLML
#include "paddle/fluid/platform/dynload/mklml.h"
#endif

namespace paddle {
namespace operators {
namespace math {
namespace jitkernel {

T
tensor-tang 已提交
33
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
34
// try to use MKL to speedup
T
tensor-tang 已提交
35 36 37 38 39 40 41 42 43 44 45 46
template <typename T>
void VExpMKL(const T* x, T* y, int n);

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

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

template <typename T>
void VSigmoidMKL(const T* x, T* y, int n) {
  const T min = SIGMOID_THRESHOLD_MIN;
  const T max = SIGMOID_THRESHOLD_MAX;
  for (int i = 0; i < n; ++i) {
    y[i] = (x[i] < min) ? min : ((x[i] > max) ? max : x[i]);
    y[i] = static_cast<T>(0) - y[i];
  }
  VExpMKL(y, y, n);
  for (int i = 0; i < n; ++i) {
    y[i] = static_cast<T>(1) / (static_cast<T>(1) + y[i]);
  }
}
T
tensor-tang 已提交
61 62 63 64 65 66 67 68 69 70 71

template <typename T>
void VTanhMKL(const T* x, T* y, int n) {
  for (int i = 0; i < n; ++i) {
    y[i] = static_cast<T>(2) * x[i];
  }
  VSigmoidMKL(y, y, n);
  for (int i = 0; i < n; ++i) {
    y[i] = static_cast<T>(2) * y[i] - static_cast<T>(1);
  }
}
T
tensor-tang 已提交
72 73
#endif

T
tensor-tang 已提交
74
/* VExp JitKernel */
T
tensor-tang 已提交
75
template <typename T>
T
tensor-tang 已提交
76 77
class VExpKernelImpl : public VExpKernel<T> {
 public:
T
tensor-tang 已提交
78 79 80 81
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit VExpKernelImpl(int d) : VExpKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
82
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 70 * 8;
83 84
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::exp,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
85 86 87 88 89 90 91 92
      this->Compute = jitcode_->getCode<void (*)(const T*, T*, int)>();
      return;
    }
#endif
#ifdef PADDLE_WITH_MKLML
    if (useMKL(d)) {
      this->Compute = VExpMKL<T>;
      return;
T
tensor-tang 已提交
93
    }
T
tensor-tang 已提交
94
#endif
95
    this->Compute = refer::VExp<T>;
T
tensor-tang 已提交
96
  }
T
tensor-tang 已提交
97

T
tensor-tang 已提交
98 99 100
#ifdef PADDLE_WITH_XBYAK

 private:
101
  std::unique_ptr<gen::VActJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
102
#endif
T
tensor-tang 已提交
103 104
};

T
tensor-tang 已提交
105 106 107
#ifdef PADDLE_WITH_XBYAK
template <>
bool VExpKernelImpl<float>::useJIT(int d) {
108
  return gen::VActJitCode::init(d, gen::operand_type::exp);
T
tensor-tang 已提交
109 110 111
}
#endif

T
tensor-tang 已提交
112
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
113 114 115 116
template <>
bool VExpKernelImpl<float>::useMKL(int d) {
  return d > 512;
}
T
tensor-tang 已提交
117

T
tensor-tang 已提交
118 119 120 121
template <>
bool VExpKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
122 123 124 125 126 127 128 129 130 131 132

#endif

/* VSigmoid JitKernel */
template <typename T>
class VSigmoidKernelImpl : public VSigmoidKernel<T> {
 public:
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit VSigmoidKernelImpl(int d) : VSigmoidKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
133
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 82 * 8;
134 135
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::sigmoid,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
136 137 138 139 140 141 142 143 144 145 146 147
      this->Compute = jitcode_->getCode<void (*)(const T*, T*, int)>();
      return;
    }
#endif

#ifdef PADDLE_WITH_MKLML
    // strictly it's a better impl with MKL, then is refer
    if (useMKL(d)) {
      this->Compute = VSigmoidMKL<T>;
      return;
    }
#endif
148
    this->Compute = refer::VSigmoid<T>;
T
tensor-tang 已提交
149
  }
T
tensor-tang 已提交
150

T
tensor-tang 已提交
151 152 153
#ifdef PADDLE_WITH_XBYAK

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

#ifdef PADDLE_WITH_XBYAK
template <>
bool VSigmoidKernelImpl<float>::useJIT(int d) {
161
  return gen::VActJitCode::init(d, gen::operand_type::sigmoid);
T
tensor-tang 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
}
#endif

#ifdef PADDLE_WITH_MKLML
template <>
bool VSigmoidKernelImpl<float>::useMKL(int d) {
  return d > 512;
}

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

T
tensor-tang 已提交
177 178 179 180 181 182 183 184
/* VTanh JitKernel */
template <typename T>
class VTanhKernelImpl : public VTanhKernel<T> {
 public:
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit VTanhKernelImpl(int d) : VTanhKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
185
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 84 * 8;
186 187
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::tanh,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
188 189 190 191 192 193 194 195 196 197 198 199
      this->Compute = jitcode_->getCode<void (*)(const T*, T*, int)>();
      return;
    }
#endif

#ifdef PADDLE_WITH_MKLML
    // strictly it's a better impl with MKL, then is refer
    if (useMKL(d)) {
      this->Compute = VTanhMKL<T>;
      return;
    }
#endif
200
    this->Compute = refer::VTanh<T>;
T
tensor-tang 已提交
201
  }
T
tensor-tang 已提交
202

T
tensor-tang 已提交
203 204 205
#ifdef PADDLE_WITH_XBYAK

 private:
206
  std::unique_ptr<gen::VActJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
207 208 209 210 211 212
#endif
};

#ifdef PADDLE_WITH_XBYAK
template <>
bool VTanhKernelImpl<float>::useJIT(int d) {
213
  return gen::VActJitCode::init(d, gen::operand_type::tanh);
T
tensor-tang 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}
#endif

#ifdef PADDLE_WITH_MKLML
template <>
bool VTanhKernelImpl<float>::useMKL(int d) {
  return d > 512;
}

template <>
bool VTanhKernelImpl<double>::useMKL(int d) {
  return true;
}
#endif

T
tensor-tang 已提交
229
REGISTER_JITKERNEL(vexp, VExpKernel);
T
tensor-tang 已提交
230
REGISTER_JITKERNEL(vsigmoid, VSigmoidKernel);
T
tensor-tang 已提交
231
REGISTER_JITKERNEL(vtanh, VTanhKernel);
232

T
tensor-tang 已提交
233 234 235 236
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle