Coverage for src/flag_gems/runtime/backend/_mthreads/ops/ones.py: 0%

31 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2026-03-21 14:31 +0800

1import logging 

2 

3import torch 

4import triton 

5import triton.language as tl 

6 

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 

11 

12device_ = device 

13logger = logging.getLogger( 

14 f'flag_gems.runtime.backend._mthreads.ops.{__name__.split(".")[-1]}' 

15) 

16 

17 

18@libentry() 

19@triton.jit 

20def ones_kernel( 

21 output_ptr, 

22 n_elements, 

23 BLOCK_SIZE: tl.constexpr, 

24): 

25 pid = tle.program_id(axis=0) 

26 block_start = (pid * BLOCK_SIZE).to(tl.int64) 

27 offsets = (block_start + tl.arange(0, BLOCK_SIZE)).to(tl.int64) 

28 mask = offsets < n_elements 

29 tl.store(output_ptr + offsets, 1.0, mask=mask) 

30 

31 

32def ones(size, *, dtype=None, layout=None, device=None, pin_memory=None): 

33 logger.debug("GEMS_MTHREADS ONES") 

34 if dtype is None: 

35 dtype = torch.get_default_dtype() 

36 if device is None: 

37 device = torch.device(device_.name) 

38 

39 out = torch.empty(size, device=device, dtype=dtype) 

40 N = volume(size) 

41 BLOCK_SIZE = 1024 

42 grid = (triton.cdiv(N, BLOCK_SIZE),) 

43 with torch_device_fn.device(device): 

44 ones_kernel[grid](out, N, BLOCK_SIZE) 

45 return out