openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / rmsnorm / kernel.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | #include <cuda_runtime.h> |
| 3 | |
| 4 | #define NT 128 |
| 5 | |
| 6 | // one block of NT threads per row |
| 7 | __global__ void rmsnorm(const float *x, const float *w, 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] * 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 rs = 1.0f / sqrtf(red[0] / (float)D + 1e-6f); |
| 24 | for (long long k = t; k < D; k += NT) |
| 25 | yr[k] = xr[k] * rs * w[k]; |
| 26 | } |
| 27 | |
| 28 | void oal_launch(const long long *p, void *const *buf) |
| 29 | { |
| 30 | rmsnorm<<<(unsigned)p[0], NT>>>((const float *)buf[0], (const float *)buf[1], (float *)buf[2], p[1]); |
| 31 | } |