openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / attn_scores / triton_kernel.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | import triton |
| 3 | import triton.language as tl |
| 4 | |
| 5 | |
| 6 | @triton.jit |
| 7 | def _attn_scores(q, k, s, T, D: tl.constexpr, BJ: tl.constexpr): |
| 8 | i = tl.program_id(0) |
| 9 | j = tl.arange(0, BJ) |
| 10 | c = tl.arange(0, D) |
| 11 | qv = tl.load(q + i * D + c) |
| 12 | kv = tl.load(k + j[:, None] * D + c[None, :], mask=j[:, None] < T, other=0.0) |
| 13 | acc = tl.sum(kv * qv[None, :], axis=1) * 0.125 |
| 14 | out = tl.where(j <= i, acc, -1e30) |
| 15 | tl.store(s + i * T + j, out, mask=j < T) |
| 16 | |
| 17 | |
| 18 | def launch(p, t): |
| 19 | n = p["T"] |
| 20 | _attn_scores[(n,)](t["q"], t["k"], t["s"], n, D=p["Dh"], BJ=triton.next_power_of_2(n)) |