5from typing
import Tuple
9import qbruntime.qbruntime
as _cQbRuntime
10from .accelerator
import Accelerator
12_Shape = Tuple[int, ...]
18 _cQbRuntime.DataType.Float32: np.float32,
19 _cQbRuntime.DataType.Float16: np.float16,
20 _cQbRuntime.DataType.Int8: np.int8,
21 _cQbRuntime.DataType.Uint8: np.uint8,
30 """@brief A model input or output tensor that can reside on the host (CPU) or NPU.
32 An NPUData backs a single model input/output tensor: it owns its host buffer,
33 holds NPU memory that belongs to the `Accelerator` while the data is on the NPU,
34 and lets the data be moved between CPU and NPU. Instances are created through
35 `Model.acquire_input_npu_data` / `Model.acquire_output_npu_data`, not constructed
38 The data is accessible as a numpy array (via indexing, e.g. `nd[...]`) only while
39 it resides on the CPU. Bring it back with `cpu` before reading results that were
42 An NPU-resident NPUData may be passed to any model that expects a tensor of the
43 same shape and dtype on the same accelerator, so several models can share one
44 NPU-resident tensor without copying it back to the host in between.
46 An NPUData is not owned by the `Model` it came from and may outlive it. Its NPU
47 memory belongs to the `Accelerator` it resides on: if that accelerator is
48 destroyed while the data is still on the NPU, the memory is reclaimed and the
49 NPUData becomes invalid (`cpu`, `launch`, `copy_from` and indexing raise, and
50 `dev_no` returns -1). A CPU-resident NPUData is unaffected.
52 @note This is an advanced API rather than a typical usage.
53 @warning This API is in beta: it may still contain bugs, and its behavior may
54 change in a future release.
55 @warning NPUData objects are not fully thread-safe; the client must serialize
56 access to an NPUData from different threads. In particular, `launch` and
57 `cpu` change where the data resides and must not run concurrently with
61 def __init__(self, _npu_data: _cQbRuntime.NPUData):
62 """@brief Internal constructor.
64 @param _npu_data The underlying C++ NPUData object. Use
65 `Model.acquire_input_npu_data` / `Model.acquire_output_npu_data`
78 """@brief Builds the numpy view, or returns None if not CPU-resident.
80 The residency is checked first (dev_no() == -1 means the data is on the host):
81 requesting the buffer protocol while the data is on the NPU would propagate a
82 C++ exception out of pybind's `extern "C"` buffer slot and call std::terminate,
83 so it must not be attempted.
89 def launch(self, acc: Accelerator) ->
None:
90 """@brief Uploads the data to the NPU memory of the given accelerator.
92 @warning Not thread-safe: see the class-level note.
94 @param acc The accelerator on which to place the data.
101 """@brief Brings the data back to host (CPU) memory.
103 @warning Not thread-safe: see the class-level note.
110 """@brief Copies the data from another NPUData.
112 Both tensors must have the same shape and dtype; the source may reside on
113 either side (a CPU-to-NPU or NPU-to-CPU copy is staged through the host).
115 @param src The source NPUData to copy from.
120 """@brief Reads element(s) from the host buffer via numpy indexing."""
122 raise ValueError(
"NPUData is not on the CPU; call cpu() first.")
126 """@brief Writes value(s) into the host buffer via numpy indexing."""
128 raise ValueError(
"NPUData is not on the CPU; call cpu() first.")
132 """@brief Returns the length of the buffer's first dimension."""
137 """@brief Returns the shape of the data."""
142 """@brief Returns the numpy dtype of the data's elements."""
144 if data_type
not in _DTYPE_MAP:
147 raise ValueError(f
"NPUData has no numpy equivalent for {data_type}.")
148 return np.dtype(_DTYPE_MAP[data_type])
152 """@brief Device number of the accelerator the data resides on.
154 @return The device number when the data is on the NPU, or -1 when it is on the
155 host (CPU) or has been invalidated (see the class-level note on
156 accelerator destruction).
162 """@brief Hardware name (device type) of the accelerator the data resides on.
164 Device numbers are assigned per hardware type (e.g. an "aries-rb" and a
165 "regulus-ra" can both be device #0), so ``dev_no`` alone does not identify a
166 physical device; pair it with this name to distinguish devices.
168 @return The hardware name (e.g. "aries-rb") when the data is on the NPU, or an
169 empty string when it is on the host (CPU) or has been invalidated (see
170 the class-level note on accelerator destruction).
A model input or output tensor that can reside on the host (CPU) or NPU.
np.dtype dtype(self)
Returns the numpy dtype of the data's elements.
_make_array(self)
Builds the numpy view, or returns None if not CPU-resident.
None launch(self, Accelerator acc)
Uploads the data to the NPU memory of the given accelerator.
__getitem__(self, key)
Reads element(s) from the host buffer via numpy indexing.
None copy_from(self, "NPUData" src)
Copies the data from another NPUData.
str hardware_name(self)
Hardware name (device type) of the accelerator the data resides on.
__init__(self, _cQbRuntime.NPUData _npu_data)
Internal constructor.
_Shape shape(self)
Returns the shape of the data.
__setitem__(self, key, value)
Writes value(s) into the host buffer via numpy indexing.
int dev_no(self)
Device number of the accelerator the data resides on.
int __len__(self)
Returns the length of the buffer's first dimension.
None cpu(self)
Brings the data back to host (CPU) memory.