openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / gemv / triton_kernel.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # Blocked loop: each program owns BM rows, accumulates elementwise products over BN-wide column blocks |
| 3 | # in f32 and reduces each row with tl.sum (a summation tree, covered by the any-order gamma(N) bound). |
| 4 | import triton |
| 5 | import triton.language as tl |
| 6 | |
| 7 | |
| 8 | @triton.jit |
| 9 | def _gemv(A, x, y, M, N, BM: tl.constexpr, BN: tl.constexpr): |
| 10 | rm = tl.program_id(0) * BM + tl.arange(0, BM) |
| 11 | rn = tl.arange(0, BN) |
| 12 | acc = tl.zeros((BM,), dtype=tl.float32) |
| 13 | for j0 in range(0, N, BN): |
| 14 | jj = j0 + rn |
| 15 | a = tl.load(A + rm[:, None] * N + jj[None, :], mask=(rm[:, None] < M) & (jj[None, :] < N), other=0.0) |
| 16 | v = tl.load(x + jj, mask=jj < N, other=0.0) |
| 17 | acc += tl.sum(a * v[None, :], axis=1) |
| 18 | tl.store(y + rm, acc, mask=rm < M) |
| 19 | |
| 20 | |
| 21 | def launch(p, t): |
| 22 | M, N = p["M"], p["N"] |
| 23 | _gemv[(triton.cdiv(M, 32),)](t["A"], t["x"], t["y"], M, N, BM=32, BN=64) |