ascendc-operator-project-init

Original🇨🇳 Chinese
Translated
5 scripts

Initialize AscendC operator project and create compilable operator skeleton. Trigger scenarios: (1) User requests to create a new operator; (2) Keywords: ascendc operator, new operator, operator directory, operator initialization; (3) Need to quickly implement based on ascend-kernel template. This skill not only creates directories, but also outputs standard files and checklists for "continuous development".

3installs
Added on

NPX Install

npx skill4agent add ascend/agent-skills ascendc-operator-project-init

SKILL.md Content (Chinese)

View Translation Comparison →

AscendC Operator Project Initialization

Skill Type: Process-oriented (multi-stage workflow with stage checkpoints)
Quickly create an Ascend-Kernel operator project, where all operators are uniformly generated under the
csrc/ops
directory, ensuring that subsequent design/coding/compilation testing stages can be directly entered.

Core Principles

  1. Single Directory: All operators are uniformly generated under the
    csrc/ops
    directory
  2. Security First: Automatically check if the directory exists to avoid overwriting existing files
  3. Naming Convention: Strictly require snake_case naming format
  4. Project Detection: Prioritize using existing ascend-kernel projects; copy the template if it does not exist
  5. Verifiable Results: Output directory structure, registration change points, and build/test commands
  6. Built-in Pitfall Prevention: Explicitly prompt for three CMake updates, left-value requirements for
    EXEC_KERNEL_CMD
    , and environment dependencies

Workflow

dot
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 existing operator directories
  • ❌ Do not use naming formats other than snake_case
  • ❌ Do not skip directory existence checks
  • ❌ Do not only create operator directories without creating
    op_host/
    and
    op_kernel/
  • ❌ Do not forget to update
    ops.h
    ,
    register.cpp
    , and
    csrc/CMakeLists.txt
    (three places)
  • ❌ Do not directly pass right-value temporary variables in
    EXEC_KERNEL_CMD
  • ❌ Do not use ACLNN as the implementation path for new operators

Step 1: Detect and Initialize Ascend-Kernel Project

1.1 Detect Ascend-Kernel Project Location

Detection Strategy:
  1. Check if the current directory is an ascend-kernel project (contains build.sh, CMakeLists.txt, csrc/)
  2. Check the ascend-kernel subdirectory under the current directory
  3. Check ascend-kernel projects in first-level subdirectories
Execution Command:
bash
bash <skill_dir>/scripts/detect_ascend_kernel_project.sh

1.2 Process Detection Results

Detection ResultProcessing Method
PROJECT_FOUND:<path>
Use the existing project and proceed to Step 2
PROJECT_FOUND_NO_OPS:<path>
Create the csrc/ops/ directory in the project and proceed to Step 2
PROJECT_NOT_FOUND
Copy the ascend-kernel template to the current directory and proceed to Step 2
MULTIPLE_PROJECTS:<path1> <path2> ...
List all projects and let the user select

1.3 Copy Project Template (if needed)

Execution Command:
bash
cp -r "<skill_dir>/templates/ascend-kernel" ./ascend-kernel
chmod +x ./ascend-kernel/build.sh
Important: Must run
chmod +x build.sh
after copying, otherwise a
Permission denied
error will occur during compilation.
Verification: Confirm that the ascend-kernel directory contains:
  • build.sh (with execution permission)
  • CMakeLists.txt
  • csrc/ directory

Step 2: Collect Operator Information (Minimum Required)

Mandatory Information to Confirm:
InformationFormat RequirementDescription
Operator Namesnake_casee.g.,
rms_norm
,
flash_attn
Confirmation Message:
=== Ascend-Kernel Operator Information ===
Operator Name: <op_name>
Generation Path: <ascend_kernel_path>/csrc/ops/<op_name>

Confirm creation? [Y/n]

Step 3: Check Preconditions

  1. Confirm being in the root directory of the ascend-kernel project (build.sh exists and has execution permission)
  2. Confirm that the csrc/ops/ directory exists (create it with
    mkdir -p csrc/ops
    if it does not exist)
  3. Confirm that the target directory
    csrc/ops/<op_name>
    does not exist

Step 4: Create Operator Skeleton

Create Directories:
bash
mkdir -p csrc/ops/<op_name>/op_host csrc/ops/<op_name>/op_kernel
Create the following files under
csrc/ops/<op_name>/
:
csrc/ops/<op_name>/
├── CMakeLists.txt
├── design.md                  # Design document placeholder (to be filled by design skill)
├── op_host/
│   └── <op_name>.cpp          # Host placeholder (to be replaced by code-gen skill)
└── op_kernel/
    └── <op_name>.cpp          # Kernel placeholder (to be replaced by code-gen skill)

4.1 Minimum File Templates

  1. CMakeLists.txt
cmake
ascendc_add_operator(OP_NAME <op_name>)
  1. op_host/<op_name>.cpp
  • Includes BSD 3-Clause header
  • Includes
    torch_kernel_helper.h
  • Includes
    aclrtlaunch_<op_name>.h
  • Provides placeholder for
    namespace ascend_kernel { at::Tensor <op_name>(...) { ... } }
  1. op_kernel/<op_name>.cpp
  • Includes BSD 3-Clause header
  • Includes
    kernel_operator.h
  • Provides placeholder for
    extern "C" __global__ __aicore__ void <op_name>(...)

Step 5: Registration Reminders (Must be Executed but Not Automatically Modified in This Skill)

After initialization, explicitly prompt the user/subsequent skills to update the following three places:
  1. csrc/ops.h
    : Add function declarations
  2. csrc/register.cpp
    : Add
    m.def
    and
    m.impl
  3. csrc/CMakeLists.txt
    :
    • Add host files to
      OP_SRCS
    • Add kernel files to
      ascendc_library(...)
    • If there is a duplicate ACLNN wrapper in the template, the corresponding
      aclnn/<op>.cpp
      must be removed
Note: This is the most frequently missed point in practice, so it must be prompted in advance during the initialization phase.

Step 6: Display Project Structure and Next Steps

Prompt User:
Ascend-Kernel project initialization succeeded!

Project Structure:
<ascend_kernel_path>/
├── build.sh                    # Build script
├── CMakeLists.txt              # CMake configuration
├── csrc/
│   ├── ops/                    # Operator directory
│   ├── aclnn/                  # ACLNN operator wrapper
│   ├── utils/                  # Utility classes
│   ├── ops.h                   # Operator declarations
│   └── register.cpp            # Operator registration
├── python/                     # Python package
└── tests/                      # Test cases

Next Steps:
  Call ascendc-operator-design skill to complete the design document

Notes

  • Operator names can only contain letters, numbers, and underscores
  • The system will automatically check if the directory exists to avoid overwriting
  • If there is no ascend-kernel project in the current directory, the system will automatically copy the template
  • New operators must use the AscendC path; ACLNN implementation path is prohibited
  • EXEC_KERNEL_CMD
    parameters must be left-values; e.g., convert
    double
    to a local float variable before passing
  • It is recommended to use PyTorch reference implementation as the test baseline; for dtypes not supported by CPU (such as some fp16 pooling), convert to float32 for comparison

Definition of Done (DoD)

After completing this skill, at least the following requirements should be met:
  • Located or created the
    ascend-kernel
    project
  • Created the standard skeleton for
    csrc/ops/<op_name>/
  • Identified three registration update points (
    ops.h
    /
    register.cpp
    /
    csrc/CMakeLists.txt
    )
  • Provided executable build, installation, and test commands
  • Prompted key pitfall prevention items (environment, left-value parameters, three CMake updates)