interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / workloads / quant_int8 / triton_kernel.py

28 lines · 894 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2import triton
3import triton.language as tl
4
5
6@triton.jit
7def _quant_int8(x, q, s, C, BC: tl.constexpr):
8 r = tl.program_id(0)
9 c = tl.arange(0, BC)
10 live = c < C
11 xv = tl.load(x + r * C + c, mask=live, other=0.0)
12 sc = tl.max(tl.abs(xv), axis=0) / 127.0
13 tl.store(s + r, sc)
14 y = tl.where(sc > 0.0, xv / tl.where(sc > 0.0, sc, 1.0), 0.0)
15 # round half to even on |y|: |y| - floor(|y|) is exact for |y| < 2^23
16 a = tl.abs(y)
17 f = tl.floor(a)
18 frac = a - f
19 fi = f.to(tl.int32)
20 up = (frac > 0.5) | ((frac == 0.5) & ((fi & 1) == 1))
21 qi = tl.where(up, fi + 1, fi)
22 qi = tl.where(y < 0.0, -qi, qi)
23 qi = tl.minimum(tl.maximum(qi, -127), 127)
24 tl.store(q + r * C + c, qi.to(tl.int8), mask=live)
25
26
27def launch(p, t):
28 _quant_int8[(p["R"],)](t["x"], t["q"], t["s"], p["C"], BC=triton.next_power_of_2(p["C"]))