torch-ascendc-op-extension

Adds PyTorch PTA interfaces to existing Ascend C operator projects via TORCH_LIBRARY or aclnn registration.

Updated Sep 15, 2026
One-click install
npx skills add https://github.com/WangWindow/CANN-BatchMatMulMaxsum --skill torch-ascendc-op-extension-wangwindow
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: torch-ascendc-op-extension
Source: https://github.com/WangWindow/CANN-BatchMatMulMaxsum/tree/main/.agents/skills/torch-ascendc-op-extension
Command: npx skills add https://github.com/WangWindow/CANN-BatchMatMulMaxsum --skill torch-ascendc-op-extension-wangwindow

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, torch_npu, torchair, and includes references (resource) components.

What problem does it solve? You have a working Ascend C operator project on Ascend NPU hardware, but no way to call it from Python. This Skill builds the PyTorch Adapter (PTA) layer so your custom operator becomes callable through torch.ops or torch_npu, without touching the operator kernel itself. ## Core Features & Use Cases - Route Detection: Inspects your project for evidence (kernel <<<>>> calls, op_host OpDef, aclnn headers, .run packages) and routes to one of two mutually exclusive integration paths: kernel direct-invoke or aclnn registry. - Direct-Invoke Path: Generates op_extension files (ops.h, torch.cpp, register.cpp) plus a dual-target CMakeLists that produces a loadable libxxx_ops.so registered under torch.ops.npu, with correct stream(true) synchronization. - aclnn Registry Path: Generates an xops wheel (setup.py with NpuExtension, ops_common boilerplate, schema registration, Meta backend impl, torchair FX-to-GE converter) that dlsyms aclnn symbols from libcust_opapi.so at runtime. - Use Case: You built a custom fused operator (e.g., a BatchMatmulMaxSum kernel) that compiles and runs standalone, and now want to call it from a PyTorch training or inference script via torch_npu.npu_x_custom_op() with torch.compile graph-mode support. ## Quick Start Ask the assistant to add a PyTorch interface to your existing Ascend operator project at a given path, and it will detect the correct route and generate the PTA layer.

Frequently Asked Questions about torch-ascendc-op-extension

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call a custom Ascend C operator from PyTorch?

Register the operator with TORCH_LIBRARY and bind a PrivateUse1 implementation, then load the built .so with torch.ops.load_library and call it via torch.ops.npu.your_op. For aclnn-registered operators, build an xops wheel with NpuExtension and call it through torch_npu.

What is the difference between kernel direct-invoke and aclnn registry routes?

Direct-invoke compiles <<<>>> kernel source with CMake into a .so and requires manual stream(true) queue synchronization. The aclnn route never compiles kernels; it dlsyms two-stage symbols from libcust_opapi.so and dispatches through EXEC_NPU_CMD_V1 with RunOpApiV2 queue semantics. The two routes cannot be mixed.

Does torch.compile work with custom NPU operators?

Yes, but you must register a Meta backend implementation via TORCH_LIBRARY_IMPL(custom, Meta) or the fx tracing fails with 'Meta backend not registered'. For graph mode on the aclnn route, you also need a torchair FX-to-GE converter whose input, attr, and output names match the OpDef exactly.

Why does my custom kernel change have no effect after rebuilding?

Two common causes: the GE op_type lacks a custom prefix and gets shadowed by a built-in CANN implementation, or libcust_opapi.so resolves to a stale nested copy under ASCEND_CUSTOM_OPP_PATH. Reinstalling the PTA wheel does not fix either, since it only replaces the torch binding layer.

When should I not use this PyTorch adapter workflow?

Do not use it if the operator project itself does not build or run yet; build the operator first with a direct-invoke or registry-invoke template skill. It also does not cover autograd backward implementations or precision validation, which are handled separately.