numojo.core.accelerator.device¶
Device (numojo.core.accelerator.device)
This module defines the Device struct, which represents an execution device for array and matrix operations.
It supports CPU and GPU devices, with GPU backends for NVIDIA CUDA, AMD ROCm, and Apple Metal.
Aliases¶
cpu¶
Value: Device.CPU
cuda¶
Value: Device.CUDA
rocm¶
Value: Device.ROCM
mps¶
Value: Device.MPS
Structs¶
Device¶
Memory convention: memory_only
Implements: AnyType, Copyable, Equatable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, Representable, Stringable, Writable
Represents an execution device for array and matrix operations.
A Device identifies where computation should run, analogous to
torch.device in PyTorch. Each device has a type ("cpu" or "gpu"),
an optional backend name ("cuda", "rocm", or "mps" for GPUs), and
a zero-based device index.
Use the predefined comptime constants for common devices:
- Device.CPU — CPU execution.
- Device.CUDA — NVIDIA CUDA GPU.
- Device.ROCM — AMD ROCm GPU.
- Device.MPS — Apple Metal GPU.
Devices can also be constructed from torch-style strings:
Fields¶
type(String): Device type: "cpu" or "gpu".name(String): Backend identifier: "" for CPU, "cuda" | "rocm" | "mps" for GPU.id(Int): Zero-based device index on the backend.
Aliases¶
CPU¶
Value: Device._unchecked_init("cpu", "", 0)
CPU device.
CUDA¶
Value: Device._unchecked_init("gpu", "cuda", 0)
NVIDIA CUDA GPU device.
ROCM¶
Value: Device._unchecked_init("gpu", "rocm", 0)
AMD ROCm GPU device.
MPS¶
Value: Device._unchecked_init("gpu", "mps", 0)
Apple Metal GPU device.
__del__is_trivial¶
Value: False
__move_ctor_is_trivial¶
Value: True
__copy_ctor_is_trivial¶
Value: False
Methods¶
__init__¶
Overload 1¶
static
Initialize a default CPU device.
Args:
self(Self)[out]
Returns:
Self
Overload 2¶
static
Initialize a device by parsing a torch-style device string.
Supported formats: "cpu", "cuda", "cuda:0", "rocm", "rocm:1", "mps", "mps:0", "gpu".
Args:
text(String): A device string to parse.self(Self)[out]
Returns:
Self
Raises
Error on invalid device string format.
Overload 3¶
static
Initialize a device with explicit type, name, and index.
Validates the arguments and falls back to CPU if the requested GPU backend is not available on the current system.
Args:
type(String): Device type, must be "cpu" or "gpu".name(String): Backend name ("" for CPU; "cuda", "rocm", or "mps" for GPU).id(Int): Zero-based device index (must be 0 for CPU, >= 0 for GPU).self(Self)[out]
Returns:
Self
__eq__¶
Check equality with another device.
Args:
self(Self)other(Self): The device to compare against.
Returns:
Bool
__ne__¶
Check inequality with another device.
Args:
self(Self)other(Self): The device to compare against.
Returns:
Bool
write_to¶
Write the string representation to a writer.
Parameters:
W(Writer): The writer type.
Args:
self(Self)writer(W)[mut]: The writer to write to.
is_available¶
Check if this device is available on the current system.
Args:
self(Self)
Returns:
Bool
default_device¶
static
Return the best available device: GPU if present, otherwise CPU.
Returns:
Self
available_gpu¶
static
Return the name of the best available GPU backend.
Checks in order: CUDA → ROCm → MPS.
Returns:
String
Raises
NumojoError if no GPU accelerator is detected.
parse_device_string¶
static
Parse a torch-style device string into a Device.
Supported formats: - "cpu" - "cuda", "cuda:0", "cuda:1", ... - "rocm", "rocm:0", "rocm:1", ... - "mps", "mps:0", "mps:1", ... - "gpu" (resolves to best available GPU backend)
Args:
text(String): The device string to parse.
Returns:
Self
Raises
Error when "gpu" is specified but no GPU backend is available.
Functions¶
is_accelerator_available¶
Check at compile time whether the given device's GPU accelerator exists.
Parameters:
device(Device): The device to check.
Returns:
Bool