Skip to content

Commit

Permalink
Gelu afp fix (Tencent#5039)
Browse files Browse the repository at this point in the history
  • Loading branch information
FhqTreap authored Sep 19, 2023
1 parent a2103be commit a12a14f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/layer/vulkan/shader/gelu.comp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/layer/vulkan/shader/gelu_pack4.comp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/layer/vulkan/shader/gelu_pack8.comp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a12a14f

Please sign in to comment.