pinned_memory.py Source File

pinned_memory.py Source File#

SDK qb Runtime Library: pinned_memory.py Source File
SDK qb Runtime Library v1.3
MCS001-EN
pinned_memory.py
Go to the documentation of this file.
1
4
5from typing import Tuple, Union
6
7import numpy as np
8
9import qbruntime.qbruntime as _cQbRuntime
10
11_Shape = Tuple[int, ...]
12
13__all__ = ["PinnedMemory"]
14
15
18
19
21 """@brief An NPU-accessible pinned (physically contiguous) memory buffer.
22
23 A PinnedMemory wraps a buffer that is pinned in device memory so the NPU can
24 read from / write to it directly, enabling zero-copy inference via
25 `Model.infer_pinned_memory`. Instances are created through
26 `Accelerator.allocate_pinned_memory`, not constructed directly.
27
28 @note This is an experimental API and is only supported on Regulus hardware.
29 """
30
32 self,
33 _pinned_memory: Union[
34 _cQbRuntime.PinnedMemoryFloat, _cQbRuntime.PinnedMemoryUint8
35 ],
36 ):
37 """@brief Internal constructor.
38
39 @param _pinned_memory The underlying C++ pinned-memory object. Use
40 `Accelerator.allocate_pinned_memory` to create instances.
41 """
42 self._pinned_memory = _pinned_memory
43 # Zero-copy numpy view over the pinned buffer. Built once here so that
44 # indexing does not rebuild it. Shares memory with the pinned buffer and
45 # is only valid while this PinnedMemory is alive and available.
46 self._array = np.asarray(_pinned_memory)
47
48 def __getitem__(self, key):
49 """@brief Reads element(s) from the pinned buffer via indexing.
50
51 Supports the same indexing as numpy (integers, slices, tuples, etc.),
52 e.g. `pm[0]`, `pm[1, 2]`, `pm[:10]`. The data is read directly from the
53 pinned buffer without an intermediate copy.
54
55 @param key The index or slice.
56 @return The selected element or a view into the pinned buffer.
57 """
58 return self._array[key]
59
60 def __setitem__(self, key, value):
61 """@brief Writes value(s) into the pinned buffer via indexing.
62
63 Writes directly into the pinned buffer, e.g. `pm[0] = 1.0`,
64 `pm[:10] = src`. This is the recommended way to fill an input buffer
65 before `Model.infer_pinned_memory`.
66
67 @param key The index or slice.
68 @param value The value(s) to write.
69 """
70 self._array[key] = value
71
72 def __len__(self) -> int:
73 """@brief Returns the length of the buffer's first dimension."""
74 return self.shape[0]
75
76 @property
77 def shape(self) -> _Shape:
78 """@brief Returns the shape of the buffer."""
79 return tuple(self._pinned_memory.shape())
80
81 @property
82 def dtype(self) -> np.dtype:
83 """@brief Returns the numpy dtype of the buffer's elements."""
84 return self._array.dtype
85
86 @property
87 def size(self) -> int:
88 """@brief Returns the size of the buffer in bytes."""
89 return self._pinned_memory.size()
90
91 @property
92 def is_available(self) -> bool:
93 """@brief Returns whether the buffer is allocated and still usable."""
94 return self._pinned_memory.is_available()
95
96
97
An NPU-accessible pinned (physically contiguous) memory buffer.
np.dtype dtype(self)
Returns the numpy dtype of the buffer's elements.
int __len__(self)
Returns the length of the buffer's first dimension.
int size(self)
Returns the size of the buffer in bytes.
__setitem__(self, key, value)
Writes value(s) into the pinned buffer via indexing.
bool is_available(self)
Returns whether the buffer is allocated and still usable.
__getitem__(self, key)
Reads element(s) from the pinned buffer via indexing.
_Shape shape(self)
Returns the shape of the buffer.
__init__(self, Union[_cQbRuntime.PinnedMemoryFloat, _cQbRuntime.PinnedMemoryUint8] _pinned_memory)
Internal constructor.