gelu.comp 700 B

12345678910111213141516171819202122232425
  1. #version 450
  2. #include "generic_head.comp"
  3. #include "types.comp"
  4. #extension GL_EXT_control_flow_attributes : enable
  5. layout(local_size_x = 512, local_size_y = 1, local_size_z = 1) in;
  6. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  7. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  8. void main() {
  9. const float GELU_COEF_A = 0.044715f;
  10. const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  11. const uint i = gl_GlobalInvocationID.x;
  12. if (i >= p.KX) {
  13. return;
  14. }
  15. const float xi = float(data_a[i]);
  16. const float val = SQRT_2_OVER_PI*xi*(1.0f + GELU_COEF_A*xi*xi);
  17. data_d[i] = D_TYPE(0.5f*xi*(2.0f - 2.0f / (exp(2 * val) + 1)));
  18. }