accelerator.py Source File

accelerator.py Source File#

SDK qb Runtime Library: accelerator.py Source File
SDK qb Runtime Library v1.4
MCS001-EN
accelerator.py
Go to the documentation of this file.
1
4
5from typing import Sequence, overload
6
7import numpy as np
8
9import qbruntime.qbruntime as _cQbRuntime
10from .pinned_memory import PinnedMemory
11
12
15
16
18 """
19 @brief Represents an accelerator, i.e., an NPU, used for executing models.
20
21 @attention For the `regulus-usb` device family, do not unplug the USB device while
22 the runtime is running. Disconnecting a device that is in use may crash
23 the program or cause unexpected misbehavior.
24 """
25
26 @overload
27 def __init__(self, dev_no: int = 0): ...
28
29 @overload
30 def __init__(self, device_name: str = "auto"): ...
31
32 @overload
33 def __init__(self, device_name: str = "auto", dev_no: int = 0): ...
34
35 def __init__(self, device_name="auto", dev_no=0):
36 """
37 @brief Creates an Accelerator object for a specific target device and device number.
38
39 The @p device_name parameter selects target device to open; case-insensitive.
40 Supported values:
41 - `"auto"`
42 - `"aries"`, `"aries-rb"`
43 - `"regulus"`, `"regulus-usb"`
44 - `"regulus-ra"`, `"regulus-ra-usb"`
45 - `"regulus-rb"`, `"regulus-rb-usb"`
46
47 Automatic resolution:
48 - `"auto"` resolves the target device automatically, but only when exactly one
49 supported kind of target device is present in the system.
50 - A base name (`"aries"`, `"regulus"`, `"regulus-usb"`, ...) is a shorthand
51 for every target device whose name begins with it (e.g. `"aries"` matches
52 `"aries-rb"`). It resolves to the concrete target device automatically, but
53 only when exactly one such matching target device is present.
54
55 The @p dev_no parameter is the device number. For example, on Linux an ARIES NPU
56 attached as `/dev/aries0` has device number `0`.
57
58 @param[in] device_name Device name to open (case-insensitive). See the list above.
59 @param[in] dev_no The device number to associate with the Accelerator.
60
61 @throws QbRuntimeError If @p device_name is unknown, if auto-resolution is ambiguous
62 (more than one candidate), or if no matching device exists.
63
64 @note Supported constructor forms:
65 - `Accelerator()`: auto-select the only attached NPU.
66 - `Accelerator(dev_no)`: auto-detect device, open target device `dev_no`.
67 - `Accelerator(device_name)`: open `device_name` at device 0.
68 - `Accelerator(device_name, dev_no)`: open `device_name` at device `dev_no`.
69 """
70 if isinstance(device_name, int): # Accelerator(dev_no) constructor
71 dev_no, device_name = device_name, "auto"
72 self._accelerator = _cQbRuntime.Accelerator(device_name, dev_no)
73
74 def get_device_name(self) -> str:
75 """
76 @brief Returns the resolved device name of this accelerator.
77
78 When created with a automatic resolve such as `"auto"`, `"aries"`,
79 this returns the real target device name that was resolved
80 (e.g. `"aries-rb"`, `"regulus-ra-usb"`).
81
82 @return The resolved device name.
83 """
84 return self._accelerator.get_device_name()
85
87 self, shape: Sequence[int], dtype: np.dtype
88 ) -> PinnedMemory:
89 """
90 @brief Allocates an NPU-accessible pinned memory buffer.
91
92 The returned buffer can be used as input or output for zero-copy inference
93 via `Model.infer_pinned_memory`.
94
95 @note This is an experimental API and is only supported on REGULUS device.
96
97 @param shape The shape of the buffer to allocate.
98 @param dtype The element type of the buffer. Must be `numpy.float32` or
99 `numpy.uint8`.
100 @return A PinnedMemory wrapping the allocated buffer.
101 """
103 [int(s) for s in shape], np.dtype(dtype)
104 )
105 return PinnedMemory(cobj)
106
107
108
Represents an accelerator, i.e., an NPU, used for executing models.
PinnedMemory allocate_pinned_memory(self, Sequence[int] shape, np.dtype dtype)
Allocates an NPU-accessible pinned memory buffer.
str get_device_name(self)
Returns the resolved device name of this accelerator.
An NPU-accessible pinned (physically contiguous) memory buffer.