Skip to content

Commit f56417e

Browse files
xuexingtuxuerui
andauthored
[KUNLUNXIN] speed up 'exp, celu, relu' (#1284)
Co-authored-by: xuerui <xuerui06@baidu.com>
1 parent 8347c7f commit f56417e

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
)
2020

2121

22-
@pointwise_dynamic(
23-
is_tensor=[True, False], promotion_methods=[(0, "DEFAULT")], config=config_
24-
)
22+
@pointwise_dynamic(is_tensor=[True, False], promotion_methods=[(0, "DEFAULT")])
2523
@triton.jit
24+
# celu another way: max(0, x) + alpha * (exp(min(0, x) / alpha) - 1), getting smaller instrs.
2625
def celu_forward_kernel(x, alpha):
27-
return tl.where(
28-
x > 0,
29-
x,
30-
alpha * (tl.exp(x / alpha) - 1),
31-
)
26+
inv_alpha = 1.0 / alpha
27+
28+
pos_part = tl.maximum(0.0, x)
29+
30+
neg_part_input = x - pos_part
31+
32+
return pos_part + alpha * (tl.exp(neg_part_input * inv_alpha) - 1.0)
3233

3334

3435
def celu(A, alpha=1.0):

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
prefer_1d_tile=True,
1717
buffer_size_limit=4096,
1818
isCloseVectorization=True,
19+
kunlunAutoGrid=True,
1920
unroll_num=8,
2021
)
2122

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])
1212
@triton.jit
13+
# relu another way: maximum(x, 0)
14+
# tl.maximum(x, 0) to one max_instr,but tl.where two instr compare and select
1315
def relu_forward(x):
14-
return tl.where(x > 0, x, 0)
16+
return tl.maximum(x, 0)
1517

1618

1719
@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])

0 commit comments

Comments
 (0)