feat(actpred): HailoPredictor 异步推理接口——run_async 流水线 2.6× 吞吐

新增 predict_async()(4 缓冲池 + 完成回调 + _AsyncJob.result),与同步
predict 语义/输出完全一致(重构公共 _prepare_inputs/_fill_bindings/
_postprocess)。真实 HEF 实测:连续吞吐 6.0ms vs 同步 15.5ms;扩展测试
10 seed 随机/60 任务乱序对应/混合调用/缓冲压力/300 次稳定性/4 线程并发
全部 0 差异,190 单测 OK。
This commit is contained in:
qing committed 2026-08-17 21:46:26 +08:00
1 parent 1348660cd8
commit 8de7f6696c
2 files changed
+171 -29

No files matched your search

+41
View File
@@ -1279,6 +1279,13 @@ class _FakeConfigured:
for b in bindings_list:
self._transform(b)
def run_async(self, bindings_list, callback=None):
# 模拟异步:同步完成 transform 后触发回调(completion_info 无异常)
for b in bindings_list:
self._transform(b)
if callback is not None:
callback(completion_info=types.SimpleNamespace(exception=None))
class _FakeInferModel:
def __init__(self, transform):
@@ -1582,6 +1589,40 @@ class TestHailoPredictor(unittest.TestCase):
result = p.predict(self._frames(2), history)
np.testing.assert_allclose(result, [3, 3, 3], atol=1e-6)
def test_predict_async_matches_predict(self):
from hailo_infer import HailoPredictor
infos_in = [
_FakeVStreamInfo("net/images", (1, 2, 3, 4, 4), _FakeFormatType.FLOAT32),
_FakeVStreamInfo("net/history", (1, 2, 3), _FakeFormatType.FLOAT32),
]
infos_out = [_FakeVStreamInfo("net/action", (1, 3), _FakeFormatType.FLOAT32)]
_install_fake_hailo(self, infos_in, infos_out,
_first_row_transform("net/history", "net/action"))
history = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
with HailoPredictor("fake.hef") as p:
sync = p.predict(self._frames(2), history)
job = p.predict_async(self._frames(2), history)
async_result = job.result(timeout=5.0)
np.testing.assert_allclose(async_result, sync, atol=1e-6)
def test_predict_async_buffer_pool_roundtrip(self):
"""连续提交超过缓冲池数量的任务,输出互不串扰"""
from hailo_infer import HailoPredictor
infos_in = [
_FakeVStreamInfo("net/images", (1, 2, 3, 4, 4), _FakeFormatType.FLOAT32),
_FakeVStreamInfo("net/history", (1, 2, 3), _FakeFormatType.FLOAT32),
]
infos_out = [_FakeVStreamInfo("net/action", (1, 3), _FakeFormatType.FLOAT32)]
_install_fake_hailo(self, infos_in, infos_out,
_first_row_transform("net/history", "net/action"))
with HailoPredictor("fake.hef") as p:
jobs = []
for _ in range(6): # 超过 _ASYNC_BUFFERS=4
jobs.append(p.predict_async(self._frames(2), [[1, 2, 3]] * 2))
for job in jobs:
np.testing.assert_allclose(job.result(timeout=5.0), [1, 2, 3],
atol=1e-6)
class TestDfcCompile(unittest.TestCase):
"""dfc_compile.py 的 HAR 输入层连通性检查(编译前拦截输入断连)"""