qten.utils.devices
Module reference for qten.utils.devices.
devices
Logical device helpers used by QTen objects.
QTen exposes a small device vocabulary through Device:
"cpu" for host execution and "gpu" for accelerated execution. The concrete
backend is resolved by PyTorch when a device is used, currently mapping "gpu"
to CUDA through Device.torch_device().
Repository usage
Use Device to store device intent in QTen data
structures and implement DeviceBounded
when an object can move or copy itself between logical devices.
Examples:
device = Device.new("gpu:0")
torch_device = device.torch_device()
obj_on_cpu = obj.cpu()
obj_on_gpu = obj.gpu(0)
Device
dataclass
Device(
name: Literal["cpu", "gpu"], index: Optional[int] = None
)
Lightweight immutable device descriptor used by QTen.
The public device model is intentionally small:
- "cpu" represents host execution.
- "gpu" represents accelerated execution.
For GPU devices, index optionally stores a CUDA device index. This index
is only meaningful on CUDA-capable systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Literal['cpu', 'gpu']
|
The logical device family. |
required |
index
|
Optional[int]
|
Optional CUDA device index. This should only be set when |
`None`
|
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Literal['cpu', 'gpu']
|
Logical device family. |
index |
Optional[int]
|
Optional CUDA device index for GPU execution. |
name
instance-attribute
name: Literal['cpu', 'gpu']
Logical device family. QTen uses "cpu" for host execution and "gpu"
for CUDA-backed execution.
index
class-attribute
instance-attribute
index: Optional[int] = None
Optional CUDA device index for GPU execution. This is meaningful only when
name == "gpu".
__repr__
class-attribute
instance-attribute
__repr__ = __str__
new
staticmethod
new(name: str) -> Device
Parse a user-facing device string into a Device.
Supported inputs
"cpu""gpu""gpu:<index>", where<index>is a non-negative integer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Device string to parse. |
required |
Returns:
| Type | Description |
|---|---|
Device
|
Parsed immutable device descriptor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input does not match one of the supported formats. |
Examples:
Device.new("cpu")
Device.new("gpu:0")
Source code in src/qten/utils/devices.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
torch_device
torch_device() -> torch.device
Resolve this logical device into the concrete PyTorch backend device.
Resolution is runtime-dependent:
- "cpu" always maps to torch.device("cpu").
- "gpu" prefers CUDA when available.
When CUDA is selected, an explicit index is used if present.
Otherwise, the current CUDA device reported by PyTorch is used.
Returns:
| Type | Description |
|---|---|
device
|
Concrete PyTorch device corresponding to this logical device. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If a GPU device is requested but neither CUDA nor MPS is available in the current environment. |
Source code in src/qten/utils/devices.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
__str__
__str__() -> str
Format the device using QTen's logical device syntax.
Returns:
| Type | Description |
|---|---|
str
|
"cpu", |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/qten/utils/devices.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
DeviceBounded
Bases: ABC
Mixin for objects that can be copied or moved between logical devices.
Concrete subclasses implement to_device
and expose their current device.
Convenience helpers cpu and
gpu are defined in terms of that
abstract interface.
device
abstractmethod
property
device: Device
Return the current device this object resides on.
Returns:
| Type | Description |
|---|---|
Device
|
The current device of this object. |
cpu
cpu() -> Self
Return a copy of this object residing on the CPU device.
Returns:
| Type | Description |
|---|---|
Self
|
A copy of this object on the logical CPU device. |
Source code in src/qten/utils/devices.py
191 192 193 194 195 196 197 198 199 200 | |
gpu
gpu(index: Optional[int] = None) -> Self
Return a copy of this object residing on a GPU device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Optional[int]
|
Optional CUDA device index. This should only be set when the target GPU backend supports multiple devices (e.g. CUDA). If not provided, the current device will be used. |
`None`
|
Returns:
| Type | Description |
|---|---|
Self
|
A copy of this object on the specified GPU device. |
Source code in src/qten/utils/devices.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
to_device
abstractmethod
to_device(device: Device) -> Self
Return a copy of this object residing on the specified device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
Device
|
The target device to move this object to. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A copy of this object on the specified device. |
Source code in src/qten/utils/devices.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |