Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/openaccel-lab/blob/main/workloads/layernorm/kernel.cu # openaccel-lab aien-dev/openaccel-lab Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `openaccel-lab / workloads / layernorm / kernel.cu` 46 lines ยท 1339 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 #include 3 4 #define NT 128 5 6 // one block of NT threads per row 7 __global__ void layernorm(const float *x, const float *g, const float *b, float *y, long long D) 8 { 9 __shared__ float red[NT]; 10 int t = threadIdx.x; 11 const float *xr = x + (long long)blockIdx.x * D; 12 float *yr = y + (long long)blockIdx.x * D; 13 float acc = 0.0f; 14 for (long long k = t; k < D; k += NT) 15 acc += xr[k]; 16 red[t] = acc; 17 __syncthreads(); 18 for (int h = NT / 2; h > 0; h >>= 1) { 19 if (t < h) 20 red[t] += red[t + h]; 21 __syncthreads(); 22 } 23 float mu = red[0] / (float)D; 24 __syncthreads(); 25 acc = 0.0f; 26 for (long long k = t; k < D; k += NT) { 27 float c = xr[k] - mu; 28 acc += c * c; 29 } 30 red[t] = acc; 31 __syncthreads(); 32 for (int h = NT / 2; h > 0; h >>= 1) { 33 if (t < h) 34 red[t] += red[t + h]; 35 __syncthreads(); 36 } 37 float rs = 1.0f / sqrtf(red[0] / (float)D + 1e-5f); 38 for (long long k = t; k < D; k += NT) 39 yr[k] = (xr[k] - mu) * rs * g[k] + b[k]; 40 } 41 42 void oal_launch(const long long *p, void *const *buf) 43 { 44 layernorm<<<(unsigned)p[0], NT>>>((const float *)buf[0], (const float *)buf[1], (const float *)buf[2], 45 (float *)buf[3], p[1]); 46 }