abracudabra.device.base ======================= .. py:module:: abracudabra.device.base .. autoapi-nested-parse:: Define the base device class. Attributes ---------- .. autoapisummary:: abracudabra.device.base.DeviceType abracudabra.device.base.DEVICE_TYPES Classes ------- .. autoapisummary:: abracudabra.device.base.Device Module Contents --------------- .. py:data:: DeviceType The device type, e.g., ``"cpu"`` or ``"cuda"``. .. py:data:: DEVICE_TYPES :type: frozenset[DeviceType] The supported device types. .. py:class:: Device Bases: :py:obj:`NamedTuple` A device with a name and index. .. py:attribute:: type :type: DeviceType The device type, e.g., ``"cpu"`` or ``"cuda"``. .. py:attribute:: idx :type: int | None :value: None The device index, e.g., ``0`` or ``None``. .. py:method:: __str__() Return the device name. .. py:method:: validate(device, idx = None) :classmethod: Return a device, validating the device type and index. :param device: The device type. :param idx: The optional device index. :returns: The device. .. py:method:: from_str(device, /) :classmethod: Return a device from a string. The string should be in the format ``"device[:idx]"``. .. rubric:: Examples >>> Device.from_str("cpu") Device(type="cpu", idx=None) >>> Device.from_str("cuda:1") Device(type="cuda", idx=1) .. py:method:: parse(device, /) :classmethod: Return a device from a string or device. If the input is already a device, it is returned as is. Otherwise, the input is parsed as a string. :param device: The device or device string (e.g., ``"cpu"`` or ``"cuda:1"``). :returns: The device. .. py:method:: to_torch() Return a torch device. .. rubric:: Examples >>> Device("cpu", None).to_torch() device(type='cpu') >>> Device("cuda", 1).to_torch() device(type='cuda', index=1)