interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / argmax / triton_kernel.py

24 lines · 856 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2import triton
3import triton.language as tl
4
5
6@triton.jit
7def _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
23def launch(p, t):
24 _amax[(1,)](t["x"], t["y"], p["n"], BLOCK=4096)