Skip to content

Commit dcdcd1c

Browse files
committed
support.tl.extra.shim.dispatch.to.backends
1 parent e345820 commit dcdcd1c

4 files changed

Lines changed: 57 additions & 8 deletions

File tree

src/flag_gems/ops/div.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
import triton
55
import triton.language as tl
66

7-
from ..utils import pointwise_dynamic, tl_extra_shim
7+
from ..utils import pointwise_dynamic
8+
from ..utils.triton_lang_extension import div_rn, div_rz, fmod, trunc
89

9-
div_rn = tl_extra_shim.div_rn
10-
div_rz = tl_extra_shim.div_rz
11-
fmod = tl_extra_shim.fmod
12-
trunc = tl_extra_shim.trunc
1310
logger = logging.getLogger(__name__)
1411

1512

src/flag_gems/ops/silu.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import triton
44
import triton.language as tl
55

6-
from ..utils import pointwise_dynamic, tl_extra_shim
7-
8-
div_rn = tl_extra_shim.div_rn
6+
from ..utils import pointwise_dynamic
7+
from ..utils.triton_lang_extension import div_rn
98

109
logger = logging.getLogger(__name__)
1110

src/flag_gems/utils/triton_lang_extension.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import triton
1515
from triton import language as tl
1616

17+
from .triton_lang_helper import use_tl_extra
18+
1719

1820
@triton.jit
1921
def program_id(
@@ -70,3 +72,34 @@ def maximum_with_index_tie_break_right(a_value, a_index, b_value, b_index):
7072
# Prefer highest index if values are equal
7173
mask |= equal & (a_index > b_index)
7274
return tl.where(mask, a_value, b_value), tl.where(mask, a_index, b_index)
75+
76+
77+
@triton.jit
78+
@use_tl_extra
79+
def div_rn(x, y):
80+
"""div_rn default - round to nearest"""
81+
result = x / y
82+
return tl.floor(result + 0.5)
83+
84+
85+
@triton.jit
86+
@use_tl_extra
87+
def div_rz(x, y):
88+
"""div_rz default - round toward zero"""
89+
result = x / y
90+
return tl.where(result >= 0, tl.floor(result), tl.ceil(result))
91+
92+
93+
@triton.jit
94+
@use_tl_extra
95+
def fmod(x, y):
96+
"""fmod default - floating point modulo"""
97+
quotient = div_rz(x, y)
98+
return x - y * quotient
99+
100+
101+
@triton.jit
102+
@use_tl_extra
103+
def trunc(x):
104+
"""trunc default - truncate to integer"""
105+
return tl.where(x >= 0, tl.floor(x), tl.ceil(x))

src/flag_gems/utils/triton_lang_helper.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@
2020
tl_extra_shim = triton.language.math
2121
except ImportError:
2222
tl_extra_shim = triton.language.libdevice
23+
24+
25+
def use_backend(module):
26+
"""using backend module impl"""
27+
28+
def decorator(func):
29+
func_name = func.__name__
30+
if hasattr(module, func_name):
31+
try:
32+
return getattr(module, func_name)
33+
except Exception:
34+
pass
35+
return func
36+
37+
return decorator
38+
39+
40+
def use_tl_extra(func):
41+
"""backend function shim"""
42+
return use_backend(tl_extra_shim)(func)

0 commit comments

Comments
 (0)