Guides usage of npugraph_ex (aclgraph) mode for Ascend NPU, using Capture & Replay to offload ops to Device and reduce Host scheduling overhead. Covers mode configuration, FX Pass, compile cache, multi-stream parallelism, memory reuse, static kernel compilation, core limiting, performance optimization, debugging, and custom op graph integration.
Ships in torch-npugraph-ex-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.
npugraph_ex 模式采用 Capture & Replay 方式,将算子任务下沉到 Device 执行,减少 Host 调度开销。该模式为试验特性,仅面向推理场景。
config.mode = "npugraph_ex"("reduce-overhead" 已废弃)。本 skill 是 npugraph_ex 文档映射和知识检索的权威入口。获取文档时采用 刷新优先、缓存兜底:不能因为 ~/.cache 已存在就默认文档是最新。
TorchAir 文档是 npugraph_ex 的主知识源,目录:https://gitcode.com/Ascend/torchair/tree/master/docs/zh。
docs/zh/ 下的一级边界:
overview.md — 简介(概念、架构总览)npugraph_ex/ — npugraph_ex(aclgraph)模式全部文档custom_op_graph/ — 自定义算子入图appendix/ — FAQ、ATen API 清单、常见案例与定位获取规则:
缓存不存在时必须 sparse clone:
git clone --depth 1 --filter=blob:none --sparse https://gitcode.com/Ascend/torchair.git ~/.cache/torchair-docs
git -C ~/.cache/torchair-docs sparse-checkout set docs/zh
缓存存在时优先刷新远端:
git -C ~/.cache/torchair-docs fetch --depth 1 origin master
git -C ~/.cache/torchair-docs merge --ff-only FETCH_HEAD
git -C ~/.cache/torchair-docs sparse-checkout set docs/zh
刷新成功后读取文档:
read_file("~/.cache/torchair-docs/docs/zh/{path}")
刷新失败时才读缓存:网络失败、远端不可达或用户明确要求离线/快速响应时,可以读取本地缓存,但回复中必须说明“当前使用本地缓存,可能不是最新”。
TTL 平衡建议:普通代码生成/配置指导可在上次刷新未超过 24 小时时直接读缓存;用户明确要求最新文档、涉及 API 行为变化、版本差异或新能力时,忽略 TTL,必须尝试刷新。
PyTorch NPU 文档来自 Ascend/pytorch 仓库(非 TorchAir),用于交叉验证 torch_npu / NPU 底层 API(stream、event、device、内存管理、单算子写法、自定义算子适配、Native API 签名等)。它不是独立场景入口。
HiAscend 官方在线文档入口用于版本导航和在线交叉验证,不替代下方 Ascend/pytorch sparse checkout、本地安装源码或用户环境源码。用户明确要求最新/特定产品版本、版本差异、API 支持度、环境变量、故障处理或 TorchAir 图模式导航时,可先查看:
Ascend Extension for PyTorch 26.0.0:https://www.hiascend.com/document/detail/zh/Pytorch/2600/index/index.html缓存不存在时按固定分支拉取:
git clone --depth 1 --filter=blob:none --sparse -b v2.7.1 https://gitcode.com/Ascend/pytorch.git ~/.cache/pytorch-npu-docs
git -C ~/.cache/pytorch-npu-docs sparse-checkout set docs/zh/framework_feature_guide_pytorch docs/zh/native_apis docs/zh/environment_variable_reference torch_npu/npu
缓存存在时优先刷新 v2.7.1 分支:
git -C ~/.cache/pytorch-npu-docs fetch --depth 1 origin v2.7.1
git -C ~/.cache/pytorch-npu-docs merge --ff-only FETCH_HEAD
git -C ~/.cache/pytorch-npu-docs sparse-checkout set docs/zh/framework_feature_guide_pytorch docs/zh/native_apis docs/zh/environment_variable_reference torch_npu/npu
读取路径:
read_file("~/.cache/pytorch-npu-docs/docs/zh/framework_feature_guide_pytorch/<filename>.md")
read_file("~/.cache/pytorch-npu-docs/torch_npu/npu/<filename>.py") # 验证函数签名
版本提醒:本地缓存的 PyTorch NPU 参考文档固定为 v2.7.1;HiAscend 在线入口对应 Ascend Extension for PyTorch 26.0.0 产品文档,页面内可能包含多个 PyTorch 原生 API 支持度版本。若用户使用其他版本,需提醒 API 可能有差异,并优先以用户环境中的源码/签名为准。
⚠️ 上述"先读文档"优先用于代码生成、概念解释、对比分析、配置指导和脚本迁移场景;当用户处于问题定位场景时,优先遵循下方"问题定位时的源码检索"。
本表路径采用仓库根相对路径(包含 docs/zh/),便于直接从 sparse checkout 缓存读取。若用于 AGENTS.md 的「📎 参考链接」基址拼接,需要先去掉开头的 docs/zh/。
| 功能 | 文档路径 |
|---|---|
| TorchAir 简介、概念、架构 | docs/zh/overview.md |
| npugraph_ex 后端概述 | docs/zh/npugraph_ex/npugraph_ex.md |
| 快速上手 | docs/zh/npugraph_ex/quick_start.md |
| 基础功能列表 | docs/zh/npugraph_ex/basic/basic.md |
| 功能 | 文档路径 | 配置方式 |
|---|---|---|
| FX 图算子融合 Pass | docs/zh/npugraph_ex/basic/pattern_fusion_pass.md | config.aclgraph_config |
| FX 图优化 Pass(In-place 等) | docs/zh/npugraph_ex/basic/inplace_pass.md | config.aclgraph_config |
| aclgraph 间内存复用 | docs/zh/npugraph_ex/basic/memory_reuse.md | config.aclgraph_config |
| 静态 Kernel 编译 | docs/zh/npugraph_ex/basic/static_kernel_compile.md | config.aclgraph_config |
| 固定权重类输入地址 | docs/zh/npugraph_ex/basic/frozen_parameter.md | config.aclgraph_config |
| 重捕获次数限制 | docs/zh/npugraph_ex/basic/capture_limit.md | config.aclgraph_config |
| 集合通信入图 | docs/zh/npugraph_ex/basic/communication_graph.md | — |
| 强制 Eager 回退 | docs/zh/npugraph_ex/basic/force_eager.md | — |
| 冗余 Cat 算子消除 | docs/zh/npugraph_ex/basic/remove_cat_ops.md | — |
| 冗余 NoOp 算子消除 | docs/zh/npugraph_ex/basic/remove_noop_ops.md | — |
| 功能 | 文档路径 | 配置方式 |
|---|---|---|
| 模型编译缓存 | docs/zh/npugraph_ex/advanced/compile_cache.md | cache_compile API |
| 图内多流表达 | docs/zh/npugraph_ex/advanced/multi_stream.md | scope API |
| AI Core / Vector Core 限核 | docs/zh/npugraph_ex/advanced/limit_cores.md | scope API |
| 自定义 FX 图 Pass | docs/zh/npugraph_ex/advanced/post_grad_custom_pass.md | — |
| 功能 | 文档路径 |
|---|---|
| 算子 Data Dump | docs/zh/npugraph_ex/dfx/data_dump.md |
| Debug 信息保存 | docs/zh/npugraph_ex/dfx/debug_save.md |
| DFX 功能概览 | docs/zh/npugraph_ex/dfx/dfx.md |
| API | 文档路径 |
|---|---|
| cache_compile | docs/zh/npugraph_ex/api/inference/cache_compile.md |
| readable_cache | docs/zh/npugraph_ex/api/inference/readable_cache.md |
| 推理 API 概览 | docs/zh/npugraph_ex/api/inference/torch-npu-npugraph_ex-inference.md |
| compile_fx | docs/zh/npugraph_ex/api/npugraph_ex/compile_fx.md |
| register_replacement | docs/zh/npugraph_ex/api/npugraph_ex/register_replacement.md |
| npugraph_ex API 概览 | docs/zh/npugraph_ex/api/npugraph_ex/torch-npu-npugraph_ex.md |
| limit_core_num | docs/zh/npugraph_ex/api/scope/limit_core_num.md |
| scope API 概览 | docs/zh/npugraph_ex/api/scope/torch-npu-npugraph_ex-scope.md |
| API 总览 | docs/zh/npugraph_ex/api/api.md |
| API 列表 | docs/zh/npugraph_ex/api/api_list.md |
| 功能 | 文档路径 |
|---|---|
| 自定义算子入图总览 | docs/zh/custom_op_graph/overview.md |
| Non-in-place 算子案例 | docs/zh/custom_op_graph/non_in_place_op_cases.md |
| In-place 算子案例 | docs/zh/custom_op_graph/in_place_op_cases.md |
| 算子适配 TorchAir | docs/zh/custom_op_graph/op_adapt_torchair.md |
| op_plugin 适配 TorchAir | docs/zh/custom_op_graph/op_plugin_adapt_torchair.md |
| 功能 | 文档路径 |
|---|---|
| 支持的 ATen API 清单 | docs/zh/appendix/aten_api.md |
| 常见案例和定位方法 | docs/zh/appendix/cases/cases.md |
| 入图失败定界与定位 | docs/zh/appendix/cases/graph_failed_cases.md |
| 图模式精度比对 | docs/zh/appendix/cases/accuracy_cases.md |
| 图模式性能分析 | docs/zh/appendix/cases/perfermance_cases.md |
| 推理案例 | docs/zh/appendix/cases/infer_cases.md |
| 动静子图专题 | docs/zh/appendix/cases/dynamic_static_graph/dynamic_static_graph.md |
| FAQ | docs/zh/appendix/faq.md |
| 功能 | 文档路径 |
|---|---|
| 流和任务队列并行下发 | docs/zh/framework_feature_guide_pytorch/stream_taskqueue_parallel_delivery.md |
| 多流内存复用 | docs/zh/framework_feature_guide_pytorch/multistream_memory_reuse.md |
| 单算子适配总览 | docs/zh/framework_feature_guide_pytorch/single_operator_adaptation.md |
| 单算子写法说明 | docs/zh/framework_feature_guide_pytorch/adaptation_description_single.md |
| 单算子调用示例 | docs/zh/framework_feature_guide_pytorch/sample_call_single.md |
| 自定义算子适配 | docs/zh/framework_feature_guide_pytorch/custom_operator_adaptation.md |
| kernel launch 算子适配 | docs/zh/framework_feature_guide_pytorch/kernel_launch_operator_adaptation.md |
| opplugin 算子适配 | docs/zh/framework_feature_guide_pytorch/opplugin_operator_adaptation.md |
| C 扩展算子适配 | docs/zh/framework_feature_guide_pytorch/c_extensions_operator_adaptation.md |
| kernel 调用示例 | docs/zh/framework_feature_guide_pytorch/sample_call_kernel.md |
| opplugin 调用示例 | docs/zh/framework_feature_guide_pytorch/sample_call_opplugin.md |
| PyTorch 侧图模式描述 | docs/zh/framework_feature_guide_pytorch/pytorch_graph_mode.md |
| NPUGraph 描述 | docs/zh/framework_feature_guide_pytorch/pytorch_npugraph_desc.md |
| 计算性能优化 | docs/zh/framework_feature_guide_pytorch/computing_performance_optimization.md |
| 内存资源优化 | docs/zh/framework_feature_guide_pytorch/memory_resource_optimization.md |
| 参数设置 | docs/zh/framework_feature_guide_pytorch/parameter_setting.md |
| v2.7.1 Native API 清单 | docs/zh/native_apis/pytorch_2-7-1/ |
| 环境变量参考 | docs/zh/environment_variable_reference/ |
| HiAscend 在线文档索引 | https://www.hiascend.com/document/detail/zh/Pytorch/2600/index/index.html |
问题定位采用"就近定位优先,三仓兜底"的策略,不维护静态的日志关键词映射表。
site-packages/torchair/、site-packages/torch_npu/、site-packages/torch/。| 仓库 | 地址 | 适用阶段 |
|---|---|---|
| TorchAir | https://gitcode.com/Ascend/torchair.git | npugraph_ex backend、编译器、pass、DFX、aclgraph 执行链路 |
| Ascend/pytorch | https://gitcode.com/Ascend/pytorch.git | torch_npu 插件、NPU API、环境变量、多流、自定义算子适配 |
| upstream PyTorch | https://github.com/pytorch/pytorch.git | torch.compile 前半程语义:Dynamo、FX、AOTAutograd、Functorch、graph break |
本地拉取示例(仅在本地无对应源码时):
# TorchAir
git clone --depth 1 --filter=blob:none --sparse https://gitcode.com/Ascend/torchair.git ~/.cache/torchair-src
cd ~/.cache/torchair-src && git sparse-checkout set npugraph_ex docs/zh
# Ascend/pytorch
git clone --depth 1 --filter=blob:none --sparse -b v2.7.1 https://gitcode.com/Ascend/pytorch.git ~/.cache/torch-src
cd ~/.cache/torch-src && git sparse-checkout set torch_npu docs/zh
# upstream PyTorch
git clone --depth 1 --filter=blob:none --sparse https://github.com/pytorch/pytorch.git ~/.cache/pytorch-src
cd ~/.cache/pytorch-src && git sparse-checkout set torch/_dynamo torch/fx torch/_functorch torch/_inductor
torch_npu / stream / event / device 问题 → 优先补查 Ascend/pytorch 或本地 torch_npubackend="aot_eager" 失败、torch._dynamo / torch.fx / FakeTensor / AOTAutograd 问题 → 优先补查本地 torch 或 upstream PyTorch,必要时再看 Ascend/pytorch 桥接代码npugraph_ex pass、graph_pass、replace_stream_event、npu_fx_compiler、fx2acl_converter、DFX 问题 → 优先补查 TorchAir 或本地 torchair→ 读取 docs/zh/npugraph_ex/advanced/compile_cache.md,引导使用 cache_compile API。
以下优化项按推荐优先级排列:
docs/zh/npugraph_ex/basic/static_kernel_compile.md)docs/zh/npugraph_ex/basic/pattern_fusion_pass.md)docs/zh/npugraph_ex/basic/inplace_pass.md)docs/zh/npugraph_ex/basic/frozen_parameter.md)docs/zh/npugraph_ex/advanced/multi_stream.md)docs/zh/npugraph_ex/advanced/limit_cores.md)→ 读取 docs/zh/npugraph_ex/basic/memory_reuse.md,引导内存复用配置。
stdout / stderr、npugraph_ex 日志和 Ascend/pytorch 日志,并在回复中注明日志文件名与关键行号)、最小可复现脚本或核心函数片段、PyTorch / torch_npu / torchair / CANN 版本、Eager 是否成功、backend="aot_eager" 是否成功、输入 shape 是否固定、是否涉及多流 / 自定义算子 / In-place。npugraph_ex backend、GE-CANN、精度还是性能层。按以下步骤逐一排查:
模型在单算子模式(Eager)下能否正常运行?
使用 backend="aot_eager" 编译是否成功?
opt_model = torch.compile(model, backend="aot_eager")
torch / torch_npu / Ascend-pytorch 桥接代码使用 backend="npugraph_ex" 编译是否成功?
docs/zh/appendix/cases/graph_failed_cases.md,并优先根据日志中的文件名、pass 名、函数名或生成代码片段读取相邻源码开启日志分析问题
docs/zh/npugraph_ex/dfx/dfx.md 了解调试功能docs/zh/npugraph_ex/dfx/debug_save.md 保存 Debug 信息docs/zh/appendix/cases/cases.md 获取定位案例npu_fx_compiler、graph_pass、replace_stream_event、fx2acl_converter、生成代码或 dump 文件,先读取对应源码附近上下文首先确认 Eager 模式下精度是否正常
使用 backend="aot_eager" 对比结果
torch 与桥接代码上下文使用 NPU backend,开启算子 dump 对比
docs/zh/npugraph_ex/dfx/data_dump.mddocs/zh/appendix/cases/accuracy_cases.md)按以下顺序处理:
torch._dynamo、torch.fx、FakeTensor、AOTAutograd → 先归类到 Dynamo / FX / AOTAutogradnpugraph_ex、torchair、graph_pass、replace_stream_event、npu_fx_compiler、fx2acl_converter → 先归类到 TorchAir / npugraph_exInferShape、CompileGraph、GE / CANN / aclnn / plog 报错 → 先归类到 GE-CANNtorch_npu.npu.*、stream / event / device 相关问题 → 先归类到 torch_npu / NPU APIdocs/zh/appendix/faq.md + docs/zh/appendix/cases/cases.md。npugraph_ex / GE-CANN / 精度 / 性能中的哪一类npugraph_ex 模式不需要实现 Ascend IR Converter,只需完成 Meta 推导函数。函数化转换由 TorchAir 自动完成(PyTorch 2.6+),In-place 算子也无需手动实现。
In-place 算子(会修改输入数据,如 add_、kv_cache 原地修改)→ Schema 定义有额外约定(需用 Tensor(a!) 标记被修改的输入、不返回被修改的输入),函数化由 TorchAir 自动完成
docs/zh/custom_op_graph/in_place_op_cases.mdOut-of-place 算子(结果写入新 tensor,如 add)→ 无额外约定
docs/zh/custom_op_graph/non_in_place_op_cases.md根据算子注册方式选择适配路径:
torch.library.custom_op 注册(Python 层):
register_fake 写在用户脚本内,与算子定义同文件docs/zh/custom_op_graph/op_adapt_torchair.md + 对应算子类型文档(non_in_place_op_cases.md / in_place_op_cases.md)纯 Python torch.library.Library 注册(Python 层):
Library("<namespace>", "FRAGMENT") + define(...) + @impl(..., "PrivateUse1") 完成;Meta 通过 Library("<namespace>", "IMPL", "Meta") + @impl(...) 完成docs/zh/custom_op_graph/op_adapt_torchair.md + 对应算子类型文档,并参考 template skill 中的 torch.library.Library 代码块Meta 编写策略:向用户请求算子签名和语义描述(输入/输出 tensor 的 shape、dtype 关系),据此编写 Meta 推导函数;custom_op 路径使用 register_fake,torch.library.Library 路径使用 Library("<namespace>", "IMPL", "Meta") + @impl(...)。若用户无法提供,给出骨架并标注 # TODO: 根据算子逻辑推导输出 shape/dtype。
npugraph_ex 模式完成 Meta 推导函数即可入图,无需额外实现 Converter。
docs/zh/custom_op_graph/overview.md、docs/zh/custom_op_graph/op_adapt_torchair.md→ 读 docs/zh/npugraph_ex/quick_start.md,使用方式 1 代码骨架
docs/zh/npugraph_ex/basic/pattern_fusion_pass.mddocs/zh/npugraph_ex/basic/inplace_pass.mddocs/zh/npugraph_ex/basic/memory_reuse.mddocs/zh/npugraph_ex/basic/static_kernel_compile.mddocs/zh/npugraph_ex/basic/frozen_parameter.mddocs/zh/npugraph_ex/advanced/multi_stream.mddocs/zh/npugraph_ex/advanced/limit_cores.md→ 读 docs/zh/npugraph_ex/advanced/compile_cache.md + docs/zh/npugraph_ex/api/inference/cache_compile.md
→ 读 docs/zh/npugraph_ex/api/npugraph_ex/register_replacement.md + docs/zh/npugraph_ex/basic/pattern_fusion_pass.md
→ 读 docs/zh/npugraph_ex/basic/capture_limit.md
backend="aot_eager" 验证脚本无问题What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/torch-npugraph-ex-skills:torch-npugraph-ex-knowledgefrom torch-npugraph-ex-skills
npx claudepluginhub catman626/cannbot-skills --plugin torch-npugraph-ex-skills7 skills in this plugin
/torch-custom-ops-guide自定义算子入图完整指南。覆盖从零开发、Eager 算子适配 npugraph_ex 图模式(torch.library.custom_op / torch.library.Library)、Meta 推导函数编写等全流程。适用于两种纯 Python 自定义算子注册场景。关键词:custom_op、torch.library.Library、register_fake、meta、mutates_args。/torch-npugraph-ex-compile-error-diagnosisPyTorch 昇腾 NPU npugraph_ex 编译期报错诊断。覆盖 torch.compile 触发后 TorchDynamo / FX / AOTAutograd / npugraph_ex backend / ACL graph capture 阶段的报错排查,包括 Unsupported / graph break / BackendCompilerFailed / Meta 推导失败 / capture 失败等场景。本 skill 由 dfx-triage 路由进入。触发:当用户遇到 npugraph_ex 入图失败、graph break、BackendCompilerFailed、Meta 推导失败或 capture 失败时加载。关键词:入图失败、断图、graph break、BackendCompilerFailed、Meta、FakeTensor、aot_eager、capture。/torch-npugraph-ex-dfx-triagePyTorch 昇腾 NPU npugraph_ex DFX 问题分诊入口。统一执行首轮全量日志收集与最少闭环信息核对,按报错栈和现象将问题路由到 compile-error / runtime-error / accuracy / performance 四个专科 sub-skill。本 skill 不输出最终诊断结论,只完成「采集 + 分类 + 加载下游 skill」。触发:当用户报告 npugraph_ex 相关报错、断图、精度差异或性能回退、需要 debug/dump 定位时加载。关键词:问题定位、报错、断图、精度、性能、debug、dump、aot_eager。/torch-npugraph-ex-performance-diagnosisPyTorch 昇腾 NPU npugraph_ex 性能诊断(FX 图静态审计,聚焦冗余 tensor move 与疑似 reinplace 未命中)。处理「5 step 全部通过、但推理慢/Device 利用率低」阶段:基于 TORCH_COMPILE_DEBUG=1 产出的 FX 图序列与 debug.log,定位 reinplace_inplaceable_ops_pass(out-of-place→in-place)与 reinplace_input_mutated_ops(折叠输入侧 copy_)回填未完成留下的 copy_/clone/out-of-place 冗余,并识别 Compressor 等 Python wrapper 显式 clone/copy_ 经 Functionalize 后伪装成 reinplace 失败的版本型问题。本 skill 由 dfx-triage 路由进入;**不**处理 graph break / recompile / guard failure(归 compile-error-diagnosis)。触发:当用户报告 npugraph_ex 推理慢、性能回退、怀疑图里有多余搬运/拷贝、reinplace 没生效时加载。关键词:性能、慢、tensormove、tensor move、reinplace、functionalize、copy_、clone、auto_functionalized、compressor、remove_noop_ops、multi_stream、can_inplace、missed reinplacing、inplace_pass、input_inplace_pass、clone_input、TORCH_COMPILE_DEBUG。/torch-npugraph-ex-runtime-error-diagnosisPyTorch 昇腾 NPU npugraph_ex 运行时报错诊断。覆盖 ACL graph 已 capture 成功之后,replay / kernel launch / 通信 / 内存 / device API 阶段的报错排查,包括 aclnnXxx 算子失败、HCCL 错误、stream/event 同步、segfault、device side assert、OOM 等场景。本 skill 由 dfx-triage 路由进入。触发:当用户遇到 npugraph_ex replay / aclnn / HCCL / stream / OOM 等运行时报错时加载。关键词:runtime、ACL、aclnn、HCCL、stream、event、device side assert、segfault、OOM、CANN plog。/torch-npugraph-ex-templatenpugraph_ex 模式的 MRE(最小可复现示例)代码模板。包含标准 npugraph_ex 编译模板和 npugraph_ex 编译缓存(cache_compile)模板。触发:当用户需要从零生成 npugraph_ex 模式代码、做概念解释、对比分析或配置指导时加载。2 plugins ship this exact skill · first indexed Oct 20, 2026
Other plugins' skills for the same job