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,135 @@
1
+ # SoftmaxV2 AR Recompute 模板
2
+
3
+ ## 1. 模板用途
4
+
5
+ 针对 2D `[A1, R]` 布局、R 超出 UB 容量的 Softmax 场景。将 R 切分为多个 chunk,通过三阶段重读 GM(求 max → 求 sum → 输出)完成 Softmax,使用 UB 间二分累加树(UpdateCache)高效合并跨 chunk 的局部 sum。
6
+
7
+ ## 2. 输入输出布局和 Softmax 轴
8
+
9
+ - 输入 `x`:`[A1, R]`,行优先,Softmax 沿 R 轴计算。
10
+ - 输出 `y`:`[A1, R]`,与输入同 shape、同 dtype。
11
+
12
+ ## 3. 适用 shape、dtype 和 UB 条件
13
+
14
+ | 条件 | 要求 |
15
+ |------|------|
16
+ | 输入维数 | 2D,A0 = 1 |
17
+ | R 大小 | R 超 UB 单次载入容量 |
18
+ | dtype | FP32、FP16、BF16 |
19
+ | UB 约束 | 预留 max(32B) + sum(32B) + cache(2048B) 后,三缓冲 xQueue + 双缓冲 yQueue + tmpBuffer |
20
+
21
+ ## 4. 类名、构造函数和 Init 接口
22
+
23
+ ```cpp
24
+ namespace SoftmaxV2Ops;
25
+
26
+ template <typename Tx, typename Ty>
27
+ class SoftmaxV2ArRecompute : public SoftmaxV2OpsBase {
28
+ public:
29
+ __aicore__ inline SoftmaxV2ArRecompute(TPipe* pipe);
30
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const SoftmaxV2ArRecomputeTilingData* tilingData);
31
+ __aicore__ inline void Process();
32
+ };
33
+ ```
34
+
35
+ - **构造函数**:接收 `TPipe*`。
36
+ - **Init**:计算每核行数 `currentRowBlock_`、`resultCacheID_`,初始化 xQueue(三缓冲)、yQueue(双缓冲)及 xTmp/xMax/xSum/cache buffer。
37
+ - **Process**:逐行执行三阶段计算。
38
+
39
+ ## 5. 对应的 TilingData 和字段说明
40
+
41
+ 对应 `SoftmaxV2ArRecomputeTilingData`,定义在 [softmax_v2_tiling_data.h](softmax_v2_tiling_data.template)。
42
+
43
+ | 字段 | 含义 | 单位 |
44
+ |------|------|------|
45
+ | a | A1 维总长度(行数) | 元素数 |
46
+ | r | R 维实际长度 | 元素数 |
47
+ | ubFactor | R 方向单 chunk 长度 | 元素数 |
48
+ | ubFactorTail | R 方向尾块长度 | 元素数 |
49
+ | aBlockFactor | 每核处理行数 | 行 |
50
+ | aLoopCountCeil | R 方向 chunk 总数 | chunk |
51
+ | basicBlockLoop | 二分树基础块数(≤ aLoopCountCeil 的最大 2^k) | 块 |
52
+ | mainFoldCount | 需折叠块数 = FloorDiv(R,ubFactor) - basicBlockLoop | 块 |
53
+
54
+ ## 6. Host tiling 参数计算方法
55
+
56
+ 使用 [softmax_v2_tiling.h](softmax_v2_tiling.template) 中的 `TilingArRecompute`:
57
+
58
+ ```cpp
59
+ softmax_tiling::CaseShape shape{a1, r, 1, dtypeCode};
60
+ softmax_tiling::PlatformParam plat{ubSize, numBlocks};
61
+ SoftmaxV2ArRecomputeTilingData td;
62
+ int64_t blockDim = softmax_tiling::TilingArRecompute(shape, plat, td);
63
+ ```
64
+
65
+ 关键计算:
66
+ - `ubFactor` = min(R, (ubFlexible / baseFactor) 按 lcm 对齐),其中 baseFactor = dtypeSize×3 + dtypeSize×2 + 4
67
+ - `aLoopCountCeil` = CeilDiv(R, ubFactor)
68
+ - `basicBlockLoop` = FindNearestPower2(aLoopCountCeil)
69
+ - `mainFoldCount` = FloorDiv(R, ubFactor) - basicBlockLoop
70
+
71
+ ## 7. CopyIn、归约、归一化和 CopyOut 流程
72
+
73
+ 三阶段处理,每行独立执行:
74
+
75
+ ```
76
+ Process(逐行 rowIdx):
77
+ Step 1 — 求 max:
78
+ for 每个 R chunk:
79
+ CopyIn chunk → CalculateMaxVF(VF 求 max,与 running max 合并)
80
+
81
+ Step 2 — 求 sum(二分累加树):
82
+ for basicBlockIdx in [0, basicBlockLoop):
83
+ a. CopyIn 主块 → MainBlockCastSubExpVF(sub max, exp, 写入 xTmp)
84
+ b. 若需折叠: CopyIn 折叠块 → FoldBlockCastSubExpVF(sub max, exp, Add 到 xTmp)
85
+ c. ReduceSum(xTmp) → UpdateCache(按 cacheID 层级合并)
86
+ basicBlockLoop == 0 时: 单 chunk 直接 ReduceSum
87
+
88
+ Step 3 — 输出:
89
+ for 每个 R chunk:
90
+ CopyIn chunk → CalculateOutVF(sub max, exp, div sum, Cast 回原精度)→ CopyOut GM
91
+ ```
92
+
93
+ ## 8. 重计算与二分累加树优化
94
+
95
+ - **三阶段重读**:输入读 3 次(max/sum/output),换取 UB 空间——R 可远超 UB 容量。
96
+ - **MainBlock / FoldBlock 配对**:主块和折叠块的 exp 结果在 xTmp 上 `Add` 合并后一次 `ReduceSum`,归约次数减半。
97
+ - **UpdateCache 二分树**:跨 chunk 的局部 sum 按 `GetCacheID(basicBlockIdx)` 层级二分合并,O(log N) 层而非 O(N) 串行。
98
+ - `GetCacheID(idx)` = `popcount(idx ^ (idx+1)) - 1`,确定当前块在二分树中的位置。
99
+ - `resultCacheID` = `GetCacheID(basicBlockLoop - 1)`,指向最终合并结果。
100
+ - **三缓冲 xQueue**:支持 Main/Fold 双 chunk 并行载入。
101
+
102
+ ## 9. 主块、尾块、对齐和数值精度处理
103
+
104
+ - **主块/尾块**:`ubFactor` 为主 chunk 长度,`ubFactorTail` 为尾 chunk(R 不能整除时)。尾块在 Step 1/3 中通过条件判断使用 `ubFactorTail`。
105
+ - **折叠尾块**:当 `basicBlockIdx == mainFoldCount` 且 `ubFactorTail > 0` 时,折叠块使用 `ubFactorTail` 长度。
106
+ - **对齐**:`ubFactor` 按 `Lcm(blockSize, Lcm(blockSize/dtypeSize, blockSize/dtypeSize))` 对齐。
107
+ - **数值精度**:FP16/BF16 通过 `LoadTensorForDtypeTIn` 升至 FP32 计算;max/sum 用 FP32 维护;输出通过 `Cast` 降回。`xToFp32_` / `yToFp32_` 编译期常量控制转换路径。
108
+
109
+ ## 10. 独立 Kernel 入口示例
110
+
111
+ ```cpp
112
+ #include "softmax_v2_tiling_data.h"
113
+ #include "dav310/softmax_v2_base.h"
114
+ #include "dav310/softmax_v2_ar_recompute.h"
115
+
116
+ using namespace SoftmaxV2Ops;
117
+
118
+ extern "C" __global__ __aicore__ __vector__ void
119
+ softmax_ar_recompute_fp32(GM_ADDR x, GM_ADDR y, SoftmaxV2ArRecomputeTilingData tiling)
120
+ {
121
+ TPipe pipe;
122
+ SoftmaxV2ArRecompute<float, float> op(&pipe);
123
+ op.Init(x, y, &tiling);
124
+ op.Process();
125
+ }
126
+ ```
127
+
128
+ ## 11. 不适用场景和可选替代模板
129
+
130
+ | 不适用场景 | 推荐替代 |
131
+ |-----------|---------|
132
+ | R 可载入 UB | [AR FullLoad](softmax_v2_ar_full_load.md)(无重读,带宽更优) |
133
+ | R 极小(≤ 16/32) | [AR SmallR](softmax_v2_ar_small_r.md)(转置向量化) |
134
+ | 3D [A1,R,A0] 布局(A0>1) | [ARA Recompute](softmax_v2_ara_recompute.md) |
135
+ | 带宽极度受限、R 适中 | 评估 FullLoad 是否可用(3× 重读代价高) |
@@ -0,0 +1,122 @@
1
+ # SoftmaxV2 AR SmallR 模板
2
+
3
+ ## 1. 模板用途
4
+
5
+ 针对 2D `[A1, R]` 布局、归约轴 R 极小(FP32 ≤ 16 / FP16/BF16 ≤ 32)的 Softmax 场景。通过将数据转置为 `(R, A1)` 布局,使短归约轴转到外层、长伴生轴转为向量化方向,将 VReg 利用率从 R/64 提升到接近 100%。
6
+
7
+ ## 2. 输入输出布局和 Softmax 轴
8
+
9
+ - 输入 `x`:`[A1, R]`,行优先,Softmax 沿 R 轴(最后一轴)计算。
10
+ - 输出 `y`:`[A1, R]`,与输入同 shape、同 dtype。
11
+ - 内部转置为 `(R, A1)` 布局进行 VF 计算,输出前转置回 `(A1, R)`。
12
+
13
+ ## 3. 适用 shape、dtype 和 UB 条件
14
+
15
+ | 条件 | 要求 |
16
+ |------|------|
17
+ | 输入维数 | 2D,A0 = 1 |
18
+ | R 大小 | FP32 ≤ 16,FP16/BF16 ≤ 32(R ≤ 2×VL_FP32 = 128 时可转置向量化) |
19
+ | dtype | FP32、FP16、BF16 |
20
+ | UB 约束 | `tileA0Len * rAligned * (dtypeSize*2 + 4) * 2 + tileA0Len * 4` ≤ UB |
21
+
22
+ ## 4. 类名、构造函数和 Init 接口
23
+
24
+ ```cpp
25
+ namespace SoftmaxV2Ops;
26
+
27
+ template <typename Tx, typename Ty>
28
+ class SoftmaxV2ArSmallR {
29
+ public:
30
+ __aicore__ inline SoftmaxV2ArSmallR(TPipe* pipe);
31
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const SoftmaxV2ArSmallRTilingData* tilingDataIn);
32
+ __aicore__ inline void Process();
33
+ };
34
+ ```
35
+
36
+ - **构造函数**:接收 `TPipe*`,保存 pipe 指针。
37
+ - **Init**:接收输入 x、输出 y 的 GM 地址和 TilingData 指针,初始化 GlobalTensor 和 UB Buffer。
38
+ - **Process**:执行完整 Softmax 计算。
39
+
40
+ ## 5. 对应的 TilingData 和字段说明
41
+
42
+ 对应 `SoftmaxV2ArSmallRTilingData`,定义在 [softmax_v2_tiling_data.h](softmax_v2_tiling_data.template)。
43
+
44
+ | 字段 | 含义 | 单位 |
45
+ |------|------|------|
46
+ | totalA0Len | 转置后 A0 维长度(= 原 A1) | 元素数 |
47
+ | totalRLen | 归约轴 R 实际长度 | 元素数 |
48
+ | totalTiles | A0 方向总 tile 数 | tile |
49
+ | tilesPerCore | 每核 tile 数 | tile |
50
+ | tileA0Len | 主块 A0 tile 长度 | 元素数 |
51
+ | tileA0Tail | 尾块 A0 长度 | 元素数 |
52
+ | rTileBase | R 转置基础块(FP32=8/FP16=16) | 元素数 |
53
+ | rAligned | R 对齐后长度 | 元素数 |
54
+
55
+ ## 6. Host tiling 参数计算方法
56
+
57
+ 使用 [softmax_v2_tiling.h](softmax_v2_tiling.template) 中的 `TilingArSmallR`:
58
+
59
+ ```cpp
60
+ softmax_tiling::CaseShape shape{a1, r, 1, dtypeCode};
61
+ softmax_tiling::PlatformParam plat{ubSize, numBlocks};
62
+ SoftmaxV2ArSmallRTilingData td;
63
+ int64_t blockDim = softmax_tiling::TilingArSmallR(shape, plat, td);
64
+ ```
65
+
66
+ 关键计算:
67
+ - `rTileBase` = FP32 ? 8 : 16
68
+ - `rAligned` = CeilDiv(R, rTileBase) × rTileBase
69
+ - `tileA0Len` = a0TileNum × VL_FP32,受 UB 容量约束
70
+ - `totalTiles` = CeilDiv(A1, tileA0Len)
71
+
72
+ ## 7. CopyIn、归约、归一化和 CopyOut 流程
73
+
74
+ ```
75
+ Process:
76
+ 1. preload 第一个 tile,CopyInAndTransPose(MultiCopy 转置 (A1,R)→(R,A1))
77
+ 2. for 每个 tile:
78
+ a. CalcMaxSubExp: 逐 A0 列求 max,sub max,exp,写入 tmpLocal 和 tmpLocal2
79
+ b. CopyInAndTransPose: 预载下一个 tile(双缓冲)
80
+ c. CalcReduceSum: 沿 R 轴 ReduceSum(Pattern::Reduce::RA)
81
+ d. CalcOutput: 逐列 div sum,FP16/BF16 做 Cast
82
+ e. CalcTranspose: TransDataTo5HD 转置回 (A1,R)
83
+ f. CopyOutY: DataCopyPad 写回 GM
84
+ ```
85
+
86
+ ## 8. 转置优化
87
+
88
+ - **MultiCopy 转置**:使用 `MultiCopyParams<Tx, 2>` + `MultiCopyLoopInfo<2>` 在 CopyIn 阶段直接完成 `(A1,R)→(R,A1)` 转置,避免额外转置 pass。
89
+ - **TransDataTo5HD**:输出前使用 `TransDataTo5HD` 将 `(R,A1)` 转回 `(A1,R)`,FP32 和 FP16/BF16 分别使用不同的 block 参数。
90
+ - **VF 向量化**:转置后长轴 A1 沿 VReg 方向,每个 VReg 可处理 64 个 FP32 元素,利用率接近 100%。
91
+
92
+ ## 9. 主块、尾块、对齐和数值精度处理
93
+
94
+ - **主块/尾块**:`tileA0Len` 为主块长度,`tileA0Tail` 为最后一个 tile 的实际长度。尾块通过 `UpdateMask` 精确掩码处理。
95
+ - **对齐**:R 按 `rTileBase`(FP32=8/FP16=16)向上对齐,A0 按 `VL_FP32`(64)对齐。
96
+ - **数值精度**:FP16/BF16 输入在计算前 `Cast` 升至 FP32,计算完成后 `Cast` 降回原精度。使用 `castTraitFp16ToFp32` / `castTraitFp32ToFp16` 控制饱和模式和舍入。
97
+
98
+ ## 10. 独立 Kernel 入口示例
99
+
100
+ ```cpp
101
+ #include "softmax_v2_tiling_data.h"
102
+ #include "dav310/softmax_v2_base.h"
103
+ #include "dav310/softmax_v2_ar_small_r.h"
104
+
105
+ using namespace SoftmaxV2Ops;
106
+
107
+ extern "C" __global__ __aicore__ __vector__ void
108
+ softmax_ar_small_r_fp32(GM_ADDR x, GM_ADDR y, SoftmaxV2ArSmallRTilingData tiling)
109
+ {
110
+ TPipe pipe;
111
+ SoftmaxV2ArSmallR<float, float> op(&pipe);
112
+ op.Init(x, y, &tiling);
113
+ op.Process();
114
+ }
115
+ ```
116
+
117
+ ## 11. 不适用场景和可选替代模板
118
+
119
+ | 不适用场景 | 推荐替代 |
120
+ |-----------|---------|
121
+ | R > 2×VL_FP32(128) | [AR FullLoad](softmax_v2_ar_full_load.md)(R 可载入 UB)或 [AR Recompute](softmax_v2_ar_recompute.md)(R 超 UB) |
122
+ | 3D [A1,R,A0] 布局(A0>1) | [ARA FullLoad](softmax_v2_ara_full_load.md) 或 [ARA Recompute](softmax_v2_ara_recompute.md) |
@@ -0,0 +1,144 @@
1
+ # SoftmaxV2 ARA FullLoad 模板
2
+
3
+ ## 1. 模板用途
4
+
5
+ 针对 3D `[A1, R, A0]` 布局、R×A0_tile 可载入 UB 的 Softmax 场景。沿 A0 切 tile,跨 R 逐列 VF 计算,根据 R 大小分支选择不同的归约路径(R≤2、R≤4、R≤8 直接累加;R>8 使用 BinaryAddVF 二分折叠)。
6
+
7
+ ## 2. 输入输出布局和 Softmax 轴
8
+
9
+ - 输入 `x`:`[A1, R, A0]`,行优先,Softmax 沿 R 轴计算。
10
+ - 输出 `y`:`[A1, R, A0]`,与输入同 shape、同 dtype。
11
+ - 数据在 UB 中布局为 `(R, tileA0Len)`,沿 R 逐行处理,A0 方向向量化。
12
+
13
+ ## 3. 适用 shape、dtype 和 UB 条件
14
+
15
+ | 条件 | 要求 |
16
+ |------|------|
17
+ | 输入维数 | 3D,A0 > 1 |
18
+ | R 大小 | R×tileA0Len 可载入 UB |
19
+ | dtype | FP32、FP16、BF16 |
20
+ | UB 约束 | `tileA0Len × (R+1) × (dtypeSize×2 + 4) + tileA0Len × 4` ≤ UB |
21
+
22
+ ## 4. 类名、构造函数和 Init 接口
23
+
24
+ ```cpp
25
+ namespace SoftmaxV2Ops;
26
+
27
+ template <typename T1, typename T2>
28
+ class SoftmaxV2ARA {
29
+ public:
30
+ __aicore__ inline SoftmaxV2ARA();
31
+ __aicore__ inline SoftmaxV2ARA(const SoftmaxV2ARATilingData* tilingDataIn);
32
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, TPipe* pipeIn);
33
+ __aicore__ inline void Process();
34
+ };
35
+ ```
36
+
37
+ - **构造函数**:无参默认构造,或接收 TilingData 指针。
38
+ - **Init**:接收 x/y GM 地址和 `TPipe*`,初始化 GlobalTensor 和 UB Buffer(xQueue/yQueue 双缓冲 + xTmpBuf + xReduceBuf)。
39
+ - **Process**:按 tile 遍历所有 (A1, A0) 组合,逐 tile 计算 Softmax。
40
+
41
+ > **注意**:ARA 系模板的构造/Init 方式与 AR 系不同——ARA 在 Init 中接收 `TPipe*`,而 AR 在构造函数中接收。
42
+
43
+ ## 5. 对应的 TilingData 和字段说明
44
+
45
+ 对应 `SoftmaxV2ARATilingData`,定义在 [softmax_v2_tiling_data.h](softmax_v2_tiling_data.template)。
46
+
47
+ 主要字段:
48
+
49
+ | 字段 | 含义 | 单位 |
50
+ |------|------|------|
51
+ | totalA1Len / totalRLen / totalA0Len | 各维总长度 | 元素数 |
52
+ | totalTiles | 总 tile 数 = A1 × a0Outer | tile |
53
+ | tilesPerCore | 每核 tile 数 | tile |
54
+ | usedCoreNums | 实际使用核数 | 核 |
55
+ | a0Outer | A0 方向 tile 总数 | tile |
56
+ | tileA0Len / tileA0Tail | 主块/尾块 A0 tile 长度 | 元素数 |
57
+ | binaryAddK | BinaryAdd 外层折叠层数 | 层 |
58
+ | binaryAddLast | 最后是否需额外 2→1 折叠 | 标志 |
59
+ | binaryAddInnerLoop | BinaryAdd 每层内循环次数 | 次 |
60
+ | remainderLoopCount | R 对 8 取余后的折叠循环数 | 次 |
61
+ | quotientLoopCount | R 整除 8 后的剩余折叠循环数 | 次 |
62
+ | remainderTailOffset0~7 | 余数尾块各行偏移(越界指向 validNumInXUb 做零填充) | 元素数 |
63
+
64
+ ## 6. Host tiling 参数计算方法
65
+
66
+ 使用 [softmax_v2_tiling.h](softmax_v2_tiling.template) 中的 `TilingAraFullLoad`:
67
+
68
+ ```cpp
69
+ softmax_tiling::CaseShape shape{a1, r, a0, dtypeCode};
70
+ softmax_tiling::PlatformParam plat{ubSize, numBlocks};
71
+ SoftmaxV2ARATilingData td;
72
+ int64_t blockDim = softmax_tiling::TilingAraFullLoad(shape, plat, td);
73
+ ```
74
+
75
+ 关键计算:
76
+ - `a0TileBase` = FP32 ? 64 : 128
77
+ - `tileA0Len` = a0Inner × a0TileBase,受 UB 容量和核数约束
78
+ - 当 R ≤ 8 时,binaryAdd* 字段不使用
79
+ - 当 R > 8 时,`binaryAddQuotient` = 最大 2^k ≤ R-1,按 8 行分组做 BinaryAddVF
80
+
81
+ ## 7. CopyIn、归约、归一化和 CopyOut 流程
82
+
83
+ ```
84
+ Process:
85
+ for 每个 tile (curA1Idx, curA0Idx):
86
+ 1. CopyInX: DataCopyPad 批量载入 (curTileRLen × curTileA0Len) 块
87
+ 2. Compute:
88
+ a. VFShiftVector: 逐 A0 列求 max → sub max → exp → 写入 xTmpLocal
89
+ b. VFReduceSum: 按 R 大小分支求 sum
90
+ c. VFCalculateOutput: 逐列 div sum → Cast 回原精度
91
+ 3. CopyOutY: DataCopyPad 写回 GM
92
+ ```
93
+
94
+ ## 8. VF、BinaryAddVF 优化
95
+
96
+ ### R 大小分支归约
97
+
98
+ | R 范围 | 归约路径 | 说明 |
99
+ |--------|---------|------|
100
+ | R ≤ 2 | `SumRLessThan2` | 逐行 VF `Add` 累加 |
101
+ | R ≤ 4 | `SumRLessThan4` | 2 行配对 + 尾行处理(`TwoRowAddWithTail`) |
102
+ | R ≤ 8 | `SumRLessThan8` | 4 行配对 + 尾行处理 |
103
+ | R > 8 | `SumRMoreThan8` + `BinaryAddVF` | 8 行分组 + 二分折叠 |
104
+
105
+ ### BinaryAddVF 二分折叠(R > 8)
106
+
107
+ 1. **行分组**:将 R 行按 8 行一组分组,每组内用 `TwoRowAdd` 做 4→2→1 折叠。
108
+ 2. **余数处理**:不满 8 行的余数部分,用 `remainderTailOffset0~7` 精确控制每行偏移,越界行指向 `validNumInXUb`(零填充区域)。
109
+ 3. **二分树**:`BinaryAddVF` 按 `binaryAddK` 层 × `binaryAddInnerLoop` 次,每次 4→1 折叠,最后若 `binaryAddLast=1` 额外做 2→1。
110
+ 4. **TwoRowAddWithTail**:通用工具函数,处理 2 行配对 + 2 个尾行偏移。
111
+
112
+ ## 9. 主块、尾块、对齐和数值精度处理
113
+
114
+ - **主块/尾块**:`tileA0Len` 为主块 A0 长度,`tileA0Tail` 为最后一个 A0 tile 的实际长度。尾块通过 `copyInParams.dstStride` 和 `copyInParams.srcStride` 处理非对齐。
115
+ - **对齐**:A0 按 `a0TileBase`(FP32=64/FP16=128)对齐,`dstStride` 按 `BLOCK_SIZE`(32B)对齐。
116
+ - **零填充**:R > 8 时,在 `xTmpLocal[validNumInXUb]` 位置写入零值,余数尾块越界行指向该区域,等效于零填充。
117
+ - **数值精度**:FP16/BF16 通过 `LoadTensorForDtypeT1`(`DIST_UNPACK_B16` + `Cast`)升至 FP32;输出通过 `Cast` + `DIST_PACK_B32` 降回。
118
+
119
+ ## 10. 独立 Kernel 入口示例
120
+
121
+ ```cpp
122
+ #include "softmax_v2_tiling_data.h"
123
+ #include "dav310/softmax_v2_base.h"
124
+ #include "dav310/softmax_v2_ara_full_load.h"
125
+
126
+ using namespace SoftmaxV2Ops;
127
+
128
+ extern "C" __global__ __aicore__ __vector__ void
129
+ softmax_ara_full_load_fp32(GM_ADDR x, GM_ADDR y, SoftmaxV2ARATilingData tiling)
130
+ {
131
+ TPipe pipe;
132
+ SoftmaxV2ARA<float, float> op(&tiling);
133
+ op.Init(x, y, &pipe);
134
+ op.Process();
135
+ }
136
+ ```
137
+
138
+ ## 11. 不适用场景和可选替代模板
139
+
140
+ | 不适用场景 | 推荐替代 |
141
+ |-----------|---------|
142
+ | R×tileA0 超 UB | [ARA Recompute](softmax_v2_ara_recompute.md)(R 切 bin 三阶段重读) |
143
+ | A0 = 1(2D) | [AR FullLoad](softmax_v2_ar_full_load.md) 或 [AR SmallR](softmax_v2_ar_small_r.md) |
144
+ | 需要在线更新(避免 3 次读入) | [ARA Online](softmax_v2_ara_online.md)(2 次读入) |
@@ -0,0 +1,185 @@
1
+ # SoftmaxV2 ARA Online Softmax 模板
2
+
3
+ ## 1. 模板用途
4
+
5
+ 针对 3D `[A1, R, A0]` 布局的独立 Softmax 场景,使用在线(online)算法沿 R 分 chunk 逐块搬入,为每个 A0 列维护 running max 和 running sum。相比 ARA Recompute(输入读 3 次:max/sum/output),Online Softmax 将 max 与 sum 融合为单遍在线更新,输入只需读 2 次(1 遍在线 max+sum,1 遍输出),减少一次 R 全量搬入。
6
+
7
+ > **重要**:本模板是**独立的 Online Softmax**,不包含 `QK^T`、`P×V`、`O_acc`、causal mask 或 FlashAttention 的 Cube/Vector 融合。它与 [融合 Attention Online Softmax 设计](fused_attention_online_softmax_design.md) 只共享在线 max/sum 数学基础,不是同一个实现。
8
+
9
+ ## 2. 输入输出布局和 Softmax 轴
10
+
11
+ - 输入 `x`:`[A1, R, A0]`,行优先,Softmax 沿 R 轴计算。
12
+ - 输出 `y`:`[A1, R, A0]`,与输入同 shape、同 dtype。
13
+ - 输入为已经生成的 Softmax 数据,输出为完整 Softmax 概率。
14
+
15
+ ## 3. 适用 shape、dtype 和 UB 条件
16
+
17
+ | 条件 | 要求 |
18
+ |------|------|
19
+ | 输入维数 | 3D,A0 > 1 |
20
+ | R 大小 | 任意(R 分 chunk 处理,不受 UB 限制) |
21
+ | dtype | FP32、FP16、BF16 |
22
+ | UB 约束 | 双缓冲 xQueue/yQueue + xMaxBuf + xSumBuf,按 rChunkFactor 分 chunk |
23
+
24
+ ## 4. 类名、构造函数和 Init 接口
25
+
26
+ ```cpp
27
+ namespace SoftmaxV2Ops;
28
+
29
+ template <typename T1, typename T2>
30
+ class SoftmaxV2ARAOnline : public SoftmaxV2OpsBase {
31
+ public:
32
+ __aicore__ inline SoftmaxV2ARAOnline();
33
+ __aicore__ inline SoftmaxV2ARAOnline(const SoftmaxV2ARAOnlineTilingData* tilingDataIn);
34
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, TPipe* pipeIn);
35
+ __aicore__ inline void Process();
36
+ };
37
+ ```
38
+
39
+ - **构造函数**:无参默认,或接收 TilingData 指针。
40
+ - **Init**:初始化 GM/UB buffer,xQueue/yQueue(双缓冲,大小 = `tileA0Len × rChunkFactor`)+ xMaxBuf/xSumBuf。
41
+ - **Process**:按 tile 遍历,每 tile 执行在线 max+sum 和输出两遍。
42
+
43
+ ## 5. 对应的 TilingData 和字段说明
44
+
45
+ 对应 `SoftmaxV2ARAOnlineTilingData`,定义在 [softmax_v2_tiling_data.h](softmax_v2_tiling_data.template)。
46
+
47
+ | 字段 | 含义 | 单位 |
48
+ |------|------|------|
49
+ | totalRLen / totalA0Len | R/A0 维总长度 | 元素数 |
50
+ | totalTiles | 总 tile 数 = A1 × tileA0Outer | tile |
51
+ | tilesPerCore | 每核 tile 数 | tile |
52
+ | usedCoreNums | 实际使用核数 | 核 |
53
+ | tileA0Outer | A0 方向 tile 总数 | tile |
54
+ | tileA0Len / tileA0Tail | 主块/尾块 A0 长度 | 元素数 |
55
+ | rChunkFactor | R 方向单 chunk 长度(默认 128) | 元素数 |
56
+ | rChunkTotalLoop | R 方向 chunk 总数 | chunk |
57
+ | rChunkTail | R 方向尾 chunk 长度 | 元素数 |
58
+
59
+ ## 6. Host tiling 参数计算方法
60
+
61
+ 使用 [softmax_v2_tiling.h](softmax_v2_tiling.template) 中的 `TilingAraOnline`:
62
+
63
+ ```cpp
64
+ softmax_tiling::CaseShape shape{a1, r, a0, dtypeCode};
65
+ softmax_tiling::PlatformParam plat{ubSize, numBlocks};
66
+ SoftmaxV2ARAOnlineTilingData td;
67
+ int64_t blockDim = softmax_tiling::TilingAraOnline(shape, plat, td);
68
+ ```
69
+
70
+ 关键计算:
71
+ - `rChunkFactor` = min(128, R)
72
+ - `rChunkTotalLoop` = CeilDiv(R, rChunkFactor)
73
+ - `rChunkTail` = R - (rChunkTotalLoop - 1) × rChunkFactor
74
+ - UB 占用(每个 A0 列):`2 × rChunkFactor × dtypeSize + 2 × rChunkFactor × 4 + 4 + 4` 字节
75
+
76
+ ## 7. 在线更新公式
77
+
78
+ 沿 R 分 chunk,按 A0 列独立维护 running max (`m`) 和 running sum (`l`):
79
+
80
+ ```text
81
+ m_new = max(m_old, chunk_max)
82
+ corr = exp(m_old - m_new)
83
+ l_new = l_old * corr + sum(exp(x - m_new))
84
+ ```
85
+
86
+ 最终 Softmax 输出:`softmax(x) = exp(x - m_final) / l_final`
87
+
88
+ ### 与 ARA Recompute 三次读取输入的区别
89
+
90
+ | 特性 | ARA Recompute | ARA Online |
91
+ |------|--------------|------------|
92
+ | 输入读取次数 | 3 次(max/sum/output) | 2 次(在线 max+sum / output) |
93
+ | max 与 sum 计算 | 分离(先 max 全量,再 sum 全量) | 融合(单遍在线更新) |
94
+ | cache buffer | 需要(二分累加树) | 不需要 |
95
+ | 数值稳定性 | Safe Softmax(先减 max) | Safe Softmax(在线减 running max) |
96
+ | 带宽 | 3× R 全量读 | 2× R 全量读 |
97
+
98
+ ## 8. CopyIn、归约、归一化和 CopyOut 流程
99
+
100
+ ```
101
+ Process(逐 tile):
102
+ 第一遍 — CalcOnlineMaxSum:
103
+ 初始化 running max = -inf, running sum = 0
104
+ for 每个 R chunk:
105
+ CopyInX → 逐 A0 列:
106
+ 1. 求 chunk 内局部 max (mLocal)
107
+ 2. m_new = max(m_old, mLocal)
108
+ 3. corr = exp(m_old - m_new)
109
+ 4. chunk_sum = Σ exp(x - m_new)
110
+ 5. l_new = l_old * corr + chunk_sum
111
+ 6. 更新 xMaxLocal = m_new, xSumLocal = l_new
112
+
113
+ 第二遍 — 输出:
114
+ for 每个 R chunk:
115
+ CopyInX → CalcOutput:
116
+ sub max → exp → div sum → Cast 回原精度
117
+ CopyOutY → GM
118
+ ```
119
+
120
+ ## 9. 主块、尾块、FP32 running 状态和数值稳定性
121
+
122
+ - **主块/尾块**:`tileA0Len` 为主块 A0 长度,`tileA0Tail` 为尾块。`rChunkTail` 为 R 方向尾 chunk 长度。
123
+ - **对齐**:A0 按 `a0TileBase`(FP32=8/FP16=16)对齐。
124
+ - **FP32 running 状态**:running max 和 running sum 始终用 FP32 维护(`xMaxBuf`/`xSumBuf` 为 `float` 类型),即使输入/输出为 FP16/BF16,确保数值稳定性。
125
+ - **数值稳定性**:
126
+ - 每步减去 running max `m_new`,避免 exp 溢出。
127
+ - `corr = exp(m_old - m_new)` ≤ 1(因 m_new ≥ m_old),修正历史 sum。
128
+ - 第一遍在线计算最终 max 和 sum,第二遍用最终值做归一化。
129
+
130
+ ## 10. 独立 Kernel 入口示例
131
+
132
+ ```cpp
133
+ #include "softmax_v2_tiling_data.h"
134
+ #include "dav310/softmax_v2_base.h"
135
+ #include "dav310/softmax_v2_ara_online.h"
136
+
137
+ using namespace SoftmaxV2Ops;
138
+
139
+ extern "C" __global__ __aicore__ __vector__ void
140
+ softmax_ara_online_fp32(GM_ADDR x, GM_ADDR y, SoftmaxV2ARAOnlineTilingData tiling)
141
+ {
142
+ TPipe pipe;
143
+ SoftmaxV2ARAOnline<float, float> op(&tiling);
144
+ op.Init(x, y, &pipe);
145
+ op.Process();
146
+ }
147
+ ```
148
+
149
+ ### Host tiling 示例
150
+
151
+ ```cpp
152
+ #include "softmax_v2_tiling.h"
153
+
154
+ softmax_tiling::PlatformParam plat;
155
+ plat.ubSize = ubSize; // 运行时查询
156
+ plat.numBlocks = numBlocks; // 运行时查询
157
+
158
+ softmax_tiling::CaseShape shape;
159
+ shape.a1 = 4; shape.r = 2049; shape.a0 = 256; shape.dtypeCode = 0; // FP32
160
+
161
+ SoftmaxV2ARAOnlineTilingData td;
162
+ int64_t blockDim = softmax_tiling::TilingAraOnline(shape, plat, td);
163
+ // blockDim 为 <<<>>> 启动核数
164
+ ```
165
+
166
+ ## 11. 本模板不包含的内容
167
+
168
+ 本模板是**独立的 Online Softmax**,不包含:
169
+
170
+ - `QK^T`(score 计算)
171
+ - `P×V`(概率与 Value 的乘积)
172
+ - `O_acc`(输出累加器)
173
+ - causal mask(因果掩码)
174
+ - Attention workspace
175
+ - FlashAttention 的 Cube/Vector 融合
176
+
177
+ 如需了解融合 Attention 中的 Online Softmax 设计,参见 [融合 Attention Online Softmax 设计](fused_attention_online_softmax_design.md)。两者只共享在线 max/sum 数学基础,不是同一个实现。
178
+
179
+ ## 12. 不适用场景和可选替代模板
180
+
181
+ | 不适用场景 | 推荐替代 |
182
+ |-----------|---------|
183
+ | A0 = 1(2D) | [AR Recompute](softmax_v2_ar_recompute.md) |
184
+ | R×tileA0 可载入 UB 且带宽充裕 | [ARA FullLoad](softmax_v2_ara_full_load.md)(单次载入) |
185
+ | R 超 UB 且不需要在线更新 | [ARA Recompute](softmax_v2_ara_recompute.md)(三阶段重读) |