1#version 450
2
3#include "generic_head.glsl"
4#include "types.glsl"
5
6#extension GL_EXT_control_flow_attributes : enable
7#define BLOCK_SIZE 512
8
9layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
10
11layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
12layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
13
14shared float tmp[BLOCK_SIZE];
15
16void main() {
17 const uint group_size = p.KX;
18 const float eps = p.param1;
19
20 const uint tid = gl_LocalInvocationID.x;
21 const uint start = gl_WorkGroupID.x * group_size + tid;
22 const uint end = (gl_WorkGroupID.x + 1) * group_size;
23
24 tmp[tid] = 0.0f;
25
26 // Calculate mean
27 [[unroll]] for (uint col = start; col < end; col += BLOCK_SIZE) {
28 tmp[tid] += float(data_a[col]);
29 }
30
31 // tmp up partial tmps and write back result
32 barrier();
33 [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
34 if (tid < s) {
35 tmp[tid] += tmp[tid + s];
36 }
37 barrier();
38 }
39
40 const float mean = tmp[0] / group_size;
41 barrier();
42 tmp[tid] = 0.0f;
43
44 // Calculate variance
45 [[unroll]] for (uint col = start; col < end; col += BLOCK_SIZE) {
46 const float xi = float(data_a[col]) - mean;
47 data_d[col] = D_TYPE(xi);
48 tmp[tid] += xi * xi;
49 }
50
51 // sum up partial sums and write back result
52 barrier();
53 [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
54 if (tid < s) {
55 tmp[tid] += tmp[tid + s];
56 }
57 barrier();
58 }
59
60 const float variance = tmp[0] / group_size;
61 const float scale = inversesqrt(variance + eps);
62
63 [[unroll]] for (uint col = start; col < end; col += BLOCK_SIZE) {
64 data_d[col] *= D_TYPE(scale);
65 }
66}