|
| 1 | +import logging |
| 2 | +import math |
| 3 | + |
| 4 | +import torch |
| 5 | +import triton |
| 6 | +import triton.language as tl |
| 7 | + |
| 8 | +from flag_gems.runtime import torch_device_fn |
| 9 | +from flag_gems.utils import pointwise_dynamic |
| 10 | + |
| 11 | +logger = logging.getLogger(f'flag_gems.runtime._ascend.ops.{__name__.split(".")[-1]}') |
| 12 | + |
| 13 | +CORE_NUM = 40 |
| 14 | + |
| 15 | +try: |
| 16 | + import torch_npu # noqa: F401 |
| 17 | + import triton.runtime.driver as driver |
| 18 | + |
| 19 | + device = torch.npu.current_device() |
| 20 | + props = driver.active.utils.get_device_properties(device) |
| 21 | + CORE_NUM = props["num_vectorcore"] |
| 22 | +except Exception: |
| 23 | + CORE_NUM = 40 |
| 24 | + |
| 25 | + |
| 26 | +@triton.jit |
| 27 | +def add_kernel( |
| 28 | + x_ptr, |
| 29 | + y_ptr, |
| 30 | + out_ptr, |
| 31 | + alpha, |
| 32 | + data_len, |
| 33 | + BLOCK_SIZE: tl.constexpr, |
| 34 | + TILE_SIZE: tl.constexpr, |
| 35 | +): |
| 36 | + pid = tl.program_id(0) |
| 37 | + iter_num = tl.cdiv(BLOCK_SIZE, TILE_SIZE) |
| 38 | + |
| 39 | + for idx in tl.range(0, iter_num): |
| 40 | + offsets = pid * BLOCK_SIZE + idx * TILE_SIZE + tl.arange(0, TILE_SIZE) |
| 41 | + mask = offsets < data_len |
| 42 | + x = tl.load(x_ptr + offsets, mask=mask, care_padding=False) |
| 43 | + y = tl.load(y_ptr + offsets, mask=mask, care_padding=False) |
| 44 | + out = x + y * alpha |
| 45 | + tl.store(out_ptr + offsets, out, mask=mask) |
| 46 | + |
| 47 | + |
| 48 | +@pointwise_dynamic( |
| 49 | + is_tensor=[True, False, False], promotion_methods=[(0, 1, "DEFAULT")] |
| 50 | +) |
| 51 | +@triton.jit |
| 52 | +def add_func_tensor_scalar(x, y, alpha): |
| 53 | + return x + y * alpha |
| 54 | + |
| 55 | + |
| 56 | +@pointwise_dynamic( |
| 57 | + is_tensor=[False, True, False], promotion_methods=[(0, 1, "DEFAULT")] |
| 58 | +) |
| 59 | +@triton.jit |
| 60 | +def add_func_scalar_tensor(x, y, alpha): |
| 61 | + return x + y * alpha |
| 62 | + |
| 63 | + |
| 64 | +def _launch_add_kernel(x_flat, y_flat, out_flat, alpha, data_len, device): |
| 65 | + TILE_SIZE = 8192 |
| 66 | + BLOCK_SIZE = math.ceil(data_len / CORE_NUM) |
| 67 | + BLOCK_SIZE = max(BLOCK_SIZE, TILE_SIZE) |
| 68 | + # Round up to multiple of TILE_SIZE for proper tiling |
| 69 | + BLOCK_SIZE = triton.cdiv(BLOCK_SIZE, TILE_SIZE) * TILE_SIZE |
| 70 | + grid = lambda meta: (triton.cdiv(data_len, meta["BLOCK_SIZE"]),) |
| 71 | + with torch_device_fn.device(device): |
| 72 | + add_kernel[grid]( |
| 73 | + x_flat, y_flat, out_flat, float(alpha), data_len, BLOCK_SIZE, TILE_SIZE |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +def add(A, B, *, alpha=1): |
| 78 | + logger.debug("GEMS_ASCEND ADD") |
| 79 | + if isinstance(A, torch.Tensor) and isinstance(B, torch.Tensor): |
| 80 | + if B.device != A.device: |
| 81 | + B = B.to(A.device) |
| 82 | + result_type = torch.result_type(A, B) |
| 83 | + A_cont = A.contiguous() |
| 84 | + B_cont = B.contiguous() |
| 85 | + if A_cont.dtype != result_type: |
| 86 | + A_cont = A_cont.to(result_type) |
| 87 | + if B_cont.dtype != result_type: |
| 88 | + B_cont = B_cont.to(result_type) |
| 89 | + A_flat = A_cont.view(-1) |
| 90 | + B_flat = B_cont.view(-1) |
| 91 | + out = torch.empty_like(A_flat, dtype=result_type) |
| 92 | + data_len = A_flat.numel() |
| 93 | + _launch_add_kernel(A_flat, B_flat, out, alpha, data_len, A.device) |
| 94 | + return out.view(A.shape) |
| 95 | + elif isinstance(A, torch.Tensor): |
| 96 | + return add_func_tensor_scalar(A, B, alpha) |
| 97 | + elif isinstance(B, torch.Tensor): |
| 98 | + return add_func_scalar_tensor(A, B, alpha) |
| 99 | + else: |
| 100 | + return torch.tensor(A + B * alpha) |
| 101 | + |
| 102 | + |
| 103 | +def add_(A, B, *, alpha=1): |
| 104 | + logger.debug("GEMS_ASCEND ADD_") |
| 105 | + if isinstance(A, torch.Tensor) and isinstance(B, torch.Tensor): |
| 106 | + if B.device != A.device: |
| 107 | + B = B.to(A.device) |
| 108 | + A_flat = A.contiguous().view(-1) |
| 109 | + B_flat = B.contiguous().view(-1) |
| 110 | + data_len = A_flat.numel() |
| 111 | + _launch_add_kernel(A_flat, B_flat, A_flat, alpha, data_len, A.device) |
| 112 | + return A |
| 113 | + elif isinstance(A, torch.Tensor): |
| 114 | + return add_func_tensor_scalar(A, B, alpha, out0=A) |
| 115 | + else: |
| 116 | + raise ValueError("Unreachable.") |
0 commit comments