Shortcuts

Struct Device

Page Contents

Struct Documentation

struct Device

Represents a compute device on which a tensor is located.

A device is uniquely identified by a type, which specifies the type of machine it is (e.g. CPU or CUDA GPU), and a device index or ordinal, which identifies the specific compute device when there is more than one of a certain type. The device index is optional, and in its defaulted state represents (abstractly) “the current device”. Further, there are two constraints on the value of the device index, if one is explicitly stored:

  1. A negative index represents the current device, a non-negative index represents a specific, concrete device,

  2. When the device type is CPU, the device index must be zero.

Public Types

using Type = DeviceType

Public Functions

inline Device(DeviceType type, DeviceIndex index = -1)

Constructs a new Device from a DeviceType and an optional device index.

Device(const std::string &device_string)

Constructs a Device from a string description, for convenience.

The string supplied must follow the following schema: (cpu|cuda)[:<device-index>] where cpu or cuda specifies the device type, and :<device-index> optionally specifies a device index.

inline bool operator==(const Device &other) const noexcept

Returns true if the type and index of this Device matches that of other.

inline bool operator!=(const Device &other) const noexcept

Returns true if the type or index of this Device differs from that of other.

inline void set_index(DeviceIndex index)

Sets the device index.

inline DeviceType type() const noexcept

Returns the type of device this is.

inline DeviceIndex index() const noexcept

Returns the optional index.

inline bool has_index() const noexcept

Returns true if the device has a non-default index.

inline bool is_cuda() const noexcept

Return true if the device is of CUDA type.

inline bool is_privateuseone() const noexcept

Return true if the device is of PrivateUse1 type.

inline bool is_mps() const noexcept

Return true if the device is of MPS type.

inline bool is_hip() const noexcept

Return true if the device is of HIP type.

inline bool is_ve() const noexcept

Return true if the device is of VE type.

inline bool is_xpu() const noexcept

Return true if the device is of XPU type.

inline bool is_ipu() const noexcept

Return true if the device is of IPU type.

inline bool is_xla() const noexcept

Return true if the device is of XLA type.

inline bool is_mtia() const noexcept

Return true if the device is of MTIA type.

inline bool is_hpu() const noexcept

Return true if the device is of HPU type.

inline bool is_lazy() const noexcept

Return true if the device is of Lazy type.

inline bool is_vulkan() const noexcept

Return true if the device is of Vulkan type.

inline bool is_metal() const noexcept

Return true if the device is of Metal type.

inline bool is_maia() const noexcept

Return true if the device is of MAIA type.

inline bool is_meta() const noexcept

Return true if the device is of META type.

inline bool is_cpu() const noexcept

Return true if the device is of CPU type.

inline bool supports_as_strided() const noexcept

Return true if the device supports arbitrary strides.

std::string str() const

Same string as returned from operator<<.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources