Coverage for src/flag_gems/runtime/backend/_metax/ops/zeros.py: 0%
32 statements
« prev ^ index » next coverage.py v7.6.9, created at 2026-03-27 02:51 +0800
« prev ^ index » next coverage.py v7.6.9, created at 2026-03-27 02:51 +0800
1import logging
3import torch
4import triton
5import triton.language as tl
7import flag_gems.runtime as runtime
8from flag_gems.runtime import device, torch_device_fn
9from flag_gems.utils import libentry
10from flag_gems.utils import triton_lang_extension as tle
11from flag_gems.utils.shape_utils import volume
13logger = logging.getLogger("flag_gems." + __name__)
14device_ = device
17@libentry()
18@triton.heuristics(runtime.get_heuristic_config("zeros"))
19@triton.jit
20def zeros_kernel(
21 output_ptr,
22 N,
23 BLOCK_SIZE: tl.constexpr,
24):
25 pid = tle.program_id(axis=0) # We use a 1D launch grid so axis is 0.
26 block_start = pid * BLOCK_SIZE
27 offsets = block_start + tl.arange(0, BLOCK_SIZE)
28 mask = offsets < N
29 tl.store(output_ptr + offsets, 0.0, mask=mask)
32def zeros(size, *, dtype=None, layout=None, device=None, pin_memory=None):
33 logger.debug("METAX GEMS ZEROS")
34 if dtype is None:
35 dtype = torch.get_default_dtype()
36 if device is None:
37 device = torch.device(device_.name)
39 out = torch.empty(size, device=device, dtype=dtype)
40 N = volume(size)
41 grid_fn = lambda meta: (triton.cdiv(N, meta["BLOCK_SIZE"]),)
42 with torch_device_fn.device(device):
43 zeros_kernel[grid_fn](out, N)
44 return out