Skip to content

Commit 46577f4

Browse files
author
xuerui
committed
[KUNLUNXIN] speed up 'any, div, neg, silu'
1 parent 8e7577d commit 46577f4

4 files changed

Lines changed: 51 additions & 16 deletions

File tree

src/flag_gems/runtime/backend/_kunlunxin/ops/any.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,31 @@ def any_kernel_2(mid, out, MID_SIZE, BLOCK_MID: tl.constexpr):
144144
def any(inp):
145145
logger.debug("GEMS ANY")
146146
n_elements = inp.numel()
147-
block_size = min(
147+
block_size = max(
148148
triton.cdiv(get_block(n_elements), cluster_num),
149149
triton.cdiv(buf_len_per_core * core_num, 4),
150150
)
151+
151152
mid_size = triton.cdiv(n_elements, block_size)
152153
block_mid = triton.next_power_of_2(mid_size)
153-
154+
154155
if n_elements >= vector_size * thread_num:
155-
# according to api, op == any, use max to calculate
156-
inpf = inp.to(torch.float)
157-
midf = torch.empty((mid_size,), dtype=torch.float, device=inp.device)
158-
outf = torch.empty([], dtype=torch.float, device=inp.device)
156+
inp_uint8 = inp.view(torch.uint8)
157+
158+
mid = torch.empty((mid_size,), dtype=torch.uint8, device=inp.device)
159+
out = torch.empty([], dtype=torch.uint8, device=inp.device)
159160

160161
with torch_device_fn.device(inp.device):
161162
max_kernel_1[(mid_size, 1)](
162-
inpf, midf, n_elements, block_size, buffer_size_limit=2048
163+
inp_uint8, mid, n_elements, block_size, buffer_size_limit=2048
163164
)
164165
if mid_size == 1:
165-
return midf.to(torch.bool).reshape([])
166+
return mid.view(torch.bool).reshape([])
167+
166168
max_kernel_2[(1, 1)](
167-
midf, outf, mid_size, block_mid, buffer_size_limit=2048
169+
mid, out, mid_size, block_mid, buffer_size_limit=2048
168170
)
169-
out = outf.to(torch.bool)
171+
out = out.view(torch.bool)
170172
else:
171173
mid = torch.empty((mid_size,), dtype=torch.bool, device=inp.device)
172174
out = torch.empty([], dtype=torch.bool, device=inp.device)

src/flag_gems/runtime/backend/_kunlunxin/ops/div.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from flag_gems.utils import tl_extra_shim
88

99
from ..utils.pointwise_dynamic import pointwise_dynamic
10+
from _kunlunxin.utils.codegen_config_utils import CodeGenConfig
1011

1112
logger = logging.getLogger("flag_gems").getChild(__name__.lstrip("."))
1213
div_rn = tl_extra_shim.div_rn
@@ -15,8 +16,18 @@
1516
trunc = tl_extra_shim.trunc
1617
xpu_trunc_div = tl_extra_shim.xpu_trunc_div # use it if we need to cmp result with xpu
1718

18-
19-
@pointwise_dynamic(promotion_methods=[(0, 1, "INT_TO_FLOAT")])
19+
config_ = CodeGenConfig(
20+
512,
21+
(65536, 65536, 65536),
22+
32,
23+
True,
24+
prefer_1d_tile=True,
25+
buffer_size_limit=4096,
26+
isCloseVectorization=True,
27+
unroll_num=8,
28+
)
29+
30+
@pointwise_dynamic(promotion_methods=[(0, 1, "INT_TO_FLOAT")], config=config_)
2031
@triton.jit
2132
def true_div_func(x, y):
2233
return x / y

src/flag_gems/runtime/backend/_kunlunxin/ops/neg.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
import triton
44

55
from ..utils.pointwise_dynamic import pointwise_dynamic
6+
from _kunlunxin.utils.codegen_config_utils import CodeGenConfig
67

78
logger = logging.getLogger("flag_gems").getChild(__name__.lstrip("."))
89

9-
10-
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])
10+
config_ = CodeGenConfig(
11+
512,
12+
(65536, 65536, 65536),
13+
32,
14+
True,
15+
prefer_1d_tile=True,
16+
buffer_size_limit=4096,
17+
isCloseVectorization=False,
18+
unroll_num=8,
19+
)
20+
21+
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")], config=config_)
1122
@triton.jit
1223
def neg_func(x):
1324
return -x

src/flag_gems/runtime/backend/_kunlunxin/ops/silu.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@
66
from flag_gems.utils import tl_extra_shim
77

88
from ..utils.pointwise_dynamic import pointwise_dynamic
9+
from _kunlunxin.utils.codegen_config_utils import CodeGenConfig
910

1011
logger = logging.getLogger("flag_gems").getChild(__name__.lstrip("."))
1112
div_rn = tl_extra_shim.div_rn
1213

13-
14-
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])
14+
config_ = CodeGenConfig(
15+
512,
16+
(65536, 65536, 65536),
17+
32,
18+
True,
19+
prefer_1d_tile=True,
20+
buffer_size_limit=4096,
21+
isCloseVectorization=True,
22+
unroll_num=8,
23+
)
24+
25+
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")], config=config_)
1526
@triton.jit
1627
def silu_forward(x):
1728
x_fp32 = x.to(tl.float32)

0 commit comments

Comments
 (0)