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 owns the storage backing a single model input/output tensor on the
33 host and/or the NPU, and lets the data be moved between CPU and NPU. Instances
34 are created through `Model.acquire_input_npu_data` /
35 `Model.acquire_output_npu_data`, not constructed directly.
37 The data is accessible as a numpy array (via indexing, e.g. `nd[...]`) only while
38 it resides on the CPU. Bring it back with `cpu` before reading results that were
41 An NPU-resident NPUData may be passed to any model that expects a tensor of the
42 same shape and dtype on the same accelerator, so several models can share one
43 NPU-resident tensor without copying it back to the host in between.
45 @note This is an advanced API rather than a typical usage.
46 @warning This API is in beta: it may still contain bugs, and its behavior may
47 change in a future release.
50 def __init__(self, _npu_data: _cQbRuntime.NPUData):
51 """@brief Internal constructor.
53 @param _npu_data The underlying C++ NPUData object. Use
54 `Model.acquire_input_npu_data` / `Model.acquire_output_npu_data`
67 """@brief Builds the numpy view, or returns None if not CPU-resident.
69 The residency is checked first (dev_no() == -1 means the data is on the host):
70 requesting the buffer protocol while the data is on the NPU would propagate a
71 C++ exception out of pybind's `extern "C"` buffer slot and call std::terminate,
72 so it must not be attempted.
78 def launch(self, acc: Accelerator) ->
None:
79 """@brief Uploads the data to the NPU memory of the given accelerator.
81 @param acc The accelerator on which to place the data.
87 def cpu(self) -> None:
88 """@brief Brings the data back to host (CPU) memory."""
94 """@brief Copies the data from another NPUData.
96 Both tensors must have the same shape and dtype; the source may reside on
97 either side (a CPU-to-NPU or NPU-to-CPU copy is staged through the host).
99 @param src The source NPUData to copy from.
104 """@brief Reads element(s) from the host buffer via numpy indexing."""
106 raise ValueError(
"NPUData is not on the CPU; call cpu() first.")
110 """@brief Writes value(s) into the host buffer via numpy indexing."""
112 raise ValueError(
"NPUData is not on the CPU; call cpu() first.")
116 """@brief Returns the length of the buffer's first dimension."""
121 """@brief Returns the shape of the data."""
126 """@brief Returns the numpy dtype of the data's elements."""
128 if data_type
not in _DTYPE_MAP:
131 raise ValueError(f
"NPUData has no numpy equivalent for {data_type}.")
132 return np.dtype(_DTYPE_MAP[data_type])
136 """@brief Device number of the accelerator the data resides on.
138 @return The device number when the data is on the NPU, or -1 when it is on the
145 """@brief Hardware name (device type) of the accelerator the data resides on.
147 Device numbers are assigned per hardware type (e.g. an "aries-rb" and a
148 "regulus-ra" can both be device #0), so ``dev_no`` alone does not identify a
149 physical device; pair it with this name to distinguish devices.
151 @return The hardware name (e.g. "aries-rb") when the data is on the NPU, or an
152 empty string when it is on the host (CPU).
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.