Skip to content

Commit db14b4b

Browse files
xuexingtuxuerui
andauthored
[KUNLUNXIN] speed up 'any, div, neg, silu' (#1270)
Co-authored-by: xuerui <xuerui06@baidu.com>
1 parent 7e3992b commit db14b4b

4 files changed

Lines changed: 50 additions & 14 deletions

File tree

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,29 @@ 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)
153154

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-
max_kernel_2[(1, 1)](
167-
midf, outf, mid_size, block_mid, buffer_size_limit=2048
168-
)
169-
out = outf.to(torch.bool)
166+
return mid.view(torch.bool).reshape([])
167+
168+
max_kernel_2[(1, 1)](mid, out, mid_size, block_mid, buffer_size_limit=2048)
169+
out = out.view(torch.bool)
170170
else:
171171
mid = torch.empty((mid_size,), dtype=torch.bool, device=inp.device)
172172
out = torch.empty([], dtype=torch.bool, device=inp.device)

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import torch
44
import triton
55
import triton.language as tl
6+
from _kunlunxin.utils.codegen_config_utils import CodeGenConfig
67

78
from flag_gems.utils import tl_extra_shim
89

@@ -15,8 +16,19 @@
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

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+
)
1829

19-
@pointwise_dynamic(promotion_methods=[(0, 1, "INT_TO_FLOAT")])
30+
31+
@pointwise_dynamic(promotion_methods=[(0, 1, "INT_TO_FLOAT")], config=config_)
2032
@triton.jit
2133
def true_div_func(x, y):
2234
return x / y

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import logging
22

33
import triton
4+
from _kunlunxin.utils.codegen_config_utils import CodeGenConfig
45

56
from ..utils.pointwise_dynamic import pointwise_dynamic
67

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

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+
)
920

10-
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])
21+
22+
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")], config=config_)
1123
@triton.jit
1224
def neg_func(x):
1325
return -x

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import triton
44
import triton.language as tl
5+
from _kunlunxin.utils.codegen_config_utils import CodeGenConfig
56

67
from flag_gems.utils import tl_extra_shim
78

@@ -10,8 +11,19 @@
1011
logger = logging.getLogger("flag_gems").getChild(__name__.lstrip("."))
1112
div_rn = tl_extra_shim.div_rn
1213

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+
)
1324

14-
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])
25+
26+
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")], config=config_)
1527
@triton.jit
1628
def silu_forward(x):
1729
x_fp32 = x.to(tl.float32)

0 commit comments

Comments
 (0)