openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / gemm_nt / triton_kernel.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # tl.dot with input_precision="ieee": full f32 products and f32 accumulation (the interpreter runs |
| 3 | # np.matmul in float32; on a GPU this forbids TF32), so the gamma(K) dot-product bound applies. |
| 4 | import triton |
| 5 | import triton.language as tl |
| 6 | |
| 7 | |
| 8 | @triton.jit |
| 9 | def _gemm_nt(A, B, C, M, N, K, BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr): |
| 10 | rm = tl.program_id(0) * BM + tl.arange(0, BM) |
| 11 | rn = tl.program_id(1) * BN + tl.arange(0, BN) |
| 12 | rk = tl.arange(0, BK) |
| 13 | acc = tl.zeros((BM, BN), dtype=tl.float32) |
| 14 | for k0 in range(0, K, BK): |
| 15 | kk = k0 + rk |
| 16 | a = tl.load(A + rm[:, None] * K + kk[None, :], mask=(rm[:, None] < M) & (kk[None, :] < K), other=0.0) |
| 17 | bt = tl.load(B + rn[None, :] * K + kk[:, None], mask=(kk[:, None] < K) & (rn[None, :] < N), other=0.0) |
| 18 | acc = tl.dot(a, bt, acc, input_precision="ieee") |
| 19 | tl.store(C + rm[:, None] * N + rn[None, :], acc, mask=(rm[:, None] < M) & (rn[None, :] < N)) |
| 20 | |
| 21 | |
| 22 | def launch(p, t): |
| 23 | M, N, K = p["M"], p["N"], p["K"] |
| 24 | _gemm_nt[(triton.cdiv(M, 16), triton.cdiv(N, 16))](t["A"], t["B"], t["C"], M, N, K, BM=16, BN=16, BK=16) |