model_variant_handle.py Source File

model_variant_handle.py Source File#

Runtime Library: model_variant_handle.py Source File
Runtime Library v0.30
Mobilint SDK qb
model_variant_handle.py
Go to the documentation of this file.
1
4
5from typing import List, Tuple
6
7import maccel.maccel as _cMaccel
8from .type import *
9
10_Shape = Tuple[int, ...]
11
12
15
16
18 """
19 @brief Handle to a specific variant of a loaded model.
20
21 This class provides access to variant-specific information such as input/output
22 shapes, buffer information, and quantization scales. It also offers APIs for
23 managing inference buffers, consistent with the interface of the `Model` class.
24
25 Objects of this class are obtained via `Model.get_model_variant_handle()`.
26 """
27
28 def __init__(self, _model_variant_handle: _cMaccel.ModelVariantHandle):
29 self._model_variant_handle = _model_variant_handle
31
32 @classmethod
33 def from_cpp(cls, _model_variant_handle: _cMaccel.ModelVariantHandle):
34 return cls(_model_variant_handle)
35
36 def get_variant_idx(self) -> int:
37 """
38 @brief Returns the index of this model variant.
39
40 @return Index of the model variant.
41 """
43
44 def get_model_input_shape(self) -> List[_Shape]:
45 """
46 @brief Returns the input shape for this model variant.
47
48 @return model variant's input shape.
49 """
51
52 def get_model_output_shape(self) -> List[_Shape]:
53 """
54 @brief Returns the output shape for this model variant.
55
56 @return model variant's output shape.
57 """
59
60 def get_input_buffer_info(self) -> List[BufferInfo]:
61 """
62 @brief Returns the input buffer information for this variant.
63
64 @return A list of input buffer information.
65 """
66 return [
67 BufferInfo.from_cpp(bi)
69 ]
70
71 def get_output_buffer_info(self) -> List[BufferInfo]:
72 """
73 @brief Returns the output buffer information for this variant.
74
75 @return A list of output buffer information.
76 """
77 return [
78 BufferInfo.from_cpp(bi)
80 ]
81
82 def get_input_scale(self) -> List[Scale]:
83 """
84 @brief Returns the input quantization scale(s) for this variant.
85
86 @return A list of input scales.
87 """
88 return [Scale.from_cpp(s) for s in self._model_variant_handle.get_input_scale()]
89
90 def get_output_scale(self) -> List[Scale]:
91 """
92 @brief Returns the output quantization scale(s) for this variant.
93
94 @return A list of output scales.
95 """
96 return [
97 Scale.from_cpp(s) for s in self._model_variant_handle.get_output_scale()
98 ]
99
100 def acquire_input_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
101 """
102 @brief Buffer Management API
103
104 Acquires list of `Buffer` for input.
105 These API is required when calling `Model.infer_buffer()`.
106
107 @note These APIs are intended for advanced use and follow the same buffer
108 management interface as the `Model` class.
109 """
110 return [
111 Buffer(b) for b in self._model_variant_handle.acquire_input_buffer(seqlens)
112 ]
113
114 def acquire_output_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
115 """
116 @brief Buffer Management API
117
118 Acquires list of `Buffer` for output.
119 These API is required when calling `Model.infer_buffer()`.
120
121 @note These APIs are intended for advanced use and follow the same buffer
122 management interface as the `Model` class.
123 """
124 return [
125 Buffer(b) for b in self._model_variant_handle.acquire_output_buffer(seqlens)
126 ]
127
128 def release_buffer(self, buffer: List[Buffer]) -> None:
129 """
130 @brief Buffer Management API
131
132 Deallocate acquired Input/Output buffer
133
134 @note These APIs are intended for advanced use and follow the same buffer
135 management interface as the `Model` class.
136 """
137 self._model_variant_handle.release_buffer([b._buffer for b in buffer])
138
140 self,
141 inputs: List[np.ndarray],
142 input_bufs: List[Buffer],
143 seqlens: List[List[int]] = [],
144 ) -> None:
145 """Reposition input"""
146 inputs = [np.ascontiguousarray(i) for i in inputs]
148 inputs, [buf._buffer for buf in input_bufs], seqlens
149 )
150
152 self,
153 output_bufs: List[Buffer],
154 outputs: List[np.ndarray],
155 seqlens: List[List[int]] = [],
156 ) -> None:
157 """Reposition output"""
158 if len(outputs) != len(self._output_shape):
159 outputs.clear()
160 for shape in self._output_shape:
161 outputs.append(np.empty(shape=shape, dtype=np.float32))
162 else:
163 for oi in range(len(outputs)):
164 outputs[oi] = np.ascontiguousarray(outputs[oi])
166 [buf._buffer for buf in output_bufs], outputs, seqlens
167 )
168
169
170
Handle to a specific variant of a loaded model.
List[BufferInfo] get_output_buffer_info(self)
Returns the output buffer information for this variant.
None release_buffer(self, List[Buffer] buffer)
Buffer Management API.
List[Buffer] acquire_output_buffer(self, List[List[int]] seqlens=[])
Buffer Management API.
List[_Shape] get_model_output_shape(self)
Returns the output shape for this model variant.
List[Buffer] acquire_input_buffer(self, List[List[int]] seqlens=[])
Buffer Management API.
List[Scale] get_input_scale(self)
Returns the input quantization scale(s) for this variant.
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.
int get_variant_idx(self)
Returns the index of this model variant.
List[Scale] get_output_scale(self)
Returns the output quantization scale(s) for this variant.
None reposition_outputs(self, List[Buffer] output_bufs, List[np.ndarray] outputs, List[List[int]] seqlens=[])
Reposition output.
List[_Shape] get_model_input_shape(self)
Returns the input shape for this model variant.
A simple byte-sized buffer.
Definition type.py:195