求助:所有算子都是运行在BPU上,模型也不是很复杂,但是实测出来的模型性能不到2hz

:white_check_mark: 确认:RDK X5 的 .bin 模型支持 Batch > 1

根据社区官方回复和实际案例,结论如下:

核心结论

特性 说明
支持 Batch :white_check_mark: 支持,需在量化配置中设置 input_batch
执行方式 并行推理(非串行)
延迟变化 总延迟 ≈ 单 batch 延迟 × batch 数量
FPS 变化 基本保持不变(单位时间处理图像数相同)
适用场景 批量离线处理、提高 BPU 利用率

:clipboard: 如何配置 Batch > 1

1. 量化配置文件修改

input_parameters:
  input_name: 'input'
  input_type_train: 'featuremap'
  input_layout_train: 'NCHW'
  input_type_rt: 'featuremap'
  input_layout_rt: 'NHWC'  # 建议用 NHWC
  input_shape: '1x5x360x640'
  # 【关键】设置 batch 大小
  input_batch: 4  # 支持 1, 2, 4, 8 等
  norm_type: 'no_preprocess'

2. 校准数据准备

校准数据数量需要匹配 batch:

# 如果 input_batch=4,校准数据应该是 4 的倍数
# 或者工具会自动重复使用数据

3. 应用层输入数据

# Batch=4 时,输入 shape 为 (4, 5, 360, 640)
# 注意布局转换为 NHWC: (4, 360, 640, 5)

import numpy as np

# 准备 4 张图片
batch_data = np.random.rand(4, 5, 360, 640).astype(np.float32)

# 转换为 NHWC
batch_nhwc = np.transpose(batch_data, (0, 2, 3, 1))

# 推理
outputs = model.forward(batch_nhwc)

:warning: 重要注意事项

1. 延迟 vs 吞吐量

从社区实测数据(topic/34102):

Batch 单次延迟 FPS 每张图平均延迟
1 26.5ms 37.75 26.5ms
4 108.5ms 36.85 27.1ms

结论

  • 总延迟增加 4 倍(因为一次处理 4 张图)
  • FPS 基本不变(单位时间处理能力相同)
  • 适合批量处理,不适合低延迟实时应用

2. 模型兼容性

某些算子可能不支持多 batch,量化时会报警告:

WARNING BPU only support Reducemean whose axes size == 1

需要检查模型结构是否兼容。

3. 内存占用

Batch 增大会线性增加 DDR 占用:

Batch=1: DDR ≈ 128MB
Batch=4: DDR ≈ 512MB

确保板端内存充足。


:bullseye: 您的场景建议

结合您之前的性能问题(286ms 延迟),不建议立即使用多 batch

优先优化单 batch 性能

  1. :white_check_mark: 先解决 Layout Convert 问题(148ms → <5ms)
  2. :white_check_mark: 再解决 CPU 算子问题(182ms → <20ms)
  3. :white_check_mark: 目标:单 batch 延迟从 286ms 降至 30-50ms

再考虑多 batch

  • 如果优化后单 batch 延迟 <50ms,但 BPU 利用率仍低
  • 且您的应用场景允许累积多帧一起处理
  • 可以尝试 input_batch: 24

:books: 相关社区资源

  1. X5 多 batch 推理耗时问题 - 官方解释延迟/FPS 关系
  2. Batch 模型推理 - 详细教程
  3. 多 batch 模型使用与部署 - 早期教程(仍有参考价值)

:wrench: 快速验证脚本

#!/usr/bin/env python3
from hobot_dnn import pyeasy_dnn as dnn
import time
import numpy as np

# 加载模型
models = dnn.load('lraspp_performance_2.bin')
model = models[0]

# 测试 Batch=1
print("=== Batch=1 ===")
input_1 = np.random.rand(1, 360, 640, 5).astype(np.float32)
for _ in range(10): _ = model.forward(input_1)  # 预热

times_1 = []
for _ in range(50):
    start = time.time()
    _ = model.forward(input_1)
    times_1.append((time.time() - start) * 1000)
print(f"平均延迟:{np.mean(times_1):.2f}ms | FPS: {1000/np.mean(times_1):.1f}")

# 测试 Batch=4
print("\n=== Batch=4 ===")
input_4 = np.random.rand(4, 360, 640, 5).astype(np.float32)
for _ in range(10): _ = model.forward(input_4)  # 预热

times_4 = []
for _ in range(50):
    start = time.time()
    _ = model.forward(input_4)
    times_4.append((time.time() - start) * 1000)
print(f"平均延迟:{np.mean(times_4):.2f}ms | FPS: {1000/np.mean(times_4):.1f}")
print(f"每张图平均:{np.mean(times_4)/4:.2f}ms")

总结:支持 Batch > 1,但请先优化单 batch 性能,再根据实际需求决定是否使用多 batch。