- constraints.py: ASW_Basic/ASW_Basic_降核 的 dValue 效率下限不再计违规 (DMA 仍能工作只是效率低; 真正不可行的只有容量/核数硬约束); IterBatch/MergeBatch/StreamK 等有替代分支的分支仍按违规处理 (不满足条件不该进) - asw_basic 枚举尾部: 严格 256B 偏好无解 -> 放开约束4 按 128B 硬下限给最优可行 tile, note 标注"效率降级"; 128B 硬下限也不满足的极端形状 (如 N=8 int8, B 侧 dValue=8B 物理不可满足) 仍给 Base tile 方案 + 标注效率降级 (搬移效率崩塌) - evaluator advice / router 仲裁文案含"效率降级"提示 (plan.note 同步) - docs/06 Step1 增加"兜底分支恒出方案"段落 (效率降级 vs 违规的语义分层) - 回归: b32_m16_n8192_k7168 分解 = Base 16x1024 + tile 16x1024 + k_l1=112 (L1 双缓冲 ⌊L1/(2·(16+1024)·2)⌋16=112 反推) 入测试; 极端形状 feasible=True + 效率降级标注; 压力 10000 例 0 崩溃/0 NaN/0 硬违规/0 GM<V_in; examples 重生成 0 diff
96 lines
4.6 KiB
Python
96 lines
4.6 KiB
Python
"""方案评估器: 对 (case, plan) 做硬件约束校验 + 时延评估 + 瓶颈分析.
|
|
|
|
评估模式入口: 用户自带实现方案 (ImplPlan), 软件评估其在 NPU 上的
|
|
各级硬件时延、流水情况与瓶颈, 并做可行性校验.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from .hardware import NpuSpec, ASCEND950PR
|
|
from .models import BmmCase, ImplPlan, EvalResult
|
|
from .timing import bound_type_of
|
|
from .branches.merge_batch import MergeBatchBranch
|
|
from .branches.iter_batch import IterBatchBranch
|
|
from .branches.to_matmul import ToMatmulBranch
|
|
from .branches.special import SpecialBranch
|
|
from .branches.stream_k import StreamKBranch
|
|
from .branches.asw_basic import AswBasicBranch
|
|
|
|
_BRANCH_EVAL = {
|
|
"MergeBatch": MergeBatchBranch,
|
|
"IterBatch": IterBatchBranch,
|
|
"转Matmul": ToMatmulBranch,
|
|
"特殊分支": SpecialBranch,
|
|
"StreamK": StreamKBranch,
|
|
"ASW_Basic": AswBasicBranch,
|
|
"ASW_Basic_降核": AswBasicBranch,
|
|
}
|
|
|
|
|
|
class PlanEvaluator:
|
|
def __init__(self, spec: NpuSpec = ASCEND950PR):
|
|
self.spec = spec
|
|
|
|
def evaluate(self, case: BmmCase, plan: ImplPlan) -> EvalResult:
|
|
res = EvalResult(case=case, plan=plan)
|
|
|
|
# 1) 硬件约束校验
|
|
violations = self._check_constraints(case, plan)
|
|
res.feasible = not violations
|
|
res.violations = "; ".join(violations)
|
|
|
|
# 2) 时延评估 (按方案分支调用对应模型)
|
|
branch_cls = _BRANCH_EVAL.get(plan.branch)
|
|
if branch_cls is None:
|
|
res.advice = (f"分支 {plan.branch!r} 的评估模型待后续迭代; "
|
|
f"当前支持: {sorted(_BRANCH_EVAL)}")
|
|
return res
|
|
timing = branch_cls(self.spec).evaluate(case, plan)
|
|
res.timing = timing
|
|
res.bound_type = bound_type_of(timing.bottleneck)
|
|
|
|
# 3) 瓶颈分析建议
|
|
res.advice = self._advice(case, plan, res)
|
|
return res
|
|
|
|
# ------------------------------------------------------------------
|
|
def _check_constraints(self, case: BmmCase, plan: ImplPlan) -> list:
|
|
"""约束校验: 委托给 constraints.py 单一约束源 (与生成同源, issue#5/#6)."""
|
|
from .constraints import check_plan_constraints
|
|
return check_plan_constraints(case, plan, self.spec)
|
|
|
|
# ------------------------------------------------------------------
|
|
def _advice(self, case: BmmCase, plan: ImplPlan, res: EvalResult) -> str:
|
|
t = res.timing
|
|
tips = []
|
|
if "效率降级" in plan.note:
|
|
tips.append("效率降级标注 (plan.note): 搬移效率下限不满足 —— 方案照常给出 "
|
|
"(兜底), 但实际效率低于模型假设, 时延可能低估; 建议调整 dtype/布局")
|
|
if not res.feasible:
|
|
tips.append("方案违反硬件约束, 需先修正: " + res.violations)
|
|
bn = t.bottleneck
|
|
if bn == "MTE2":
|
|
tips.append("瓶颈在 MTE2 搬移链 (GM 读写共享总线 + L2 重复读): 可增大 tile "
|
|
"提升 dValue/单核搬移量、利用 L2 驻留吸收重复读 (ASW swizzle/"
|
|
"分组方向), 或评估输出驻留 L2 以减少 GM 直写与读竞争")
|
|
elif bn == "MMAD":
|
|
tips.append("瓶颈在 Cube 计算: 已接近理论算力上限, 检查是否有冗余计算 "
|
|
"(MergeBatch 交叉项) 可消除")
|
|
elif bn == "FIXPIPE":
|
|
if plan.branch == "StreamK":
|
|
# StreamK 部分和按 L0C dtype 4B 防精度丢失, 不随 C 的 fp16/fp8 转换,
|
|
# dtype 减半提示不适用 (issue#14); 写账已并入归约, 需查归约侧配置
|
|
tips.append("瓶颈标注在 Fixpipe: StreamK 的部分和写出已并入归约计账 "
|
|
"(4B 防精度丢失, 不可随 C dtype 减半), 请核查 L2 写口/"
|
|
"归约并行度(grid_K) 设置")
|
|
else:
|
|
tips.append("瓶颈在 Fixpipe 写出: 检查输出 dtype (fp16/fp8 可减半写出量), "
|
|
"或评估输出驻留 L2 异步回写策略")
|
|
elif bn == "REDUCE":
|
|
tips.append("瓶颈在 StreamK 归约 (串行追加): 可增大 grid_K 摊薄归约 "
|
|
"或核对确定性要求是否允许 StreamK")
|
|
if plan.branch == "MergeBatch" and plan.k_l1 < case.k:
|
|
tips.append("警告: MergeBatch 处于 L1 绑定情形 (k_L1<K), 理论证明其恒劣于 "
|
|
"IterBatch (v1.1 §4.4), 建议改用 IterBatch")
|
|
return " | ".join(tips) if tips else "方案合理, 流水均衡"
|