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 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
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
35
// try to use MKL to speedup
T
tensor-tang 已提交
36 37 38 39 40 41 42 43 44 45 46 47
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 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61

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 已提交
62 63 64 65 66 67 68 69 70 71 72

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 已提交
73 74
#endif

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

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

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

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

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

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

#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)) {
134
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 82 * 8;
135 136
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::sigmoid,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
137 138 139 140 141 142 143 144 145 146 147 148
      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
149
    this->Compute = refer::VSigmoid<T>;
T
tensor-tang 已提交
150
  }
T
tensor-tang 已提交
151

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

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

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

T
tensor-tang 已提交
178 179 180 181 182 183 184 185
/* 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)) {
186
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 84 * 8;
187 188
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::tanh,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
189 190 191 192 193 194 195 196 197 198 199 200
      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
201
    this->Compute = refer::VTanh<T>;
T
tensor-tang 已提交
202
  }
T
tensor-tang 已提交
203

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

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

#ifdef PADDLE_WITH_XBYAK
template <>
bool VTanhKernelImpl<float>::useJIT(int d) {
214
  return gen::VActJitCode::init(d, gen::operand_type::tanh);
T
tensor-tang 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}
#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 已提交
230
REGISTER_JITKERNEL(vexp, VExpKernel);
T
tensor-tang 已提交
231
REGISTER_JITKERNEL(vsigmoid, VSigmoidKernel);
T
tensor-tang 已提交
232
REGISTER_JITKERNEL(vtanh, VTanhKernel);
233

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