Coverage for src/flag_gems/runtime/backend/_tsingmicro/ops/randn_like.py: 0%
22 statements
« prev ^ index » next coverage.py v7.6.9, created at 2026-05-06 06:51 +0800
« prev ^ index » next coverage.py v7.6.9, created at 2026-05-06 06:51 +0800
1import logging
3import torch
4import triton
6from flag_gems.runtime import torch_device_fn
7from flag_gems.utils.random_utils import philox_backend_seed_offset
9from .randn import randn_kernel
11logger = logging.getLogger(__name__)
12UNROLL = 4
15def randn_like(
16 x, *, dtype=None, layout=None, device=None, pin_memory=None, memory_format=None
17):
18 logger.debug("GEMS_TSINGMICRO RANDN_LIKE")
19 if device is None:
20 device = x.device.index
21 if dtype is None:
22 dtype = x.dtype
23 out = torch.empty_like(x, device=device, dtype=dtype)
24 N = x.numel()
25 grid_fn = lambda meta: (triton.cdiv(N, meta["BLOCK"] * UNROLL),)
26 # (TODO) Using Triton autotuner makes kernel parameters opaque to the caller,
27 # hence we cannot obtain the per thread offset as in Pytorch.
28 increment = triton.cdiv(N, UNROLL)
29 philox_seed, philox_offset = philox_backend_seed_offset(increment)
30 with torch_device_fn.device(x.device):
31 randn_kernel[grid_fn](out, N, philox_seed, philox_offset)
32 return out