Browse Source

gelu vk op tanh fix (#5008)

tags/20231027
FhqTreap GitHub 2 years ago
parent
commit
e14cc272ac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions
  1. +5
    -0
      src/layer/vulkan/shader/gelu.comp
  2. +5
    -0
      src/layer/vulkan/shader/gelu_pack4.comp
  3. +6
    -0
      src/layer/vulkan/shader/gelu_pack8.comp

+ 5
- 0
src/layer/vulkan/shader/gelu.comp View File

@@ -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);


+ 5
- 0
src/layer/vulkan/shader/gelu_pack4.comp View File

@@ -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);


+ 6
- 0
src/layer/vulkan/shader/gelu_pack8.comp View File

@@ -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);


Loading…
Cancel
Save