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

35 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2026-03-16 02:02 +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 triton_lang_extension as tle 

9from flag_gems.utils.shape_utils import volume 

10 

11device_ = device 

12logger = logging.getLogger( 

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

14) 

15 

16 

17@triton.jit 

18def zeros_kernel( 

19 output_ptr, 

20 n_elements, 

21 BLOCK_SIZE: tl.constexpr, 

22): 

23 pid = tle.program_id(axis=0) # We use a 1D launch grid so axis is 0. 

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

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

26 mask = offsets < n_elements 

27 tl.store(output_ptr + offsets, 0.0, mask=mask) 

28 

29 

30def zeros(size, *, dtype=None, layout=None, device=None, pin_memory=None): 

31 logger.debug("GEMS_MTHREADS ZEROS") 

32 if dtype is None: 

33 dtype = torch.get_default_dtype() 

34 if device is None: 

35 device = torch.device(device_.name) 

36 

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

38 N = volume(size) 

39 grid_fn = lambda meta: (triton.cdiv(N, meta["BLOCK_SIZE"]),) 

40 with torch_device_fn.device(device): 

41 zeros_kernel[grid_fn](out, N, BLOCK_SIZE=1024) 

42 return out 

43 

44 

45def zero_(x: torch.Tensor) -> torch.Tensor: 

46 logger.debug("GEMS_MTHREADS ZERO_") 

47 N = x.numel() 

48 grid_fn = lambda meta: (triton.cdiv(N, meta["BLOCK_SIZE"]),) 

49 with torch_device_fn.device(x.device): 

50 zeros_kernel[grid_fn](x, N, BLOCK_SIZE=1024) 

51 return x