Skip to content

Commit 315e08e

Browse files
committed
speedup vInvSqrt vLogqp vTanh with mklml
1 parent cd8700f commit 315e08e

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

paddle/math/MathFunctions.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
1313
limitations under the License. */
1414

15-
#include "MathFunctions.h"
15+
#include "paddle/math/MathFunctions.h"
1616
#include "hl_matrix_apply.cuh"
1717
#include "hl_matrix_ops.cuh"
1818
#include "paddle/utils/DynamicLoader.h"
@@ -240,6 +240,36 @@ template <>
240240
void vAdd<double>(const int n, const double* a, const double* b, double* r) {
241241
vdAdd(n, a, b, r);
242242
}
243+
244+
template <>
245+
void vTanh<float>(const int n, const float* a, float* r) {
246+
vsTanh(n, a, r);
247+
}
248+
249+
template <>
250+
void vTanh<double>(const int n, const double* a, double* r) {
251+
vdTanh(n, a, r);
252+
}
253+
254+
template <>
255+
void vInvSqrt<float>(const int n, const float* a, float* r) {
256+
vsInvSqrt(n, a, r);
257+
}
258+
259+
template <>
260+
void vInvSqrt<double>(const int n, const double* a, double* r) {
261+
vdInvSqrt(n, a, r);
262+
}
263+
264+
template <>
265+
void vLog1p<float>(const int n, const float* a, float* r) {
266+
vsLog1p(n, a, r);
267+
}
268+
269+
template <>
270+
void vLog1p<double>(const int n, const double* a, double* r) {
271+
vdLog1p(n, a, r);
272+
}
243273
#else
244274

245275
DEFINE_MATRIX_BINARY_OP(vExp, b = std::exp(a));
@@ -287,35 +317,4 @@ template void vAdd(const int n, const float* a, const float* b, float* r);
287317
template void vAdd(const int n, const double* a, const double* b, double* r);
288318

289319
#endif
290-
291-
DEFINE_MATRIX_BINARY_OP(vInvSqrt, b = 1.0f / std::sqrt(a));
292-
template <class T>
293-
void vInvSqrt(const int n, const T* a, T* r) {
294-
hl_cpu_apply_binary_op<T, binary::vInvSqrt<T>, 0, 0>(
295-
binary::vInvSqrt<T>(), const_cast<T*>(a), r, 1, n, n, n);
296-
}
297-
298-
DEFINE_MATRIX_BINARY_OP(vLog1p, b = std::log(1.0f + a));
299-
template <class T>
300-
void vLog1p(const int n, const T* a, T* r) {
301-
hl_cpu_apply_binary_op<T, binary::vLog1p<T>, 0, 0>(
302-
binary::vLog1p<T>(), const_cast<T*>(a), r, 1, n, n, n);
303-
}
304-
305-
DEFINE_MATRIX_BINARY_OP(vTanh, T tmp = -2.0 * a;
306-
tmp = (tmp > EXP_MAX_INPUT) ? EXP_MAX_INPUT : tmp;
307-
b = 2.0 / (1.0 + std::exp(tmp)) - 1.0);
308-
template <class T>
309-
void vTanh(const int n, const T* a, T* r) {
310-
hl_cpu_apply_binary_op<T, binary::vTanh<T>, 0, 0>(
311-
binary::vTanh<T>(), const_cast<T*>(a), r, 1, n, n, n);
312-
}
313-
314-
template void vInvSqrt(const int n, const double* a, double* r);
315-
template void vInvSqrt(const int n, const float* a, float* r);
316-
template void vLog1p(const int n, const float* a, float* r);
317-
template void vLog1p(const int n, const double* a, double* r);
318-
template void vTanh(const int n, const float* a, float* r);
319-
template void vTanh(const int n, const double* a, double* r);
320-
321320
} // namespace paddle

0 commit comments

Comments
 (0)