Fix #27-#30: dtype感知算力(Cube/AIV速率表) / GM首读下限与整芯片字节列口径+设计文档 / Fixpipe输出落点R4(整case驻留L2否则直写GM) / MergeBatch Cube公式复核注释

- docs/05_L2驻留GM读写与dtype算力口径_设计分析.md: R1-R6公理、S_A/S_B/S_C场景、输出落点R4、dtype速率表(白皮书出处+待标定假设)、逐分支GM/L2归属表 (issue#29/#30 先文档后代码)
- hardware: CUBE_DTYPE_FACTOR(f16/bf16=1, fp8=2x, fp4=4x, fp32=1/2假设) + AIV_DTYPE_FACTOR + q_cube/aiv_elem_rate (issue#28)
- 全分支 t_mmad/t_comp/drain/尾轮主导项/θ_c/R16 语义按输入dtype取算力; 混精度取慢侧; StreamK归约保持fp32(AIV fp32部分和)
- Fixpipe输出落点R4: to_l2 <=> V_in+V_out(+workspace)<=L2; 否则直写GM计入共享总线; ASW场景改S_A整case全驻留(原单batch驻留判定漏计整case输出累积逐出)
- 字节列统一整芯片口径(gm/l2/fix/cube_flops), dma_cmd_count注明单核; GM>=V_in不变量入测试; MergeBatch每步flops=2(b0M)(b0N)K公式注释显式化(#27复核与CSV一致无数值改动)
- tests 39->49 全过; 双压力seed7/6000+seed2024/4000: 0违规/0占位/0NaN/0GM<输入; examples三件套重生成且可复现0diff
This commit is contained in:
2026-09-04 16:26:10 +08:00
parent 4843053ad3
commit b9e07edc1d
15 changed files with 793 additions and 239 deletions

View File

@@ -4,12 +4,36 @@
换芯片时逻辑结构不变, 只需新增一份同结构参数表.
单位约定: 算力 FLOP/s, 带宽 Byte/s, 容量 Byte, 时延 秒.
dtype 感知算力 (issue#28, 设计文档 docs/05 §2):
Cube 算力按输入 dtype 分档, 基准 = BF16 (fp16 同速); 白皮书: FP8/MXFP8/HiF8
提供 2x FP16 张量 TFLOPS, MXFP4 提供 4x FP16; FP32/TF32 同代比值为假设值
(按 DaVinci 惯例 = 1/2, 白皮书未给同代比值), int8 假设同 FP8, 均待实测标定.
AIV 逐元素通量按 lane 位宽等比假设 (16bit x2 / 8bit x4 / 4bit x8, 待标定).
"""
from __future__ import annotations
from dataclasses import dataclass
# Cube 精度因子 (相对 BF16/FP16 基准档; A/B 不一致时取较慢一侧 = min 因子)
CUBE_DTYPE_FACTOR = {
"fp32": 0.5, "f32": 0.5, "tf32": 0.5, # 假设 = 1/2, 待实测标定
"fp16": 1.0, "f16": 1.0, "bf16": 1.0,
"fp8": 2.0, "fp8_e4m3": 2.0, "fp8_e5m2": 2.0, "int8": 2.0, # 白皮书 FP8=2xFP16; int8 假设同 FP8
"fp4": 4.0, "fp4_e2m1": 4.0, # 白皮书 MXFP4=4xFP16; 普通 fp4 假设同 MXFP4
}
# AIV 逐元素通量因子 (相对 fp32 128 lane/拍/核; 位宽等比假设, 待实测标定)
AIV_DTYPE_FACTOR = {
"fp32": 1.0, "f32": 1.0, "tf32": 1.0,
"fp16": 2.0, "f16": 2.0, "bf16": 2.0,
"fp8": 4.0, "fp8_e4m3": 4.0, "fp8_e5m2": 4.0, "int8": 4.0,
"fp4": 8.0, "fp4_e2m1": 8.0,
}
_RATE_FALLBACK = 1.0 # 未知 dtype 按基准档 (models.dtype_bytes 已先行校验, 正常不会到达)
@dataclass(frozen=True)
class NpuSpec:
@@ -55,13 +79,50 @@ class NpuSpec:
# ---- 派生量 (属性) ----
@property
def q16(self) -> float:
"""单核 Cube BF16 峰值算力 (FLOP/s)."""
"""单核 Cube BF16/FP16 峰值算力 (FLOP/s) = 15.1875T."""
return self.cube_peak_tflops * 1e12 / self.aic_num
@staticmethod
def _dtype_key(dtype) -> str:
return str(dtype).strip().lower()
def cube_factor(self, dtype_a, dtype_b=None) -> float:
"""按输入 dtype 取 Cube 精度因子 (issue#28).
A/B 不一致时取**较慢一侧** (因子较小者, 等价字节较大者);
Cube 乘法两侧的实际吞吐受较慢精度限制.
"""
keys = [self._dtype_key(dtype_a)]
if dtype_b is not None:
keys.append(self._dtype_key(dtype_b))
factors = [CUBE_DTYPE_FACTOR.get(k, _RATE_FALLBACK) for k in keys]
return min(factors)
def q_cube(self, dtype_a, dtype_b=None) -> float:
"""单核 Cube 峰值算力 (FLOP/s), 按输入 dtype 分档 (issue#28)."""
return self.q16 * self.cube_factor(dtype_a, dtype_b)
def aiv_elem_factor(self, dtype) -> float:
"""按 dtype 取 AIV 逐元素通量因子 (相对 fp32 基准)."""
return AIV_DTYPE_FACTOR.get(self._dtype_key(dtype), _RATE_FALLBACK)
@property
def aiv_elem_rate_fp32(self) -> float:
"""AIV fp32 逐元素吞吐 (元素/s), 64 核合计."""
return self.aiv_num * self.aiv_fp32_per_cycle * self.aiv_freq_ghz * 1e9
def aiv_elem_rate(self, dtype) -> float:
"""AIV 逐元素吞吐 (元素/s, 64 核合计), 按 dtype 分档 (issue#28)."""
return self.aiv_elem_rate_fp32 * self.aiv_elem_factor(dtype)
@property
def q_aiv(self) -> float:
"""AIV 向量求和吞吐 (元素/s), 64 核合计."""
return self.aiv_num * self.aiv_fp32_per_cycle * self.aiv_freq_ghz * 1e9
"""AIV fp32 逐元素吞吐 (元素/s, 64 核合计) = aiv_elem_rate_fp32.
注意: 该值只适用于 fp32 数据 (如 StreamK 对 fp32 部分和求和);
逐元素运算按输入 dtype 用 aiv_elem_rate(dtype) (issue#28).
"""
return self.aiv_elem_rate_fp32
@property
def bw_pc(self) -> float:
@@ -75,7 +136,11 @@ class NpuSpec:
@property
def r16(self) -> float:
"""16bit 位宽平衡点算存比 R_16 = Cube峰值 / (GM带宽/2B) ≈ 607.5 FLOP/元素."""
"""16bit 位宽平衡点算存比 R_16 = Cube峰值 / (GM带宽/2B) ≈ 607.5 FLOP/元素.
按 issue#28 速率表该比值对全 dtype 不变 (Cube 因子与元素字节数互成反比),
故入口条件沿用单一 R_16.
"""
return self.cube_peak_tflops * 1e12 / (self.bw_gm / 2)
@property