Skip to content

Commit

Permalink
gelu vk op tanh fix (#5008)
Browse files Browse the repository at this point in the history
  • Loading branch information
FhqTreap authored Sep 6, 2023
1 parent 4a2620d commit e14cc27
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/layer/vulkan/shader/gelu.comp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ void main()
#endif

// 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)))));
#else
v = 0.5f * v * (1.0f + tanh(0.79788452f * (v + 0.044715f * v * v * v)));
#endif

#if NCNN_image_shader
image3d_st1(top_blob_3d, ivec3(gx, gy, gz), v);
Expand Down
5 changes: 5 additions & 0 deletions src/layer/vulkan/shader/gelu_pack4.comp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ void main()
#endif

// 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)))));
#else
v = 0.5f * v * (1.0f + tanh(0.79788452f * (v + 0.044715f * v * v * v)));
#endif

#if NCNN_image_shader
image3d_st4(top_blob_3d, ivec3(gx, gy, gz), v);
Expand Down
6 changes: 6 additions & 0 deletions src/layer/vulkan/shader/gelu_pack8.comp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ void main()
#endif

// 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])))));
#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])));
#endif

#if NCNN_image_shader
image3d_st8(top_blob_3d, ivec3(gx, gy, gz), v);
Expand Down

0 comments on commit e14cc27

Please sign in to comment.