T_cmd=0 仲裁改为策略优先 MergeBatch: K截断即优先(指令/命令次数省b0倍,未量化收益), L1绑定恒劣; 路由层以策略覆盖时延模型差额并显式标注

This commit is contained in:
2026-09-04 10:47:02 +08:00
parent a071d87831
commit 5051e00fa0
3 changed files with 59 additions and 26 deletions

View File

@@ -370,25 +370,30 @@ class TestTransposeModeling(unittest.TestCase):
class TestZeroCmdHandling(unittest.TestCase):
"""T_cmd=0 (无 DMA 命令固定开销) 时整链路不得除零/崩溃, 建模语义正确."""
"""T_cmd=0 (无命令时延/未标定) 时整链路不得除零/崩溃, 按策略优先 MergeBatch."""
def test_beats_iterbatch_no_division_error(self):
# T_cmd=0 时阈值 b0*(T_comp+T_write)/T_cmd -> +inf, MergeBatch 必须判不胜出
def test_beats_iterbatch_policy(self):
# T_cmd<=0: 阈值 +inf 不可除零; K截断按策略判 MergeBatch 胜 (指令级收益未量化),
# L1 绑定仍恒劣
from bmm_theory.hardware import NpuSpec
from bmm_theory.branches.merge_batch import MergeBatchBranch
spec0 = NpuSpec(t_cmd_ns=0.0)
mb = MergeBatchBranch(spec0)
for b, m, n, k in [(2048, 32, 32, 256), (128, 64, 64, 512), (256, 128, 128, 4096)]:
# (b, m, n, k, K截断与否)
cases = [(2048, 32, 32, 256, True), (128, 64, 64, 512, True),
(256, 128, 128, 4096, False)]
for b, m, n, k, truncated in cases:
win, detail = mb.beats_iterbatch(mkcase(b, m, n, k))
self.assertFalse(win, f"T_cmd=0 时 MergeBatch 不应胜出: {detail}")
self.assertEqual(win, truncated, f"{b},{m},{n},{k}: {detail}")
self.assertIn("T_cmd=0", detail)
def test_route_with_zero_cmd_no_crash(self):
# 整条路由链 (含仲裁) 在 T_cmd=0 下无异常, 时延有限且 MergeBatch 让位 IterBatch
def test_route_with_zero_cmd_prefers_merge(self):
# T_cmd=0 且 K截断时路由应优先 MergeBatch (命令/指令次数少 b0 倍, 结构性收益)
from bmm_theory.hardware import NpuSpec
router = BranchRouter(NpuSpec(t_cmd_ns=0.0))
r = router.route(mkcase(2048, 32, 32, 256)) # 原 MergeBatch 典型胜场形状
self.assertEqual(r["branch"], "IterBatch") # 命令节省消失 -> 无合并价值
r = router.route(mkcase(2048, 32, 32, 256)) # 大 B 小 MN 典型合并场景
self.assertEqual(r["branch"], "MergeBatch")
self.assertIn("策略", r["arbitration"])
self.assertIsNotNone(r["timing"])
shapes = [(128, 64, 64, 512), (64, 64, 64, 8192), (512, 128, 128, 128),
(128, 128, 128, 1024), (32, 4096, 4096, 4096)]