109 def __init__(self, path: str, model_config: Optional[ModelConfig] =
None):
110 if model_config
is None:
111 self.
_model = _cMaccel.Model(path)
113 self.
_model = _cMaccel.Model(path, model_config._model_config)
120 def launch(self, acc: Accelerator) ->
None:
121 self.
_model.launch(acc._accelerator)
124 def dispose(self) -> None:
128 def is_target(self, core_id: CoreId) -> bool:
129 return self.
_model.is_target(core_id._core_id)
131 def get_core_mode(self) -> CoreMode:
134 def get_target_cores(self) -> List[CoreId]:
135 return [CoreId.from_cpp(target)
for target
in self.
_model.target_cores]
139 def target_cores(self) -> List[CoreId]:
140 return [CoreId.from_cpp(target)
for target
in self.
_model.target_cores]
150 inputs: Union[np.ndarray, List[np.ndarray]],
151 outputs: Optional[List[np.ndarray]] =
None,
153 ) -> Optional[List[np.ndarray]]:
154 if not isinstance(inputs, list):
157 variant_idx, is_hwc = _find_matching_variant_idx_and_is_hwc(self, inputs)
158 inputs = [np.ascontiguousarray(i)
for i
in inputs]
162 infer_func = self.
_model.infer
if is_hwc
else self.
_model.infer_chw
163 return [np.asarray(o)
for o
in infer_func(inputs, cache_size)]
167 _check_output_shapes(
173 for oi
in range(len(outputs)):
174 outputs[oi] = np.ascontiguousarray(outputs[oi])
176 outputs[:] = _build_outputs(
183 self.
_model.infer(inputs, outputs, cache_size)
185 self.
_model.infer_chw(inputs, outputs, cache_size)
194 ) -> List[np.ndarray]:
195 if not isinstance(inputs, list):
198 _, is_hwc = _find_matching_variant_idx_and_is_hwc(self, inputs)
199 inputs = [np.ascontiguousarray(i)
for i
in inputs]
202 outputs = self.
_model.infer_to_float(inputs, cache_size)
204 outputs = self.
_model.infer_chw_to_float(inputs, cache_size)
206 return [np.asarray(o)
for o
in outputs]
210 infer_chw_to_float = infer_to_float
214 inputs: List[Buffer],
215 outputs: List[Buffer],
216 shape: List[List[int]] = [],
220 [i._buffer
for i
in inputs], [o._buffer
for o
in outputs], shape, cache_size
223 def infer_speedrun(self) -> None:
224 self.
_model.infer_speedrun()
228 inputs: Union[np.ndarray, List[np.ndarray]],
230 if not isinstance(inputs, list):
232 _, is_hwc = _find_matching_variant_idx_and_is_hwc(self, inputs)
233 inputs = [np.ascontiguousarray(i)
for i
in inputs]
235 self.
_model.infer_async
if is_hwc
else self.
_model.infer_async_chw
237 return Future.from_cpp(infer_async_func(inputs), inputs)
239 def reposition_inputs(
241 inputs: List[np.ndarray],
242 input_bufs: List[Buffer],
243 seqlens: List[List[int]] = [],
245 inputs = [np.ascontiguousarray(i)
for i
in inputs]
246 self.
_model.reposition_inputs(
247 inputs, [buf._buffer
for buf
in input_bufs], seqlens
250 def reposition_outputs(
252 output_bufs: List[Buffer],
253 outputs: List[np.ndarray],
254 seqlens: List[List[int]] = [],
259 outputs.append(np.empty(shape=shape, dtype=np.float32))
261 for oi
in range(len(outputs)):
262 outputs[oi] = np.ascontiguousarray(outputs[oi])
263 self.
_model.reposition_outputs(
264 [buf._buffer
for buf
in output_bufs], outputs, seqlens
267 def get_num_model_variants(self) -> int:
268 return self.
_model.get_num_model_variants()
270 def get_model_variant_handle(self, variant_idx) -> ModelVariantHandle:
271 return ModelVariantHandle.from_cpp(
272 self.
_model.get_model_variant_handle(variant_idx)
275 def get_model_input_shape(self) -> List[_Shape]:
276 return self.
_model.get_model_input_shape()
278 def get_model_output_shape(self) -> List[_Shape]:
279 return self.
_model.get_model_output_shape()
281 def get_input_scale(self) -> List[Scale]:
282 return [Scale.from_cpp(s)
for s
in self.
_model.get_input_scale()]
284 def get_output_scale(self) -> List[Scale]:
285 return [Scale.from_cpp(s)
for s
in self.
_model.get_output_scale()]
287 def get_input_buffer_info(self) -> List[BufferInfo]:
288 return [BufferInfo.from_cpp(bi)
for bi
in self.
_model.get_input_buffer_info()]
290 def get_output_buffer_info(self) -> List[BufferInfo]:
291 return [BufferInfo.from_cpp(bi)
for bi
in self.
_model.get_output_buffer_info()]
293 def acquire_input_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
294 return [
Buffer(b)
for b
in self.
_model.acquire_input_buffer(seqlens)]
296 def acquire_output_buffer(self, seqlens: List[List[int]] = []) -> List[Buffer]:
297 return [
Buffer(b)
for b
in self.
_model.acquire_output_buffer(seqlens)]
299 def release_buffer(self, buffer: List[Buffer]) ->
None:
300 self.
_model.release_buffer([b._buffer
for b
in buffer])
302 def get_identifier(self) -> int:
303 return self.
_model.get_identifier()
305 def get_model_path(self) -> str:
306 return self.
_model.get_model_path()
308 def get_cache_infos(self) -> List[CacheInfo]:
309 return [CacheInfo.from_cpp(c)
for c
in self.
_model.get_cache_infos()]
311 def get_schedule_policy(self) -> SchedulePolicy:
314 def get_latency_set_policy(self) -> LatencySetPolicy:
317 def get_maintenance_policy(self) -> MaintenancePolicy:
320 def get_latency_consumed(self) -> int:
321 return self.
_model.get_latency_consumed()
323 def get_latency_finished(self) -> int:
324 return self.
_model.get_latency_finished()
326 def reset_cache_memory(self) -> None:
327 self.
_model.reset_cache_memory()
329 def dump_cache_memory(self) -> List[bytes]:
330 bufs = self.
_model.dump_cache_memory()
331 return [np.asarray(buf, np.int8).tobytes()
for buf
in bufs]
333 def load_cache_memory(self, bufs: List[bytes]) ->
None:
334 self.
_model.load_cache_memory(
335 [np.frombuffer(buf, dtype=np.int8)
for buf
in bufs]
338 def dump_cache_memory_to(self, cache_dir: str) ->
None:
339 self.
_model.dump_cache_memory(cache_dir)
341 def load_cache_memory_from(self, cache_dir: str) ->
None:
342 self.
_model.load_cache_memory(cache_dir)
344 def filter_cache_tail(
345 self, cache_size: int, tail_size: int, mask: List[bool]
347 return self.
_model.filter_cache_tail(cache_size, tail_size, mask)
349 def move_cache_tail(self, num_head: int, num_tail: int, cache_size: int) -> int:
350 return self.
_model.move_cache_tail(num_head, num_tail, cache_size)