interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / batched_gemm / triton_kernel.py

29 lines · 1270 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2# Grid axis 2 is the batch index. tl.dot with input_precision="ieee": full f32 products and f32
3# accumulation (the interpreter runs np.matmul in float32; on a GPU this forbids TF32).
4import triton
5import triton.language as tl
6
7
8@triton.jit
9def _batched_gemm(A, B, C, M, N, K, BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr):
10 bt = tl.program_id(2)
11 A = A + bt * M * K
12 B = B + bt * K * N
13 C = C + bt * M * N
14 rm = tl.program_id(0) * BM + tl.arange(0, BM)
15 rn = tl.program_id(1) * BN + tl.arange(0, BN)
16 rk = tl.arange(0, BK)
17 acc = tl.zeros((BM, BN), dtype=tl.float32)
18 for k0 in range(0, K, BK):
19 kk = k0 + rk
20 a = tl.load(A + rm[:, None] * K + kk[None, :], mask=(rm[:, None] < M) & (kk[None, :] < K), other=0.0)
21 b = tl.load(B + kk[:, None] * N + rn[None, :], mask=(kk[:, None] < K) & (rn[None, :] < N), other=0.0)
22 acc = tl.dot(a, b, acc, input_precision="ieee")
23 tl.store(C + rm[:, None] * N + rn[None, :], acc, mask=(rm[:, None] < M) & (rn[None, :] < N))
24
25
26def launch(p, t):
27 Bt, M, N, K = p["BATCH"], p["M"], p["N"], p["K"]
28 grid = (triton.cdiv(M, 16), triton.cdiv(N, 16), Bt)
29 _batched_gemm[grid](t["A"], t["B"], t["C"], M, N, K, BM=16, BN=16, BK=16)