基于 PyTorch 框架的昇腾 NPU 模型推理 SuperKernel 适配技能。当用户需要启用 SuperKernel 算子二进制融合技术优化昇腾 NPU 推理性能时使用此技能。触发场景包括:用户询问 SuperKernel、算子融合、二进制融合、启用 superkernel、superkernel_scope、减少任务调度开销、优化 decode 性能等。SuperKernel 仅支持 ge_graph 模式、Atlas A3 硬件,且仅在 decode 阶段生效。
Ships in model-infer-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
提供 SuperKernel 算子二进制融合技术的完整适配流程,包括模型分析、Scope 范围确定、代码实施和性能验证。
exe_mode: "ge_graph"、Atlas A3 硬件、PyTorch 框架superkernel_scope 上下文管理器标记融合范围eager 模式和 aclgraph 模式必须完成所有四个步骤:
最终产出:
superkernel_optimization_report.md)目标:理解模型架构,识别可以应用 SuperKernel 的部分
读取模型代码:
cann-recipes-infer/models/{model_name}/models/modeling_*.py)cann-recipes-infer/models/{model_name}/config.yaml)cann-recipes-infer/models/{model_name}/models/runner_*.py)识别模型架构特征:
检查当前配置:
exe_mode 是否为 ge_graph(必须)enable_superkernel 当前状态torch.compile)识别潜在问题:
产出:
目标:确定 SuperKernel 的融合范围
使用 AskUserQuestion 提供以下选项:
| 选项 | 描述 | 适用场景 | 优缺点 |
|---|---|---|---|
| 仅 Attention 模块 | 只将 Attention 计算纳入 SuperKernel scope | 首次尝试、模型瓶颈在 Attention | 风险小、易调试 |
| 仅 MoE 模块 | 只将 MoE 专家计算纳入 SuperKernel scope | MoE 架构模型 | 针对性强、效果明显 |
| 全模型 Decoder 层 | 将整个 Decoder 层纳入 SuperKernel scope | 模型结构简单、已有成功案例 | 融合范围最大、性能提升潜力最高、调试难度较大 |
| 使用自动分析工具 | 启动 subagent 分析最优 Scope 划分 | 追求最优性能 | 基于性能分析自动推荐、耗时较长(5-10分钟) |
| 自定义 Scope | 手动指定需要融合的模块 | 有特殊需求或已知最优配置 | 灵活性高 |
执行内容:
产出:
详细的 Scope 分析方法见
references/scope-analysis-guide.md
目标:实施 SuperKernel 适配代码
重要:这一步只修改代码,不输出任何中间文档。
修改 cann-recipes-infer/models/{model_name}/config.yaml:
# 确保以下配置
exe_mode: "ge_graph" # 必须是 ge_graph
model_config:
enable_superkernel: True # 启用 SuperKernel
enable_multi_streams: False # 根据需求配置
enable_cache_compile: False # 根据需求配置
在 cann-recipes-infer/models/{model_name}/models/modeling_*.py 中:
# 1. 导入上下文管理器
from executor.utils import superkernel_scope
# 2. 在 decode 方法中添加 Scope 标记
class YourModel:
def decode(self, input_ids, ...):
is_prefill = False # decode 阶段
# 根据选择的 Scope 策略添加
with superkernel_scope(
self.enable_superkernel and not is_prefill,
label="decode_layers", # 或 "attention_only", "moe_only"
option="stream-fusion=1" # 编译选项
):
for decoder_layer in self.layers:
# 运算逻辑
hidden_states = decoder_layer(
hidden_states,
attention_mask=attention_mask,
...
)
return hidden_states
Scope 标记位置示例:
产出:
目标:验证 SuperKernel 适配的正确性和性能提升
重要:这一步必须执行,不可跳过。完成代码修改后立即进行性能测试。
临时禁用 SuperKernel:
cd cann-recipes-infer/models/{model_name}
# 备份当前配置
cp config.yaml config.yaml.superkernel
# 禁用 SuperKernel
sed -i 's/enable_superkernel: True/enable_superkernel: False/' config.yaml
运行基线测试:
bash infer.sh 2>&1 | tee baseline.log
记录基线性能:
恢复 SuperKernel 配置:
# 恢复 SuperKernel 配置
cp config.yaml.superkernel config.yaml
运行优化测试:
bash infer.sh 2>&1 | tee optimized.log
记录优化性能:
计算性能提升:
性能提升 = (基线耗时 - 优化后耗时) / 基线耗时 × 100%
吞吐量提升 = (优化后吞吐量 - 基线吞吐量) / 基线吞吐量 × 100%
必须生成:创建 superkernel_optimization_report.md,包含:
# SuperKernel 优化报告
## 优化配置
- 模型:{model_name}
- Scope 策略:{scope_strategy}
- 优化日期:{date}
## 代码修改
### 配置文件修改
- 文件:`config.yaml`
- 修改内容:启用 `enable_superkernel: True`
### 模型代码修改
- 文件:`models/modeling_*.py`
- 修改内容:添加 SuperKernel scope 标记
- Scope 范围:{scope_description}
## 性能测试结果
### 基线性能(SuperKernel 禁用)
- Decode 单步耗时:{baseline_latency} ms
- 吞吐量:{baseline_throughput} tokens/s
### 优化后性能(SuperKernel 启用)
- Decode 单步耗时:{optimized_latency} ms
- 吞吐量:{optimized_throughput} tokens/s
### 性能提升
- 延迟降低:{latency_improvement}%
- 吞吐量提升:{throughput_improvement}%
## 结论
{conclusion}
## 使用说明
优化后的模型配置已保存,可直接使用:
\`\`\`bash
cd cann-recipes-infer/models/{model_name}
bash infer.sh
\`\`\`
产出:
superkernel_optimization_report.md)详细的性能基线建立方法见
references/performance-baseline-guide.md
完成所有四个步骤后,必须提供:
优化后的代码文件:
cann-recipes-infer/models/{model_name}/config.yaml(已启用 SuperKernel)cann-recipes-infer/models/{model_name}/models/modeling_*.py(已添加 Scope 标记)SuperKernel 优化报告:
superkernel_optimization_report.md不需要输出:
模型类型?
├─ MoE 模型 → 优先选择"仅 MoE 模块"
├─ 标准 Transformer → 优先选择"仅 Attention 模块"
└─ 简单模型 + 有成功案例 → 可选择"全 Decoder 层"
首次尝试?
└─ 是 → 选择"仅 Attention 模块"(风险最小)
追求最优性能?
└─ 是 → 选择"使用自动分析工具"
已知最优配置?
└─ 是 → 选择"自定义 Scope"
详细的 Scope 模板见 resources/scope-templates/ 目录:
attention-only.md - 仅 Attention 的 Scope 模板moe-only.md - 仅 MoE 的 Scope 模板full-model.md - 全模型 Scope 模板启用 SuperKernel 前,请确保:
exe_mode 设置为 ge_graph(不能是 eager 或 aclgraph)references/scope-analysis-guide.md - Scope 分析详细指南references/performance-baseline-guide.md - 性能基线建立指南resources/scope-templates/ - Scope 模板What SKILL.md sets; muted values are Claude Code's defaults
/model-infer-skills:model-infer-superkernelfrom model-infer-skills
npx claudepluginhub jieran-zhang/cannbot-skills --plugin model-infer-skills11 skills in this plugin
/model-infer-fusion基于 PyTorch 框架的昇腾 NPU 模型推理融合算子优化技能。分析模型代码,识别可替换为 torch_npu 融合算子的计算模式,生成替换方案。触发场景:torch_npu 融合算子替换、MoE/Attention/FFN/Norm 等模块的推理算子适配、torch_npu API 使用咨询。基于仓库已有模型的融合算子经验,按计算语义推荐最佳方案。/model-infer-graph-mode基于 PyTorch 框架的昇腾 NPU 模型推理图模式适配技能。将模型适配到 torch.compile 图模式以加速推理性能。触发场景:npugraph_ex 或 GE 图模式适配、torch.compile 在昇腾 NPU 上的使用、图中断(Graph Break)修复、aclgraph 图编译问题。LLM 模型的图模式适配优先阅读 LLM 模型改造指南。/model-infer-kvcache基于 PyTorch 框架的昇腾 NPU 模型推理 KVCache 优化技能。分析和优化 LLM 文本推理模型中的 KVCache 实现,包括连续缓存、分页注意力(Paged Attention)配合 FA 融合算子、MLA 压缩缓存。触发场景包括:KVCache 管理实现、paged attention、KV 压缩、FA 融合算子、OOM/性能问题、block_table/slot_mapping 构造。基于本仓库已有模型的 KVCache 实现经验,按模型类型和场景推荐最佳方案。Other plugins' skills for the same job