提交 341851b9 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!5817 [MS][LITE][Develop]optimize tanh kernel

Merge pull request !5817 from sunsuodong/tanh_opt
......@@ -81,9 +81,22 @@ int SigmoidFp16(const float16_t *src, float16_t *dst, int ele_num) {
return NNACL_OK;
}
float16_t TanhOpt(float16_t src) {
if (src > 5.0) {
return 1.0f;
} else if (src < -5.0) {
return -1.0f;
} else {
float square = src * src;
float a = (((square + 378.0f) * square + 17325.0f) * square + 135135.0f) * src;
float b = ((28.0f * square + 3150.0f) * square + 62370.0f) * square + 135135.0f;
return a / b;
}
}
int TanhFp16(const float16_t *src, float16_t *dst, int ele_num) {
for (int i = 0; i < ele_num; ++i) {
dst[i] = (float16_t)1.0f - (float16_t)2.0f / (float16_t)(exp(2 * src[i]) + 1);
dst[i] = TanhOpt(src[i]);
}
return NNACL_OK;
}
......
......@@ -60,16 +60,22 @@ int Sigmoid(const float *src, int length, float *dst) {
return NNACL_OK;
}
float TanhOpt(float src) {
if (src > 5.0) {
return 1.0f;
} else if (src < -5.0) {
return -1.0f;
} else {
float square = src * src;
float a = (((square + 378.0f) * square + 17325.0f) * square + 135135.0f) * src;
float b = ((28.0f * square + 3150.0f) * square + 62370.0f) * square + 135135.0f;
return a / b;
}
}
int Tanh(const float *src, int length, float *dst) {
for (int i = 0; i < length; ++i) {
float tmp_in = src[i];
if (tmp_in > 5.0) {
dst[i] = 1.0f;
} else if (tmp_in < -5.0) {
dst[i] = -1.0f;
} else {
dst[i] = 1.0f - 2.0f / (exp(2 * tmp_in) + 1);
}
dst[i] = TanhOpt(src[i]);
}
return NNACL_OK;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册