jit_kernel_exp.cc 4.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 {

/* VExp JitKernel */
T
tensor-tang 已提交
34
template <typename T>
T
tensor-tang 已提交
35 36
class VExpKernelImpl : public VExpKernel<T> {
 public:
T
tensor-tang 已提交
37 38 39 40
  JITKERNEL_DECLARE_STATIC_FUNC;
  explicit VExpKernelImpl(int d) : VExpKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(d)) {
41
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 70 * 8;
42 43
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::exp,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
44 45 46 47 48 49 50 51
      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 已提交
52
    }
T
tensor-tang 已提交
53
#endif
54
    this->Compute = refer::VExp<T>;
T
tensor-tang 已提交
55
  }
T
tensor-tang 已提交
56

T
tensor-tang 已提交
57 58 59
#ifdef PADDLE_WITH_XBYAK

 private:
60
  std::unique_ptr<gen::VActJitCode> jitcode_{nullptr};
T
tensor-tang 已提交
61
#endif
T
tensor-tang 已提交
62 63
};

T
tensor-tang 已提交
64 65 66
#ifdef PADDLE_WITH_XBYAK
template <>
bool VExpKernelImpl<float>::useJIT(int d) {
67
  return gen::VActJitCode::init(d, gen::operand_type::exp);
T
tensor-tang 已提交
68 69 70
}
#endif

T
tensor-tang 已提交
71
#ifdef PADDLE_WITH_MKLML
T
tensor-tang 已提交
72 73 74 75
template <>
bool VExpKernelImpl<float>::useMKL(int d) {
  return d > 512;
}
T
tensor-tang 已提交
76

T
tensor-tang 已提交
77 78 79 80
template <>
bool VExpKernelImpl<double>::useMKL(int d) {
  return true;
}
T
tensor-tang 已提交
81 82 83 84 85 86 87 88 89 90 91

#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)) {
92
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 82 * 8;
93 94
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::sigmoid,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
95 96 97 98 99 100 101 102 103 104 105 106
      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
107
    this->Compute = refer::VSigmoid<T>;
T
tensor-tang 已提交
108
  }
T
tensor-tang 已提交
109

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

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

#ifdef PADDLE_WITH_XBYAK
template <>
bool VSigmoidKernelImpl<float>::useJIT(int d) {
120
  return gen::VActJitCode::init(d, gen::operand_type::sigmoid);
T
tensor-tang 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133
}
#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 已提交
134 135
#endif

T
tensor-tang 已提交
136 137 138 139 140 141 142 143
/* 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)) {
144
      size_t sz = 96 + d / YMM_FLOAT_BLOCK * 84 * 8;
145 146
      jitcode_.reset(new gen::VActJitCode(d, gen::operand_type::tanh,
                                          sz > 4096 ? sz : 4096));
T
tensor-tang 已提交
147 148 149 150 151 152 153 154 155 156 157 158
      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
159
    this->Compute = refer::VTanh<T>;
T
tensor-tang 已提交
160
  }
T
tensor-tang 已提交
161

T
tensor-tang 已提交
162 163 164
#ifdef PADDLE_WITH_XBYAK

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

#ifdef PADDLE_WITH_XBYAK
template <>
bool VTanhKernelImpl<float>::useJIT(int d) {
172
  return gen::VActJitCode::init(d, gen::operand_type::tanh);
T
tensor-tang 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}
#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 已提交
188
REGISTER_JITKERNEL(vexp, VExpKernel);
T
tensor-tang 已提交
189
REGISTER_JITKERNEL(vsigmoid, VSigmoidKernel);
T
tensor-tang 已提交
190
REGISTER_JITKERNEL(vtanh, VTanhKernel);
191

T
tensor-tang 已提交
192 193 194 195
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle