openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / gemm_bias_relu / 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), then the bias add and relu epilogue. |
| 4 | import triton |
| 5 | import triton.language as tl |
| 6 | |
| 7 | |
| 8 | @triton.jit |
| 9 | def _gemm_bias_relu(A, B, bias, 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 | b = tl.load(B + kk[:, None] * N + rn[None, :], mask=(kk[:, None] < K) & (rn[None, :] < N), other=0.0) |
| 18 | acc = tl.dot(a, b, acc, input_precision="ieee") |
| 19 | bv = tl.load(bias + rn, mask=rn < N, other=0.0) |
| 20 | out = tl.maximum(acc + bv[None, :], 0.0) |
| 21 | tl.store(C + rm[:, None] * N + rn[None, :], out, mask=(rm[:, None] < M) & (rn[None, :] < N)) |
| 22 | |
| 23 | |
| 24 | def launch(p, t): |
| 25 | M, N, K = p["M"], p["N"], p["K"] |
| 26 | _gemm_bias_relu[(triton.cdiv(M, 16), triton.cdiv(N, 16))](t["A"], t["B"], t["bias"], t["C"], M, N, K, |
| 27 | BM=16, BN=16, BK=16) |