From a12a14f3a62ee2d60dda2694487b992eda5ced1a Mon Sep 17 00:00:00 2001 From: FhqTreap <45459183+FhqTreap@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:45:37 +0800 Subject: [PATCH] Gelu afp fix (#5039) --- src/layer/vulkan/shader/gelu.comp | 4 ++-- src/layer/vulkan/shader/gelu_pack4.comp | 4 ++-- src/layer/vulkan/shader/gelu_pack8.comp | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/layer/vulkan/shader/gelu.comp b/src/layer/vulkan/shader/gelu.comp index f389101d195e..b85cf2f6d0ac 100644 --- a/src/layer/vulkan/shader/gelu.comp +++ b/src/layer/vulkan/shader/gelu.comp @@ -64,9 +64,9 @@ void main() // y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3))) #if NCNN_moltenvk - v = 0.5f * v * (1.0f + afp(tanh(float(0.79788452f * (v + 0.044715f * v * v * v))))); + v = afp(0.5f) * v * (afp(1.0f) + afp(tanh(float(afp(0.79788452f) * (v + afp(0.044715f) * v * v * v))))); #else - v = 0.5f * v * (1.0f + tanh(0.79788452f * (v + 0.044715f * v * v * v))); + v = afp(0.5f) * v * (afp(1.0f) + tanh(afp(0.79788452f) * (v + afp(0.044715f) * v * v * v))); #endif #if NCNN_image_shader diff --git a/src/layer/vulkan/shader/gelu_pack4.comp b/src/layer/vulkan/shader/gelu_pack4.comp index 3d9ee1bf0b33..2fde1f584026 100644 --- a/src/layer/vulkan/shader/gelu_pack4.comp +++ b/src/layer/vulkan/shader/gelu_pack4.comp @@ -64,9 +64,9 @@ void main() // y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3))) #if NCNN_moltenvk - v = 0.5f * v * (1.0f + afpvec4(tanh(vec4(0.79788452f * (v + 0.044715f * v * v * v))))); + v = afpvec4(0.5f) * v * (afpvec4(1.0f) + afpvec4(tanh(vec4(afpvec4(0.79788452f) * (v + afpvec4(0.044715f) * v * v * v))))); #else - v = 0.5f * v * (1.0f + tanh(0.79788452f * (v + 0.044715f * v * v * v))); + v = afpvec4(0.5f) * v * (afpvec4(1.0f) + tanh(afpvec4(0.79788452f) * (v + afpvec4(0.044715f) * v * v * v))); #endif #if NCNN_image_shader diff --git a/src/layer/vulkan/shader/gelu_pack8.comp b/src/layer/vulkan/shader/gelu_pack8.comp index 47d181147d69..8ad3d66ed9fc 100644 --- a/src/layer/vulkan/shader/gelu_pack8.comp +++ b/src/layer/vulkan/shader/gelu_pack8.comp @@ -65,11 +65,11 @@ void main() // y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3))) #if NCNN_moltenvk - v[0] = 0.5f * v[0] * (1.0f + afpvec4(tanh(vec4(0.79788452f * (v[0] + 0.044715f * v[0] * v[0] * v[0]))))); - v[1] = 0.5f * v[1] * (1.0f + afpvec4(tanh(vec4(0.79788452f * (v[1] + 0.044715f * v[1] * v[1] * v[1]))))); + v[0] = afpvec4(0.5f) * v[0] * (afpvec4(1.0f) + afpvec4(tanh(vec4(afpvec4(0.79788452f) * (v[0] + afpvec4(0.044715f) * v[0] * v[0] * v[0]))))); + v[1] = afpvec4(0.5f) * v[1] * (afpvec4(1.0f) + afpvec4(tanh(vec4(afpvec4(0.79788452f) * (v[1] + afpvec4(0.044715f) * v[1] * v[1] * v[1]))))); #else - v[0] = 0.5f * v[0] * (1.0f + tanh(0.79788452f * (v[0] + 0.044715f * v[0] * v[0] * v[0]))); - v[1] = 0.5f * v[1] * (1.0f + tanh(0.79788452f * (v[1] + 0.044715f * v[1] * v[1] * v[1]))); + v[0] = afpvec4(0.5f) * v[0] * (afpvec4(1.0f) + tanh(afpvec4(0.79788452f) * (v[0] + afpvec4(0.044715f) * v[0] * v[0] * v[0]))); + v[1] = afpvec4(0.5f) * v[1] * (afpvec4(1.0f) + tanh(afpvec4(0.79788452f) * (v[1] + afpvec4(0.044715f) * v[1] * v[1] * v[1]))); #endif #if NCNN_image_shader