89 def __init__(self, path: str, model_config: Optional[ModelConfig] =
None):
90 if model_config
is None:
91 self.
_model = _cMaccel.Model(path)
93 self.
_model = _cMaccel.Model(path, model_config._model_config)
100 def launch(self, acc: Accelerator) ->
None:
101 self.
_model.launch(acc._accelerator)
104 def dispose(self) -> None:
108 def is_target(self, core_id: CoreId) -> bool:
109 return self.
_model.is_target(core_id._core_id)
111 def get_core_mode(self) -> CoreMode:
114 def get_target_cores(self) -> List[CoreId]:
115 return [CoreId.from_cpp(target)
for target
in self.
_model.target_cores]
119 def target_cores(self) -> List[CoreId]:
120 return [CoreId.from_cpp(target)
for target
in self.
_model.target_cores]
130 inputs: Union[np.ndarray, List[np.ndarray]],
131 outputs: Optional[List[np.ndarray]] =
None,
133 ) -> Optional[List[np.ndarray]]:
134 if not isinstance(inputs, list):
138 inputs = [np.ascontiguousarray(i)
for i
in inputs]
142 infer_func = self.
_model.infer
if is_hwc
else self.
_model.infer_chw
143 return [np.asarray(o)
for o
in infer_func(inputs, cache_size)]
147 _check_output_shapes(
150 for oi
in range(len(outputs)):
151 outputs[oi] = np.ascontiguousarray(outputs[oi])
153 outputs[:] = _build_outputs(self.
_output_shape, is_hwc, inputs[0].dtype)
156 self.
_model.infer(inputs, outputs, cache_size)
158 self.
_model.infer_chw(inputs, outputs, cache_size)
167 ) -> List[np.ndarray]:
168 if not isinstance(inputs, list):
172 inputs = [np.ascontiguousarray(i)
for i
in inputs]
175 outputs = self.
_model.infer_to_float(inputs, cache_size)
177 outputs = self.
_model.infer_chw_to_float(inputs, cache_size)
179 return [np.asarray(o)
for o
in outputs]
183 infer_chw_to_float = infer_to_float
187 inputs: List[Buffer],
188 outputs: List[Buffer],
189 shape: List[List[int]] = [],
193 [i._buffer
for i
in inputs], [o._buffer
for o
in outputs], shape, cache_size
196 def infer_speedrun(self) -> None:
197 self.
_model.infer_speedrun()
201 inputs: Union[np.ndarray, List[np.ndarray]],
203 if not isinstance(inputs, list):
206 inputs = [np.ascontiguousarray(i)
for i
in inputs]
208 self.
_model.infer_async
if is_hwc
else self.
_model.infer_async_chw
210 return Future.from_cpp(infer_async_func(inputs), inputs)
212 def reposition_inputs(
214 inputs: List[np.ndarray],
215 input_bufs: List[Buffer],
216 seqlens: List[List[int]] = [],
218 inputs = [np.ascontiguousarray(i)
for i
in inputs]
219 self.
_model.reposition_inputs(
220 inputs, [buf._buffer
for buf
in input_bufs], seqlens
223 def reposition_outputs(
225 output_bufs: List[Buffer],
226 outputs: List[np.ndarray],
227 seqlens: List[List[int]] = [],
232 outputs.append(np.empty(shape=shape, dtype=np.float32))
234 for oi
in range(len(outputs)):
235 outputs[oi] = np.ascontiguousarray(outputs[oi])
236 self.
_model.reposition_outputs(
237 [buf._buffer
for buf
in output_bufs], outputs, seqlens
240 def get_model_input_shape(self) -> List[_Shape]:
241 return self.
_model.get_model_input_shape()
243 def get_model_output_shape(self) -> List[_Shape]:
244 return self.
_model.get_model_output_shape()
246 def get_input_scale(self) -> List[Scale]:
247 return [Scale.from_cpp(s)
for s
in self.
_model.get_input_scale()]
249 def get_output_scale(self) -> List[Scale]:
250 return [Scale.from_cpp(s)
for s
in self.
_model.get_output_scale()]
252 def get_input_buffer_info(self) -> List[BufferInfo]:
253 return [BufferInfo.from_cpp(bi)
for bi
in self.
_model.get_input_buffer_info()]
255 def get_output_buffer_info(self) -> List[BufferInfo]:
256 return [BufferInfo.from_cpp(bi)
for bi
in self.
_model.get_output_buffer_info()]
258 def acquire_input_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
259 return [
Buffer(b)
for b
in self.
_model.acquire_input_buffer(seqlens)]
261 def acquire_output_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
262 return [
Buffer(b)
for b
in self.
_model.acquire_output_buffer(seqlens)]
264 def release_buffer(self, buffer: List[Buffer]) ->
None:
265 self.
_model.release_buffer([b._buffer
for b
in buffer])
267 def get_identifier(self) -> int:
268 return self.
_model.get_identifier()
270 def get_model_path(self) -> str:
271 return self.
_model.get_model_path()
273 def get_schedule_policy(self) -> SchedulePolicy:
276 def get_latency_set_policy(self) -> LatencySetPolicy:
279 def get_maintenance_policy(self) -> MaintenancePolicy:
282 def get_latency_consumed(self) -> int:
283 return self.
_model.get_latency_consumed()
285 def get_latency_finished(self) -> int:
286 return self.
_model.get_latency_finished()
288 def reset_cache_memory(self) -> None:
289 self.
_model.reset_cache_memory()
291 def dump_cache_memory(self) -> bytes:
292 buf = self.
_model.dump_cache_memory()
293 return np.asarray(buf, np.int8).tobytes()
295 def load_cache_memory(self, buf: bytes) ->
None:
296 self.
_model.load_cache_memory(np.frombuffer(buf, dtype=np.int8))
298 def dump_cache_memory_to(self, cache_path: str) ->
None:
299 self.
_model.dump_cache_memory(cache_path)
301 def load_cache_memory_from(self, cache_path: str) ->
None:
302 self.
_model.load_cache_memory(cache_path)
304 def filter_cache_tail(
305 self, cache_size: int, tail_size: int, mask: List[bool]
307 return self.
_model.filter_cache_tail(cache_size, tail_size, mask)
309 def move_cache_tail(self, num_head: int, num_tail: int, cache_size: int) -> int:
310 return self.
_model.move_cache_tail(num_head, num_tail, cache_size)