kali20gakki/msAgent / skills/profiler/ascend-fusion-op-replace
ascend-fusion-op-replace
Verified针对昇腾 NPU 上的 PyTorch 模型,识别可做融合算子替换的代码段、替换为 torch_npu 融合算子并做性能/精度验证。当用户提到融合算子、NPU 算子替换、RMSNorm/SwiGLU/RoPE/Attention 融合、torch_npu 亲和算子等关键词是,使用本 Skill。
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 10.0 KB |
| assets/validation_report_template.html | other | 22.9 KB |
| references/common_fusion_ops.md | markdown | 14.1 KB |
| references/fusion_value_assessment.md | markdown | 4.5 KB |
| references/identification_report_format.md | markdown | 2.6 KB |
| references/verification_workflow.md | markdown | 11.9 KB |
| scripts/merge_jsonl.py | script | 3.7 KB |
| scripts/query_torch_npu_api.py | script | 21.4 KB |
references/fusion_value_assessment.md
View on GitHub# 融合价值评估
本文件是 SKILL.md §1.2 的展开:如何基于已有 profiling 数据和模型代码判断一个高频算子序列是否值得尝试融合。**有 profiling 数据时读本文件**。
## 输入与边界
输入来自 profiling 的 `kernel_details.csv`,或由 `ascend_pytorch_profiler_{rank_id}.db` 通过外部 SQL 导出的等价数据。
判断时需要的最小信息:
| 信息或字段 | 用途 |
|------|------|
| 算子序列、模块名、代码位置或 trace 关联 | 判断是否是同一段有数据依赖的计算 |
| 序列总耗时、整体 device 耗时占比、调用次数 | 判断收益是否足以摊销改造和验证成本 |
| `*_mac_ratio`、`*_vec_ratio` | 判断主导计算是否接近 Cube 或 Vector 计算饱和 |
| `*_mte2_ratio` | 判断主导瓶颈是否偏数据搬运 |
| `Task Wait Time` | 判断重复的 task 间等待、launch 或同步空隙 |
## 筛选关卡
按顺序通过以下关卡。前一关不满足时,不要继续给出融合收益估计。
### 第一关:语义连续性(必须满足)
候选序列必须同时满足:
- 属于同一个模块、同一次 forward/backward 的同一段高层计算逻辑。
- 相邻算子存在明确的数据依赖。通常前一个算子的输出是后一个算子的输入;必要的 `view`、`reshape`、`cast` 等中间变换可以包含在序列内。
- 能在代码和 trace/profiling 中建立对应关系,而非只因时间上相邻就拼接。
- 不跨独立模块、无关分支、通信、优化器或不相关同步操作。
不满足时,结论写为“不是可证明的连续语义链”,不要把它列为融合候选。
### 第二关:热度与摊销价值(至少满足一项)
先确认这条语义链在整体性能中值得花时间:
- 序列总耗时或其所属模块耗时在整体 device 时间中占比较高。
- 序列调用次数高,并且反复出现多个短小 kernel。
### 第三关:Bound 信号与收益方向
以序列中**耗时占主导的 kernel**为主判断。
| 观测信号 | 判断 | 融合价值倾向 |
|----------|------|--------------|
| 主导 kernel 的 `*_mac_ratio` 或 `*_vec_ratio` 大于约 80% | Compute-bound | 融合通常不减少计算量,优先级低;只有能同时消除明显中间搬运或高频 launch 时再考虑 |
| 主导 kernel 的 `*_mte2_ratio` 大于约 80% | Memory-bound | 优先检查能否避免中间结果落 HBM,融合价值通常较高 |
| 高频短 kernel,且同一序列中 `Task Wait Time` 持续明显 | Launch/Sync-bound | 优先检查能否减少 kernel launch、task 间空隙或不必要同步,融合价值通常中到高 |
| 指标混合、字段缺失,或不同主导 kernel 指向不同瓶颈 | Mixed / evidence insufficient | 只给保守结论;优先查现成融合 API 和仓库实现,不夸大收益 |
注意:
- `Task Wait Time` 是上一个 Task 结束与当前 Task 开始的间隔,不是当前 kernel 的执行耗时;只在它随同一序列重复出现时,才作为 launch/sync 证据。
- 如果等待主要由 HCCL、跨模块依赖或其他通信造成,不应直接归因于模型局部融合机会。
- Compute-bound 不是绝对不能融合,而是“仅靠融合”的端到端收益通常有限,应降低优先级。
## 结论与报告证据
通过三关后,再给出高 / 中 / 低优先级:
| 结论 | 典型条件 |
|------|----------|
| 高 | 语义连续、热度明显,且为 Memory-bound 或重复的 Launch/Sync-bound |
| 中 | 语义和热度成立,但 PMU 信号混合;或存在可复用的官方/仓库融合实现,值得用验证确认 |
| 低 | 主要为 Compute-bound,且没有明显中间搬运或重复 launch 开销 |
| 不列入候选 | 语义连续性或热度不成立,或主要等待来自无关通信/跨模块依赖 |
识别报告为每个基于 profiling 的候选补充以下证据,不需要在本 Skill 内计算复杂评分:
```markdown
- 语义连续性:通过 / 不通过。说明模块、代码位置和数据依赖链。
- 热度:调用次数、序列总耗时、整体 device 占比或相对排名。
- Bound 判断:Memory-bound / Compute-bound / Launch/Sync-bound / Mixed,并列出使用的 PMU 或 Task Wait 证据。
- 融合价值:高 / 中 / 低,以及一句原因。
```
## 后续动作
只有“高”或“中”候选才进入 API 选型:先搜索目标仓库已有融合实现,再用 `scripts/query_torch_npu_api.py` 确认当前环境 API 文档;没有等价官方 API 时,明确标注“需要自定义融合算子”,不要把它当作可直接替换项。
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |