Skill type: process-oriented (multi-stage workflow, stage checkpoints)
Quickly create an Ascend-Kernel operator project; all operators are uniformly generated under csrc/ops directory, and ensure that subsequent design/coding/compilation testing phases can be entered directly.
Core principles
- Single directory: all operators are uniformly generated under
csrc/opsdirectory - Safety first: automatically check whether the directory exists to avoid overwriting existing files
- Naming convention: strictly require snake_case naming format
- Project detection: prioritize using an existing ascend-kernel project; if it does not exist, copy the template
- Verifiable results: output directory structure, registration change points, build test commands
- Built-in pitfall prevention: explicitly prompt the three CMake updates,
EXEC_KERNEL_CMDlvalue requirement, environment dependencies
Workflow
digraph project_init {
rankdir=TB;
node [shape=box];
start [label="用户请求创建算子" shape=ellipse];
detect_ascend_kernel [label="检测ascend-kernel项目是否存在" shape=diamond];
detect_ops [label="检测csrc/ops/目录是否存在" shape=diamond];
copy_template [label="复制ascend-kernel项目模板到当前目录"];
create_ops_dir [label="创建csrc/ops目录"];
collect [label="收集算子信息"];
check_path [label="检查目标算子路径是否存在" shape=diamond];
show_exists [label="提示路径已存在,询问是否更换"];
show_result [label="展示目录结构和下一步指引"];
start -> detect_ascend_kernel;
detect_ascend_kernel -> detect_ops [label="存在"];
detect_ascend_kernel -> copy_template [label="不存在"];
copy_template -> collect;
detect_ops -> collect [label="存在ops/"];
detect_ops -> create_ops_dir [label="不存在ops/"];
create_ops_dir -> collect;
collect -> check_path;
check_path -> show_exists [label="存在"];
show_exists -> collect [label="更换名称"];
check_path -> show_result [label="不存在"];
}
Anti-pattern checklist (NEVER DO THESE)
- ❌ Do not generate operators outside the csrc/ops directory
- ❌ Do not overwrite an existing operator directory
- ❌ Do not use any naming format other than snake_case
- ❌ Do not skip the directory existence check
- ❌ Do not only create the operator directory without creating
op_host/andop_kernel/ - ❌ Do not forget to update
ops.h、register.cpp、csrc/CMakeLists.txt(three places) - ❌ Do not directly pass rvalue temporary variables in
EXEC_KERNEL_CMDPass rvalue temporary variables directly in - ❌ Do not use ACLNN as the implementation path for new operators
Step 1: Detect and initialize the Ascend-Kernel project
1.1 Detect the location of the Ascend-Kernel project
Detection strategy:
- Check whether the current directory is an ascend-kernel project (contains build.sh, CMakeLists.txt, csrc/)
- Check the ascend-kernel subdirectory under the current directory
- Check for an ascend-kernel project in the first-level subdirectories
Execute command:
bash <skill_dir>/scripts/detect_ascend_kernel_project.sh
1.2 Handle detection results
| Detection result | Handling method |
|---|---|
PROJECT_FOUND:<path> |
Use the existing project, continue to step 2 |
PROJECT_FOUND_NO_OPS:<path> |
Create the csrc/ops/ directory in the project, continue to step 2 |
PROJECT_NOT_FOUND |
Copy the ascend-kernel template to the current directory, continue to step 2 |
MULTIPLE_PROJECTS:<path1> <path2> ... |
List all projects and let the user choose |
1.3 Copy the project template (if needed)
Execute command:
cp -r "<skill_dir>/templates/ascend-kernel" ./ascend-kernel
chmod +x ./ascend-kernel/build.sh
Important: after copying, you must chmod +x build.sh, otherwise compilation will report Permission denied。
Verification: Confirm that the ascend-kernel directory contains:
- build.sh (and it has execute permission)
- CMakeLists.txt
- csrc/ directory
Step 2: Collect operator information (minimum required)
Information that must be confirmed:
| Information | Format requirement | Description |
|---|---|---|
| Operator name | snake_case | such as rms_norm, flash_attn |
Confirmation information:
=== Ascend-Kernel算子信息 ===
算子名称: <op_name>
生成路径: <ascend_kernel_path>/csrc/ops/<op_name>
确认创建?[Y/n]
Step 3: Check prerequisites
- Confirm you are in the ascend-kernel project root directory (build.sh exists and has execute permission)
- Confirm the csrc/ops/ directory exists (if it does not exist, create it
mkdir -p csrc/ops) - Confirm the target directory
csrc/ops/<op_name>does not exist
Step 4: Create the operator skeleton
Create directory:
mkdir -p csrc/ops/<op_name>/op_host csrc/ops/<op_name>/op_kernel
Under csrc/ops/<op_name>/ create the following files:
csrc/ops/<op_name>/
├── CMakeLists.txt
├── design.md # 设计文档占位(由 design skill 填充)
├── op_host/
│ └── <op_name>.cpp # Host 占位(由 code-gen skill 替换)
└── op_kernel/
└── <op_name>.cpp # Kernel 占位(由 code-gen skill 替换)
4.1 Minimal file content template
CMakeLists.txt
ascendc_add_operator(OP_NAME <op_name>)
op_host/<op_name>.cpp
- Include the BSD 3-Clause header
- include
torch_kernel_helper.h - include
aclrtlaunch_<op_name>.h - Provide the
namespace ascend_kernel { at::Tensor <op_name>(...) { ... } }placeholder
op_kernel/<op_name>.cpp
- Include the BSD 3-Clause header
- include
kernel_operator.h - Provide the
extern "C" __global__ __aicore__ void <op_name>(...)placeholder
Step 5: Registration reminder (must be performed but this skill does not automatically modify code)
After initialization is complete, clearly prompt the user/subsequent skill to update the following three places:
csrc/ops.h: Add function declarationcsrc/register.cpp: Addm.defandm.implcsrc/CMakeLists.txt:OP_SRCSAdd host fileascendc_library(...)Add kernel file- If a same-named ACLNN wrapper exists in the template, the corresponding
aclnn/<op>.cpp
Note: This is the most frequently missed point in practice and must be prompted in advance during the initialization phase.
Step 6: Show the project structure and next steps
Prompt the user:
Ascend-Kernel项目初始化成功!
项目结构:
<ascend_kernel_path>/
├── build.sh # 构建脚本
├── CMakeLists.txt # CMake配置
├── csrc/
│ ├── ops/ # 算子目录
│ ├── aclnn/ # ACLNN算子封装
│ ├── utils/ # 工具类
│ ├── ops.h # 算子声明
│ └── register.cpp # 算子注册
├── python/ # Python包
└── tests/ # 测试用例
下一步操作:
调用 ascendc-operator-design skill 完成设计文档
Notes
- Operator names can contain only letters, digits, and underscores
- The system automatically checks whether the directory exists to avoid overwriting
- If the current directory does not contain an ascend-kernel project, the system automatically copies the template
- New operators must use the AscendC path; the ACLNN implementation path is prohibited
EXEC_KERNEL_CMDThe parameter must be an lvalue; ifdoublefirst convertfloatto a local variable before passing- The test baseline should preferably be based on the PyTorch reference implementation; dtypes not supported on CPU (such as some fp16 pooling) need to be converted to float32 for comparison
Definition of Done (DoD)
After completing this skill, at least the following should be satisfied:
- has located or created the
ascend-kernelproject - has created the
csrc/ops/<op_name>/standard skeleton - has clearly identified the three registration update points (
ops.h/register.cpp/csrc/CMakeLists.txt) - has provided executable build, install, and test commands
- has prompted the key pitfall-prevention items (environment, lvalue parameters, three CMake updates)