Skip to content

Commit d4e490e

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

4 files changed

Lines changed: 53 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import triton
1515
from triton import language as tl
16+
from .triton_lang_helper import use_tl_extra
1617

1718

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

src/flag_gems/utils/triton_lang_helper.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,20 @@
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+
def decorator(func):
28+
func_name = func.__name__
29+
if hasattr(module, func_name):
30+
try:
31+
return getattr(module, func_name)
32+
except Exception:
33+
pass
34+
return func
35+
return decorator
36+
37+
def use_tl_extra(func):
38+
"""backend function shim"""
39+
return use_backend(tl_extra_shim)(func)

0 commit comments

Comments
 (0)