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

PyTorch · Function

torch.device

torch.device identifies where a tensor is or will be allocated. It supports device strings, type-plus-index construction, accelerator-relative integer indices, and temporary default-device contexts.

How it works

torch.device describes where a tensor is allocated or resides. Create one from a device string, a type plus ordinal, or an ordinal that uses the current accelerator type; it can also temporarily control the default allocation device in a context manager.

Syntax

torch.device(device=None, index=None)

Parameters

NameTypeDescription
devicetorch.device | str | intDevice specification: a string, a device type with an ordinal, or an ordinal using the current accelerator type.
indexintOptional device ordinal when the device type is supplied separately.

Returns

A torch.device object representing the selected device.

Key points

  • It identifies the target or current location of a tensor allocation.
  • A device combines a backend type with an optional ordinal.
  • Strings can specify forms such as cpu, cuda, or cuda:0.
  • The type and index can be supplied as separate constructor arguments.
  • An integer-only constructor selects an index on the current accelerator backend.
  • Inspect a tensor's placement through its device property.

Common mistakes

  • Constructing torch.device from an integer requires a currently detected accelerator; otherwise it raises RuntimeError.
  • Creating or selecting a device does not migrate existing tensors; move them explicitly when needed.
  • Zero-dimensional tensors are automatically transferred from CPU to GPU when required, unlike other tensors.

Example: Choose an available device

import torch

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.randn(2, 3, device=device)
print(x.device)

Selects CUDA when available, otherwise CPU, then allocates a tensor on that device.

When to use it

Device-aware allocation

Select CPU or an available accelerator before creating tensors and models.

Scoped default device

Temporarily make device-unspecified tensor constructors allocate on a chosen device.

Placement inspection

Inspect where an existing tensor resides during debugging or device checks.

Interview questions

QWhat does torch.device represent?

torch.device represents the device on which a tensor is or will be allocated. It contains a device type and optionally a device ordinal, such as cpu, cuda, mps, xpu, xla, or meta.

Follow-up: What does torch.device("cuda") mean? It refers to the current CUDA device, and remains tied to the current device rather than a fixed ordinal.

QHow can torch.device be constructed?

It accepts a device string, a device type with an ordinal, or an integer ordinal. An integer uses the current accelerator type, so it requires an accelerator to be detected.

Follow-up: Why can torch.device(0) fail? It raises RuntimeError when no accelerator is currently detected.

QWhat does using torch.device as a context manager do?

Used as a context manager, torch.device temporarily changes the default device for tensor allocations made inside the block. This is useful when code constructs tensors without passing device explicitly.

Follow-up: How does this differ from torch.set_default_device? The context-manager form is temporary, whereas torch.set_default_device changes the default globally.

  • torch.Tensor.device
  • torch.cuda.current_device
  • torch.cuda.set_device
  • torch.set_default_device

Source: Tensor Attributes — PyTorch 2.14 documentation. Checked 2026-10-16.