type.py Source File

type.py Source File#

SDK qb Runtime Library: type.py Source File
SDK qb Runtime Library v0.28
MCS001-EN
type.py
Go to the documentation of this file.
1
4
5from typing import List, Optional, Tuple
6from enum import Enum
7
8import numpy as np
9
10import maccel.maccel as _cMaccel
11
12
15
16class Cluster(Enum):
17 Cluster0 = _cMaccel.Cluster.Cluster0
18 Cluster1 = _cMaccel.Cluster.Cluster1
19 Error = _cMaccel.Cluster.Error
20
21
22class Core(Enum):
23 Core0 = _cMaccel.Core.Core0
24 Core1 = _cMaccel.Core.Core1
25 Core2 = _cMaccel.Core.Core2
26 Core3 = _cMaccel.Core.Core3
27 All = _cMaccel.Core.All
28 GlobalCore = _cMaccel.Core.GlobalCore
29 Error = _cMaccel.Core.Error
30
31
33 Auto = _cMaccel.CoreAllocationPolicy.Auto
34 Manual = _cMaccel.CoreAllocationPolicy.Manual
35
36
37class LatencySetPolicy(Enum):
38 Auto = _cMaccel.LatencySetPolicy.Auto
39 Manual = _cMaccel.LatencySetPolicy.Manual
40
41
43 Maintain = _cMaccel.MaintenancePolicy.Maintain
44 DropExpired = _cMaccel.MaintenancePolicy.DropExpired
45 Undefined = _cMaccel.MaintenancePolicy.Undefined
46
47
48class SchedulePolicy(Enum):
49 FIFO = _cMaccel.SchedulePolicy.FIFO
50 LIFO = _cMaccel.SchedulePolicy.LIFO
51 ByPriority = _cMaccel.SchedulePolicy.ByPriority
52 Undefined = _cMaccel.SchedulePolicy.Undefined
53
54
55class Scale:
56 def __init__(self, scale: float, is_uniform: bool, scale_list: List[float]):
57 self._scale = _cMaccel.Scale()
58 self._scale.scale = scale
59 self._scale.is_uniform = is_uniform
60 self._scale.scale_list = scale_list
61
62 @classmethod
63 def from_cpp(cls, _scale: _cMaccel.Scale):
64 return cls(_scale.scale, _scale.is_uniform, _scale.scale_list)
65
66 @property
67 def scale_list(self) -> List[float]:
68 return self._scale.scale_list
69
70 @property
71 def scale(self) -> float:
72 return self._scale.scale
73
74 @property
75 def is_uniform(self) -> bool:
76 return self._scale.is_uniform
77
78 @scale_list.setter
79 def scale_list(self, value: List[float]):
80 self._scale.scale_list = value
81
82 @scale.setter
83 def scale(self, value: float):
84 self._scale.scale = value
85
86 @is_uniform.setter
87 def is_uniform(self, value: bool):
88 self._scale.is_uniform = value
89
90 def __getitem__(self, i: int) -> float:
91 return self._scale[i]
92
93 def __repr__(self):
94 d = {
95 "scale": self.scale,
96 "is_uniform": self.is_uniform,
97 "scale_list": self.scale_list,
98 }
99 return "{}({})".format(
100 self.__class__.__name__,
101 ", ".join("{}={}".format(k, v) for k, v in d.items()),
102 )
103
104
105class CoreId:
106 def __init__(self, cluster: Cluster, core: Core):
107 self._core_id = _cMaccel.CoreId()
108 self._core_id.cluster = cluster.value
109 self._core_id.core = core.value
110
111 @classmethod
112 def from_cpp(cls, _core_id: _cMaccel.CoreId):
113 return cls(Cluster(_core_id.cluster), Core(_core_id.core))
114
115 @property
116 def cluster(self) -> Cluster:
117 return Cluster(self._core_id.cluster)
118
119 @property
120 def core(self) -> Core:
121 return Core(self._core_id.core)
122
123 @cluster.setter
124 def cluster(self, value: Cluster):
125 self._core_id.cluster = value.value
126
127 @core.setter
128 def core(self, value: Core):
129 self._core_id.core = value.value
130
131 def __eq__(self, other) -> bool:
132 return self._core_id == other._core_id
133
134 def __lt__(self, other) -> bool:
135 return self._core_id < other._core_id
136
137 def __repr__(self):
138 d = {"cluster": self.cluster, "core": self.core}
139 return "{}({})".format(
140 self.__class__.__name__,
141 ", ".join("{}={}".format(k, v) for k, v in d.items()),
142 )
143
144
145class Buffer:
146 def __init__(self, _buffer: Optional[_cMaccel.Buffer] = None):
147 self._buffer = _cMaccel.Buffer() if _buffer is None else _buffer
148
149 @property
150 def size(self) -> int:
151 return self._buffer.size
152
153 @size.setter
154 def size(self, value: int):
155 self._buffer.size = value
156
157 def set_buffer(self, arr: np.ndarray):
158 self._buffer.set_buffer(np.ascontiguousarray(arr))
159
160 def __repr__(self):
161 return f"{self.__class__.__name__}(size={self._buffer.size})"
162
163
164class CoreMode(Enum):
165 Single = _cMaccel.CoreMode.Single
166 Multi = _cMaccel.CoreMode.Multi
167 Global = _cMaccel.CoreMode.Global
168 Global4 = _cMaccel.CoreMode.Global4
169 Global8 = _cMaccel.CoreMode.Global8
170 Error = _cMaccel.CoreMode.Error
171
172
174 def __init__(
175 self,
176 original_height: int = 0,
177 original_width: int = 0,
178 original_channel: int = 0,
179 reshaped_height: int = 0,
180 reshaped_width: int = 0,
181 reshaped_channel: int = 0,
182 height: int = 0,
183 width: int = 0,
184 channel: int = 0,
185 max_height: int = 0,
186 max_width: int = 0,
187 max_channel: int = 0,
188 max_cache_size: int = 0,
189 ):
190 self._buffer_info = _cMaccel.BufferInfo()
191 self._buffer_info.original_height = original_height
192 self._buffer_info.original_width = original_width
193 self._buffer_info.original_channel = original_channel
194 self._buffer_info.reshaped_height = reshaped_height
195 self._buffer_info.reshaped_width = reshaped_width
196 self._buffer_info.reshaped_channel = reshaped_channel
197 self._buffer_info.height = height
198 self._buffer_info.width = width
199 self._buffer_info.channel = channel
200 self._buffer_info.max_height = max_height
201 self._buffer_info.max_width = max_width
202 self._buffer_info.max_channel = max_channel
203 self._buffer_info.max_cache_size = max_cache_size
204
205 @classmethod
206 def from_cpp(cls, _buffer_info: _cMaccel.BufferInfo):
207 return cls(
208 _buffer_info.original_height,
209 _buffer_info.original_width,
210 _buffer_info.original_channel,
211 _buffer_info.reshaped_height,
212 _buffer_info.reshaped_width,
213 _buffer_info.reshaped_channel,
214 _buffer_info.height,
215 _buffer_info.width,
216 _buffer_info.channel,
217 _buffer_info.max_height,
218 _buffer_info.max_width,
219 _buffer_info.max_channel,
220 _buffer_info.max_cache_size,
221 )
222
223 @property
224 def original_height(self) -> int:
225 return self._buffer_info.original_height
226
227 @property
228 def original_width(self) -> int:
229 return self._buffer_info.original_width
230
231 @property
232 def original_channel(self) -> int:
233 return self._buffer_info.original_channel
234
235 @property
236 def reshaped_height(self) -> int:
237 return self._buffer_info.reshaped_height
238
239 @property
240 def reshaped_width(self) -> int:
241 return self._buffer_info.reshaped_width
242
243 @property
244 def reshaped_channel(self) -> int:
245 return self._buffer_info.reshaped_channel
246
247 @property
248 def height(self) -> int:
249 return self._buffer_info.height
250
251 @property
252 def width(self) -> int:
253 return self._buffer_info.width
254
255 @property
256 def channel(self) -> int:
257 return self._buffer_info.channel
258
259 @property
260 def max_height(self) -> int:
261 return self._buffer_info.max_height
262
263 @property
264 def max_width(self) -> int:
265 return self._buffer_info.max_width
266
267 @property
268 def max_channel(self) -> int:
269 return self._buffer_info.max_channel
270
271 @property
272 def max_cache_size(self) -> int:
273 return self._buffer_info.max_cache_size
274
275 @original_height.setter
276 def original_height(self, value: int):
277 self._buffer_info.original_height = value
278
279 @original_width.setter
280 def original_width(self, value: int):
281 self._buffer_info.original_width = value
282
283 @original_channel.setter
284 def original_channel(self, value: int):
285 self._buffer_info.original_channel = value
286
287 @reshaped_height.setter
288 def reshaped_height(self, value: int):
289 self._buffer_info.reshaped_height = value
290
291 @reshaped_width.setter
292 def reshaped_width(self, value: int):
293 self._buffer_info.reshaped_width = value
294
295 @reshaped_channel.setter
296 def reshaped_channel(self, value: int):
297 self._buffer_info.reshaped_channel = value
298
299 @height.setter
300 def height(self, value: int):
301 self._buffer_info.height = value
302
303 @width.setter
304 def width(self, value: int):
305 self._buffer_info.width = value
306
307 @channel.setter
308 def channel(self, value: int):
309 self._buffer_info.channel = value
310
311 @max_height.setter
312 def max_height(self, value: int):
313 self._buffer_info.max_height = value
314
315 @max_width.setter
316 def max_width(self, value: int):
317 self._buffer_info.max_width = value
318
319 @max_channel.setter
320 def max_channel(self, value: int):
321 self._buffer_info.max_channel = value
322
323 @max_cache_size.setter
324 def max_cache_size(self, value: int):
325 self._buffer_info.max_cache_size = value
326
327 def original_size(self) -> int:
328 return self._buffer_info.original_size()
329
330 def reshaped_size(self) -> int:
331 return self._buffer_info.reshaped_size()
332
333 def size(self) -> int:
334 return self._buffer_info.size()
335
336 def original_shape(self) -> Tuple[int, int, int]:
337 return self._buffer_info.original_shape()
338
339 def original_shape_chw(self) -> Tuple[int, int, int]:
340 return self._buffer_info.original_shape_chw()
341
342 def reshaped_shape(self) -> Tuple[int, int, int]:
343 return self._buffer_info.reshaped_shape()
344
345 def reshaped_shape_chw(self) -> Tuple[int, int, int]:
346 return self._buffer_info.reshaped_shape_chw()
347
348 def shape(self) -> Tuple[int, int, int]:
349 return self._buffer_info.shape()
350
351 def shape_chw(self) -> Tuple[int, int, int]:
352 return self._buffer_info.shape_chw()
353
354 def __repr__(self):
355 d = {
356 "original_height": self._buffer_info.original_height,
357 "original_width": self._buffer_info.original_width,
358 "original_channel": self._buffer_info.original_channel,
359 "reshaped_height": self._buffer_info.reshaped_height,
360 "reshaped_width": self._buffer_info.reshaped_width,
361 "reshaped_channel": self._buffer_info.reshaped_channel,
362 "height": self._buffer_info.height,
363 "width": self._buffer_info.width,
364 "channel": self._buffer_info.channel,
365 "max_height": self._buffer_info.max_height,
366 "max_width": self._buffer_info.max_width,
367 "max_channel": self._buffer_info.max_channel,
368 "max_cache_size": self._buffer_info.max_cache_size,
369 }
370 return "{}({})".format(
371 self.__class__.__name__,
372 ", ".join("{}={}".format(k, v) for k, v in d.items()),
373 )
374
375
377 def __init__(self, num_cores: Optional[int] = None):
378 self._model_config = (
379 _cMaccel.ModelConfig()
380 if num_cores is None
381 else _cMaccel.ModelConfig(num_cores)
382 )
383
384 def include_all_cores(self) -> bool:
385 return self._model_config.include_all_cores()
386
387 def exclude_all_cores(self) -> bool:
388 return self._model_config.exclude_all_cores()
389
390 def include(self, cluster: Cluster, core: Optional[Core] = None) -> bool:
391 if core is None:
392 return self._model_config.include(cluster.value)
393 else:
394 return self._model_config.include(cluster.value, core.value)
395
396 def exclude(self, cluster: Cluster, core: Optional[Core] = None) -> bool:
397 if core is None:
398 return self._model_config.exclude(cluster.value)
399 else:
400 return self._model_config.exclude(cluster.value, core.value)
401
402 def set_single_core_mode(
403 self, num_cores: Optional[int] = None, core_ids: Optional[List[CoreId]] = None
404 ) -> bool:
405 if num_cores is not None and core_ids is None:
406 return self._model_config.set_single_core_mode(num_cores)
407 elif core_ids is not None and num_cores is None:
408 return self._model_config.set_single_core_mode(
409 [core_id._core_id for core_id in core_ids]
410 )
411 raise ValueError(
412 "`set_single_core_mode` needs either `num_cores` and `core_ids`."
413 )
414
415 def set_global_core_mode(self, clusters: List[Cluster]) -> bool:
416 return self._model_config.set_global_core_mode([c.value for c in clusters])
417
418 def set_global4_core_mode(self, clusters: List[Cluster]) -> bool:
419 return self._model_config.set_global4_core_mode([c.value for c in clusters])
420
421 def set_global8_core_mode(self) -> bool:
422 return self._model_config.set_global8_core_mode()
423
424 def get_core_mode(self) -> CoreMode:
425 return CoreMode(self._model_config.get_core_mode())
426
427 def set_multi_core_mode(self, clusters: List[Cluster]) -> bool:
428 return self._model_config.set_multi_core_mode([c.value for c in clusters])
429
430 def set_auto_mode(self, num_cores: int = 1) -> bool:
431 return self._model_config.set_auto_mode(num_cores)
432
433 def set_manual_mode(self) -> bool:
434 return self._model_config.set_manual_mode()
435
436 def get_core_allocation_policy(self) -> CoreAllocationPolicy:
437 return CoreAllocationPolicy(self._model_config.get_core_allocation_policy())
438
439 def get_num_cores(self) -> int:
440 return self._model_config.get_num_cores()
441
442 def force_single_npu_bundle(self, npu_bundle_index: int) -> bool:
443 return self._model_config.force_single_npu_bundle(npu_bundle_index)
444
445 def get_forced_npu_bundle_index(self) -> bool:
446 return self._model_config.get_forced_npu_bundle_index()
447
448 def set_async_pipeline_enabled(self, enable: bool):
449 return self._model_config.set_async_pipeline_enabled(enable)
450
451 def get_async_pipeline_enabled(self) -> bool:
452 return self._model_config.get_async_pipeline_enabled()
453
454 @property
455 def schedule_policy(self) -> SchedulePolicy:
456 return SchedulePolicy(self._model_config.schedule_policy)
457
458 @property
459 def latency_set_policy(self) -> LatencySetPolicy:
460 return LatencySetPolicy(self._model_config.latency_set_policy)
461
462 @property
463 def maintenance_policy(self) -> MaintenancePolicy:
464 return MaintenancePolicy(self._model_config.maintenance_policy)
465
466 @property
467 def early_latencies(self) -> List[int]:
468 return self._model_config.early_latencies
469
470 @property
471 def finish_latencies(self) -> List[int]:
472 return self._model_config.finish_latencies
473
474 @schedule_policy.setter
475 def schedule_policy(self, policy: SchedulePolicy):
476 self._model_config.schedule_policy = policy.value
477
478 @latency_set_policy.setter
479 def latency_set_policy(self, policy: LatencySetPolicy):
480 self._model_config.latency_set_policy = policy.value
481
482 @maintenance_policy.setter
483 def maintenance_policy(self, policy: MaintenancePolicy):
484 self._model_config.maintenance_policy = policy.value
485
486 @early_latencies.setter
487 def early_latencies(self, latencies: List[int]):
488 self._model_config.early_latencies = latencies
489
490 @finish_latencies.setter
491 def finish_latencies(self, latencies: List[int]):
492 self._model_config.finish_latencies = latencies
493
494 def get_core_ids(self) -> List[CoreId]:
495 return [
496 CoreId(Cluster(core_id.cluster), Core(core_id.core))
497 for core_id in self._model_config.core_ids
498 ]
499
500 def __repr__(self):
501 d = {
502 "core_mode": self.get_core_mode(),
503 "core_allocation_policy": self.get_core_allocation_policy(),
504 "core_ids": self.get_core_ids(),
505 "num_cores": self.get_num_cores(),
506 "forced_npu_bundle_index": self.get_forced_npu_bundle_index(),
507 }
508 return "{}({})".format(
509 self.__class__.__name__,
510 ", ".join("{}={}".format(k, v) for k, v in d.items()),
511 )
512
513
514class LogLevel(Enum):
515 DEBUG = _cMaccel.LogLevel.DEBUG
516 INFO = _cMaccel.LogLevel.INFO
517 WARN = _cMaccel.LogLevel.WARN
518 ERR = _cMaccel.LogLevel.ERR
519 FATAL = _cMaccel.LogLevel.FATAL
520 OFF = _cMaccel.LogLevel.OFF
521
522
523def set_log_level(level: LogLevel):
524 _cMaccel.set_log_level(level.value)
525
526
527def start_tracing_events(path: str) -> bool:
528 return _cMaccel.start_tracing_events(path)
529
530
531def stop_tracing_events():
532 _cMaccel.stop_tracing_events()
533
534
Core core(self)
Definition type.py:120
Cluster cluster(self)
Definition type.py:116
int get_num_cores(self)
Definition type.py:439
CoreAllocationPolicy get_core_allocation_policy(self)
Definition type.py:436
bool get_forced_npu_bundle_index(self)
Definition type.py:445
List[CoreId] get_core_ids(self)
Definition type.py:494
CoreMode get_core_mode(self)
Definition type.py:424
bool is_uniform(self)
Definition type.py:75
List[float] scale_list(self)
Definition type.py:67
float scale(self)
Definition type.py:71