openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / rmsnorm / triton_kernel.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | import triton |
| 3 | import triton.language as tl |
| 4 | |
| 5 | |
| 6 | @triton.jit |
| 7 | def _rmsnorm(x, w, y, D, BLOCK: tl.constexpr): |
| 8 | r = tl.program_id(0) |
| 9 | k = tl.arange(0, BLOCK) |
| 10 | m = k < D |
| 11 | v = tl.load(x + r * D + k, mask=m, other=0.0) |
| 12 | rs = 1.0 / tl.sqrt(tl.sum(v * v, axis=0) / D + 1e-6) |
| 13 | tl.store(y + r * D + k, v * rs * tl.load(w + k, mask=m, other=0.0), mask=m) |
| 14 | |
| 15 | |
| 16 | def launch(p, t): |
| 17 | _rmsnorm[(p["R"],)](t["x"], t["w"], t["y"], p["D"], BLOCK=triton.next_power_of_2(p["D"])) |