Guides adapting custom PyTorch operators for graph mode (npugraph_ex). Covers torch.library.custom_op and torch.library.Library registration, writing Meta derivation functions for shape/dtype inference, Eager-to-graph migration.
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.
若用户未说明算子状态,使用交互式提问工具发起问题:您的算子处于什么状态?
路由:
torch-npugraph-ex-template skill 中的「自定义算子代码块」)+ 图模式编译调用的完整 MRE,算子实现用 # <用户实现> 占位。In-place 与 Out-of-place 的区别在 custom_op 路径上体现在 mutates_args 与 register_fake 返回值,在 torch.library.Library 路径上体现在 schema 的 alias 标注和 Meta 返回值;函数化转换由 TorchAir 自动完成,无需手动实现torch-npugraph-ex-dfx-triage skill)若用户未说明注册方式,使用交互式提问工具发起问题:您的算子是通过哪种方式注册的?
路由:
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(取决于算子类型)torch.library.Library(Python 层,在用户脚本中通过 Library(..., "FRAGMENT") + define(...) + @impl(...) 注册 schema 和 Eager 实现,再通过 Library(..., "IMPL", "Meta") 注册 Meta)→ schema、Eager 实现和 Meta 注册都写在用户脚本内;Eager 实现通常使用 @impl(mylib, "op_name", "PrivateUse1"),Meta 使用 meta_lib = Library("mylib", "IMPL", "Meta") + @impl(meta_lib, "op_name")@torch.library.custom_op 装饰器定义则为 torch.library.custom_op;如果脚本中显式创建 Library("<namespace>", "FRAGMENT"),并调用 define(...)、@impl(..., "PrivateUse1")、Library(..., "IMPL", "Meta") 则为纯 Python torch.library.Libraryregister_fake / Meta impl)agent 行为规则:
torch.library.custom_op 路径 → 在用户脚本内使用 @my_op.register_fake 编写 Meta 推导函数torch.library.Library 路径 → 在用户脚本内使用 meta_lib = Library("<namespace>", "IMPL", "Meta") + @impl(meta_lib, "op_name") 编写 Meta 推导函数# TODO: 根据算子语义推导输出 shape/dtype 标注需用户填充的部分关键点:Meta 推导函数只做 shape/dtype/device 推导,不执行实际计算。写 Meta 函数只需理解操作的数学语义和 input→output 的映射关系。In-place 算子返回
None,Out-of-place 算子返回与输出同 shape/dtype 的空 tensor。
完成 Meta 推导函数后,即可支持 npugraph_ex 入图,无需额外步骤。
下一步:加载 torch-npugraph-ex-knowledge skill,继续处理编译调用、调试定位或性能优化。
torch.library.Library 写法对照用途:为不使用 @torch.library.custom_op 的用户提供一份对照表,帮助他们在同一脚本内完成 schema、Eager 实现和 Meta 注册。
| 阶段 | 常见写法 | 说明 |
|---|---|---|
| Schema 定义 | mylib = Library("mylib", "FRAGMENT") + mylib.define("my_op(Tensor x) -> Tensor") | 定义算子 schema,In-place 算子需在 schema 中显式标注 alias,如 Tensor(a!) |
| Eager 实现 | @impl(mylib, "my_op", "PrivateUse1") | 为 NPU 后端注册实际执行逻辑 |
| Meta 注册 | meta_lib = Library("mylib", "IMPL", "Meta") + @impl(meta_lib, "my_op") | 注册 Meta 推导函数,用于 torch.compile 入图 |
agent 使用流程:收到纯 Python
torch.library.Library算子需求 → 先确认 schema 和算子语义 → 再按define(...)、@impl(..., "PrivateUse1")、@impl(..., "Meta")三步生成对应骨架。
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
custom-ops, pytorch, graph-compilation, meta-functions, npugraph-ex, torch-compile
7 skills in this plugin
/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-knowledgenpugraph_ex(aclgraph)模式使用指南。采用 Capture & Replay 方式将算子任务下沉至 Device 执行,减少 Host 调度开销,适用于固定 shape 在线推理低延迟场景。涵盖模式配置、FX Pass、编译缓存、多流并行、内存复用、静态 Kernel 编译、限核、性能优化、调试定位、自定义算子入图等。关键词:npugraph_ex、aclgraph、backend="npugraph_ex"、capture、replay、reduce-overhead、config.aclgraph_config。/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 模式代码、做概念解释、对比分析或配置指导时加载。/torch-npugraph-ex-skills:torch-custom-ops-guidefrom torch-npugraph-ex-skills
npx claudepluginhub hicann/cannbot-skills --plugin torch-npugraph-ex-skills2 plugins ship this exact skill · first indexed Oct 20, 2026
Other plugins' skills for the same job