model_variant_handle.py Source File

model_variant_handle.py Source File#

SDK qb Runtime Library: model_variant_handle.py Source File
SDK qb Runtime Library v1.4
MCS001-KR
model_variant_handle.py
Go to the documentation of this file.
1
4
5from typing import List, Tuple
6
7import qbruntime.qbruntime as _cQbRuntime
8from .npu_data import NPUData
9from .type import *
10
11_Shape = Tuple[int, ...]
12
13
16
17
19 """
20 @brief Handle to a specific variant of a loaded model.
21
22 This class provides access to variant-specific information such as input/output
23 shapes, buffer information, and quantization scales. It also offers APIs for
24 managing inference buffers, consistent with the interface of the `Model` class.
25
26 Objects of this class are obtained via `Model.get_model_variant_handle()`.
27 """
28
29 def __init__(self, _model_variant_handle: _cQbRuntime.ModelVariantHandle):
30 self._model_variant_handle = _model_variant_handle
32
33 @classmethod
34 def from_cpp(cls, _model_variant_handle: _cQbRuntime.ModelVariantHandle):
35 return cls(_model_variant_handle)
36
37 def get_variant_idx(self) -> int:
38 """
39 @brief Returns the index of this model variant.
40
41 @return Index of the model variant.
42 """
44
45 def get_model_input_shape(self) -> List[_Shape]:
46 """
47 @brief Returns the input shape for this model variant.
48
49 @return model variant's input shape.
50 """
52
53 def get_model_output_shape(self) -> List[_Shape]:
54 """
55 @brief Returns the output shape for this model variant.
56
57 @return model variant's output shape.
58 """
60
61 def get_input_buffer_info(self) -> List[BufferInfo]:
62 """
63 @brief Returns the input buffer information for this variant.
64
65 @return A list of input buffer information.
66 """
67 return [
68 BufferInfo.from_cpp(bi)
70 ]
71
72 def get_output_buffer_info(self) -> List[BufferInfo]:
73 """
74 @brief Returns the output buffer information for this variant.
75
76 @return A list of output buffer information.
77 """
78 return [
79 BufferInfo.from_cpp(bi)
81 ]
82
83 def get_input_scale(self) -> List[Scale]:
84 """
85 @brief Returns the input quantization scale(s) for this variant.
86
87 @return A list of input scales.
88 """
89 return [Scale.from_cpp(s) for s in self._model_variant_handle.get_input_scale()]
90
91 def get_output_scale(self) -> List[Scale]:
92 """
93 @brief Returns the output quantization scale(s) for this variant.
94
95 @return A list of output scales.
96 """
97 return [
98 Scale.from_cpp(s) for s in self._model_variant_handle.get_output_scale()
99 ]
100
101 def get_model_input_data_type(self) -> DataType:
102 """
103 @brief Returns the input buffer information for the model.
104
105 @return A list of input buffer information.
106 """
108
109 def get_model_output_data_type(self) -> DataType:
110 """
111 @brief Returns the output buffer information of the model.
112
113 @return A list of output buffer information.
114 """
116
117 def acquire_input_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
118 """
119 @brief Buffer Management API
120
121 Acquires list of `Buffer` for input.
122 These API is required when calling `Model.infer_buffer()`.
123
124 @note These APIs are intended for advanced use and follow the same buffer
125 management interface as the `Model` class.
126 """
127 return [
128 Buffer(b) for b in self._model_variant_handle.acquire_input_buffer(seqlens)
129 ]
130
131 def acquire_output_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
132 """
133 @brief Buffer Management API
134
135 Acquires list of `Buffer` for output.
136 These API is required when calling `Model.infer_buffer()`.
137
138 @note These APIs are intended for advanced use and follow the same buffer
139 management interface as the `Model` class.
140 """
141 return [
142 Buffer(b) for b in self._model_variant_handle.acquire_output_buffer(seqlens)
143 ]
144
146 self, shape: List[int], idx: int, upload: bool, dtype: DataType
147 ) -> NPUData:
148 """
149 @brief Acquires an NPUData for this variant's input at the given index.
150
151 Equivalent to `Model.acquire_input_npu_data`, except that the tensor layout
152 comes from this variant instead of variant 0.
153
154 @note This is an advanced API rather than a typical usage.
155 @warning This API is in beta: it may still contain bugs, and its behavior may
156 change in a future release.
157 """
158 return NPUData(
160 [int(s) for s in shape], idx, upload, dtype.value
161 )
162 )
163
165 self, shape: List[int], idx: int, upload: bool, dtype: DataType
166 ) -> NPUData:
167 """
168 @brief Acquires an NPUData for this variant's output at the given index.
169
170 Equivalent to `Model.acquire_output_npu_data`, except that the tensor layout
171 comes from this variant instead of variant 0.
172
173 @note This is an advanced API rather than a typical usage.
174 @warning This API is in beta: it may still contain bugs, and its behavior may
175 change in a future release.
176 """
177 return NPUData(
179 [int(s) for s in shape], idx, upload, dtype.value
180 )
181 )
182
183 def release_buffer(self, buffer: List[Buffer]) -> None:
184 """
185 @brief Buffer Management API
186
187 Deallocate acquired Input/Output buffer
188
189 @note These APIs are intended for advanced use and follow the same buffer
190 management interface as the `Model` class.
191 """
192 self._model_variant_handle.release_buffer([b._buffer for b in buffer])
193
195 self,
196 inputs: List[np.ndarray],
197 input_bufs: List[Buffer],
198 seqlens: List[List[int]] = [],
199 ) -> None:
200 """Reposition input"""
201 inputs = [np.ascontiguousarray(i) for i in inputs]
203 inputs, [buf._buffer for buf in input_bufs], seqlens
204 )
205
207 self,
208 output_bufs: List[Buffer],
209 outputs: List[np.ndarray],
210 seqlens: List[List[int]] = [],
211 ) -> None:
212 """Reposition output"""
213 if len(outputs) != len(self._output_shape):
214 outputs.clear()
215 for shape in self._output_shape:
216 outputs.append(np.empty(shape=shape, dtype=np.float32))
217 else:
218 for oi in range(len(outputs)):
219 outputs[oi] = np.ascontiguousarray(outputs[oi])
221 [buf._buffer for buf in output_bufs], outputs, seqlens
222 )
223
224
225
Handle to a specific variant of a loaded model.
None release_buffer(self, List[Buffer] buffer)
Buffer Management API.
List[BufferInfo] get_output_buffer_info(self)
Returns the output buffer information for this variant.
None reposition_outputs(self, List[Buffer] output_bufs, List[np.ndarray] outputs, List[List[int]] seqlens=[])
Reposition output.
DataType get_model_input_data_type(self)
Returns the input buffer information for the model.
NPUData acquire_input_npu_data(self, List[int] shape, int idx, bool upload, DataType dtype)
Acquires an NPUData for this variant's input at the given index.
List[_Shape] get_model_input_shape(self)
Returns the input shape for this model variant.
List[Scale] get_input_scale(self)
Returns the input quantization scale(s) for this variant.
int get_variant_idx(self)
Returns the index of this model variant.
List[_Shape] get_model_output_shape(self)
Returns the output shape for this model variant.
DataType get_model_output_data_type(self)
Returns the output buffer information of the model.
List[Scale] get_output_scale(self)
Returns the output quantization scale(s) for this variant.
List[Buffer] acquire_input_buffer(self, List[List[int]] seqlens=[])
Buffer Management API.
None reposition_inputs(self, List[np.ndarray] inputs, List[Buffer] input_bufs, List[List[int]] seqlens=[])
Reposition input.
List[BufferInfo] get_input_buffer_info(self)
Returns the input buffer information for this variant.
NPUData acquire_output_npu_data(self, List[int] shape, int idx, bool upload, DataType dtype)
Acquires an NPUData for this variant's output at the given index.
List[Buffer] acquire_output_buffer(self, List[List[int]] seqlens=[])
Buffer Management API.
A model input or output tensor that can reside on the host (CPU) or NPU.
Definition npu_data.py:29
A simple byte-sized buffer.
Definition type.py:219