cann-workbench 0.0.0-stage → 0.0.2-alpha

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 (408) hide show
  1. package/README.md +183 -2
  2. package/THIRD_PARTY_NOTICES.md +85 -0
  3. package/cordis.patch.yml +3 -0
  4. package/doctor/LICENSE +202 -0
  5. package/doctor/NOTICE +12 -0
  6. package/doctor/README.md +18 -0
  7. package/lib/client.js +94437 -0
  8. package/lib/index.d.ts +722 -0
  9. package/lib/index.js +33711 -0
  10. package/locale/en.json +6 -0
  11. package/locale/zh.json +6 -0
  12. package/package.json +171 -4
  13. package/skills/asc-tools/tool-ops-tune/INSTALL.md +52 -0
  14. package/skills/asc-tools/tool-ops-tune/LICENSE +38 -0
  15. package/skills/asc-tools/tool-ops-tune/README.md +167 -0
  16. package/skills/asc-tools/tool-ops-tune/SKILL.md +93 -0
  17. package/skills/asc-tools/tool-ops-tune/agent/AGENTS.md +195 -0
  18. package/skills/asc-tools/tool-ops-tune/agent/agents/perf-implementer.md +68 -0
  19. package/skills/asc-tools/tool-ops-tune/agent/agents/perf-reviewer.md +86 -0
  20. package/skills/asc-tools/tool-ops-tune/agent/workflows/task-prompts.md +362 -0
  21. package/skills/asc-tools/tool-ops-tune/agent/workflows/troubleshooting.md +43 -0
  22. package/skills/asc-tools/tool-ops-tune/docs/figures/opstune-architecture.png +0 -0
  23. package/skills/asc-tools/tool-ops-tune/docs/figures/opstune-architecture.svg +253 -0
  24. package/skills/asc-tools/tool-ops-tune/docs/ops-tune-design.md +404 -0
  25. package/skills/asc-tools/tool-ops-tune/docs//347/224/250/344/276/213/346/236/204/345/273/272.md +730 -0
  26. package/skills/asc-tools/tool-ops-tune/docs//347/237/245/350/257/206/345/272/223/350/247/204/350/214/203.md +103 -0
  27. package/skills/asc-tools/tool-ops-tune/evals/evals.json +242 -0
  28. package/skills/asc-tools/tool-ops-tune/ops-collect/SKILL.md +146 -0
  29. package/skills/asc-tools/tool-ops-tune/ops-collect/references/tool-routing.md +86 -0
  30. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/collect_board.sh +458 -0
  31. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/collect_cases.py +368 -0
  32. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/collect_sim.sh +134 -0
  33. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/csv_to_db.py +314 -0
  34. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/db_bench.py +545 -0
  35. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/facts_bridge.py +1102 -0
  36. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/isolation_test.py +452 -0
  37. package/skills/asc-tools/tool-ops-tune/ops-collect/scripts/jsonl_to_cases.py +163 -0
  38. package/skills/asc-tools/tool-ops-tune/ops-family/SKILL.md +76 -0
  39. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/advanced_tiling.md +92 -0
  40. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/broadcast_design.md +160 -0
  41. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_kernel.cpp +126 -0
  42. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_tiling.cpp +144 -0
  43. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/code/broadcast_tiling_common.h +401 -0
  44. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_kernel.cpp +107 -0
  45. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_tiling.cpp +64 -0
  46. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/datacopypad_design.md +46 -0
  47. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/guide.md +14 -0
  48. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/nddma_design.md +53 -0
  49. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/onedim_design.md +114 -0
  50. package/skills/asc-tools/tool-ops-tune/ops-family/references/broadcast/ub_broadcast_design.md +67 -0
  51. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/datacopy_optimization_design.md +351 -0
  52. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/index.md +9 -0
  53. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/tail_block_design.md +268 -0
  54. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/timing_harness/index.md +38 -0
  55. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_aclrt_event.cpp +199 -0
  56. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_torch_event.py +130 -0
  57. package/skills/asc-tools/tool-ops-tune/ops-family/references/common/ub_resident_design.md +342 -0
  58. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/guide.md +69 -0
  59. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/index.md +4 -0
  60. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_base.template +81 -0
  61. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_big_dim.template +217 -0
  62. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_one_axis.template +336 -0
  63. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_two_axis.template +781 -0
  64. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_n_last.template +340 -0
  65. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_small_shape.template +321 -0
  66. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_tensor_move.template +102 -0
  67. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd.template +480 -0
  68. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd_021axis.template +533 -0
  69. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_with_gather.template +688 -0
  70. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/templates/transpose_tiling_data.template +192 -0
  71. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_big_dim.md +220 -0
  72. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_cut_one_axis.md +175 -0
  73. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_cut_two_axis.md +177 -0
  74. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_fusion_design.md +398 -0
  75. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_gather.md +191 -0
  76. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_n_last.md +276 -0
  77. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_small_shape.md +315 -0
  78. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_tensor_move.md +203 -0
  79. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_tiling_data.md +176 -0
  80. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_vconv_021.md +206 -0
  81. package/skills/asc-tools/tool-ops-tune/ops-family/references/conversion/transpose_vconv_5hd.md +179 -0
  82. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/double_buffer_design.md +288 -0
  83. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/guide.md +46 -0
  84. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/index.md +10 -0
  85. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/CMakeLists.txt +131 -0
  86. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/README.md +55 -0
  87. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/add_custom_torch.cpp +80 -0
  88. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/ops.h +23 -0
  89. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/register.cpp +30 -0
  90. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/add_custom.asc +149 -0
  91. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/data_utils.h +97 -0
  92. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_kernel.asc +136 -0
  93. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_tiling.h +31 -0
  94. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/run.sh +87 -0
  95. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/gen_data.py +70 -0
  96. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/golden.py +34 -0
  97. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/test_torch.py +99 -0
  98. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/verify_result.py +56 -0
  99. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/templates/usage_guide.md +61 -0
  100. package/skills/asc-tools/tool-ops-tune/ops-family/references/elementwise/vector_efficiency_design.md +203 -0
  101. package/skills/asc-tools/tool-ops-tune/ops-family/references/fa/fa_mixed_kernel_design.md +174 -0
  102. package/skills/asc-tools/tool-ops-tune/ops-family/references/fa/fa_roofline_design.md +141 -0
  103. package/skills/asc-tools/tool-ops-tune/ops-family/references/fa/guide.md +87 -0
  104. package/skills/asc-tools/tool-ops-tune/ops-family/references/knowledge_gaps.json +56 -0
  105. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/best-practices-guide.md +14 -0
  106. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/fullload_design.md +344 -0
  107. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/guide.md +52 -0
  108. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/mte2_preload_design.md +320 -0
  109. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/pingpong_design.md +254 -0
  110. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/scale_coalescing_design.md +282 -0
  111. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/streamk_design.md +374 -0
  112. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/swat_design.md +423 -0
  113. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/templates/matmul_basic_blaze.template +165 -0
  114. package/skills/asc-tools/tool-ops-tune/ops-family/references/matmul/templates/usage_guide.md +29 -0
  115. package/skills/asc-tools/tool-ops-tune/ops-family/references/mc2/best-practices-guide.md +43 -0
  116. package/skills/asc-tools/tool-ops-tune/ops-family/references/mc2/guide.md +42 -0
  117. package/skills/asc-tools/tool-ops-tune/ops-family/references/mc2/local_matmul_design.md +130 -0
  118. package/skills/asc-tools/tool-ops-tune/ops-family/references/mc2/pipeline_balancing_design.md +502 -0
  119. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/guide.md +178 -0
  120. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/kernel_utils.template +75 -0
  121. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_full_load.template +429 -0
  122. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_recompute.template +485 -0
  123. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_small_r.template +348 -0
  124. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_full_load.template +574 -0
  125. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_online.template +324 -0
  126. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_recompute.template +436 -0
  127. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_base.template +950 -0
  128. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/fused_attention_online_softmax_design.md +428 -0
  129. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_full_load.md +137 -0
  130. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_recompute.md +135 -0
  131. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_small_r.md +122 -0
  132. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_full_load.md +144 -0
  133. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_online.md +185 -0
  134. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_recompute.md +163 -0
  135. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling.template +460 -0
  136. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling_data.template +278 -0
  137. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/state_resident_design.md +337 -0
  138. package/skills/asc-tools/tool-ops-tune/ops-family/references/reduce/templates/usage_guide.md +253 -0
  139. package/skills/asc-tools/tool-ops-tune/ops-family/references/scalar/coding_principles.md +476 -0
  140. package/skills/asc-tools/tool-ops-tune/ops-family/references/scalar/conv_scalar.md +163 -0
  141. package/skills/asc-tools/tool-ops-tune/ops-family/references/scalar/guide.md +30 -0
  142. package/skills/asc-tools/tool-ops-tune/ops-family/references/simt/guide.md +91 -0
  143. package/skills/asc-tools/tool-ops-tune/ops-family/references/simt/optimization-guide.md +110 -0
  144. package/skills/asc-tools/tool-ops-tune/ops-family/references/sort/guide.md +83 -0
  145. package/skills/asc-tools/tool-ops-tune/ops-family/references/sort/radix_sort.md +354 -0
  146. package/skills/asc-tools/tool-ops-tune/ops-family/scripts/family_asset_audit.py +68 -0
  147. package/skills/asc-tools/tool-ops-tune/ops-family/scripts/tiling_candidates.py +419 -0
  148. package/skills/asc-tools/tool-ops-tune/ops-mechanism/SKILL.md +23 -0
  149. package/skills/asc-tools/tool-ops-tune/ops-mechanism/cases/ascendc/case_groupedmatmul_pipeline.md +23 -0
  150. package/skills/asc-tools/tool-ops-tune/ops-mechanism/cases/ascendc/case_triton_atomic_revert.md +19 -0
  151. package/skills/asc-tools/tool-ops-tune/ops-mechanism/cases/mc2/case_mc2_fusion.md +19 -0
  152. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/api-usage.md +70 -0
  153. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/ascendc-regbase-vf.md +45 -0
  154. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/ascendc-tiling.md +51 -0
  155. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/data-copy.md +67 -0
  156. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/index.md +45 -0
  157. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/mc2-balancing.md +77 -0
  158. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/mc2.md +103 -0
  159. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/memory-hierarchy.md +40 -0
  160. package/skills/asc-tools/tool-ops-tune/ops-mechanism/references/optimize/pipeline.md +95 -0
  161. package/skills/asc-tools/tool-ops-tune/ops-mechanism/scripts/case_backfill.py +170 -0
  162. package/skills/asc-tools/tool-ops-tune/ops-mechanism/scripts/log_gap.py +145 -0
  163. package/skills/asc-tools/tool-ops-tune/ops-mechanism/scripts/mc2_search.py +367 -0
  164. package/skills/asc-tools/tool-ops-tune/ops-report/SKILL.md +77 -0
  165. package/skills/asc-tools/tool-ops-tune/ops-verdict/evals/run_evals.py +963 -0
  166. package/skills/asc-tools/tool-ops-tune/ops-verdict/evals/smoke/install-hooks.sh +65 -0
  167. package/skills/asc-tools/tool-ops-tune/ops-verdict/evals/smoke/run_smoke.sh +535 -0
  168. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/core_tail_a5.csv +25 -0
  169. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/few_cores_a5.csv +5 -0
  170. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/mte2_bound_a2.csv +17 -0
  171. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/multi_kernel_mix_a5.csv +7 -0
  172. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/no_bound_a5.csv +9 -0
  173. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/serial_no_overlap_a2.csv +9 -0
  174. package/skills/asc-tools/tool-ops-tune/ops-verdict/fixtures/vec_bound_a5.csv +9 -0
  175. package/skills/asc-tools/tool-ops-tune/ops-verdict/references/io-contracts.md +94 -0
  176. package/skills/asc-tools/tool-ops-tune/ops-verdict/references/rule-schema.md +130 -0
  177. package/skills/asc-tools/tool-ops-tune/ops-verdict/references/scope-schema.md +61 -0
  178. package/skills/asc-tools/tool-ops-tune/ops-verdict/references/soc-spec.yaml +140 -0
  179. package/skills/asc-tools/tool-ops-tune/ops-verdict/rules/L0-instruction.yaml +67 -0
  180. package/skills/asc-tools/tool-ops-tune/ops-verdict/rules/L1-single-core.yaml +316 -0
  181. package/skills/asc-tools/tool-ops-tune/ops-verdict/rules/L2-inter-core.yaml +173 -0
  182. package/skills/asc-tools/tool-ops-tune/ops-verdict/rules/L3-inter-card.yaml +156 -0
  183. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/batch_pipeline.py +466 -0
  184. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/calibrate.py +249 -0
  185. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/delta_compare.py +278 -0
  186. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/render_html.py +304 -0
  187. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/snapshot.py +200 -0
  188. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/trace_bubble.py +331 -0
  189. package/skills/asc-tools/tool-ops-tune/ops-verdict/scripts/verdict.py +1094 -0
  190. package/skills/asc-tools/tool-ops-tune/scripts/diff_audit.py +281 -0
  191. package/skills/asc-tools/tool-precision-debug/LICENSE +38 -0
  192. package/skills/asc-tools/tool-precision-debug/README.md +190 -0
  193. package/skills/asc-tools/tool-precision-debug/scripts/install.sh +475 -0
  194. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/LICENSE +38 -0
  195. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/SKILL.md +136 -0
  196. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/evals/evals.json +932 -0
  197. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/baseline.md +69 -0
  198. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/checkpoint-design.md +82 -0
  199. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/dumptensor.md +83 -0
  200. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/hypothesis-experiment.md +114 -0
  201. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/index.md +20 -0
  202. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/localization.md +85 -0
  203. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/numerical-modeling.md +94 -0
  204. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/operator-modeling.md +116 -0
  205. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/progressive-error-analysis.md +86 -0
  206. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/reporting.md +140 -0
  207. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/reproducibility.md +121 -0
  208. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/semantic-bisection.md +72 -0
  209. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/diagnosis/sensitive-points.md +146 -0
  210. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/fp16-promotion-boundary.md +46 -0
  211. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/fused-rmsnorm-gelu-residual.md +162 -0
  212. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/index.md +16 -0
  213. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/integer-float-collision.md +44 -0
  214. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/partial-statistics-merge.md +30 -0
  215. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/softplus-v2-grad-family.md +156 -0
  216. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/sync-dataflow-handshake.md +89 -0
  217. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/tail-valid-region-state.md +30 -0
  218. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/cases/thnn-fused-gru-bias-reduction.md +73 -0
  219. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/index.md +34 -0
  220. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/accumulation-order.md +108 -0
  221. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/algorithm-parameter-contract.md +118 -0
  222. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/buffer-lifetime-alias.md +133 -0
  223. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/cast-rounding.md +76 -0
  224. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/complex-operator-boundary.md +48 -0
  225. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/data-movement-alignment.md +104 -0
  226. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/golden-host-contract.md +84 -0
  227. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/index-address-ownership.md +57 -0
  228. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/index.md +32 -0
  229. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/multicore-ownership.md +117 -0
  230. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/pipeline-sync-visibility.md +146 -0
  231. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/precision-mode-approximation.md +90 -0
  232. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/shape-boundary-contract.md +57 -0
  233. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/special-values.md +122 -0
  234. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/mechanisms/tiling-tail-state.md +146 -0
  235. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/broadcast-transpose.md +64 -0
  236. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/convolution.md +60 -0
  237. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/dynamic-quant.md +47 -0
  238. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/index.md +18 -0
  239. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/matmul-mmad.md +131 -0
  240. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/normalization.md +145 -0
  241. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/reduction.md +167 -0
  242. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/softplus-v2-grad.md +58 -0
  243. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/patterns/operators/sort-merge.md +65 -0
  244. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/tools/index.md +18 -0
  245. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/tools/kernel-debug-data.md +133 -0
  246. package/skills/asc-tools/tool-precision-debug/skills/tool-precision-debug/references/tools/msobjdump.md +150 -0
  247. package/skills/cann-doctor/SKILL.md +15 -0
  248. package/skills/catalog.json +79 -0
  249. package/skills/mattpocock/GLOSSARY.md +30 -0
  250. package/skills/mattpocock/LICENSE +21 -0
  251. package/skills/mattpocock/README.md +258 -0
  252. package/skills/mattpocock/SCOPE.md +35 -0
  253. package/skills/mattpocock/docs/agents/triage-labels.md +15 -0
  254. package/skills/mattpocock/docs/engineering/ask-matt.md +91 -0
  255. package/skills/mattpocock/docs/engineering/code-review.md +101 -0
  256. package/skills/mattpocock/docs/engineering/codebase-design.md +88 -0
  257. package/skills/mattpocock/docs/engineering/diagnosing-bugs.md +94 -0
  258. package/skills/mattpocock/docs/engineering/domain-modeling.md +86 -0
  259. package/skills/mattpocock/docs/engineering/grill-with-docs.md +82 -0
  260. package/skills/mattpocock/docs/engineering/implement-spec.md +86 -0
  261. package/skills/mattpocock/docs/engineering/implement.md +97 -0
  262. package/skills/mattpocock/docs/engineering/improve-codebase-architecture.md +113 -0
  263. package/skills/mattpocock/docs/engineering/pr.md +79 -0
  264. package/skills/mattpocock/docs/engineering/prototype.md +67 -0
  265. package/skills/mattpocock/docs/engineering/research.md +73 -0
  266. package/skills/mattpocock/docs/engineering/resolving-merge-conflicts.md +53 -0
  267. package/skills/mattpocock/docs/engineering/retro.md +81 -0
  268. package/skills/mattpocock/docs/engineering/setup-matt-pocock-skills.md +94 -0
  269. package/skills/mattpocock/docs/engineering/tdd.md +94 -0
  270. package/skills/mattpocock/docs/engineering/to-spec.md +81 -0
  271. package/skills/mattpocock/docs/engineering/to-tickets.md +99 -0
  272. package/skills/mattpocock/docs/engineering/triage.md +109 -0
  273. package/skills/mattpocock/docs/engineering/wayfinder.md +103 -0
  274. package/skills/mattpocock/docs/engineering/wizard.md +96 -0
  275. package/skills/mattpocock/docs/productivity/grill-me.md +76 -0
  276. package/skills/mattpocock/docs/productivity/grilling.md +87 -0
  277. package/skills/mattpocock/docs/productivity/handoff.md +76 -0
  278. package/skills/mattpocock/docs/productivity/teach.md +97 -0
  279. package/skills/mattpocock/docs/productivity/to-questionnaire.md +78 -0
  280. package/skills/mattpocock/docs/productivity/wait-what.md +36 -0
  281. package/skills/mattpocock/docs/productivity/writing-for-agents.md +70 -0
  282. package/skills/mattpocock/scripts/link-skills.sh +62 -0
  283. package/skills/mattpocock/scripts/list-skills.sh +7 -0
  284. package/skills/mattpocock/scripts/sync-plugin-version.mjs +41 -0
  285. package/skills/mattpocock/skills/deprecated/README.md +3 -0
  286. package/skills/mattpocock/skills/engineering/README.md +34 -0
  287. package/skills/mattpocock/skills/engineering/ask-matt/PHASE-BOUNDARIES.md +55 -0
  288. package/skills/mattpocock/skills/engineering/ask-matt/SKILL.md +97 -0
  289. package/skills/mattpocock/skills/engineering/ask-matt/agents/openai.yaml +5 -0
  290. package/skills/mattpocock/skills/engineering/code-review/SKILL.md +89 -0
  291. package/skills/mattpocock/skills/engineering/code-review/agents/openai.yaml +3 -0
  292. package/skills/mattpocock/skills/engineering/codebase-design/DEEPENING.md +37 -0
  293. package/skills/mattpocock/skills/engineering/codebase-design/DESIGN-IT-TWICE.md +44 -0
  294. package/skills/mattpocock/skills/engineering/codebase-design/SKILL.md +114 -0
  295. package/skills/mattpocock/skills/engineering/codebase-design/agents/openai.yaml +3 -0
  296. package/skills/mattpocock/skills/engineering/diagnosing-bugs/SKILL.md +138 -0
  297. package/skills/mattpocock/skills/engineering/diagnosing-bugs/agents/openai.yaml +3 -0
  298. package/skills/mattpocock/skills/engineering/diagnosing-bugs/scripts/hitl-loop.template.sh +44 -0
  299. package/skills/mattpocock/skills/engineering/domain-modeling/ADR-FORMAT.md +47 -0
  300. package/skills/mattpocock/skills/engineering/domain-modeling/GLOSSARY-FORMAT.md +60 -0
  301. package/skills/mattpocock/skills/engineering/domain-modeling/SKILL.md +74 -0
  302. package/skills/mattpocock/skills/engineering/domain-modeling/agents/openai.yaml +3 -0
  303. package/skills/mattpocock/skills/engineering/grill-with-docs/SKILL.md +7 -0
  304. package/skills/mattpocock/skills/engineering/grill-with-docs/agents/openai.yaml +5 -0
  305. package/skills/mattpocock/skills/engineering/implement/SKILL.md +17 -0
  306. package/skills/mattpocock/skills/engineering/implement/agents/openai.yaml +5 -0
  307. package/skills/mattpocock/skills/engineering/implement-spec/SKILL.md +42 -0
  308. package/skills/mattpocock/skills/engineering/implement-spec/agents/openai.yaml +5 -0
  309. package/skills/mattpocock/skills/engineering/improve-codebase-architecture/HTML-REPORT.md +123 -0
  310. package/skills/mattpocock/skills/engineering/improve-codebase-architecture/SKILL.md +71 -0
  311. package/skills/mattpocock/skills/engineering/improve-codebase-architecture/agents/openai.yaml +5 -0
  312. package/skills/mattpocock/skills/engineering/pr/CREDITS.md +3 -0
  313. package/skills/mattpocock/skills/engineering/pr/SKILL.md +170 -0
  314. package/skills/mattpocock/skills/engineering/pr/agents/openai.yaml +3 -0
  315. package/skills/mattpocock/skills/engineering/prototype/LOGIC.md +67 -0
  316. package/skills/mattpocock/skills/engineering/prototype/SKILL.md +26 -0
  317. package/skills/mattpocock/skills/engineering/prototype/UI.md +112 -0
  318. package/skills/mattpocock/skills/engineering/prototype/agents/openai.yaml +3 -0
  319. package/skills/mattpocock/skills/engineering/research/SKILL.md +12 -0
  320. package/skills/mattpocock/skills/engineering/research/agents/openai.yaml +3 -0
  321. package/skills/mattpocock/skills/engineering/retro/SKILL.md +44 -0
  322. package/skills/mattpocock/skills/engineering/retro/agents/openai.yaml +5 -0
  323. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/SKILL.md +120 -0
  324. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/agents/openai.yaml +5 -0
  325. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/domain.md +51 -0
  326. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/issue-tracker-github.md +50 -0
  327. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/issue-tracker-gitlab.md +46 -0
  328. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/issue-tracker-local.md +30 -0
  329. package/skills/mattpocock/skills/engineering/setup-matt-pocock-skills/triage-labels.md +15 -0
  330. package/skills/mattpocock/skills/engineering/tdd/SKILL.md +38 -0
  331. package/skills/mattpocock/skills/engineering/tdd/agents/openai.yaml +3 -0
  332. package/skills/mattpocock/skills/engineering/tdd/mocking.md +59 -0
  333. package/skills/mattpocock/skills/engineering/tdd/tests.md +77 -0
  334. package/skills/mattpocock/skills/engineering/to-spec/SKILL.md +75 -0
  335. package/skills/mattpocock/skills/engineering/to-spec/agents/openai.yaml +5 -0
  336. package/skills/mattpocock/skills/engineering/to-tickets/SKILL.md +105 -0
  337. package/skills/mattpocock/skills/engineering/to-tickets/agents/openai.yaml +5 -0
  338. package/skills/mattpocock/skills/engineering/triage/AGENT-BRIEF.md +207 -0
  339. package/skills/mattpocock/skills/engineering/triage/OUT-OF-SCOPE.md +105 -0
  340. package/skills/mattpocock/skills/engineering/triage/SKILL.md +112 -0
  341. package/skills/mattpocock/skills/engineering/triage/agents/openai.yaml +5 -0
  342. package/skills/mattpocock/skills/engineering/wayfinder/SKILL.md +128 -0
  343. package/skills/mattpocock/skills/engineering/wayfinder/agents/openai.yaml +5 -0
  344. package/skills/mattpocock/skills/engineering/wizard/SKILL.md +44 -0
  345. package/skills/mattpocock/skills/engineering/wizard/agents/openai.yaml +3 -0
  346. package/skills/mattpocock/skills/engineering/wizard/template.sh +223 -0
  347. package/skills/mattpocock/skills/in-progress/README.md +17 -0
  348. package/skills/mattpocock/skills/in-progress/chief-of-staff/SKILL.md +75 -0
  349. package/skills/mattpocock/skills/in-progress/chief-of-staff/agents/openai.yaml +5 -0
  350. package/skills/mattpocock/skills/in-progress/claude-handoff/SKILL.md +18 -0
  351. package/skills/mattpocock/skills/in-progress/claude-handoff/agents/openai.yaml +5 -0
  352. package/skills/mattpocock/skills/in-progress/loop-me/SKILL.md +32 -0
  353. package/skills/mattpocock/skills/in-progress/loop-me/agents/openai.yaml +5 -0
  354. package/skills/mattpocock/skills/in-progress/setup-ts-deep-modules/SKILL.md +102 -0
  355. package/skills/mattpocock/skills/in-progress/setup-ts-deep-modules/agents/openai.yaml +5 -0
  356. package/skills/mattpocock/skills/in-progress/setup-ts-deep-modules/dependency-cruiser.config.cjs +95 -0
  357. package/skills/mattpocock/skills/in-progress/software-factory/EXAMPLE-TASKS.md +80 -0
  358. package/skills/mattpocock/skills/in-progress/software-factory/GIT-HOSTS.md +84 -0
  359. package/skills/mattpocock/skills/in-progress/software-factory/OBSERVABILITY-PLATFORMS.md +75 -0
  360. package/skills/mattpocock/skills/in-progress/software-factory/RUNNERS.md +94 -0
  361. package/skills/mattpocock/skills/in-progress/software-factory/SKILL.md +98 -0
  362. package/skills/mattpocock/skills/in-progress/software-factory/TICKET-TRACKERS.md +85 -0
  363. package/skills/mattpocock/skills/in-progress/writing-beats/SKILL.md +67 -0
  364. package/skills/mattpocock/skills/in-progress/writing-beats/agents/openai.yaml +5 -0
  365. package/skills/mattpocock/skills/in-progress/writing-fragments/SKILL.md +79 -0
  366. package/skills/mattpocock/skills/in-progress/writing-fragments/agents/openai.yaml +5 -0
  367. package/skills/mattpocock/skills/in-progress/writing-shape/SKILL.md +79 -0
  368. package/skills/mattpocock/skills/in-progress/writing-shape/agents/openai.yaml +5 -0
  369. package/skills/mattpocock/skills/misc/README.md +8 -0
  370. package/skills/mattpocock/skills/misc/git-guardrails-claude-code/SKILL.md +95 -0
  371. package/skills/mattpocock/skills/misc/git-guardrails-claude-code/agents/openai.yaml +3 -0
  372. package/skills/mattpocock/skills/misc/git-guardrails-claude-code/scripts/block-dangerous-git.sh +25 -0
  373. package/skills/mattpocock/skills/misc/migrate-to-shoehorn/SKILL.md +118 -0
  374. package/skills/mattpocock/skills/misc/migrate-to-shoehorn/agents/openai.yaml +3 -0
  375. package/skills/mattpocock/skills/misc/scaffold-exercises/SKILL.md +106 -0
  376. package/skills/mattpocock/skills/misc/scaffold-exercises/agents/openai.yaml +3 -0
  377. package/skills/mattpocock/skills/misc/setup-pre-commit/SKILL.md +91 -0
  378. package/skills/mattpocock/skills/misc/setup-pre-commit/agents/openai.yaml +3 -0
  379. package/skills/mattpocock/skills/productivity/README.md +20 -0
  380. package/skills/mattpocock/skills/productivity/grill-me/SKILL.md +7 -0
  381. package/skills/mattpocock/skills/productivity/grill-me/agents/openai.yaml +5 -0
  382. package/skills/mattpocock/skills/productivity/grilling/SKILL.md +30 -0
  383. package/skills/mattpocock/skills/productivity/grilling/agents/openai.yaml +3 -0
  384. package/skills/mattpocock/skills/productivity/handoff/SKILL.md +16 -0
  385. package/skills/mattpocock/skills/productivity/handoff/agents/openai.yaml +5 -0
  386. package/skills/mattpocock/skills/productivity/teach/GLOSSARY-FORMAT.md +35 -0
  387. package/skills/mattpocock/skills/productivity/teach/LEARNING-RECORD-FORMAT.md +46 -0
  388. package/skills/mattpocock/skills/productivity/teach/MISSION-FORMAT.md +31 -0
  389. package/skills/mattpocock/skills/productivity/teach/RESOURCES-FORMAT.md +32 -0
  390. package/skills/mattpocock/skills/productivity/teach/SKILL.md +140 -0
  391. package/skills/mattpocock/skills/productivity/teach/agents/openai.yaml +5 -0
  392. package/skills/mattpocock/skills/productivity/to-questionnaire/SKILL.md +54 -0
  393. package/skills/mattpocock/skills/productivity/to-questionnaire/agents/openai.yaml +5 -0
  394. package/skills/mattpocock/skills/productivity/wait-what/SKILL.md +7 -0
  395. package/skills/mattpocock/skills/productivity/wait-what/agents/openai.yaml +5 -0
  396. package/skills/mattpocock/skills/productivity/writing-for-agents/SKILL-MECHANICS.md +22 -0
  397. package/skills/mattpocock/skills/productivity/writing-for-agents/SKILL.md +81 -0
  398. package/skills/mattpocock/skills/productivity/writing-for-agents/agents/openai.yaml +3 -0
  399. package/skills/task-agents/operator-development/Agent.md +71 -0
  400. package/skills/task-agents/operator-development/skills.json +120 -0
  401. package/skills/task-agents/operator-development/stages.schema.json +532 -0
  402. package/skills/task-agents/performance-optimization/Agent.md +78 -0
  403. package/skills/task-agents/performance-optimization/skills.json +64 -0
  404. package/skills/task-agents/response-protocol.md +35 -0
  405. package/skills/task-agents/sidebar-contracts.schema.json +4168 -0
  406. package/skills/task-agents/sidebar-selection.schema.json +971 -0
  407. package/skills/task-agents/visuals/components.json +283 -0
  408. package/skills/task-agents/visuals/contracts.schema.json +3628 -0
@@ -0,0 +1,288 @@
1
+ # Double Buffering / 双缓冲流水线优化设计
2
+
3
+ ## 1. 优化目标
4
+
5
+ 在 memory-bound 的逐元素算子中,单缓冲(InitBuffer num=1)导致 MTE2(数据搬入)、Vector(计算)、MTE3(数据搬出)三个阶段串行执行,计算单元大量空闲。本优化通过双缓冲(InitBuffer num=2)或多级缓冲策略,使数据搬运与计算流水线并行,**隐藏内存访问延迟**。
6
+
7
+ | 指标 | naive | optimized | 收益 |
8
+ |------|-------|-----------|------|
9
+ | MTE2/VECTOR 重叠度 | 0%(串行) | ~80-90%(流水线并行) | 吞吐量提升 20-50% |
10
+ | 计算单元利用率 | 低(大量 idle) | 高(持续有数据可算) | 硬件利用率最大化 |
11
+ | UB 内存占用 | `tile_size × num_tensors` | `2 × tile_size × num_tensors` | 增加一倍,需精确预算 |
12
+ | 代码复杂度 | 低 | 中(需管理同步事件) | 预取循环 + 事件同步 |
13
+
14
+ > 适用算子族:`elementwise`(`sin`, `cos`, `abs`, `exp`, `foreach_add` 等所有 memory-bound 逐元素算子),以及其它 `omni-ops` 通用场景。
15
+
16
+ ## 2. 架构概览
17
+
18
+ ### 2.1 存储层级与数据流
19
+
20
+ > 直接引用 `double_buffering.md` Pipeline Timeline Comparison:
21
+
22
+ **单缓冲:**
23
+ ```
24
+ Single buffer:
25
+ MTE2: [LOAD0] [LOAD1] [LOAD2]
26
+ VEC: [COMP0] [COMP1] [COMP2]
27
+ MTE3: [STORE0] [STORE1] [STORE2]
28
+ Time: =========================>
29
+ ```
30
+
31
+ **双缓冲:**
32
+ ```
33
+ Double buffer:
34
+ MTE2: [LOAD0][LOAD1][LOAD2][LOAD3]...
35
+ VEC: [COMP0][COMP1][COMP2]...
36
+ MTE3: [STORE0][STORE1]...
37
+ Time: ============> (significantly shorter)
38
+ ```
39
+
40
+ ### 2.3 事件同步模型
41
+
42
+ | 事件类型 | 含义 | 用途 |
43
+ |---------|------|------|
44
+ | `HardEvent::MTE2_V` | MTE2 搬入完成 → Vector 可读取 | 数据就绪通知 |
45
+ | `HardEvent::V_MTE3` | Vector 完成 → MTE3 可写回 | 计算完成通知 |
46
+ | `PipeBarrier<PIPE_V>` | Vector PIPE 内同步 | 指令间数据依赖 |
47
+
48
+ ### 2.4 缓冲策略扩展
49
+
50
+ | 策略 | Buffer 数量 | 适用场景 | 流水阶段 |
51
+ |------|------------|---------|---------|
52
+ | Double Buffer | 2 | 通用 memory-bound | 搬入/计算/搬出三级重叠 |
53
+ | Triple Buffer | 3 | AIC/AIV 混合核(Cube+Vector) | 搬运/Cube/Vector 三级同时 |
54
+ | 2×2 Matrix Buffer | 4 | Matmul 切 M+K 双维度 | M/K 双维度同时流水 |
55
+ | TQueBind | 1(VECIN/VECOUT 共享) | 读-改-写 in-place | 节省 50% buffer |
56
+ | Custom PingPong | 2+(手写 flag) | FlashAttention 多级存储 | L1/L0 多级 PingPong |
57
+
58
+ ## 3. 关键参数配置
59
+
60
+ ```cpp
61
+ // Host 侧 TilingData
62
+ struct DoubleBufferTiling {
63
+ uint32_t tileSize; // 单 tile 数据量(元素数)
64
+ uint32_t bufferNum; // 缓冲数量:2(双缓冲)或 3(三缓冲)
65
+ uint32_t ubFactor; // UB 分块因子,bufferNum 翻倍时减半
66
+ };
67
+ ```
68
+
69
+ ### 3.1 Tile 大小选取原则
70
+
71
+ | 参数 | 典型值 | 说明 |
72
+ |------|--------|------|
73
+ | `tileSize` | 2048 / 4096 / 12288 | 需 32B 对齐;双缓冲时减半 |
74
+ | `bufferNum` | 2(通用)/ 3(CV 融合) | 双缓冲为标准,三缓冲用于 Cube+Vector |
75
+ | `ubFactor` | 原值 / `bufferNum` | InitBuffer 的 num 翻倍时 ub_factor 必须减半 |
76
+
77
+ **UB 内存预算校验:**
78
+ ```
79
+ Required UB = tile_size × sizeof(dtype) × buffer_num × num_tensors
80
+ Available UB = 192KB (Ascend 910B3)
81
+
82
+ Example: FP16, 2 tensors (in+out), double buffer
83
+ = 12288 × 2 × 2 × 2 = 98304 bytes = 96KB [PASS]
84
+ ```
85
+
86
+ ### 3.2 自适应双缓冲决策
87
+
88
+ 当 UB 足够大时可一次性容纳所有数据时,**无需双缓冲**(减少同步开销);否则启用双缓冲隐藏延迟。
89
+
90
+ ```cpp
91
+ // Host 侧自适应决策
92
+ bool useDb = true;
93
+ if (maxRow > rowPerHeadCore) { // UB 足够大
94
+ useDb = false; // 不使用双缓冲
95
+ }
96
+ SetTilingKey(context, xDtype, useDb);
97
+ ```
98
+
99
+ ## 4. 核心计算循环
100
+
101
+ ### 4.1 naive 版本(优化前)
102
+
103
+ ```cpp
104
+ // 单缓冲,串行执行
105
+ TQue<QuePosition::VECIN, 1> inQueue;
106
+ TQue<QuePosition::VECOUT, 1> outQueue;
107
+ pipe.InitBuffer(inQueue, 1, tileSize * sizeof(T));
108
+ pipe.InitBuffer(outQueue, 1, tileSize * sizeof(T));
109
+
110
+ for (uint32_t i = 0; i < tileNum; i++) {
111
+ // 阶段 1:MTE2 搬入(Vector idle!)
112
+ LocalTensor<T> inLocal = inQueue.AllocTensor<T>();
113
+ DataCopy(inLocal, inGm[i * tileSize], tileSize);
114
+ inQueue.EnQue(inLocal);
115
+
116
+ // 阶段 2:Vector 计算(MTE2 idle!)
117
+ LocalTensor<T> computeLocal = inQueue.DeQue<T>();
118
+ Compute(computeLocal, tileSize);
119
+
120
+ // 阶段 3:MTE3 搬出(Vector idle!)
121
+ LocalTensor<T> outLocal = outQueue.AllocTensor<T>();
122
+ DataCopy(outGm[i * tileSize], computeLocal, tileSize);
123
+ outQueue.EnQue(outLocal);
124
+ outQueue.FreeTensor(outLocal);
125
+ inQueue.FreeTensor(computeLocal);
126
+ }
127
+ ```
128
+
129
+ ### 4.2 optimized 版本(优化后):标准双缓冲
130
+
131
+ ```cpp
132
+ // 双缓冲 + 预取循环重构
133
+ // 注意:TQue 模板参数 depth 为队列深度(连续 EnQue 次数),与 double buffer 无关。
134
+ // 非原地操作场景下 depth 推荐为 1(编译器有特殊优化)。
135
+ // double buffer 仅通过 InitBuffer 的 num 参数控制(num=2 开启)。
136
+ TQue<QuePosition::VECIN, 1> inQueue; // depth = 1(推荐值)
137
+ TQue<QuePosition::VECOUT, 1> outQueue; // depth = 1(推荐值)
138
+ pipe.InitBuffer(inQueue, 2, tileSize * sizeof(T)); // num = 2 开启 double buffer
139
+ pipe.InitBuffer(outQueue, 2, tileSize * sizeof(T)); // num = 2 开启 double buffer
140
+
141
+ // 预取:加载第一个 tile
142
+ CopyIn(0);
143
+
144
+ // 主循环:计算 i,同时预取 i+1,写回 i-1
145
+ // 实现 MTE2 / Vector / MTE3 三级硬件流水线并行
146
+ for (uint32_t i = 0; i < tileNum - 1; i++) {
147
+ CopyIn(i + 1); // MTE2: 预取下一个 tile(与本轮 Vector 并行)
148
+ Compute(i); // Vector: 计算当前 tile(与上一轮 MTE3 并行)
149
+ if (i > 0) {
150
+ CopyOut(i - 1); // MTE3: 写回前一个结果(与下一轮 MTE2 并行)
151
+ }
152
+ }
153
+
154
+ // 收尾:处理最后两个 tile(循环内未写回的尾部)
155
+ Compute(tileNum - 1);
156
+ for (uint32_t i = (tileNum > 1) ? tileNum - 2 : 0; i < tileNum; i++) {
157
+ CopyOut(i);
158
+ }
159
+ ```
160
+
161
+ ### 4.3 optimized 版本:事件同步双缓冲(精细控制)
162
+
163
+ ```cpp
164
+ // 使用 HardEvent 实现更精细的流水线同步
165
+ // 注意:以下使用 TBuf 显式分配两份物理 buffer,通过 event 手动同步
166
+ TBuf<TPosition::VECCALC> pingBuf, pongBuf;
167
+ pipe.InitBuffer(pingBuf, tileSize * sizeof(T));
168
+ pipe.InitBuffer(pongBuf, tileSize * sizeof(T));
169
+
170
+ event_t pingId = EVENT_ID6;
171
+ event_t pongId = EVENT_ID7;
172
+
173
+ for (uint32_t idx = 0; idx < tileNum; idx++) {
174
+ auto pipeId = (idx % 2 == 0) ? pingId : pongId;
175
+ LocalTensor<T> local = (idx % 2 == 0) ? pingBuf.Get<T>() : pongBuf.Get<T>();
176
+
177
+ // 等待上一轮该 pipe 的 MTE3 写回完成(避免覆盖未写出的数据)
178
+ WaitFlag<HardEvent::MTE3_MTE2>(pipeId);
179
+ CopyIn(local, idx * tileSize, tileSize); // MTE2 搬入
180
+ SetFlag<HardEvent::MTE2_V>(pipeId); // 通知 Vector 数据就绪
181
+
182
+ WaitFlag<HardEvent::MTE2_V>(pipeId); // 等待数据就绪
183
+ Compute(local, tileSize); // Vector 计算
184
+ SetFlag<HardEvent::V_MTE3>(pipeId); // 通知 MTE3 计算完成
185
+ }
186
+ ```
187
+
188
+ ### 4.4 optimized 版本:三缓冲(Cube+Vector 融合场景)
189
+
190
+ ```cpp
191
+ // 搬运 / Cube / Vector 三级流水
192
+ template<BufferType bufferType, SyncType syncType>
193
+ class BuffersPolicy3buff {
194
+ Buffer<bufferType, syncType> a_, b_, c_;
195
+ uint32_t flag1_ = 0, flag1_vec1_ = 0, flag1_bmm2_ = 0;
196
+ public:
197
+ Buffer<bufferType, syncType>& Get() {
198
+ if (flag1_ == 0) { flag1_ = 1; return a_; }
199
+ else if (flag1_ == 1) { flag1_ = 2; return b_; }
200
+ else { flag1_ = 0; return c_; }
201
+ }
202
+ Buffer<bufferType, syncType>& GetVec() {
203
+ if (flag1_vec1_ == 0) { flag1_vec1_ = 1; return a_; }
204
+ else if (flag1_vec1_ == 1) { flag1_vec1_ = 2; return b_; }
205
+ else { flag1_vec1_ = 0; return c_; }
206
+ }
207
+ Buffer<bufferType, syncType>& GetCube() {
208
+ if (flag1_bmm2_ == 0) { flag1_bmm2_ = 1; return a_; }
209
+ else if (flag1_bmm2_ == 1) { flag1_bmm2_ = 2; return b_; }
210
+ else { flag1_bmm2_ = 0; return c_; }
211
+ }
212
+ };
213
+ ```
214
+
215
+ ## 5. 从 naive 到 double_buffer 的关键修改点
216
+
217
+ | 修改项 | naive(优化前) | double_buffer(优化后) |
218
+ |--------|---------------|----------------------|
219
+ | InitBuffer 内存块数 | `num = 1` | `num = 2`(或 3/4) |
220
+ | 循环结构 | `for(i) { CopyIn(i); Compute(i); CopyOut(i); }` | 预取 + `for(i) { CopyIn(i+1); Compute(i); CopyOut(i-1); }` |
221
+ | UB 用量 | `tile_size × num_tensors` | `2 × tile_size × num_tensors` |
222
+ | 同步方式 | 隐式(EnQue/DeQue)或 无 | HardEvent 显式同步 + PipeBarrier |
223
+ | Tiling 联动 | 固定 tile_size | tile_size 减半(保持总 UB 预算) |
224
+ | 适用场景 | 所有场景(但性能低) | memory-bound、大数据量场景 |
225
+
226
+ ## 6. 注意事项 / 约束
227
+
228
+ 1. **仅改 InitBuffer 的 num 不够!必须重构循环为预取模式**。
229
+ - 错误:仅改 `InitBuffer(..., num, ...)` 的 `num=1→2`,循环不变 → 无性能提升。
230
+ - 正确:必须实现预取(prefetch)循环。
231
+
232
+ 2. **Tiling 联动:InitBuffer 的 num 翻倍时 ub_factor 必须减半**。双缓冲使队列内存翻倍,若不调整 ub_factor,总 UB 用量超限会导致编译失败或运行时越界。
233
+
234
+ 3. **同步指令开销**:HardEvent `SetFlag/WaitFlag` 有轻微开销,仅在必要路径使用。过度同步会抵消流水收益。
235
+
236
+ 4. **Tail tile 处理**:tile_size 减半后,tile 数量翻倍,需确保尾 tile 处理逻辑正确更新。
237
+
238
+ 5. **不同 buffer 策略的适用边界**:
239
+ - 纯 Vector 算子 → Double Buffer(2 个 buffer)
240
+ - Cube+Vector 融合 → Triple Buffer(3 个 buffer)
241
+ - Matmul 双维度切分 → 2×2 Matrix Buffer(4 个 buffer)
242
+ - 读-改-写 in-place → TQueBind(共享物理 buffer)
243
+
244
+ 6. **自适应策略**:UB 足够大时(可一次容纳所有数据),不使用双缓冲,减少同步开销。
245
+
246
+ 7. **同一 TPosition 上 QUE Buffer 数量受限**(参考官方文档)。
247
+ - Atlas A2 训练系列产品(如 910B1):同一 TPosition 上 QUE Buffer 不超过 **8** 块。
248
+ - Atlas 训练/推理系列产品:同一 TPosition 上 QUE Buffer 不超过 **4** 块。
249
+ - `QuePosition::VECIN` 与 `QuePosition::VECOUT` 底层均映射到 `TPosition::VECCALC`(UB),因此 `inQueue(2) + outQueue(2)` 合计占用 **4 块**。虽未达到 910B1 的上限,但扩展时需注意。若需更多 buffer,应按官方建议**合并到一块 buffer 通过偏移使用**,而非继续增加独立队列。
250
+
251
+ 8. **事件 ID 管理**:自定义 PingPong 需手动管理 event ID 数组,避免复用冲突。
252
+
253
+ ## 7. 实施常见问题与解决方案
254
+
255
+ | 问题 | 根因 | 解决方案 |
256
+ |------|------|---------|
257
+ | 启用双缓冲后无性能提升 | 循环未重构为预取模式 | 必须实现 `CopyIn(i+1); Compute(i); CopyOut(i-1)` 三级流水结构 |
258
+ | UB 溢出编译失败 | tile_size 未减半 | InitBuffer 的 num: 1→2 时,ub_factor 必须同步减半 |
259
+ | 数据竞争/静默错误 | 同步缺失或事件 ID 复用 | 每对 SetFlag/WaitFlag 使用独立 event ID |
260
+ | 尾 tile 处理异常 | tile 数量翻倍后尾 tile 逻辑未更新 | 更新 Host 侧 tiling 计算:tileNum = ceil(total / newTileSize) |
261
+ | 三缓冲调试困难 | 三套独立 flag 管理复杂 | 使用类封装(BuffersPolicy3buff),统一 Get/GetVec/GetCube 接口 |
262
+
263
+ ## 8. 选型决策与自检清单
264
+
265
+ ### 8.1 选型决策
266
+
267
+ ```
268
+ if (算子是 memory-bound && MTE2 active >> VECTOR active):
269
+ if (纯 Vector 算子):
270
+ → 启用 Double Buffer(InitBuffer num=2)
271
+ → tile_size 减半,保持 UB 预算
272
+ elif (Cube+Vector 融合):
273
+ → 启用 Triple Buffer(InitBuffer num=3)
274
+ → 搬运/Cube/Vector 三级流水
275
+ elif (UB 足够大,可一次容纳所有数据):
276
+ → 不使用双缓冲(减少同步开销)
277
+ else:
278
+ → 单缓冲即可(compute-bound 场景双缓冲无收益)
279
+ ```
280
+
281
+ ### 8.2 自检清单
282
+
283
+ - [ ] 循环重构为预取模式:`CopyIn(0)` + `for(i) { CopyIn(i+1); Compute(i); CopyOut(i-1); }`
284
+ - [ ] `InitBuffer num: 1→2` 时 `ub_factor` 同步减半
285
+ - [ ] 使用 `HardEvent` 或 `PipeBarrier` 确保数据依赖正确
286
+ - [ ] 尾 tile 数量随 tile_size 减半而翻倍,尾 tile 逻辑已更新
287
+ - [ ] UB 内存预算校验通过:`tile_size × sizeof(dtype) × buffer_num × num_tensors < UB_capacity`
288
+ - [ ] 事件 ID 无复用冲突(每对 SetFlag/WaitFlag 独立)
@@ -0,0 +1,46 @@
1
+ # Elementwise 族指南(guide)
2
+
3
+ > elementwise 此前为 🟡 骨架(仅 index + 2 篇设计文档,无 guide
4
+ > 路由层)——本 guide 补路由,设计文档补实施深度。
5
+
6
+ ## 族特点
7
+
8
+ 所有输入输出同 shape、无跨元素依赖——瓶颈形态高度一致:**memory-bound**(MTE2
9
+ 搬入 / MTE3 搬出主导),或小 shape 下的 launch/头开销主导。判定入口:
10
+ `L1_MTE2_BOUND` / `L1_MTE3_BOUND` / `L1_SERIAL_NO_OVERLAP` / `L2_UNDERUTILIZATION`。
11
+
12
+ ## 优化路由(按 verdict 命中)
13
+
14
+ | verdict 命中 | 首选机制 | 文档 |
15
+ |-------------|---------|------|
16
+ | L1_SERIAL_NO_OVERLAP / LATENCY_SYNC | 双缓冲流水(TQue bufNum=2) | [double_buffer_design.md](double_buffer_design.md) |
17
+ | L1_VEC_BOUND(含 cast-heavy 反证) | 向量化效率 / 减 Cast | [vector_efficiency_design.md](vector_efficiency_design.md) |
18
+ | L1_MTE2_BOUND(真饱和) | dtype 收窄 / 数据量削减 | 机制卡 data-copy.md(族外共享) |
19
+ | L2_UNDERUTILIZATION | Block Dim 提升 | `ops-family/scripts/tiling_candidates.py --family elementwise`(候选生成 + 容量校验) |
20
+ | L2_HEAD_OVERHEAD(小 shape) | 减核 / 缩 TilingData | 机制卡 api-usage.md |
21
+
22
+ ## Tiling 候选生成
23
+
24
+ ```bash
25
+ python3 ops-family/scripts/tiling_candidates.py \
26
+ --family elementwise --shape 8448,4096 --dtype fp16 --soc a5
27
+ ```
28
+
29
+ 多核 512 元素对齐 + UB 256B 对齐 + 每核 ≥4KB 下限已内置(候选表逐条附容量
30
+ 占用计算);fp16 add/sub 的 fp32 中间精度分支(use_fp32_cast)按算子属性开启。
31
+
32
+ ## 模板货架(templates/)
33
+
34
+ [`templates/add_custom/`](templates/add_custom/)——可裁剪工程模板(源自
35
+ 参考实现 验证样例):双缓冲 TQue kernel + 多核均分/尾核
36
+ 特化 + DataCopyPad 尾块 + host 直调 + torch 扩展双通路 + 验证脚本。
37
+ 转换纪律见 [`templates/usage_guide.md`](templates/usage_guide.md)(只改
38
+ [MODIFY] 标记点,计时结构不许改)。
39
+
40
+ 设计深度参考:[double_buffer_design.md](double_buffer_design.md)(双缓冲流水)/
41
+ [vector_efficiency_design.md](vector_efficiency_design.md)(向量化与减 Cast)。
42
+
43
+ ## 知识缺口
44
+
45
+ 发现本 guide 未覆盖的场景 → `ops-mechanism/scripts/log_gap.py --family elementwise
46
+ --kind guide_missing --note "<场景>"` 计数上报(季度排产输入)。
@@ -0,0 +1,10 @@
1
+ # Elementwise 族
2
+
3
+ | 文档 | 内容 |
4
+ |------|------|
5
+ | [guide.md](guide.md) | **族指南(入口)**:verdict 命中 → 机制路由表 + tiling 候选生成 + 模板货架入口 |
6
+ | [templates/add_custom/](templates/) | **可裁剪工程模板**:双缓冲 kernel + host + torch 扩展(usage_guide 转换纪律) |
7
+ | double_buffer_design.md | 双缓冲设计(对应 L1_SERIAL_NO_OVERLAP) |
8
+ | vector_efficiency_design.md | 向量化效率(对应 L1_VEC_BOUND / 减 Cast) |
9
+
10
+ 内容沉淀自 Elementwise 类算子调优实践。
@@ -0,0 +1,131 @@
1
+ # ----------------------------------------------------------------------------------------------------------
2
+ # Copyright (c) 2026 Huawei Technologies Co., Ltd.
3
+ # This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4
+ # CANN Open Software License Agreement Version 2.0 (the "License").
5
+ # Please refer to the License for details. You may not use this file except in compliance with the License.
6
+ # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7
+ # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8
+ # See LICENSE in the root of the software repository for the full text of the License.
9
+ # ----------------------------------------------------------------------------------------------------------
10
+
11
+ # ============================================================================
12
+ # CMake 构建配置 - Ascend C Kernel 直调
13
+ # ============================================================================
14
+ #
15
+ # 目录结构:
16
+ # op_kernel/ - NPU 计算层(tiling.h + kernel.asc)
17
+ # op_host/ - Host 直调层(.asc 入口 + data_utils.h)
18
+ # op_extension/ - PyTorch 接入层(torch.cpp + register.cpp + ops.h)
19
+ # scripts/ - 测试脚本
20
+ #
21
+ # [MODIFY] 创建新算子时修改:
22
+ # 1. 所有 add_custom → <your_op>(add_custom 是整体算子名,_kernel/_host/_ops 是固定后缀)
23
+ # 2. target_compile_options 中的 --npu-arch
24
+ # ============================================================================
25
+
26
+ cmake_minimum_required(VERSION 3.16)
27
+
28
+ find_package(ASC REQUIRED)
29
+
30
+ # [MODIFY] 工程名
31
+ project(add_custom LANGUAGES ASC CXX)
32
+
33
+ set(CMAKE_CXX_STANDARD 17)
34
+
35
+ set(ACL_INCLUDE_DIR "$ENV{ASCEND_HOME_PATH}/aarch64-linux/include")
36
+ set(ACL_LIB_DIR "$ENV{ASCEND_HOME_PATH}/lib64")
37
+
38
+ # ============================================================================
39
+ # Target 1: 可执行文件(直调验证)
40
+ # ============================================================================
41
+
42
+ # [MODIFY] 目标名和源文件名
43
+ add_executable(add_custom op_host/add_custom.asc)
44
+
45
+ target_link_libraries(add_custom PRIVATE
46
+ tiling_api
47
+ register
48
+ platform
49
+ unified_dlog
50
+ dl
51
+ m
52
+ graph_base
53
+ )
54
+
55
+ target_include_directories(add_custom PRIVATE
56
+ ${CMAKE_CURRENT_SOURCE_DIR}/op_kernel
57
+ ${CMAKE_CURRENT_SOURCE_DIR}/op_host
58
+ )
59
+
60
+ # [CONFIG] NPU 架构配置
61
+ # - dav-2201: Atlas A2 系列(默认)
62
+ # - dav-3510: Atlas A5 系列
63
+ # 更多架构详见 npu-arch skill
64
+ # [MODIFY] 工程名 - add_custom → <your_op>
65
+ target_compile_options(add_custom PRIVATE
66
+ $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201>
67
+ )
68
+
69
+ # ============================================================================
70
+ # Target 2: TORCH_LIBRARY 模块 (libadd_custom_ops.so)
71
+ # Python: torch.ops.load_library("build/libadd_custom_ops.so")
72
+ # torch.ops.npu.add_custom(x1, x2)
73
+ # ============================================================================
74
+
75
+ find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
76
+
77
+ execute_process(
78
+ COMMAND ${Python3_EXECUTABLE} -c "import torch; print(torch.utils.cmake_prefix_path)"
79
+ OUTPUT_STRIP_TRAILING_WHITESPACE
80
+ OUTPUT_VARIABLE TORCH_CMAKE_PREFIX_PATH
81
+ )
82
+ find_package(Torch REQUIRED HINTS ${TORCH_CMAKE_PREFIX_PATH})
83
+
84
+ execute_process(
85
+ COMMAND ${Python3_EXECUTABLE} -c "import os, torch_npu; print(os.path.dirname(torch_npu.__file__))"
86
+ OUTPUT_STRIP_TRAILING_WHITESPACE
87
+ OUTPUT_VARIABLE TORCH_NPU_PATH
88
+ )
89
+ set(TORCH_NPU_INCLUDE_DIRS ${TORCH_NPU_PATH}/include)
90
+ set(TORCH_NPU_LIBRARIES ${TORCH_NPU_PATH}/lib)
91
+
92
+ # [MODIFY] 所有 add_custom → <your_op>(保持 _ops 后缀)
93
+ add_library(add_custom_ops SHARED
94
+ op_kernel/add_custom_kernel.asc
95
+ op_extension/add_custom_torch.cpp
96
+ op_extension/register.cpp
97
+ )
98
+
99
+ target_include_directories(add_custom_ops PRIVATE
100
+ ${CMAKE_CURRENT_SOURCE_DIR}/op_kernel
101
+ ${CMAKE_CURRENT_SOURCE_DIR}/op_extension
102
+ ${TORCH_INCLUDE_DIRS}
103
+ ${TORCH_NPU_INCLUDE_DIRS}
104
+ ${ACL_INCLUDE_DIR}
105
+ )
106
+
107
+ target_link_libraries(add_custom_ops PRIVATE
108
+ torch_npu
109
+ tiling_api
110
+ register
111
+ platform
112
+ unified_dlog
113
+ dl
114
+ m
115
+ graph_base
116
+ ascendcl
117
+ ascendc_runtime
118
+ )
119
+
120
+ target_link_directories(add_custom_ops PRIVATE
121
+ ${ACL_LIB_DIR}
122
+ "$ENV{ASCEND_HOME_PATH}/aarch64-linux/lib64"
123
+ ${TORCH_NPU_LIBRARIES}
124
+ )
125
+
126
+ target_compile_options(add_custom_ops PRIVATE
127
+ ${TORCH_CXX_FLAGS}
128
+ $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201>
129
+ )
130
+
131
+ message(STATUS "TORCH_LIBRARY module 'libadd_custom_ops.so' will be built")
@@ -0,0 +1,55 @@
1
+ # Add Custom Kernel 直调样例
2
+
3
+ add_custom 算子的 Kernel 直调实现示例,同时支持 PyTorch 对接。
4
+
5
+ 详细代码说明见 `op_kernel/add_custom_kernel.asc` 和 `op_kernel/add_custom_tiling.h` 中的注释(搜索 `[MODIFY]` 标记)。
6
+
7
+ ## 文件结构
8
+
9
+ ```
10
+ ├── op_kernel/
11
+ │ ├── add_custom_tiling.h Tiling 常量 + 结构体(kernel 和 host 共用)
12
+ │ └── add_custom_kernel.asc 纯 kernel 代码(KernelAdd 类 + add_custom_kernel 核函数入口)
13
+ ├── op_host/
14
+ │ ├── add_custom.asc Host + main 入口(#include "add_custom_kernel.asc")
15
+ │ └── data_utils.h 数据读写工具
16
+ ├── op_extension/
17
+ │ ├── add_custom_torch.cpp PyTorch host 实现(Tiling 计算 + kernel launch)
18
+ │ ├── register.cpp TORCH_LIBRARY 注册(含 Meta backend)
19
+ │ └── ops.h 函数声明
20
+ ├── CMakeLists.txt 双 target:可执行文件 + libadd_custom_ops.so
21
+ └── scripts/ 数据生成和验证脚本
22
+ ```
23
+
24
+ ## 快速开始
25
+
26
+ ### 方式一:直调验证(可执行文件)
27
+
28
+ ```bash
29
+ source ${ASCEND_HOME_PATH}/set_env.sh
30
+ bash run.sh
31
+ ```
32
+
33
+ 或手动执行:
34
+
35
+ ```bash
36
+ mkdir -p build && cd build && cmake .. && make -j
37
+ cd .. && python3 scripts/gen_data.py
38
+ cd build && ./add_custom
39
+ python3 ../scripts/verify_result.py output/output.bin output/golden.bin
40
+ ```
41
+
42
+ ### 方式二:PyTorch 调用
43
+
44
+ ```python
45
+ import torch
46
+ import torch_npu
47
+
48
+ torch.ops.load_library("build/libadd_custom_ops.so")
49
+
50
+ x1 = torch.randn(8, 2048, dtype=torch.float32).npu()
51
+ x2 = torch.randn(8, 2048, dtype=torch.float32).npu()
52
+ y = torch.ops.npu.add_custom(x1, x2)
53
+
54
+ assert torch.allclose(y.cpu(), x1.cpu() + x2.cpu())
55
+ ```
@@ -0,0 +1,80 @@
1
+ // ----------------------------------------------------------------------------------------------------------
2
+ // Copyright (c) 2026 Huawei Technologies Co., Ltd.
3
+ // This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4
+ // CANN Open Software License Agreement Version 2.0 (the "License").
5
+ // Please refer to the License for details. You may not use this file except in compliance with the License.
6
+ // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7
+ // INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8
+ // See LICENSE in the root of the software repository for the full text of the License.
9
+ // ----------------------------------------------------------------------------------------------------------
10
+
11
+ // ⚠️ Stream 同步反模式(详见 torch-ascendc-op-extension SKILL.md「Stream 同步模式」和 references/anti_patterns.md):
12
+ // ❌ stream(false) + 函数调用 → 乱序:不清 queue,kernel 先于之前操作执行
13
+ // ❌ lambda 内传 NPUStream + OpCommand → 死锁:queue 等 lambda,lambda 等 queue 空
14
+ // ❌ zeros_like 创建输出 → 乱序:zeros_like 入 queue 但 kernel 不入 queue,用 empty_like
15
+ // ✅ stream(true) + 函数调用 → 清 queue,安全(本文件使用此模式)
16
+
17
+ #include <cstdint>
18
+ #include "acl/acl.h"
19
+ #include <torch/extension.h>
20
+ #include "torch_npu/csrc/core/npu/NPUStream.h"
21
+ #include "../op_kernel/add_custom_tiling.h"
22
+
23
+ // [MODIFY] extern 声明 kernel 入口 - add_custom → <your_op>,保持 _kernel 后缀
24
+ // 从 add_custom_kernel.asc 中的核函数签名复制,去掉 __global__ __vector__,加 extern "C"
25
+ extern "C" void add_custom_kernel(
26
+ uint32_t blockDim, void* l2Ctrl, aclrtStream stream, uint8_t* x, uint8_t* y, uint8_t* z, uint8_t* tiling);
27
+
28
+ // [MODIFY] 算子 PyTorch 实现 - add_custom → <your_op>,保持 _torch 后缀
29
+ namespace ascend_kernel {
30
+
31
+ at::Tensor add_custom_torch(const at::Tensor& x1, const at::Tensor& x2)
32
+ {
33
+ TORCH_CHECK(x1.scalar_type() == at::kFloat, "only FP32 supported");
34
+ TORCH_CHECK(x1.scalar_type() == x2.scalar_type(), "x1 and x2 must have the same dtype");
35
+ TORCH_CHECK(x1.is_privateuseone(), "x1 must be on NPU");
36
+ TORCH_CHECK(x2.is_privateuseone(), "x2 must be on NPU");
37
+
38
+ at::Tensor y = at::empty_like(x1);
39
+
40
+ int64_t totalElements = x1.numel();
41
+ TORCH_CHECK(totalElements > 0, "input tensors must not be empty");
42
+
43
+ // stream(true) 在返回 ACL stream 前会清 queue,确保与之前 NPU 操作的正确同步
44
+ // 禁止使用 stream(false):不清 queue + 直接调用 kernel = 乱序风险
45
+ auto aclStream = c10_npu::getCurrentNPUStream().stream(true);
46
+
47
+ // [MODIFY] 查询核数 - 从 main() 中复制的 Tiling 计算逻辑
48
+ int32_t deviceId = -1;
49
+ aclrtGetDevice(&deviceId);
50
+ int64_t availableCoreNum = 0;
51
+ auto ret = aclrtGetDeviceInfo(deviceId, ACL_DEV_ATTR_VECTOR_CORE_NUM, &availableCoreNum);
52
+ TORCH_CHECK(ret == ACL_SUCCESS && availableCoreNum > 0, "failed to get NPU core count");
53
+
54
+ // [MODIFY] Tiling 计算 - 与 main() 中的逻辑一致
55
+ AddTilingData tiling;
56
+ tiling.totalLength = totalElements;
57
+ uint64_t totalTiles = (totalElements + TILE_LENGTH - 1) / TILE_LENGTH;
58
+ uint64_t tilesPerCore = (totalTiles + availableCoreNum - 1) / availableCoreNum;
59
+ tiling.blockNum = (totalTiles + tilesPerCore - 1) / tilesPerCore;
60
+ tiling.numPerCore = tilesPerCore * TILE_LENGTH;
61
+ tiling.tailNumLastCore = totalElements - tiling.numPerCore * (tiling.blockNum - 1);
62
+
63
+ uint32_t blockNum = static_cast<uint32_t>(tiling.blockNum);
64
+
65
+ // Tiling 数据搬到 device
66
+ at::Tensor tilingTensor = at::empty({static_cast<int64_t>(sizeof(AddTilingData))}, x1.options().dtype(at::kByte));
67
+ aclrtMemcpy(
68
+ tilingTensor.mutable_data_ptr(), sizeof(AddTilingData), &tiling, sizeof(AddTilingData),
69
+ ACL_MEMCPY_HOST_TO_DEVICE);
70
+
71
+ // [MODIFY] 调用 kernel - add_custom → <your_op>,保持 _kernel 后缀
72
+ add_custom_kernel(
73
+ blockNum, nullptr, aclStream, reinterpret_cast<uint8_t*>(x1.mutable_data_ptr()),
74
+ reinterpret_cast<uint8_t*>(x2.mutable_data_ptr()), reinterpret_cast<uint8_t*>(y.mutable_data_ptr()),
75
+ reinterpret_cast<uint8_t*>(tilingTensor.mutable_data_ptr()));
76
+
77
+ return y;
78
+ }
79
+
80
+ } // namespace ascend_kernel
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3
+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4
+ * CANN Open Software License Agreement Version 2.0 (the "License").
5
+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7
+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8
+ * See LICENSE in the root of the software repository for the full text of the License.
9
+ */
10
+
11
+ #ifndef OPS_H
12
+ #define OPS_H
13
+
14
+ #include <torch/extension.h>
15
+
16
+ // [MODIFY] 函数声明 - add_custom → <your_op>,保持 _torch 后缀
17
+ namespace ascend_kernel {
18
+
19
+ at::Tensor add_custom_torch(const at::Tensor& x1, const at::Tensor& x2);
20
+
21
+ } // namespace ascend_kernel
22
+
23
+ #endif // OPS_H
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3
+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4
+ * CANN Open Software License Agreement Version 2.0 (the "License").
5
+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7
+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8
+ * See LICENSE in the root of the software repository for the full text of the License.
9
+ */
10
+
11
+ #include <torch/extension.h>
12
+ #include <torch/library.h>
13
+ #include "ops.h"
14
+
15
+ namespace {
16
+
17
+ // [MODIFY] 注册算子签名 - add_custom → <your_op>
18
+ TORCH_LIBRARY_FRAGMENT(npu, m) { m.def("add_custom(Tensor x1, Tensor x2) -> Tensor"); }
19
+
20
+ // [MODIFY] 绑定 NPU 实现 - add_custom → <your_op>,保持 _torch 后缀
21
+ TORCH_LIBRARY_IMPL(npu, PrivateUse1, m) { m.impl("add_custom", TORCH_FN(ascend_kernel::add_custom_torch)); }
22
+
23
+ // [MODIFY] 绑定 Meta 实现(torch.compile / fx 需要)
24
+ // Meta 函数只推导输出 shape/dtype,不执行实际计算。
25
+ // 简单算子可直接返回 at::empty_like;多输出或 shape 变化的算子需按实际逻辑推导。
26
+ at::Tensor add_custom_meta(const at::Tensor& x1, const at::Tensor& x2) { return at::empty_like(x1); }
27
+
28
+ TORCH_LIBRARY_IMPL(npu, Meta, m) { m.impl("add_custom", &add_custom_meta); }
29
+
30
+ } // namespace