openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / controls / reduce_drop_last / triton_kernel.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # CONTROL (deliberately wrong): sums x[0..n-2] only, dropping the last element. |
| 3 | import triton |
| 4 | import triton.language as tl |
| 5 | |
| 6 | |
| 7 | @triton.jit |
| 8 | def _rsum(x, y, n, BLOCK: tl.constexpr): |
| 9 | # one program: BLOCK lane accumulators over the strided input, then a tree sum of the lanes |
| 10 | lane = tl.arange(0, BLOCK) |
| 11 | acc = tl.zeros([BLOCK], dtype=tl.float32) |
| 12 | for off in range(0, n, BLOCK): |
| 13 | i = off + lane |
| 14 | acc += tl.load(x + i, mask=i < n, other=0.0) |
| 15 | tl.store(y, tl.sum(acc, axis=0)) |
| 16 | |
| 17 | |
| 18 | def launch(p, t): |
| 19 | _rsum[(1,)](t["x"], t["y"], p["n"] - 1, BLOCK=4096) |