Efficient workspace allocation
A later computation will overwrite every element.
PyTorch · Function
Allocates a tensor with a requested shape and optional dtype, layout, device, gradient, memory, and format settings. Its elements are uninitialized, so write valid values before reading them.
torch.empty allocates a tensor using the requested shape and options, but does not normally initialize its elements. Because the contents are undefined, initialize the tensor before reading it; the function can also configure dtype, layout, device, gradient tracking, pinned memory, and memory format.
torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor| Name | Type | Description |
|---|---|---|
size | int... | A sequence of integers defining the output shape; provide separate dimensions or a list/tuple. |
out | Tensor, optional | Optional output tensor to receive the result. |
dtype | torch.dtype, optional | Desired data type; defaults to the global default dtype when omitted. |
layout | torch.layout, optional | Desired tensor layout; defaults to torch.strided. |
device | torch.device, optional | Target device; omitted value uses the current default device. |
requires_grad | bool, optional | Whether autograd records operations on the returned tensor; defaults to False. |
pin_memory | bool, optional | Whether to allocate pinned memory; works only for CPU tensors and defaults to False. |
memory_format | torch.memory_format, optional | Desired tensor memory format; defaults to torch.contiguous_format. |
A Tensor containing uninitialized data.
import torch
x = torch.empty((2, 3), dtype=torch.float32)
x.fill_(0)
print(x)Allocates a 2-by-3 tensor, then initializes it before use.
A later computation will overwrite every element.
A custom initialization routine determines values after allocation.
A preallocated CPU tensor must support faster host-to-device transfers.
torch.empty creates a tensor with the requested shape but leaves its storage uninitialized. Its elements may contain arbitrary prior memory contents, so code should write valid values before consuming them.
Follow-up: How would you obtain reproducible behavior when uninitialized memory is involved? Enable deterministic algorithms; PyTorch can fill uninitialized memory when its deterministic fill option is enabled.
The size argument defines the output shape and may be supplied as separate integer arguments or as one list or tuple. Keyword options select properties such as dtype, device, layout, gradient tracking, and memory format.
Follow-up: Why might a tuple be preferable in generic code? It packages the complete shape as one value, which can be passed directly or manipulated before allocation.
torch.empty is useful when allocation and initialization are separate steps because it avoids an initial value fill. That can reduce unnecessary work, but only if every element is overwritten before use.
Follow-up: What should be used when values must be initialized immediately? Choose a factory such as torch.zeros, torch.ones, or torch.full, depending on the required initial value.
torch.empty_liketorch.empty_permutedtorch.empty_quantizedtorch.empty_stridedtorch.randtorch.zerostorch.onestorch.full