Coverage for src/flag_gems/runtime/backend/_cambricon/ops/neg.py: 0%
45 statements
« prev ^ index » next coverage.py v7.6.9, created at 2026-06-05 07:36 +0800
« prev ^ index » next coverage.py v7.6.9, created at 2026-06-05 07:36 +0800
1import logging
3import torch
4import triton
5import triton.language as tl
7from flag_gems.runtime import torch_device_fn
8from flag_gems.utils import libentry, libtuner
10from ..utils import TOTAL_CORE_NUM
12logger = logging.getLogger("flag_gems").getChild(__name__.lstrip("."))
15@libentry()
16@libtuner(
17 configs=[
18 triton.Config(kwargs={"BLOCK_SIZE": 4096}, num_stages=1, num_warps=1),
19 triton.Config(kwargs={"BLOCK_SIZE": 16384}, num_stages=1, num_warps=1),
20 triton.Config(kwargs={"BLOCK_SIZE": 65536}, num_stages=1, num_warps=1),
21 triton.Config(kwargs={"BLOCK_SIZE": 131072}, num_stages=1, num_warps=1),
22 ],
23 key=["n_elements"],
24)
25@triton.jit
26def neg_func(
27 X_ptr,
28 OUT_ptr,
29 n_elements,
30 BLOCK_SIZE: tl.constexpr,
31):
32 pid = tl.program_id(0)
33 num_jobs = tl.num_programs(0)
34 block_start = pid * BLOCK_SIZE
35 step = num_jobs * BLOCK_SIZE
36 block_start = block_start.to(tl.int64)
37 for off in range(block_start, n_elements, step):
38 offsets = off + tl.arange(0, BLOCK_SIZE)
39 mask = offsets < n_elements
40 x = tl.load(X_ptr + offsets, mask=mask)
41 tl.store(OUT_ptr + offsets, -x, mask=mask)
44def neg(A):
45 logger.debug("GEMS_CAMBRICON NEG")
46 A = A.contiguous()
47 out = torch.empty_like(A)
48 N = A.numel()
49 if N == 0:
50 return out
51 grid_fn = lambda meta: (min(triton.cdiv(N, meta["BLOCK_SIZE"]), TOTAL_CORE_NUM),)
52 with torch_device_fn.device(A.device):
53 neg_func[grid_fn](A, out, N)
54 return out
57def neg_(A):
58 logger.debug("GEMS_CAMBRICON NEG_")
59 A_contig = A.contiguous()
60 N = A_contig.numel()
61 if N == 0:
62 return A
63 grid_fn = lambda meta: (min(triton.cdiv(N, meta["BLOCK_SIZE"]), TOTAL_CORE_NUM),)
64 with torch_device_fn.device(A.device):
65 neg_func[grid_fn](A_contig, A_contig, N)
66 if not A.is_contiguous():
67 A.copy_(A_contig)
68 return A