From a6bf1d0ad8cfd6457b7b4cec74b84c5a24046fcd Mon Sep 17 00:00:00 2001 From: Yufeng Li Date: Fri, 4 Oct 2019 15:17:26 -0700 Subject: [PATCH] use mlaserf (#1999) 1. use MlasErf for Gelu. Eigen's erf is very slow. 2. change the ErfUpperAbsRange to 3.925 because MlasErf doesn't return 1 for 3.725 Motivation and Context --- onnxruntime/contrib_ops/cpu/activations.h | 6 +++++- onnxruntime/core/mlas/lib/erf.cpp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/onnxruntime/contrib_ops/cpu/activations.h b/onnxruntime/contrib_ops/cpu/activations.h index 19f730cbd2..efb258d03b 100644 --- a/onnxruntime/contrib_ops/cpu/activations.h +++ b/onnxruntime/contrib_ops/cpu/activations.h @@ -6,6 +6,7 @@ #include "core/common/common.h" #include "core/framework/op_kernel.h" #include "core/util/math_cpuonly.h" +#include "core/mlas/inc/mlas.h" #include namespace onnxruntime { @@ -38,7 +39,10 @@ class Gelu : public OpKernel { const auto* X = context->Input(0); Tensor* Y = context->Output(0, X->Shape()); EIGEN_X_VAR(xm); - EIGEN_Y = xm * 0.5f * ((xm * static_cast(M_SQRT1_2)).erf() + 1.0f); + EIGEN_Y_VAR(ym); + ym = xm * static_cast(M_SQRT1_2); + MlasComputeErf(Y->template MutableData(), Y->template MutableData(), X->Shape().Size()); + ym = xm * 0.5f * (ym + 1.0f); return Status::OK(); } }; diff --git a/onnxruntime/core/mlas/lib/erf.cpp b/onnxruntime/core/mlas/lib/erf.cpp index c1f4a7e6c2..1fc538087c 100644 --- a/onnxruntime/core/mlas/lib/erf.cpp +++ b/onnxruntime/core/mlas/lib/erf.cpp @@ -64,7 +64,7 @@ MLAS_INTERNAL_DATA const struct { float Exp_C; int32_t Exp_X7F; } MlasErfConstants = { - 3.725f, + 3.925f, 0.921875f, -5.99104969e-4f, 4.99339588e-3f,