openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / argmax / 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 _amax(x, y, n, BLOCK: tl.constexpr): |
| 8 | # one program: lane j keeps the first maximum of x[j], x[j+BLOCK], ... (strict > keeps the |
| 9 | # earlier index); then the smallest index among the lanes that hold the overall maximum. |
| 10 | lane = tl.arange(0, BLOCK) |
| 11 | bv = tl.full([BLOCK], float("-inf"), dtype=tl.float32) |
| 12 | bi = tl.full([BLOCK], 2147483647, dtype=tl.int32) |
| 13 | for off in range(0, n, BLOCK): |
| 14 | i = off + lane |
| 15 | v = tl.load(x + i, mask=i < n, other=float("-inf")) |
| 16 | take = (v > bv) & (i < n) |
| 17 | bv = tl.where(take, v, bv) |
| 18 | bi = tl.where(take, i, bi) |
| 19 | m = tl.max(bv, axis=0) |
| 20 | tl.store(y, tl.min(tl.where(bv == m, bi, 2147483647), axis=0)) |
| 21 | |
| 22 | |
| 23 | def launch(p, t): |
| 24 | _amax[(1,)](t["x"], t["y"], p["n"], BLOCK=4096) |