norm.comp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #version 450
  2. #include "generic_head.comp"
  3. #include "types.comp"
  4. #extension GL_EXT_control_flow_attributes : enable
  5. #define BLOCK_SIZE 512
  6. layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
  7. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  8. layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
  9. shared vec2 sum[BLOCK_SIZE];
  10. void main() {
  11. const uint row = gl_WorkGroupID.x;
  12. const uint tid = gl_LocalInvocationID.x;
  13. sum[tid] = vec2(0.0f, 0.0f);
  14. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  15. const float xi = float(data_a[row*p.KX + col]);
  16. sum[tid].x += xi;
  17. sum[tid].y += xi * xi;
  18. }
  19. // sum up partial sums and write back result
  20. barrier();
  21. [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
  22. if (tid < s) {
  23. sum[tid] += sum[tid + s];
  24. }
  25. barrier();
  26. }
  27. const float mean = sum[0].x / p.KX;
  28. const float var = sum[0].y / p.KX - mean * mean;
  29. const float inv_std = inversesqrt(var + p.param1);
  30. [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) {
  31. data_d[row*p.KX + col] = D_TYPE((float(data_a[row*p.KX + col]) - mean) * inv_std);
  32. }
  33. }