Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.empty

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.

How it works

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.

Syntax

torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor

Parameters

NameTypeDescription
sizeint...A sequence of integers defining the output shape; provide separate dimensions or a list/tuple.
outTensor, optionalOptional output tensor to receive the result.
dtypetorch.dtype, optionalDesired data type; defaults to the global default dtype when omitted.
layouttorch.layout, optionalDesired tensor layout; defaults to torch.strided.
devicetorch.device, optionalTarget device; omitted value uses the current default device.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.
pin_memorybool, optionalWhether to allocate pinned memory; works only for CPU tensors and defaults to False.
memory_formattorch.memory_format, optionalDesired tensor memory format; defaults to torch.contiguous_format.

Returns

A Tensor containing uninitialized data.

Key points

  • The output shape comes from size, which accepts dimensions as separate arguments or as a collection.
  • Both torch.empty(2, 3) and torch.empty((2, 3)) express a two-dimensional shape.
  • If dtype is omitted, PyTorch selects the current global default data type.
  • The default layout is torch.strided unless another supported layout is requested.
  • An omitted device follows the current default device for the default tensor type.
  • requires_grad controls whether autograd records operations involving the returned tensor.

Common mistakes

  • The returned elements do not have meaningful initial values; initialize the tensor before reading it.
  • The pin_memory option applies only when creating CPU tensors.

Example: Allocate and initialize a tensor

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.

When to use it

Efficient workspace allocation

A later computation will overwrite every element.

Separate allocation from initialization

A custom initialization routine determines values after allocation.

Pinned host storage

A preallocated CPU tensor must support faster host-to-device transfers.

Interview questions

QWhat does torch.empty return?

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.

QHow can the shape and tensor properties be specified?

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.

QWhen is torch.empty preferable to an initialized factory?

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_like
  • torch.empty_permuted
  • torch.empty_quantized
  • torch.empty_strided
  • torch.rand
  • torch.zeros
  • torch.ones
  • torch.full

Source: torch.empty — PyTorch 2.14 documentation. Checked 2026-10-16.