Coverage for src/flag_gems/ops/ones.py: 81%
31 statements
« prev ^ index » next coverage.py v7.6.9, created at 2026-03-21 14:31 +0800
« prev ^ index » next coverage.py v7.6.9, created at 2026-03-21 14:31 +0800
1import logging
3import torch
4import triton
5import triton.language as tl
7from flag_gems.runtime import device, torch_device_fn
8from flag_gems.utils import libentry
9from flag_gems.utils import triton_lang_extension as tle
10from flag_gems.utils.shape_utils import volume
12device_ = device
13logger = logging.getLogger(__name__)
16@libentry()
17@triton.jit
18def ones_kernel(
19 output_ptr,
20 n_elements,
21 BLOCK_SIZE: tl.constexpr,
22):
23 pid = tle.program_id(axis=0)
24 block_start = pid * BLOCK_SIZE
25 offsets = block_start + tl.arange(0, BLOCK_SIZE)
26 mask = offsets < n_elements
27 tl.store(output_ptr + offsets, 1.0, mask=mask)
30def ones(size, *, dtype=None, layout=None, device=None, pin_memory=None):
31 logger.debug("GEMS ONES")
32 if dtype is None:
33 dtype = torch.get_default_dtype()
34 if device is None:
35 device = torch.device(device_.name)
37 out = torch.empty(size, device=device, dtype=dtype)
38 N = volume(size)
39 BLOCK_SIZE = 1024
40 grid = (triton.cdiv(N, BLOCK_SIZE),)
41 with torch_device_fn.device(device):
42 ones_kernel[grid](out, N, BLOCK_SIZE)
43 return out