Skip to content

Commit e97c09b

Browse files
authored
cambricon: fix kernel (#1263)
1 parent 8e7577d commit e97c09b

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/flag_gems/runtime/backend/_cambricon/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
dispatch_key="PrivateUse1",
2525
)
2626

27-
CUSTOMIZED_UNUSED_OPS = ("_upsample_bicubic2d_aa",) # skip now
27+
CUSTOMIZED_UNUSED_OPS = (
28+
"_upsample_bicubic2d_aa", # skip now
29+
"scatter_add_",
30+
)
2831

2932
__all__ = ["*"]

src/flag_gems/runtime/backend/_cambricon/ops/log_softmax.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,13 @@ def log_softmax_kernel_inner_k_partial_stats(
408408
).to(tl.float32)
409409

410410
tile_max = tl.max(tile, axis=1)
411-
tile_sum = tl.sum(tl.exp(tile - tile_max[:, None]), axis=1)
411+
all_neg_inf = tile_max == -float("inf")
412+
413+
tile_sum = tl.where(
414+
all_neg_inf,
415+
0.0,
416+
tl.sum(tl.exp(tile - tile_max[:, None]), axis=1),
417+
)
412418

413419
tl.store(max_buf_ptr + offs_m * T + tile_id, tile_max, mask=(offs_m < M))
414420
tl.store(sum_buf_ptr + offs_m * T + tile_id, tile_sum, mask=(offs_m < M))
@@ -487,7 +493,13 @@ def log_softmax_kernel_inner_k_write_logsoftmax(
487493
other=-float("inf"),
488494
).to(tl.float32)
489495

490-
o = tl.exp(tile - gmax[:, None]) / gsum[:, None]
496+
valid = gsum[:, None] > 0
497+
498+
o = tl.where(
499+
valid,
500+
tl.exp(tile - gmax[:, None]) / gsum[:, None],
501+
0.0,
502+
)
491503
out = tl.log2(o) / log2e
492504

493505
tl.store(y_ptr + offs_m[:, None] * N + offs_n[None, :], out, mask=mask)

src/flag_gems/runtime/backend/_cambricon/ops/softmax.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,13 @@ def softmax_kernel_inner_k_partial_stats(
380380
).to(tl.float32)
381381

382382
tile_max = tl.max(tile, axis=1)
383-
tile_sum = tl.sum(tl.exp(tile - tile_max[:, None]), axis=1)
383+
all_neg_inf = tile_max == -float("inf")
384+
385+
tile_sum = tl.where(
386+
all_neg_inf,
387+
0.0,
388+
tl.sum(tl.exp(tile - tile_max[:, None]), axis=1),
389+
)
384390

385391
tl.store(max_buf_ptr + offs_m * T + tile_id, tile_max, mask=(offs_m < M))
386392
tl.store(sum_buf_ptr + offs_m * T + tile_id, tile_sum, mask=(offs_m < M))
@@ -459,7 +465,13 @@ def softmax_kernel_inner_k_write_softmax(
459465
other=-float("inf"),
460466
).to(tl.float32)
461467

462-
out = tl.exp(tile - gmax[:, None]) / gsum[:, None]
468+
valid = gsum[:, None] > 0
469+
470+
out = tl.where(
471+
valid,
472+
tl.exp(tile - gmax[:, None]) / gsum[:, None],
473+
0.0,
474+
)
463475

464476
tl.store(y_ptr + offs_m[:, None] * N + offs_n[None, :], out, mask=mask)
465477

0 commit comments

Comments
 (0)