AGENTS.md - CANN Development Guide
Overview
Documentation repository for Huawei Ascend NPU (CANN) development. Two focus areas:
- ONNX Plugin Development (
onnx_plugin_guide/): C++ code mapping ONNX operators to GE graph IR - PyTorch Model Development (
onnx_plugin_guide/moel_xllm.py): T5-style model for Ascend NPU
Directory Structure
onnx_plugin_guide/
├── ParseOpToGraph_Development_Guide.md # Detailed ONNX→GE mapping guide (Chinese)
├── default_gather.cpp # Example: MultiHeadAttention plugin
├── moel_xllm.py # T5 model for NPU compilation
├── AGENTS.md # PyTorch model-specific instructions
ONNX Plugin Development (C++)
Key Constraints
-
ONNX attributes cannot be accessed via
GetAttrdirectly in ParseParamsByOperatorFn - must parse from"attribute"field containing JSON:ge::AscendString attrs_string; op_src.GetAttr("attribute", attrs_string); json attrs = json::parse(attrs_string.GetString()); for (json& attr : attrs["attribute"]) { if (attr["name"] == "num_heads") int value = attr["i"]; } -
ONNX attribute type → JSON field: int→
"i", float→"f", string→"s", ints→"ints" -
In ParseOpToGraphFn,
GetAttrworks directly (no JSON parsing needed) -
Registration pattern:
REGISTER_CUSTOM_OP("PartitionedCall")for operators without direct GE IR mapping -
Two functions required:
ParseParamsByOperatorFn: Parse ONNX JSON attrs → set to PartitionCallParseOpToGraphFn: Build GE subgraph from PartitionCall attrs
-
Data node index corresponds to ONNX input edge number (not always sequential)
-
Graph output count must match original ONNX operator output count
CANN Header Paths
Example code references CANN 8.5.0 headers at /usr/local/Ascend/cann-8.5.0/opp/built-in/op_graph/inc/. Adjust for actual installation.
PyTorch Model Development
See onnx_plugin_guide/AGENTS.md for details on moel_xllm.py:
- Dependencies:
torch_npu,torchair(Ascend repos only, not PyPI) - Requires
fullgraph=Truecompatible code: no.item(), no data-dependent branches, pure tensor ops - Run:
python moel_xllm.py(requires Ascend environment)
Reference
ParseOpToGraph_Development_Guide.md: Comprehensive guide for ONNX plugin development (702 lines, Chinese)