|
|
@@ -3,9 +3,13 @@
|
|
|
部署要点
|
|
|
--------
|
|
|
* 转换脚本:``scripts/onnx_to_rknn.py``(INT8 量化需要量化校准图集 txt);
|
|
|
-* **NPU core 数随 SoC 不同**:RK3588/RK3588S 是 3 核,RK3576/RK3568/RK3566/RK3562 只有
|
|
|
- 1 核。``model.rknn_core`` 若配成多核组合(``0_1`` / ``0_1_2`` 等),在单核板子上
|
|
|
- ``init_runtime`` 会失败;:func:`resolve_core_mask` 会按板型**收敛为 auto 并告警**,
|
|
|
+* **NPU core 数随 SoC 不同**:RK3588/RK3588S 是 3 核,**RK3576 是 2 核**,RK3568/RK3566/
|
|
|
+ RK3562 只有 1 核。RK3576 的双核有两条独立证据:内核 ``rknpu_drv.c`` 里
|
|
|
+ ``rk3576_npu_irqs[]`` 注册了 ``npu0_irq`` + ``npu1_irq``(RK3588 是三个),板端
|
|
|
+ ``/sys/kernel/debug/rknpu/load`` 也打印 ``Core0``/``Core1``。因此运行时**先从 debugfs
|
|
|
+ 实测**核数(:func:`detect_npu_cores_live`),读不到才退回按板型查表;
|
|
|
+ ``model.rknn_core`` 配成超过实际核数的组合(``0_1_2`` / ``0_2`` 等)时 ``init_runtime``
|
|
|
+ 会失败,:func:`resolve_core_mask` 按实测核数**收敛为 auto 并告警**,
|
|
|
而不是让现场看到一个莫名其妙的初始化错误;
|
|
|
* INT8 模型输入一般是 **NHWC uint8**,因此 ``model.input_format`` 用 ``rgb_uint8``;
|
|
|
若在转换时用了 ``mean_values``/``std_values`` 归一化,就不要再手工 /255;
|
|
|
@@ -40,6 +44,7 @@ __all__ = [
|
|
|
"RknnDetector",
|
|
|
"detect_board",
|
|
|
"detect_npu_cores",
|
|
|
+ "detect_npu_cores_live",
|
|
|
"model_chain_ratio",
|
|
|
"parse_npu_load",
|
|
|
"read_npu_load",
|
|
|
@@ -58,11 +63,14 @@ _CORE_MASKS = {
|
|
|
"0_1_2": 7,
|
|
|
}
|
|
|
|
|
|
-#: 各 SoC 的 NPU core 数(RKNPU2)。RK3576(本项目板卡 WXD-A576 / DC-A576)只有 1 核。
|
|
|
+#: 各 SoC 的 NPU core 数(RKNPU2 驱动 ``num_irqs``)。**仅作 fallback**:优先用
|
|
|
+#: :func:`detect_npu_cores_live` 从 debugfs 实测,因为同一颗 SoC 的不同 BSP 内核可能只开了
|
|
|
+#: 部分 core。RK3576(本项目板卡 WXD-A576 / DC-A576)= 2 核:``rk3576_npu_irqs[]`` 是
|
|
|
+#: ``npu0_irq`` + ``npu1_irq``,板端 ``load`` 也打印 ``Core0``/``Core1``。
|
|
|
_NPU_CORES = {
|
|
|
"rk3588": 3,
|
|
|
"rk3588s": 3,
|
|
|
- "rk3576": 1,
|
|
|
+ "rk3576": 2,
|
|
|
"rk3568": 1,
|
|
|
"rk3566": 1,
|
|
|
"rk3562": 1,
|
|
|
@@ -76,6 +84,8 @@ _NPU_LOAD_PATHS = (
|
|
|
|
|
|
_PERCENT_RE = re.compile(r"(\d+)\s*%")
|
|
|
_DEVFREQ_RE = re.compile(r"load\s*=\s*(\d+)")
|
|
|
+#: debugfs ``load`` 里的 core 下标:``NPU load: Core0: 37%, Core1: 0%,``
|
|
|
+_CORE_INDEX_RE = re.compile(r"core\s*(\d+)\s*:", re.IGNORECASE)
|
|
|
|
|
|
|
|
|
def detect_board() -> str:
|
|
|
@@ -103,11 +113,36 @@ def detect_npu_cores(board: Optional[str] = None) -> int:
|
|
|
return 0
|
|
|
|
|
|
|
|
|
+def detect_npu_cores_live(paths: Optional[Sequence[str]] = None) -> int:
|
|
|
+ """从 RKNPU 驱动的 debugfs 节点**实测** NPU core 数;读不到返回 ``0``。
|
|
|
+
|
|
|
+ 比按 SoC 名查表可靠:厂商 BSP 内核可能只启用部分 core,或同一 SoC 有不同 SKU;
|
|
|
+ RK3576 实测输出 ``NPU load: Core0: 0%, Core1: 0%,`` → 2。
|
|
|
+ 节点不存在(非 RK 板 / 没挂 debugfs)时返回 ``0``,由调用方退回查表。
|
|
|
+ """
|
|
|
+
|
|
|
+ for path in (paths if paths is not None else _NPU_LOAD_PATHS):
|
|
|
+ if not path:
|
|
|
+ continue
|
|
|
+ try:
|
|
|
+ with open(path, "r", encoding="utf-8", errors="ignore") as fh:
|
|
|
+ text = fh.read(512)
|
|
|
+ except OSError:
|
|
|
+ continue
|
|
|
+ indexes = {int(match.group(1)) for match in _CORE_INDEX_RE.finditer(text)}
|
|
|
+ if indexes:
|
|
|
+ return len(indexes)
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
def resolve_core_mask(core: str, npu_cores: int = 0) -> Tuple[int, str]:
|
|
|
"""把 ``model.rknn_core`` 收敛成 rknnlite 的 ``core_mask``。
|
|
|
|
|
|
返回 ``(mask, note)``;``note`` 非空表示配置被收敛过(调用方应打日志)。
|
|
|
- ``npu_cores <= 0``(板型未知)时不做收敛,尊重用户配置。
|
|
|
+ ``npu_cores <= 0``(核数未知)时不做收敛,尊重用户配置。
|
|
|
+
|
|
|
+ 判据是**核下标**而不是核个数:2 核板上 ``0_2``(要 core0 + core2)个数没超标,
|
|
|
+ 但 core2 根本不存在,``init_runtime`` 一样会失败。
|
|
|
"""
|
|
|
|
|
|
key = str(core).strip().lower()
|
|
|
@@ -116,11 +151,14 @@ def resolve_core_mask(core: str, npu_cores: int = 0) -> Tuple[int, str]:
|
|
|
"model.rknn_core 不支持: %r(可选 %s)" % (core, " / ".join(sorted(_CORE_MASKS)))
|
|
|
)
|
|
|
mask = _CORE_MASKS[key]
|
|
|
- wanted = [bit for bit in (1, 2, 4) if mask & bit]
|
|
|
- if npu_cores > 0 and len(wanted) > npu_cores:
|
|
|
+ indexes = [idx for idx, bit in ((0, 1), (1, 2), (2, 4)) if mask & bit]
|
|
|
+ if not indexes or npu_cores <= 0:
|
|
|
+ return mask, ""
|
|
|
+ if max(indexes) >= npu_cores:
|
|
|
return 0, (
|
|
|
- "model.rknn_core=%s 需要 %d 个 NPU core,但当前板卡只有 %d 个"
|
|
|
- "(RK3576 / RK3568 等单核 NPU)→ 已收敛为 auto" % (key, len(wanted), npu_cores)
|
|
|
+ "model.rknn_core=%s 需要 core %s,但当前板卡只有 %d 个 NPU core"
|
|
|
+ "(板端实测;RK3568/RK3566/RK3562 单核、RK3576 双核、RK3588 三核)→ 已收敛为 auto"
|
|
|
+ % (key, "/".join(str(idx) for idx in indexes), npu_cores)
|
|
|
)
|
|
|
return mask, ""
|
|
|
|
|
|
@@ -212,7 +250,15 @@ class RknnDetector(Detector):
|
|
|
) from exc
|
|
|
logger = get_logger("rknn")
|
|
|
self.board = detect_board()
|
|
|
- self.npu_cores = detect_npu_cores(self.board)
|
|
|
+ live_cores = detect_npu_cores_live()
|
|
|
+ self.npu_cores = live_cores or detect_npu_cores(self.board)
|
|
|
+ if self.npu_cores:
|
|
|
+ logger.info(
|
|
|
+ "NPU 板型=%s core 数=%d(来源=%s)",
|
|
|
+ self.board or "unknown",
|
|
|
+ self.npu_cores,
|
|
|
+ "debugfs 实测" if live_cores else "SoC 查表",
|
|
|
+ )
|
|
|
mask, note = resolve_core_mask(self.core, self.npu_cores)
|
|
|
if note:
|
|
|
logger.warning(note)
|
|
|
@@ -220,9 +266,24 @@ class RknnDetector(Detector):
|
|
|
if rknn.load_rknn(self.model_path) != 0:
|
|
|
raise RuntimeError("load_rknn 失败: %s" % self.model_path)
|
|
|
if rknn.init_runtime(core_mask=mask) != 0:
|
|
|
- raise RuntimeError(
|
|
|
- "init_runtime 失败(core=%s, board=%s)" % (self.core, self.board or "unknown")
|
|
|
+ if mask == 0:
|
|
|
+ raise RuntimeError(
|
|
|
+ "init_runtime 失败(core=%s, board=%s)" % (self.core, self.board or "unknown")
|
|
|
+ )
|
|
|
+ # 多核掩码被驱动拒(BSP 只启用了部分 core,或该版本 lite2 不支持组合核):
|
|
|
+ # 退回 auto 再试一次——一个「性能选项」不该让整条产线起不来
|
|
|
+ logger.warning(
|
|
|
+ "init_runtime(core_mask=%d) 失败(core=%s, 实测 core 数=%d)→ 退回 auto 重试",
|
|
|
+ mask,
|
|
|
+ self.core,
|
|
|
+ self.npu_cores,
|
|
|
)
|
|
|
+ mask = 0
|
|
|
+ if rknn.init_runtime(core_mask=mask) != 0:
|
|
|
+ raise RuntimeError(
|
|
|
+ "init_runtime 失败(core=%s, board=%s,auto 也失败)"
|
|
|
+ % (self.core, self.board or "unknown")
|
|
|
+ )
|
|
|
self.core_mask = mask
|
|
|
self.rknn = rknn
|
|
|
if self.input_format.lower() == "rgb_uint8":
|