fn

empty_like

Tensor
empty_like(t: Tensor, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

Return an uninitialised tensor with the same shape, dtype, and device as t.

Allocates backing memory matching t's metadata but performs no initialisation write. Reading elements before overwriting them is undefined behaviour. The typical use is to pre-allocate an output buffer for an in-place kernel call:

allocate(t)    kernel_write(buf)    read(buf)\text{allocate}(|\texttt{t}|) \;\to\; \text{kernel\_write}(\text{buf}) \;\to\; \text{read}(\text{buf})

Parameters

tTensor
Reference tensor whose shape, dtype, and device are inherited by the output.
dtypelucid.dtype
Override the data type. Defaults to t.dtype.
devicestr or lucid.device
Override the device. Defaults to t.device.
requires_gradbool
Enable autograd tracking. Default: False.

Returns

Tensor

Uninitialised tensor shaped like t.

Notes

Skipping the zero-fill write is only a win when the allocation is large and will be fully overwritten before any read. For small tensors the branch-prediction and cache effects of the explicit initialisation kernel are negligible; prefer zeros_like in those cases for safety.

Examples

>>> import lucid
>>> x = lucid.randn(1024, 1024)
>>> buf = lucid.empty_like(x)     # fast scratch space
>>> buf.shape
(1024, 1024)