|
14 | 14 |
|
15 | 15 | from fla.ops.utils.op import exp, log |
16 | 16 | 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 |
17 | 18 |
|
18 | 19 | # Ascend launch limits: grid dim and per-core vector width. |
19 | | -_PREFERRED_BLOCK_SIZES = (2048, 1024, 512) |
20 | | -_MAX_GRID_DIM = 65535 |
21 | 20 | _MAX_CORE_DIM = 65535 |
22 | 21 |
|
23 | 22 |
|
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 |
31 | 38 |
|
32 | 39 |
|
33 | 40 | @triton.jit |
@@ -470,7 +477,7 @@ def gelu_bwd_npu(g: torch.Tensor, x: torch.Tensor) -> torch.Tensor: |
470 | 477 | g = _ensure_inner_contiguous(g) |
471 | 478 | T, D = x.numel(), x.shape[-1] |
472 | 479 | dx = _alloc_output(x) |
473 | | - grid, B = _activation_launch_config(T) |
| 480 | + grid, B = _activation_launch_config(T, is_backward=True) |
474 | 481 | gelu_bwd_kernel[grid]( |
475 | 482 | x, g, dx, T=T, D=D, |
476 | 483 | stride_x_row=_get_stride(x), |
@@ -502,7 +509,7 @@ def sqrelu_bwd_npu(g: torch.Tensor, x: torch.Tensor) -> torch.Tensor: |
502 | 509 | g = _ensure_inner_contiguous(g) |
503 | 510 | T, D = x.numel(), x.shape[-1] |
504 | 511 | dx = _alloc_output(x) |
505 | | - grid, B = _activation_launch_config(T) |
| 512 | + grid, B = _activation_launch_config(T, is_backward=True) |
506 | 513 | sqrelu_bwd_kernel[grid]( |
507 | 514 | x, g, dx, T=T, D=D, |
508 | 515 | stride_x_row=_get_stride(x), |
@@ -534,7 +541,7 @@ def sigmoid_bwd_npu(x: torch.Tensor, dy: torch.Tensor, output_contiguous: bool = |
534 | 541 | dy = _ensure_inner_contiguous(dy) |
535 | 542 | T, D = x.numel(), x.shape[-1] |
536 | 543 | dx = _alloc_output(x, output_contiguous) |
537 | | - grid, B = _activation_launch_config(T) |
| 544 | + grid, B = _activation_launch_config(T, is_backward=True) |
538 | 545 | sigmoid_bwd_kernel[grid]( |
539 | 546 | x, dy, dx, T=T, D=D, |
540 | 547 | stride_x_row=_get_stride(x), |
@@ -575,7 +582,7 @@ def logsigmoid_bwd_npu( |
575 | 582 | dy = _ensure_inner_contiguous(dy) |
576 | 583 | T, D = x.numel(), x.shape[-1] |
577 | 584 | dx = _alloc_output(x, output_contiguous) |
578 | | - grid, B = _activation_launch_config(T) |
| 585 | + grid, B = _activation_launch_config(T, is_backward=True) |
579 | 586 | logsigmoid_bwd_kernel[grid]( |
580 | 587 | x=x, |
581 | 588 | dx=dx, |
@@ -612,7 +619,7 @@ def swish_bwd_npu(x: torch.Tensor, dy: torch.Tensor, output_contiguous: bool = F |
612 | 619 | dy = _ensure_inner_contiguous(dy) |
613 | 620 | T, D = x.numel(), x.shape[-1] |
614 | 621 | dx = _alloc_output(x, output_contiguous) |
615 | | - grid, B = _activation_launch_config(T) |
| 622 | + grid, B = _activation_launch_config(T, is_backward=True) |
616 | 623 | swish_bwd_kernel[grid]( |
617 | 624 | x, dy, dx, T=T, D=D, |
618 | 625 | stride_x_row=_get_stride(x), |
@@ -660,7 +667,7 @@ def swiglu_fwdbwd_npu( |
660 | 667 | z = _alloc_output(x, output_contiguous) |
661 | 668 | else: |
662 | 669 | z = None |
663 | | - grid, B = _activation_launch_config(T) |
| 670 | + grid, B = _activation_launch_config(T, is_backward=True) |
664 | 671 | swiglu_fwdbwd_kernel[grid]( |
665 | 672 | x, y, g, dx, dy, z, T=T, D=D, |
666 | 673 | stride_x_row=_get_stride(x), |
@@ -807,14 +814,19 @@ def powglu_fwdbwd_kernel( |
807 | 814 | tl.store(z + z_off, b_z.to(z.dtype.element_ty), mask=mask) |
808 | 815 |
|
809 | 816 |
|
| 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 | + |
810 | 822 | @torch.compiler.disable |
811 | 823 | def powglu_fwd_npu(x: torch.Tensor, y: torch.Tensor, power: float = 3.0, output_contiguous: bool = False) -> torch.Tensor: |
812 | 824 | assert x.shape == y.shape, f"powglu_fwd: shape mismatch x={x.shape} y={y.shape}" |
813 | 825 | x = _ensure_inner_contiguous(x) |
814 | 826 | y = _ensure_inner_contiguous(y) |
815 | 827 | T, D = x.numel(), x.shape[-1] |
816 | 828 | 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) |
818 | 830 | powglu_fwd_kernel[grid]( |
819 | 831 | x=x, |
820 | 832 | y=y, |
@@ -850,7 +862,7 @@ def powglu_fwdbwd_npu( |
850 | 862 | z = _alloc_output(x, output_contiguous) |
851 | 863 | else: |
852 | 864 | 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) |
854 | 866 | powglu_fwdbwd_kernel[grid]( |
855 | 867 | x=x, |
856 | 868 | y=y, |
|
0 commit comments