ops-tune-workbench-plugin 0.0.0-stage → 0.0.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/NOTICE.md +14 -0
  2. package/README.md +39 -2
  3. package/cordis.patch.yml +20 -0
  4. package/lib/client.js +4253 -0
  5. package/lib/host.js +2518 -0
  6. package/package.json +53 -4
  7. package/skills/tool-ops-tune/INSTALL.md +52 -0
  8. package/skills/tool-ops-tune/README.md +167 -0
  9. package/skills/tool-ops-tune/SKILL.md +93 -0
  10. package/skills/tool-ops-tune/agent/AGENTS.md +195 -0
  11. package/skills/tool-ops-tune/agent/agents/perf-implementer.md +68 -0
  12. package/skills/tool-ops-tune/agent/agents/perf-reviewer.md +86 -0
  13. package/skills/tool-ops-tune/agent/workflows/task-prompts.md +362 -0
  14. package/skills/tool-ops-tune/agent/workflows/troubleshooting.md +43 -0
  15. package/skills/tool-ops-tune/docs/figures/opstune-architecture.png +0 -0
  16. package/skills/tool-ops-tune/docs/figures/opstune-architecture.svg +253 -0
  17. package/skills/tool-ops-tune/docs/ops-tune-design.md +404 -0
  18. package/skills/tool-ops-tune/docs//347/224/250/344/276/213/346/236/204/345/273/272.md +714 -0
  19. package/skills/tool-ops-tune/docs//347/237/245/350/257/206/345/272/223/350/247/204/350/214/203.md +103 -0
  20. package/skills/tool-ops-tune/evals/evals.json +242 -0
  21. package/skills/tool-ops-tune/ops-collect/SKILL.md +146 -0
  22. package/skills/tool-ops-tune/ops-collect/references/tool-routing.md +86 -0
  23. package/skills/tool-ops-tune/ops-collect/scripts/collect_board.sh +444 -0
  24. package/skills/tool-ops-tune/ops-collect/scripts/collect_cases.py +368 -0
  25. package/skills/tool-ops-tune/ops-collect/scripts/collect_sim.sh +134 -0
  26. package/skills/tool-ops-tune/ops-collect/scripts/csv_to_db.py +314 -0
  27. package/skills/tool-ops-tune/ops-collect/scripts/db_bench.py +545 -0
  28. package/skills/tool-ops-tune/ops-collect/scripts/facts_bridge.py +1063 -0
  29. package/skills/tool-ops-tune/ops-collect/scripts/isolation_test.py +454 -0
  30. package/skills/tool-ops-tune/ops-collect/scripts/jsonl_to_cases.py +163 -0
  31. package/skills/tool-ops-tune/ops-family/SKILL.md +76 -0
  32. package/skills/tool-ops-tune/ops-family/references/broadcast/advanced_tiling.md +92 -0
  33. package/skills/tool-ops-tune/ops-family/references/broadcast/broadcast_design.md +160 -0
  34. package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_kernel.cpp +126 -0
  35. package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_tiling.cpp +144 -0
  36. package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_tiling_common.h +401 -0
  37. package/skills/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_kernel.cpp +107 -0
  38. package/skills/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_tiling.cpp +64 -0
  39. package/skills/tool-ops-tune/ops-family/references/broadcast/datacopypad_design.md +46 -0
  40. package/skills/tool-ops-tune/ops-family/references/broadcast/guide.md +14 -0
  41. package/skills/tool-ops-tune/ops-family/references/broadcast/nddma_design.md +53 -0
  42. package/skills/tool-ops-tune/ops-family/references/broadcast/onedim_design.md +114 -0
  43. package/skills/tool-ops-tune/ops-family/references/broadcast/ub_broadcast_design.md +67 -0
  44. package/skills/tool-ops-tune/ops-family/references/common/datacopy_optimization_design.md +351 -0
  45. package/skills/tool-ops-tune/ops-family/references/common/index.md +9 -0
  46. package/skills/tool-ops-tune/ops-family/references/common/tail_block_design.md +268 -0
  47. package/skills/tool-ops-tune/ops-family/references/common/timing_harness/index.md +38 -0
  48. package/skills/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_aclrt_event.cpp +199 -0
  49. package/skills/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_torch_event.py +130 -0
  50. package/skills/tool-ops-tune/ops-family/references/common/ub_resident_design.md +342 -0
  51. package/skills/tool-ops-tune/ops-family/references/conversion/guide.md +69 -0
  52. package/skills/tool-ops-tune/ops-family/references/conversion/index.md +4 -0
  53. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_base.template +81 -0
  54. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_big_dim.template +217 -0
  55. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_one_axis.template +336 -0
  56. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_two_axis.template +781 -0
  57. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_n_last.template +340 -0
  58. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_small_shape.template +321 -0
  59. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_tensor_move.template +102 -0
  60. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd.template +480 -0
  61. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd_021axis.template +533 -0
  62. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_with_gather.template +688 -0
  63. package/skills/tool-ops-tune/ops-family/references/conversion/templates/transpose_tiling_data.template +192 -0
  64. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_big_dim.md +220 -0
  65. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_cut_one_axis.md +175 -0
  66. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_cut_two_axis.md +177 -0
  67. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_fusion_design.md +398 -0
  68. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_gather.md +191 -0
  69. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_n_last.md +276 -0
  70. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_small_shape.md +315 -0
  71. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_tensor_move.md +203 -0
  72. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_tiling_data.md +176 -0
  73. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_vconv_021.md +206 -0
  74. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_vconv_5hd.md +179 -0
  75. package/skills/tool-ops-tune/ops-family/references/elementwise/double_buffer_design.md +288 -0
  76. package/skills/tool-ops-tune/ops-family/references/elementwise/guide.md +46 -0
  77. package/skills/tool-ops-tune/ops-family/references/elementwise/index.md +10 -0
  78. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/CMakeLists.txt +131 -0
  79. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/README.md +55 -0
  80. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/add_custom_torch.cpp +80 -0
  81. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/ops.h +23 -0
  82. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/register.cpp +30 -0
  83. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/add_custom.asc +149 -0
  84. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/data_utils.h +97 -0
  85. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_kernel.asc +136 -0
  86. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_tiling.h +31 -0
  87. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/run.sh +86 -0
  88. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/gen_data.py +70 -0
  89. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/golden.py +34 -0
  90. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/test_torch.py +99 -0
  91. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/verify_result.py +56 -0
  92. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/usage_guide.md +61 -0
  93. package/skills/tool-ops-tune/ops-family/references/elementwise/vector_efficiency_design.md +203 -0
  94. package/skills/tool-ops-tune/ops-family/references/fa/fa_mixed_kernel_design.md +174 -0
  95. package/skills/tool-ops-tune/ops-family/references/fa/fa_roofline_design.md +141 -0
  96. package/skills/tool-ops-tune/ops-family/references/fa/guide.md +87 -0
  97. package/skills/tool-ops-tune/ops-family/references/knowledge_gaps.json +56 -0
  98. package/skills/tool-ops-tune/ops-family/references/matmul/best-practices-guide.md +14 -0
  99. package/skills/tool-ops-tune/ops-family/references/matmul/fullload_design.md +344 -0
  100. package/skills/tool-ops-tune/ops-family/references/matmul/guide.md +52 -0
  101. package/skills/tool-ops-tune/ops-family/references/matmul/mte2_preload_design.md +320 -0
  102. package/skills/tool-ops-tune/ops-family/references/matmul/pingpong_design.md +254 -0
  103. package/skills/tool-ops-tune/ops-family/references/matmul/scale_coalescing_design.md +282 -0
  104. package/skills/tool-ops-tune/ops-family/references/matmul/streamk_design.md +374 -0
  105. package/skills/tool-ops-tune/ops-family/references/matmul/swat_design.md +423 -0
  106. package/skills/tool-ops-tune/ops-family/references/matmul/templates/matmul_basic_blaze.template +165 -0
  107. package/skills/tool-ops-tune/ops-family/references/matmul/templates/usage_guide.md +29 -0
  108. package/skills/tool-ops-tune/ops-family/references/mc2/best-practices-guide.md +43 -0
  109. package/skills/tool-ops-tune/ops-family/references/mc2/guide.md +42 -0
  110. package/skills/tool-ops-tune/ops-family/references/mc2/local_matmul_design.md +130 -0
  111. package/skills/tool-ops-tune/ops-family/references/mc2/pipeline_balancing_design.md +502 -0
  112. package/skills/tool-ops-tune/ops-family/references/reduce/guide.md +178 -0
  113. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/kernel_utils.template +75 -0
  114. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_full_load.template +429 -0
  115. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_recompute.template +485 -0
  116. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_small_r.template +348 -0
  117. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_full_load.template +574 -0
  118. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_online.template +324 -0
  119. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_recompute.template +436 -0
  120. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_base.template +950 -0
  121. package/skills/tool-ops-tune/ops-family/references/reduce/templates/fused_attention_online_softmax_design.md +428 -0
  122. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_full_load.md +137 -0
  123. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_recompute.md +135 -0
  124. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_small_r.md +122 -0
  125. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_full_load.md +144 -0
  126. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_online.md +185 -0
  127. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_recompute.md +163 -0
  128. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling.template +460 -0
  129. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling_data.template +278 -0
  130. package/skills/tool-ops-tune/ops-family/references/reduce/templates/state_resident_design.md +337 -0
  131. package/skills/tool-ops-tune/ops-family/references/reduce/templates/usage_guide.md +253 -0
  132. package/skills/tool-ops-tune/ops-family/references/scalar/coding_principles.md +476 -0
  133. package/skills/tool-ops-tune/ops-family/references/scalar/conv_scalar.md +163 -0
  134. package/skills/tool-ops-tune/ops-family/references/scalar/guide.md +30 -0
  135. package/skills/tool-ops-tune/ops-family/references/simt/guide.md +91 -0
  136. package/skills/tool-ops-tune/ops-family/references/simt/optimization-guide.md +110 -0
  137. package/skills/tool-ops-tune/ops-family/references/sort/guide.md +83 -0
  138. package/skills/tool-ops-tune/ops-family/references/sort/radix_sort.md +354 -0
  139. package/skills/tool-ops-tune/ops-family/scripts/family_asset_audit.py +68 -0
  140. package/skills/tool-ops-tune/ops-family/scripts/tiling_candidates.py +419 -0
  141. package/skills/tool-ops-tune/ops-mechanism/SKILL.md +23 -0
  142. package/skills/tool-ops-tune/ops-mechanism/cases/ascendc/case_groupedmatmul_pipeline.md +23 -0
  143. package/skills/tool-ops-tune/ops-mechanism/cases/ascendc/case_triton_atomic_revert.md +19 -0
  144. package/skills/tool-ops-tune/ops-mechanism/cases/mc2/case_mc2_fusion.md +19 -0
  145. package/skills/tool-ops-tune/ops-mechanism/references/optimize/api-usage.md +70 -0
  146. package/skills/tool-ops-tune/ops-mechanism/references/optimize/ascendc-regbase-vf.md +45 -0
  147. package/skills/tool-ops-tune/ops-mechanism/references/optimize/ascendc-tiling.md +51 -0
  148. package/skills/tool-ops-tune/ops-mechanism/references/optimize/data-copy.md +67 -0
  149. package/skills/tool-ops-tune/ops-mechanism/references/optimize/index.md +45 -0
  150. package/skills/tool-ops-tune/ops-mechanism/references/optimize/mc2-balancing.md +77 -0
  151. package/skills/tool-ops-tune/ops-mechanism/references/optimize/mc2.md +103 -0
  152. package/skills/tool-ops-tune/ops-mechanism/references/optimize/memory-hierarchy.md +40 -0
  153. package/skills/tool-ops-tune/ops-mechanism/references/optimize/pipeline.md +95 -0
  154. package/skills/tool-ops-tune/ops-mechanism/scripts/case_backfill.py +170 -0
  155. package/skills/tool-ops-tune/ops-mechanism/scripts/log_gap.py +145 -0
  156. package/skills/tool-ops-tune/ops-mechanism/scripts/mc2_search.py +367 -0
  157. package/skills/tool-ops-tune/ops-report/SKILL.md +77 -0
  158. package/skills/tool-ops-tune/ops-verdict/evals/run_evals.py +837 -0
  159. package/skills/tool-ops-tune/ops-verdict/evals/smoke/install-hooks.sh +65 -0
  160. package/skills/tool-ops-tune/ops-verdict/evals/smoke/run_smoke.sh +405 -0
  161. package/skills/tool-ops-tune/ops-verdict/fixtures/core_tail_a5.csv +25 -0
  162. package/skills/tool-ops-tune/ops-verdict/fixtures/few_cores_a5.csv +5 -0
  163. package/skills/tool-ops-tune/ops-verdict/fixtures/mte2_bound_a2.csv +17 -0
  164. package/skills/tool-ops-tune/ops-verdict/fixtures/multi_kernel_mix_a5.csv +7 -0
  165. package/skills/tool-ops-tune/ops-verdict/fixtures/no_bound_a5.csv +9 -0
  166. package/skills/tool-ops-tune/ops-verdict/fixtures/serial_no_overlap_a2.csv +9 -0
  167. package/skills/tool-ops-tune/ops-verdict/fixtures/vec_bound_a5.csv +9 -0
  168. package/skills/tool-ops-tune/ops-verdict/references/io-contracts.md +94 -0
  169. package/skills/tool-ops-tune/ops-verdict/references/rule-schema.md +130 -0
  170. package/skills/tool-ops-tune/ops-verdict/references/scope-schema.md +61 -0
  171. package/skills/tool-ops-tune/ops-verdict/references/soc-spec.yaml +140 -0
  172. package/skills/tool-ops-tune/ops-verdict/rules/L0-instruction.yaml +67 -0
  173. package/skills/tool-ops-tune/ops-verdict/rules/L1-single-core.yaml +316 -0
  174. package/skills/tool-ops-tune/ops-verdict/rules/L2-inter-core.yaml +173 -0
  175. package/skills/tool-ops-tune/ops-verdict/rules/L3-inter-card.yaml +156 -0
  176. package/skills/tool-ops-tune/ops-verdict/scripts/batch_pipeline.py +444 -0
  177. package/skills/tool-ops-tune/ops-verdict/scripts/calibrate.py +249 -0
  178. package/skills/tool-ops-tune/ops-verdict/scripts/delta_compare.py +244 -0
  179. package/skills/tool-ops-tune/ops-verdict/scripts/render_html.py +274 -0
  180. package/skills/tool-ops-tune/ops-verdict/scripts/snapshot.py +200 -0
  181. package/skills/tool-ops-tune/ops-verdict/scripts/trace_bubble.py +331 -0
  182. package/skills/tool-ops-tune/ops-verdict/scripts/verdict.py +1072 -0
  183. package/skills/tool-ops-tune/scripts/diff_audit.py +281 -0
@@ -0,0 +1,68 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # ----------------------------------------------------------------------------------------------------------
4
+ # Copyright (c) 2026 Huawei Technologies Co., Ltd.
5
+ # This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6
+ # CANN Open Software License Agreement Version 2.0 (the "License").
7
+ # Please refer to the License for details. You may not use this file except in compliance with the License.
8
+ # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9
+ # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10
+ # See LICENSE in the root of the software repository for the full text of the License.
11
+ # ----------------------------------------------------------------------------------------------------------
12
+ """family_asset_audit.py — ops-family 族表数据防漂移静态断言(F13 整改验收)
13
+
14
+ 对 SKILL.md 族分类表中的「模板数」列做实测校验:模板数必须与
15
+ references/<族>/ 下 .h/.cpp/.template 文件的实际计数一致。
16
+ 不一致 → exit 1(族表失真,模板路由第一跳数据错误)。
17
+
18
+ 用法:python3 family_asset_audit.py (在 ops-family SKILL 根目录或仓根执行)
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import re
24
+ import sys
25
+ from pathlib import Path
26
+
27
+ SKILL_MD = Path(__file__).resolve().parents[1] / "SKILL.md"
28
+ REFERENCES = Path(__file__).resolve().parents[1] / "references"
29
+
30
+ TEMPLATE_EXTS = (".h", ".cpp", ".template")
31
+
32
+
33
+ def actual_template_count(family: str) -> int:
34
+ d = REFERENCES / family
35
+ if not d.is_dir():
36
+ return 0
37
+ return sum(1 for p in d.rglob("*") if p.suffix in TEMPLATE_EXTS and p.is_file())
38
+
39
+
40
+ def main() -> int:
41
+ text = SKILL_MD.read_text(encoding="utf-8")
42
+ # 解析族表行:| <族> | ... | <模板数> | <状态> | <文档> |
43
+ rows = re.findall(r"^\|\s*([a-z0-9_]+)\s*\|[^|]+\|[^|]+\|\s*(\d+)\s*\|", text, re.M)
44
+ if not rows:
45
+ print(
46
+ "ERROR: SKILL.md 族表未解析出「模板数」列(表结构变化?)", file=sys.stderr
47
+ )
48
+ return 1
49
+ failed = []
50
+ for family, declared in rows:
51
+ actual = actual_template_count(family)
52
+ mark = "OK" if int(declared) == actual else "MISMATCH"
53
+ print(f"[{mark}] {family}: 表={declared} 实测={actual}")
54
+ if mark == "MISMATCH":
55
+ failed.append(family)
56
+ if failed:
57
+ print(
58
+ f"\n族表模板数与实测不一致:{', '.join(failed)} —— 模板路由第一跳数据失真,"
59
+ f"请更新 SKILL.md 族表(检视 F13)",
60
+ file=sys.stderr,
61
+ )
62
+ return 1
63
+ print(f"\n{len(rows)} 族模板数全部与实测一致")
64
+ return 0
65
+
66
+
67
+ if __name__ == "__main__":
68
+ sys.exit(main())
@@ -0,0 +1,419 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # ----------------------------------------------------------------------------------------------------------
4
+ # Copyright (c) 2026 Huawei Technologies Co., Ltd.
5
+ # This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6
+ # CANN Open Software License Agreement Version 2.0 (the "License").
7
+ # Please refer to the License for details. You may not use this file except in compliance with the License.
8
+ # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9
+ # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10
+ # See LICENSE in the root of the software repository for the full text of the License.
11
+ # ----------------------------------------------------------------------------------------------------------
12
+ """tiling_candidates.py — Tiling 候选参数生成器·四族(REVIEW2 G6 短期 / 整改 P0-5)
13
+
14
+ 设计对齐:
15
+ - 机制卡 ascendc-tiling.md「禁止经验值交付」与 mc2-balancing.md 四剪枝此前
16
+ 只有 markdown 描述,全仓无候选生成执行载体——Block Dim 是最低垂果实
17
+ (L2 yaml 引用 4→20 核 4.75x 实测案例),也是最容易翻车的(容量联动
18
+ device fault 是 troubleshooting 运行类第一条)。
19
+ - 本脚本把候选生成 + 容量预算校验变成可执行载体:L2_UNDERUTILIZATION
20
+ 命中后不再靠 LLM 经验拍 block_dim / tile。
21
+ - 推导逻辑以契约方式移植一线 tiling 推导脚本
22
+ (elewise/reduction/broadcast/conversion 四族 fallback 参考实现),
23
+ 不直接复制其代码——本仓输出的是「候选表 + 逐候选容量占用计算过程」,
24
+ 单候选交付仍走 perf-implementer 单变量铁律。
25
+
26
+ 覆盖四族(首期):
27
+ elementwise:一维展开 → 多核均分(512 元素对齐)→ UB 切分(256B 对齐,双缓冲)
28
+ reduction:A/R 轴折叠 → 行块切分(rows_per_ub 受 UB 预算)→ 多核按 outer 分
29
+ broadcast:输出 shape 折叠 → UB 切分(多输入共享预算)→ 多核按 tile 数分
30
+ conversion:channel-flat → tile 元素对齐 → 多核按 tile 分(offset 表)
31
+
32
+ 用法:
33
+ python3 tiling_candidates.py --family elementwise --shape 8448,4096 --dtype fp16 \\
34
+ --soc a5 [--input-count 1] [--output candidates.json]
35
+
36
+ 输出(opstune.tiling-candidates/1.0):
37
+ { soc, family, shape, dtype, soc_spec_source,
38
+ candidates: [ {variant, block_dim, tile..., capacity_kb, capacity_note,
39
+ derived: {…计算过程}, aligned: true/false} ],
40
+ pruned: [ {variant, reason} ], // 剪枝记录(容量超限/未对齐)——可审计证据
41
+ warning: "候选表仅供参数选择,实施须单变量 + 容量复核 + 全量精度" }
42
+ """
43
+
44
+ from __future__ import annotations
45
+
46
+ import argparse
47
+ import json
48
+ import sys
49
+ from pathlib import Path
50
+
51
+ SCHEMA = "opstune.tiling-candidates/1.0"
52
+
53
+ # soc-spec 峰值标尺文件名(判定引擎同源数据)
54
+ SOC_SPEC_NAME = "soc-spec" + ".yaml"
55
+
56
+ # 常量(与参考实现同款契约值;来源标注)
57
+ ELEM_ALIGN_512 = 512 # [tuning-practice] 多核元素对齐粒度
58
+ ALIGN_256B = 256 # [tuning-practice] UB 对齐(字节)
59
+ MIN_TILING_BITS = 32768 # [tuning-practice] 每核最小 4KB(bits)
60
+ BUFFER_NUM_DEFAULT = 2 # [tuning-practice] 双缓冲队列数
61
+ DTYPE_BYTES = {
62
+ "fp16": 2,
63
+ "float16": 2,
64
+ "bf16": 2,
65
+ "bfloat16": 2,
66
+ "fp32": 4,
67
+ "float32": 4,
68
+ "int8": 1,
69
+ "int32": 4,
70
+ }
71
+
72
+
73
+ def load_soc_spec(path: Path) -> dict:
74
+ import yaml
75
+
76
+ return yaml.safe_load(path.read_text(encoding="utf-8"))
77
+
78
+
79
+ def soc_caps(soc_spec: dict, soc: str) -> dict:
80
+ """解析 SoC 约束:核数(reference 默认)+ UB 容量(同架构一致)。"""
81
+ block = soc_spec.get("socs", {}).get(soc)
82
+ if not block:
83
+ raise SystemExit(
84
+ f"ERROR: soc-spec 无 {soc} 条目(支持:{list(soc_spec.get('socs', {}))})"
85
+ )
86
+ ref = block.get("reference", {})
87
+ buf = block.get("buffer", {})
88
+ return {
89
+ "core_num": ref.get("ai_core_cnt", {}).get("value")
90
+ or ref.get("vector_core_cnt", {}).get("value")
91
+ or 1,
92
+ "vec_core_num": ref.get("vector_core_cnt", {}).get("value") or 1,
93
+ "ub_kb": buf.get("ub_kb", 192),
94
+ "l1_kb": buf.get("l1_kb", 512),
95
+ "soc_name": block.get("name", soc),
96
+ }
97
+
98
+
99
+ def _align_up(v: int, a: int) -> int:
100
+ return (v + a - 1) // a * a
101
+
102
+
103
+ def _multicore_core_num(total_elems: int, elem_bytes: int, available_cores: int) -> int:
104
+ """每核最小 4KB 约束下的用核数(与参考实现 calc_multicore_split 同款契约)。"""
105
+ min_bits = elem_bytes * 8
106
+ core_num = (total_elems * min_bits + MIN_TILING_BITS - 1) // MIN_TILING_BITS
107
+ return max(1, min(core_num, available_cores))
108
+
109
+
110
+ def candidates_elementwise(
111
+ shape: list[int], dtype: str, caps: dict, input_count: int
112
+ ) -> tuple[list, list]:
113
+ dim0 = 1
114
+ for d in shape:
115
+ dim0 *= d
116
+ elem_bytes = DTYPE_BYTES.get(dtype, 2)
117
+ cores = _multicore_core_num(dim0, elem_bytes, caps["core_num"])
118
+ ub_bytes = caps["ub_kb"] * 1024
119
+
120
+ out, pruned = [], []
121
+ # 候选 1:满核均分(Block Dim 提升机制——L2_UNDERUTILIZATION 的首选候选)
122
+ block_former = _align_up((dim0 + cores - 1) // cores, ELEM_ALIGN_512)
123
+ block_num = (dim0 + block_former - 1) // block_former
124
+ block_tail = dim0 - (block_num - 1) * block_former
125
+ # 候选 2/3:UB tile 缩减变体(容量联动防护:tile 越小 UB 占用越低,搬运次数越多)
126
+ for variant, buf_div in (
127
+ ("full-cores", BUFFER_NUM_DEFAULT * max(input_count, 1)),
128
+ ("half-tile", BUFFER_NUM_DEFAULT * max(input_count, 1) * 2),
129
+ ):
130
+ divisor = buf_div * elem_bytes
131
+ max_elem = ub_bytes // divisor
132
+ align_elems = max(ALIGN_256B // elem_bytes, 1)
133
+ ub_former = max((max_elem // align_elems) * align_elems, align_elems)
134
+ if variant == "half-tile":
135
+ ub_former = max(ub_former // 2 // align_elems * align_elems, align_elems)
136
+ cap_kb = round(
137
+ ub_former * elem_bytes * BUFFER_NUM_DEFAULT * max(input_count, 1) / 1024, 1
138
+ )
139
+ if cap_kb > caps["ub_kb"]:
140
+ pruned.append(
141
+ {
142
+ "variant": variant,
143
+ "reason": f"UB 预算超限({cap_kb}KB > {caps['ub_kb']}KB)",
144
+ }
145
+ )
146
+ continue
147
+ out.append(
148
+ {
149
+ "variant": variant,
150
+ "block_dim": cores,
151
+ "block_former": block_former,
152
+ "block_num": block_num,
153
+ "block_tail": block_tail if block_tail > 0 else block_former,
154
+ "ub_former": ub_former,
155
+ "ub_loop_former": (block_former + ub_former - 1) // ub_former,
156
+ "capacity_kb": cap_kb,
157
+ "capacity_note": f"UB {cap_kb}KB / {caps['ub_kb']}KB(双缓冲 × {max(input_count, 1)} 输入)",
158
+ "derived": {
159
+ "dim0": dim0,
160
+ "cores": cores,
161
+ "per_core_min_4kb": "dim0×dtype_bits / 32768 向上取整,min(可用核数)",
162
+ "block_former": f"ceil(dim0/cores) 向上对齐 {ELEM_ALIGN_512} 元素",
163
+ "ub_former": f"(ub_size / (buffer_num×inputs×elem_bytes)) 向下对齐 {ALIGN_256B}B",
164
+ },
165
+ "aligned": block_former % ELEM_ALIGN_512 == 0
166
+ and ub_former % align_elems == 0,
167
+ }
168
+ )
169
+ return out, pruned
170
+
171
+
172
+ def candidates_reduction(
173
+ shape: list[int], dtype: str, caps: dict, axis: int
174
+ ) -> tuple[list, list]:
175
+ """行块切分(AR 型:outer × reduce 折叠)。axis 为归约轴(-1 语义 = 最后一维)。"""
176
+ if axis < 0:
177
+ axis = len(shape) + axis
178
+ outer = 1
179
+ for i, d in enumerate(shape):
180
+ if i != axis:
181
+ outer *= d
182
+ r = shape[axis]
183
+ elem_bytes = DTYPE_BYTES.get(dtype, 2)
184
+ ub_bytes = caps["ub_kb"] * 1024
185
+
186
+ out, pruned = [], []
187
+ # rows_per_ub 受 UB 预算(一行 r 元素 × 双缓冲)
188
+ row_bytes = r * elem_bytes
189
+ rows_per_ub = max(ub_bytes // (row_bytes * BUFFER_NUM_DEFAULT), 1)
190
+ full_load = outer <= rows_per_ub
191
+ for variant, cores in (
192
+ ("full-load", min(outer, caps["core_num"])),
193
+ ("split-rows", caps["core_num"]),
194
+ ):
195
+ if variant == "full-load" and not full_load:
196
+ pruned.append(
197
+ {
198
+ "variant": variant,
199
+ "reason": f"outer({outer}) > rows_per_ub({rows_per_ub}),整行驻留不可行",
200
+ }
201
+ )
202
+ continue
203
+ tiles_per_core = max(outer // cores, 1)
204
+ cap_kb = round(
205
+ min(rows_per_ub, outer) * row_bytes * BUFFER_NUM_DEFAULT / 1024, 1
206
+ )
207
+ if cap_kb > caps["ub_kb"]:
208
+ pruned.append(
209
+ {
210
+ "variant": variant,
211
+ "reason": f"UB 预算超限({cap_kb}KB > {caps['ub_kb']}KB)",
212
+ }
213
+ )
214
+ continue
215
+ out.append(
216
+ {
217
+ "variant": variant,
218
+ "block_dim": cores,
219
+ "rows_per_ub": min(rows_per_ub, outer),
220
+ "tiles_per_core": tiles_per_core,
221
+ "r_chunk": r,
222
+ "is_full_load": variant == "full-load",
223
+ "capacity_kb": cap_kb,
224
+ "capacity_note": f"UB {cap_kb}KB / {caps['ub_kb']}KB(行块 × 双缓冲)",
225
+ "derived": {
226
+ "outer": outer,
227
+ "reduce_dim": r,
228
+ "rows_per_ub": "ub_size / (行字节 × buffer_num=2)",
229
+ "cores": "min(outer, 物理核数)(full-load)/ 物理核数(split)",
230
+ },
231
+ "aligned": True,
232
+ }
233
+ )
234
+ return out, pruned
235
+
236
+
237
+ def candidates_broadcast(
238
+ shape: list[int], dtype: str, caps: dict, input_count: int
239
+ ) -> tuple[list, list]:
240
+ """输出 shape 折叠后按末轴 UB 切分(broadcast 侧 stride=0 不占额外预算)。"""
241
+ total = 1
242
+ for d in shape:
243
+ total *= d
244
+ last = shape[-1] if shape else 1
245
+ elem_bytes = DTYPE_BYTES.get(dtype, 2)
246
+ ub_bytes = caps["ub_kb"] * 1024
247
+ align_elems = max(ALIGN_256B // elem_bytes, 1)
248
+ max_tile = max(
249
+ ub_bytes // (BUFFER_NUM_DEFAULT * max(input_count, 1) * elem_bytes), align_elems
250
+ )
251
+ ub_former = max_tile // align_elems * align_elems
252
+
253
+ out, pruned = [], []
254
+ ub_outer = (last + ub_former - 1) // ub_former
255
+ total_tiles = ub_outer
256
+ for d in shape[:-1]:
257
+ total_tiles *= d
258
+ for variant, cores in (
259
+ ("balanced", min(total_tiles, caps["core_num"])),
260
+ ("few-cores", max(min(total_tiles, caps["core_num"]) // 2, 1)),
261
+ ):
262
+ block_former = max(total_tiles // cores, 1)
263
+ cap_kb = round(
264
+ ub_former * elem_bytes * BUFFER_NUM_DEFAULT * max(input_count, 1) / 1024, 1
265
+ )
266
+ if cap_kb > caps["ub_kb"]:
267
+ pruned.append(
268
+ {
269
+ "variant": variant,
270
+ "reason": f"UB 预算超限({cap_kb}KB > {caps['ub_kb']}KB)",
271
+ }
272
+ )
273
+ continue
274
+ out.append(
275
+ {
276
+ "variant": variant,
277
+ "block_dim": cores,
278
+ "ub_split_axis": len(shape) - 1,
279
+ "ub_former": ub_former,
280
+ "ub_outer": ub_outer,
281
+ "block_former": block_former,
282
+ "capacity_kb": cap_kb,
283
+ "capacity_note": f"UB {cap_kb}KB / {caps['ub_kb']}KB(tile × 双缓冲 × {max(input_count, 1)} 输入)",
284
+ "derived": {
285
+ "total_tiles": total_tiles,
286
+ "ub_former": f"ub_size/(buffer×inputs×dtype)对齐 {ALIGN_256B}B",
287
+ },
288
+ "aligned": ub_former % align_elems == 0,
289
+ }
290
+ )
291
+ return out, pruned
292
+
293
+
294
+ def candidates_conversion(
295
+ shape: list[int], dtype: str, caps: dict, channel: int
296
+ ) -> tuple[list, list]:
297
+ """channel-flat 形态:按 (channel, 空间) 折叠,tile 元素对齐。"""
298
+ space = 1
299
+ for d in shape:
300
+ space *= d
301
+ space = space // channel if channel else space
302
+ elem_bytes = DTYPE_BYTES.get(dtype, 2)
303
+ ub_bytes = caps["ub_kb"] * 1024
304
+ align_elems = max(ALIGN_256B // elem_bytes, 1)
305
+ tile = max(
306
+ ub_bytes // (BUFFER_NUM_DEFAULT * 2 * elem_bytes) // align_elems * align_elems,
307
+ align_elems,
308
+ )
309
+
310
+ out, pruned = [], []
311
+ tiles = (space + tile - 1) // tile * channel
312
+ for variant, cores in (
313
+ ("full", min(tiles, caps["core_num"])),
314
+ ("half", max(min(tiles, caps["core_num"]) // 2, 1)),
315
+ ):
316
+ per_core = max(tiles // cores, 1)
317
+ cap_kb = round(tile * elem_bytes * BUFFER_NUM_DEFAULT * 2 / 1024, 1)
318
+ if cap_kb > caps["ub_kb"]:
319
+ pruned.append({"variant": variant, "reason": "UB 预算超限"})
320
+ continue
321
+ out.append(
322
+ {
323
+ "variant": variant,
324
+ "block_dim": cores,
325
+ "tile_elements": tile,
326
+ "channel": channel,
327
+ "tiles_per_core": per_core,
328
+ "capacity_kb": cap_kb,
329
+ "capacity_note": f"UB {cap_kb}KB / {caps['ub_kb']}KB(in+out 双缓冲)",
330
+ "derived": {
331
+ "tiles": tiles,
332
+ "tile": f"ub_size/(buffer×2×dtype) 对齐 {ALIGN_256B}B",
333
+ },
334
+ "aligned": tile % align_elems == 0,
335
+ }
336
+ )
337
+ return out, pruned
338
+
339
+
340
+ def main() -> int:
341
+ here = Path(__file__).resolve().parent
342
+ ap = argparse.ArgumentParser(
343
+ description="Tiling 候选生成器·四族(容量预算校验 + 剪枝记录)"
344
+ )
345
+ ap.add_argument(
346
+ "--family",
347
+ required=True,
348
+ choices=["elementwise", "reduction", "broadcast", "conversion"],
349
+ help="算子族(首期四族;matmul/mc2 走 Blaze 模板,sort/simt 见族表)",
350
+ )
351
+ ap.add_argument("--shape", required=True, help="逗号分隔维度(如 8448,4096)")
352
+ ap.add_argument("--dtype", required=True, help="fp16/bf16/fp32/int8")
353
+ ap.add_argument(
354
+ "--soc", required=True, help="a2/a3/a5(核数与 UB 容量读 soc-spec)"
355
+ )
356
+ ap.add_argument(
357
+ "--soc-spec",
358
+ default=str(here.parents[1] / "ops-verdict" / "references" / SOC_SPEC_NAME),
359
+ )
360
+ ap.add_argument(
361
+ "--input-count", type=int, default=1, help="输入张量数(UB 预算摊入)"
362
+ )
363
+ ap.add_argument(
364
+ "--axis", type=int, default=-1, help="reduction 族:归约轴(默认末轴)"
365
+ )
366
+ ap.add_argument("--channel", type=int, default=1, help="conversion 族:channel 数")
367
+ ap.add_argument("--output", default=None, help="candidates.json(缺省只打印)")
368
+ args = ap.parse_args()
369
+
370
+ shape = [int(x) for x in args.shape.split(",") if x.strip()]
371
+ soc_spec = load_soc_spec(Path(args.soc_spec))
372
+ caps = soc_caps(soc_spec, args.soc)
373
+
374
+ if args.family == "elementwise":
375
+ cands, pruned = candidates_elementwise(
376
+ shape, args.dtype, caps, args.input_count
377
+ )
378
+ elif args.family == "reduction":
379
+ cands, pruned = candidates_reduction(shape, args.dtype, caps, args.axis)
380
+ elif args.family == "broadcast":
381
+ cands, pruned = candidates_broadcast(shape, args.dtype, caps, args.input_count)
382
+ else:
383
+ cands, pruned = candidates_conversion(shape, args.dtype, caps, args.channel)
384
+
385
+ result = {
386
+ "schema": SCHEMA,
387
+ "soc": args.soc,
388
+ "family": args.family,
389
+ "shape": shape,
390
+ "dtype": args.dtype,
391
+ "input_count": args.input_count,
392
+ "soc_caps": caps,
393
+ "candidates": cands,
394
+ "pruned": pruned,
395
+ "warning": (
396
+ "候选表仅供参数选择起点:实施须单变量铁律(一次一个机制)、"
397
+ "容量运行时复核(GetCoreMemSize 为准,本表为静态估算)、"
398
+ "全量 case 精度验证(semantic_risk=altered 须边界用例)"
399
+ ),
400
+ }
401
+ if args.output:
402
+ out = Path(args.output)
403
+ out.parent.mkdir(parents=True, exist_ok=True)
404
+ out.write_text(
405
+ json.dumps(result, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
406
+ )
407
+ print(f"→ {out}")
408
+ print(f"{args.family} × {args.soc}:{len(cands)} 候选 / {len(pruned)} 剪枝")
409
+ for c in cands:
410
+ print(
411
+ f" [{c['variant']}] block_dim={c['block_dim']} {c['capacity_note']} aligned={c['aligned']}"
412
+ )
413
+ for p in pruned:
414
+ print(f" ✂ pruned [{p['variant']}]: {p['reason']}")
415
+ return 0
416
+
417
+
418
+ if __name__ == "__main__":
419
+ sys.exit(main())
@@ -0,0 +1,23 @@
1
+ ---
2
+ name: ops-mechanism
3
+ description: 算子优化机制库与案例证据库。按判定结果(verdict findings 的 mechanism_refs)路由到具体优化机制卡(原理/适用前提/预期收益/架构限制/精度风险/验证命令/回滚条件),并提供同型案例检索(A-E 证据分级、反例入库、无同型降级策略)。触发:瓶颈判定已出、选择优化机制、查案例参考、候选方案卡编写时使用。
4
+ ---
5
+
6
+ # 优化机制库与案例证据库(mechanism)
7
+
8
+ > **本子域知识资产(机制卡 / 案例卡 / 索引 / 回灌脚本)随「优化卡片知识」PR 交付**,
9
+ > 当前为骨架占位——SKILL.md 先行声明消费时机纪律与路由契约,供判定引擎
10
+ > `mechanism_refs` 字段的前向引用对齐。
11
+
12
+ ## 消费时机纪律(渐进式披露)
13
+
14
+ **判定之后才加载**。没有 verdict 证据时禁止先加载机制/案例猜瓶颈——证据先行是
15
+ 判定质量与上下文预算的双重保障。
16
+
17
+ ## 路由契约
18
+
19
+ - 判定引擎每条 HIT 规则携带 `mechanism_refs`(指向本子域 `references/optimize/`
20
+ 下机制卡路径);主 agent 按 findings 逐张加载(≤3 张候选方案卡);
21
+ - 机制卡八要素与案例卡 A-E 证据分级的书写规范见套件根 `docs/知识库规范.md`;
22
+ - 无同型案例的降级策略:证据链 + 机制卡驱动,实施细节自行推导并提高验证强度,
23
+ 报告标注「无同型案例」,本次证据沉淀为 E 级案例候选。
@@ -0,0 +1,23 @@
1
+ # 案例卡:GroupedMatmul 流水线气泡三连优化(AIC:AIV 配比 → workspace 份数 → DoubleBuffer)
2
+
3
+ - **算子类别**: matmul / grouped matmul
4
+ - **DSL**: Ascend C
5
+ - **类型**: 非 PR(官方递进教程,cann-samples 2_Performance story 系列之一)
6
+ - **来源可信度**: C(官方指南/sample,可运行代码)
7
+ - **来源链接**: gitcode.com/cann/cann-samples 2_Performance story(**验证日期: 2026-10**)
8
+ - **瓶颈证据**: 多单元 30-50% 占比、无明显主导(流水气泡形态;对应本体系 L1_LATENCY_SYNC_SUSPECT / 串行指纹)
9
+ - **优化机制**: ① AIC:AIV 启动比例 1:1→1:2(Cube/Vector 耗时不匹配) ② workspace 份数 2→4(减少互等) ③ DoubleBuffer(锦上添花)
10
+ - **原文性能数字**:
11
+
12
+ | 阶段 | 耗时 | 增量收益 |
13
+ |------|------|---------|
14
+ | 基线 | 218.1 us | — |
15
+ | AIC:AIV=1:2 | 154.2 us | 29.3% |
16
+ | workspace 2→4 | 131.8 us | 14.5% |
17
+ | DoubleBuffer | 128.1 us | 2.8% |
18
+ | **合计** | **128.1 us** | **41.3%** |
19
+
20
+ - **适用范围与警示**: ⚠ 数字只代表来源环境;三步是**机制优先级**证据(配比 > workspace > DB),
21
+ 非每步收益承诺;混合绑核 UB 预算须重算。
22
+ - **对应机制卡**: pipeline.md
23
+ - **对应判定规则**: L1_LATENCY_SYNC_SUSPECT / L1_SERIAL_NO_OVERLAP
@@ -0,0 +1,19 @@
1
+ # 案例卡:Triton SIMT→SIMD 原子归约 24.5x —— 反例入库样本(后被 revert)
2
+
3
+ - **算子类别**: reduction
4
+ - **DSL**: Triton-Ascend
5
+ - **类型**: PR(#208/#218 合并;**#376/#405 revert**)
6
+ - **来源可信度**: A(官方已合并 PR 含测试表)→ 收录为**反例**(被 revert)
7
+ - **来源链接**: github.com/triton-lang/triton-ascend PR #208/#218/#376/#405
8
+ (**验证日期: 2026-10**)
9
+ - **瓶颈证据**: atomic_add_3d 原子操作 SIMT 路径串行化
10
+ - **优化机制**: SIMT→SIMD 原子操作改写
11
+ - **原文性能数字**: 81.163 → 3.309 µs(~24.5x)
12
+ - **适用范围与警示**:
13
+ - ⚠⚠ **反例**:24.5x 收益后被 revert(PR #376/#405)——合并窗口的收益不等于可持续收益,
14
+ revert 原因(正确性/兼容性)在采纳同类机制前必须查明;
15
+ - ⚠ 机制可迁移,数字不可迁移;原子归约类改动属高风险语义变更,验证强度须提高
16
+ (并发场景 + 异常位型)。
17
+ - **对应机制卡**: api-usage.md(向量化)+ pipeline.md
18
+ - **对应判定规则**: L1_SCALAR_BOUND / L1_LATENCY_SYNC_SUSPECT
19
+ - **收录理由**: 与收益案例同等地位的反例——「负优化/回退记录是瓶颈证据的组成部分」。
@@ -0,0 +1,19 @@
1
+ # 案例卡:MC² 通算融合(R 值五档驱动配平 — 反例警示样本)
2
+
3
+ - **算子类别**: communication / MC²(matmul_all_reduce 类)
4
+ - **DSL**: Ascend C + SHMEM
5
+ - **类型**: 非 PR(社区文章)+ 内部调优实战综合
6
+ - **来源可信度**: D(二手来源,仅作线索与背景)——**本卡价值在方法论示例而非数字**
7
+ - **来源链接**: 社区文章(端到端 1.3x–1.5x 区间报道)+ MC² 通算融合调优实践文档
8
+ (**验证日期: 2026-10**)
9
+ - **瓶颈证据**: 隔离测试 R 值实测跨 0.3 ~ 9.8(30 倍区间)——R 不是常数,随 shape/量化/
10
+ rank 数剧烈变化,每 case 必须实测,禁止沿用上一次的 R
11
+ - **优化机制**: Local Matmul(首选,不依赖 R)→ R 驱动配平方向(R<1 计算掩盖通信 /
12
+ R>1 通信掩盖计算)→ 膨胀控制(最小化瓶颈方膨胀)
13
+ - **原文性能数字**: 端到端 1.3x–1.5x(D 级,不作预期收益依据)
14
+ - **适用范围与警示**:
15
+ - ⚠ D 级来源:数字仅证明「该机制类有收益记录」,禁止作为当前算子目标;
16
+ - ⚠ MTE2 污染必须先判:污染场景下做单核访存优化的案例多为负优化;
17
+ - ⚠ 跨 rank 取 max 口径:用平均口径报告会系统性高估收益。
18
+ - **对应机制卡**: mc2.md / mc2-balancing.md
19
+ - **对应判定规则**: L3_MC2_* 全档 / L3_MTE2_POLLUTED
@@ -0,0 +1,70 @@
1
+ # 机制卡:API 使用优化(标量削减 / Cast 削减 / 融合指令)
2
+
3
+ > 对应判定规则:L1_SCALAR_BOUND、L1_VEC_BOUND(Cast 重反证)、L0_ICACHE_MISS、
4
+ > L2_HEAD_OVERHEAD。内容沉淀自标量与 API 层调优一线实践。
5
+ > **DSL 适用域(P2-5/G19)**:本卡实施细节以 **Ascend C** 为准;Triton / CATLASS / TileLang 的等价原语与实施差异见 [index.md 跨 DSL 原语对照表](index.md#跨-dsl-原语对照六原语--dsl)——跨 DSL 实施时以该表为路由、对应 DSL 专项文档为深度参考,不按本卡字面照搬。
6
+
7
+ ## 标量削减(L1_SCALAR_BOUND)
8
+
9
+ | # | 方法 | 说明 |
10
+ |---|------|------|
11
+ | 1 | 缩小 TilingData | 字段越少 host→device 搬运越快 |
12
+ | 2 | 减少核数 | 数据量小时少核 = 少头开销(与 A2 满核 ~20-21us 头开销权衡) |
13
+ | 3 | TPipe 外置 | 避免 TPipe 对象内创建初始化 |
14
+ | 4 | 循环不变量外提 | 移到 Host 侧或 kernel 顶层 |
15
+ | 5 | 标量参数向量化 | 常量标量用 `Muls`/广播替代逐元素标量路径 |
16
+ | 6 | 地址计算优化 | areg 地址生成(LayerNormV3 案例:原文未附数字,机制见 cases/) |
17
+
18
+ scalar >90% 且 vec/mte 低 → 标量访问/地址计算主导:查指针运算与分支,而不是数据量。
19
+
20
+ ## Cast 削减(L1_VEC_BOUND + fp32 ratio 高反证)
21
+
22
+ - 检查 `aiv_vec_fp32_ratio` vs `aiv_vec_fp16_ratio`:fp32 远高于预期 = 大量 fp16→fp32 转换;
23
+ - 精度允许 → 直接 fp16 计算(**语义变更类**:须精度代价标注 + 边界位型验证);
24
+ - 必须转换 → 批量一次转换,禁止循环内逐元素 Cast。
25
+
26
+ ## 融合指令
27
+
28
+ mul+add → Axpy 类、mul+sub 等可融合序列查表合并;`Compare+Select` 链合并。
29
+
30
+ ## 头开销(L2_HEAD_OVERHEAD)
31
+
32
+ Task Duration − max(aic,aiv) 占比 >30%:数据量太小或核数太多——先减核再谈其它。
33
+
34
+ ## 减少计算量(深度 VEC BOUND 核心策略,沉淀自一线调优实践)
35
+
36
+ VEC >80% 时微架构调整(DoubleBuffer/融合指令/Bank Align)收益有限——瓶颈在
37
+ 计算量本身,须从**算法层减少向量指令数**:
38
+
39
+ | 手段 | 说明 | 适用条件 |
40
+ |------|------|---------|
41
+ | **LUT + 插值** | 查表+插值替代 Taylor 展开(超越函数每元素数十条 FMA 是计算量大头) | `aiv_vec_ratio` >90% 且 elementwise/activation 类 |
42
+ | 减少超越函数项数 | Taylor 12→8 项 | ❌ 有精度风险,不推荐 |
43
+
44
+ **Exp 案例(公开 API 路径)**:`AscendC::Exp<T, expandLevel, isReuseSrc>` 的
45
+ `expandLevel = 0` 即内部走查表+插值——无需手写查表逻辑。Log/Tanh 等同理,
46
+ 按 asc-devkit 对应 API 的模板参数取查表模式。
47
+
48
+ **检查方法**:trace 里 `vec_exp`/`vec_log`/`vec_tanh` 等超越函数指令占比
49
+ (trace_bubble 的空泡分类 + 指令统计);profiling 里 `aiv_vec_ratio` >90% +
50
+ 算子含超越函数 → 本策略优先级最高。
51
+
52
+ ## Matmul API 常量折叠(scalar bound 的 matmul 场景,蒸馏)
53
+
54
+ Kernel 使用 Matmul API 且 `aic_scalar_ratio` ≥ 0.15、shape 可固化或可取上界 →
55
+ 把 `TCubeTiling` 解析迁到编译期(constexpr),降低 `aic_scalar_time`。
56
+
57
+ ## 性能友好 API 选择(scalar 削减第 7 法,蒸馏)
58
+
59
+ - `SetFlag/WaitFlag` 代替 Queue、`LocalTensor` 代替 TBuffer、去除 TPipe——
60
+ 减少封装带来的 scalar 开销;
61
+ - 循环展开小循环减少分支代价;按 tiling 最简化循环轴。
62
+
63
+ ## 验证与回滚
64
+
65
+ 同口径 verdict 复跑(scalar_ratio / vec_fp32_ratio 下降为机制生效证据);语义变更类
66
+ (fp16 计算)精度用例必附。噪声带内 → revert。
67
+
68
+ ## 模板资产
69
+
70
+ `ops-family:references/scalar/guide.md`、`ops-family:references/elementwise/vector_efficiency_design.md`。