Skip to content

Commit 0daf2e3

Browse files
committed
[Fix] Add Ascend UB management and fix NPU benchmark issues
1 parent ac6c648 commit 0daf2e3

10 files changed

Lines changed: 1228 additions & 240 deletions

File tree

benchmarks/modules/benchmark_conv.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,28 @@
1111

1212
from fla.modules.convolution import causal_conv1d
1313
from fla.ops.utils.index import prepare_sequence_ids
14+
from fla.utils import IS_NPU
1415

1516
try:
1617
from causal_conv1d import causal_conv1d_fn
1718
except ImportError:
1819
causal_conv1d_fn = None
1920

2021

21-
@triton.testing.perf_report(
22-
triton.testing.Benchmark(
23-
# argument names to use as an x-axis for the plot
24-
x_names=['T', 'D'],
25-
# different possible values for `x_name`
26-
x_vals=[(128 * 2 ** i, d) for d in [256, 512, 1024, 2048, 4096] for i in range(1, 10)],
27-
# argument name whose value corresponds to a different line in the plot
28-
line_arg='provider',
29-
# possible values for `line_arg``
30-
line_vals=['causal_conv1d_fwd', 'causal_conv1d_cuda_fwd', 'causal_conv1d_fwdbwd', 'causal_conv1d_cuda_fwdbwd'],
31-
# label name for the lines
32-
line_names=['causal_conv1d_fwd', 'causal_conv1d_cuda_fwd', 'causal_conv1d_fwdbwd', 'causal_conv1d_cuda_fwdbwd'],
33-
# line styles
34-
styles=[('green', '-'), ('blue', '--'), ('red', '-.'),
35-
('cyan', ':'), ('yellow', 'dotted'), ('cyan', '--'), ('cyan', '-'), ('black', ':')],
36-
ylabel="Execution Time (ms)", # label name for the y-axis
37-
# name for the plot. Used also as a file name for saving the plot.
38-
plot_name="Performance",
39-
args={},
40-
),
41-
)
22+
def _conv_benchmark_providers():
23+
providers = ['causal_conv1d_fwd', 'causal_conv1d_fwdbwd']
24+
if not IS_NPU and causal_conv1d_fn is not None:
25+
providers.extend(['causal_conv1d_cuda_fwd', 'causal_conv1d_cuda_fwdbwd'])
26+
return providers
27+
28+
29+
_LINE_VALS = _conv_benchmark_providers()
30+
_STYLES = [
31+
('green', '-'), ('blue', '--'), ('red', '-.'), ('cyan', ':'),
32+
('yellow', 'dotted'), ('cyan', '--'), ('cyan', '-'), ('black', ':'),
33+
]
34+
35+
4236
def benchmark(T, D, provider):
4337
from fla.utils import device
4438
dtype = torch.bfloat16
@@ -100,5 +94,27 @@ def benchmark(T, D, provider):
10094
return results
10195

10296

97+
benchmark = triton.testing.perf_report(
98+
triton.testing.Benchmark(
99+
# argument names to use as an x-axis for the plot
100+
x_names=['T', 'D'],
101+
# different possible values for `x_name`
102+
x_vals=[(128 * 2 ** i, d) for d in [256, 512, 1024, 2048, 4096] for i in range(1, 10)],
103+
# argument name whose value corresponds to a different line in the plot
104+
line_arg='provider',
105+
# possible values for `line_arg``
106+
line_vals=_LINE_VALS,
107+
# label name for the lines
108+
line_names=_LINE_VALS,
109+
# line styles
110+
styles=_STYLES[:len(_LINE_VALS)],
111+
ylabel="Execution Time (ms)", # label name for the y-axis
112+
# name for the plot. Used also as a file name for saving the plot.
113+
plot_name="Performance",
114+
args={},
115+
),
116+
)(benchmark)
117+
118+
103119
if __name__ == '__main__':
104120
benchmark.run(print_data=True)

fla/modules/backends/triton_ascend/activations.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,27 @@
1414

1515
from fla.ops.utils.op import exp, log
1616
from fla.utils import autocast_custom_bwd, autocast_custom_fwd, input_guard
17+
from fla.utils.ascend_ub_manager import ASCEND_MAX_GRID_DIM, compute_activation_block_size
1718

1819
# Ascend launch limits: grid dim and per-core vector width.
19-
_PREFERRED_BLOCK_SIZES = (2048, 1024, 512)
20-
_MAX_GRID_DIM = 65535
2120
_MAX_CORE_DIM = 65535
2221

2322

24-
def _activation_launch_config(T: int) -> tuple[tuple[int], int]:
25-
"""Pick block size under Ascend launch limits."""
26-
for B in _PREFERRED_BLOCK_SIZES:
27-
if triton.cdiv(T, B) <= _MAX_GRID_DIM and B * 8 <= _MAX_CORE_DIM:
28-
return (triton.cdiv(T, B),), B
29-
B = min(triton.cdiv(T, _MAX_GRID_DIM), _MAX_CORE_DIM // 8)
30-
return (triton.cdiv(T, B),), max(B, 1)
23+
def _activation_launch_config(
24+
T: int,
25+
is_backward: bool = False,
26+
*,
27+
memory_multiplier: float | None = None,
28+
) -> tuple[tuple[int], int]:
29+
"""Pick block size under Ascend launch and UB limits."""
30+
B = compute_activation_block_size(
31+
T,
32+
is_backward,
33+
max_grid=ASCEND_MAX_GRID_DIM,
34+
max_core_dim=_MAX_CORE_DIM,
35+
memory_multiplier=memory_multiplier,
36+
)
37+
return (triton.cdiv(T, B),), B
3138

3239

3340
@triton.jit
@@ -470,7 +477,7 @@ def gelu_bwd_npu(g: torch.Tensor, x: torch.Tensor) -> torch.Tensor:
470477
g = _ensure_inner_contiguous(g)
471478
T, D = x.numel(), x.shape[-1]
472479
dx = _alloc_output(x)
473-
grid, B = _activation_launch_config(T)
480+
grid, B = _activation_launch_config(T, is_backward=True)
474481
gelu_bwd_kernel[grid](
475482
x, g, dx, T=T, D=D,
476483
stride_x_row=_get_stride(x),
@@ -502,7 +509,7 @@ def sqrelu_bwd_npu(g: torch.Tensor, x: torch.Tensor) -> torch.Tensor:
502509
g = _ensure_inner_contiguous(g)
503510
T, D = x.numel(), x.shape[-1]
504511
dx = _alloc_output(x)
505-
grid, B = _activation_launch_config(T)
512+
grid, B = _activation_launch_config(T, is_backward=True)
506513
sqrelu_bwd_kernel[grid](
507514
x, g, dx, T=T, D=D,
508515
stride_x_row=_get_stride(x),
@@ -534,7 +541,7 @@ def sigmoid_bwd_npu(x: torch.Tensor, dy: torch.Tensor, output_contiguous: bool =
534541
dy = _ensure_inner_contiguous(dy)
535542
T, D = x.numel(), x.shape[-1]
536543
dx = _alloc_output(x, output_contiguous)
537-
grid, B = _activation_launch_config(T)
544+
grid, B = _activation_launch_config(T, is_backward=True)
538545
sigmoid_bwd_kernel[grid](
539546
x, dy, dx, T=T, D=D,
540547
stride_x_row=_get_stride(x),
@@ -575,7 +582,7 @@ def logsigmoid_bwd_npu(
575582
dy = _ensure_inner_contiguous(dy)
576583
T, D = x.numel(), x.shape[-1]
577584
dx = _alloc_output(x, output_contiguous)
578-
grid, B = _activation_launch_config(T)
585+
grid, B = _activation_launch_config(T, is_backward=True)
579586
logsigmoid_bwd_kernel[grid](
580587
x=x,
581588
dx=dx,
@@ -612,7 +619,7 @@ def swish_bwd_npu(x: torch.Tensor, dy: torch.Tensor, output_contiguous: bool = F
612619
dy = _ensure_inner_contiguous(dy)
613620
T, D = x.numel(), x.shape[-1]
614621
dx = _alloc_output(x, output_contiguous)
615-
grid, B = _activation_launch_config(T)
622+
grid, B = _activation_launch_config(T, is_backward=True)
616623
swish_bwd_kernel[grid](
617624
x, dy, dx, T=T, D=D,
618625
stride_x_row=_get_stride(x),
@@ -660,7 +667,7 @@ def swiglu_fwdbwd_npu(
660667
z = _alloc_output(x, output_contiguous)
661668
else:
662669
z = None
663-
grid, B = _activation_launch_config(T)
670+
grid, B = _activation_launch_config(T, is_backward=True)
664671
swiglu_fwdbwd_kernel[grid](
665672
x, y, g, dx, dy, z, T=T, D=D,
666673
stride_x_row=_get_stride(x),
@@ -807,14 +814,19 @@ def powglu_fwdbwd_kernel(
807814
tl.store(z + z_off, b_z.to(z.dtype.element_ty), mask=mask)
808815

809816

817+
# Peak fp32 temporaries: sigmoid, sqrt, log, exp, pow, gate, output.
818+
_POWGLU_FWD_MEM_MULT = 8.0
819+
_POWGLU_BWD_MEM_MULT = 10.0
820+
821+
810822
@torch.compiler.disable
811823
def powglu_fwd_npu(x: torch.Tensor, y: torch.Tensor, power: float = 3.0, output_contiguous: bool = False) -> torch.Tensor:
812824
assert x.shape == y.shape, f"powglu_fwd: shape mismatch x={x.shape} y={y.shape}"
813825
x = _ensure_inner_contiguous(x)
814826
y = _ensure_inner_contiguous(y)
815827
T, D = x.numel(), x.shape[-1]
816828
z = _alloc_output(x, output_contiguous)
817-
grid, B = _activation_launch_config(T)
829+
grid, B = _activation_launch_config(T, memory_multiplier=_POWGLU_FWD_MEM_MULT)
818830
powglu_fwd_kernel[grid](
819831
x=x,
820832
y=y,
@@ -850,7 +862,7 @@ def powglu_fwdbwd_npu(
850862
z = _alloc_output(x, output_contiguous)
851863
else:
852864
z = None
853-
grid, B = _activation_launch_config(T)
865+
grid, B = _activation_launch_config(T, is_backward=True, memory_multiplier=_POWGLU_BWD_MEM_MULT)
854866
powglu_fwdbwd_kernel[grid](
855867
x=x,
856868
y=y,

0 commit comments

Comments
 (0)