跳到正文
cccskills
免费在 GitHub 公开

add-new-op

Add new operators to MNN through a staged workflow covering data definitions, output shapes, computation, and tests. It prioritizes combining existing operators, with CPU implementations and additional hardware backends used when needed.

中文简介正在准备中。当前显示英语简介。

原文查看原文说明

为 MNN 框架新增算子。包含 Schema 定义、形状计算、几何计算、后端实现、单元测试的完整 TDD 流程。分 5 步执行,每步有独立测试标准。

查看安装方法

适用场景

  • Adding a new operator to MNN
  • Implementing output shape calculations
  • Combining existing operators for new operations
  • Extending operators to additional hardware backends

包含的文件(6)

  • SKILL.md5.9 KB
  • step1-schema.md6.3 KB
  • step2-shape.md4.7 KB
  • step3-compute.md9.3 KB
  • step4-test.md6.5 KB
  • step5-optimize.md6.8 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

MNN 新增算子 SKILL

触发条件:当用户请求为 MNN 添加/实现一个新的算子时触发。常见表述包括:"添加xxx算子"、"实现xxx op"、"支持xxx操作"等。

概述

本 SKILL 指导 AI Agent 为 MNN 框架新增算子支持。采用 TDD(测试驱动)模式,每一步都有明确的测试标准。

核心原则

优先级顺序:几何计算 > 后端实现

  • 几何计算:如果新算子可以通过已有算子的拆解、组合实现,优先用几何计算,不需要为每个后端单独实现
  • 后端实现:如果算子无法拆解,或性能敏感需要原生实现,则添加后端实现

注意事项

严禁访问以下目录:schema/private/ 和 source/internal/,包含内部私有代码,不得读取、修改或引用。

名字必须等于真实物理量:新增或修改任何后端算子 / kernel 代码时,变量、字段、宏名指代的必须是代码里真正装的那个量——blockSize 是"块内元素数"还是"每行块数"、step 是元素数还是字节数、shape 门禁的自变量是通道数还是 tile 数。名字骗人不会报错(编译、对拍、单测都过),但后续基于它写的阈值、门控、分档会一起错。写完对着实际索引 / 指针算术复核一遍,不符就改名。


核心文件清单

目录/文件作用何时修改
schema/default/MNN.fbs算子类型和参数定义(开源版)每个新算子都需要;内部仓库实际生成走 schema/private/,计划开源发布的算子两份都要改,详见 step1-schema.md §1.5
schema/default/CaffeOps.fbsCaffe 框架算子参数有参数时
schema/default/TensorflowOp.fbsTF 框架算子参数有参数时
source/shape/Shape*.cpp形状(维度)计算输出形状与输入不同时
source/geometry/Geometry*.cpp几何计算(算子拆解)优先实现
source/backend/cpu/CPU*.cpp/.hppCPU 后端实现无法几何拆解时
source/backend/metal/Metal*.cpp/.hpp/.metalMetal 后端实现扩展到 Metal
source/backend/opencl/execution/*.cpp/.h/.clOpenCL 后端实现扩展到 OpenCL
source/backend/vulkan/image/execution/Vulkan*.cpp/.hppVulkan 后端实现扩展到 Vulkan
source/backend/cuda/execution/*.cu/.cuhCUDA 后端实现扩展到 CUDA
include/MNN/expr/ExprCreator.hpp表达式 API 函数需要用户可调用时
test/op/*Test.cpp单元测试每个新算子都需要
tools/script/register.py注册脚本添加 shape/geometry/backend 后运行

分步流程总览

┌──────────────────────────────────────────────────────────┐
│  步骤 1: Schema 定义 (step1-schema.md)                    │
│  输入: 算子名称、语义、输入输出定义                          │
│  输出: MNN.fbs 中的 OpType + 可选的参数 table              │
│  测试: flatc 编译通过                                      │
├──────────────────────────────────────────────────────────┤
│  步骤 2: 形状计算 (step2-shape.md)                        │
│  输入: 算子的输入输出维度关系                                │
│  输出: source/shape/Shape*.cpp                             │
│  测试: register.py 运行成功 + cmake 编译通过               │
├──────────────────────────────────────────────────────────┤
│  步骤 3: 几何计算或 CPU 实现 (step3-compute.md)            │
│  输入: 算子的计算逻辑                                      │
│  输出: Geometry*.cpp 或 CPU*.cpp                           │
│  测试: register.py + 编译通过                              │
├──────────────────────────────────────────────────────────┤
│  步骤 4: 单元测试 (step4-test.md)                         │
│  输入: 算子的预期行为                                      │
│  输出: test/op/*Test.cpp                                  │
│  测试: ./run_test.out op/MyOp 全部通过                     │
├──────────────────────────────────────────────────────────┤
│  步骤 5: 扩展后端与优化 (step5-optimize.md)                │
│  输入: 步骤 4 通过                                        │
│  输出: Metal/OpenCL/Vulkan/CUDA 实现                      │
│  测试: 各后端单元测试通过                                   │
└──────────────────────────────────────────────────────────┘

步骤选择指南

情况需要执行的步骤
输出形状与输入完全一致,可几何拆解1 → 3(几何) → 4
输出形状与输入不同,可几何拆解1 → 2 → 3(几何) → 4
输出形状与输入不同,需要后端实现1 → 2 → 3(CPU) → 4 → 5
输出形状与输入完全一致,需要后端实现1 → 3(CPU) → 4 → 5

开始执行

现在请打开 skills/add-new-op/step1-schema.md,开始步骤 1。

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

bugfix

免费

Triage incorrect MNN outputs and regressions by matching symptoms to focused debugging guides. Use backend comparisons, minimal reproductions, and controlled experiments to investigate memory issues, quantization, concurrency, numeric limits, caches, and inconsistent results across runs.

中文简介正在准备中。当前显示英语简介。

  • Investigating garbled or degraded MNN output
  • Comparing results across execution backends
  • Tracking regressions after optimization changes
alibaba/MNN1.6万2026年10月21日 更新

cpu

免费

Find the right workflow for MNN CPU work, from investigating slow execution to implementing calculation routines and planning tests. Compare ARM, x86_64, and RISC-V backend structures before choosing the relevant guidance.

中文简介正在准备中。当前显示英语简介。

  • Choosing where to start an MNN CPU performance investigation
  • Finding guidance for CPU kernel implementation
  • Comparing ARM, x86_64, and RISC-V backend structures
alibaba/MNN1.6万2026年10月21日 更新

Develop or port CPU kernels, the low-level calculation routines used by MNN. Follow staged implementation guidance, check data layout agreements, register functions, and verify correctness across processor instruction sets and numerical precision settings.

中文简介正在准备中。当前显示英语简介。

  • Implementing or porting an MNN CPU kernel
  • Adding support for a processor instruction set
  • Changing packed data layouts or tile sizes
alibaba/MNN1.6万2026年10月21日 更新

Find why MNN CPU inference is slow by examining threads, scheduling, memory layout, and the processor instruction path actually used. Operator-level measurements and comparisons with other inference frameworks help identify which layer needs improvement across ARM, x86_64, and RISC-V.

中文简介正在准备中。当前显示英语简介。

  • Investigate slow CPU operators or LLM inference
  • Diagnose thread count and memory usage anomalies
  • Locate the cause of a performance regression
alibaba/MNN1.6万2026年10月21日 更新

Optimize and troubleshoot MNN's Hexagon DSP backend, which runs computation on a dedicated processor. The workflow uses device measurements to identify bottlenecks, compare v79 and v81 architectures, verify correctness, and investigate crashes or memory usage.

中文简介正在准备中。当前显示英语简介。

  • Finding bottlenecks in Hexagon operators
  • Checking changes on v79 and v81 devices
  • Investigating cDSP crashes and device disconnects
alibaba/MNN1.6万2026年10月21日 更新

Guide MNN Metal backend development and optimization, from finding performance bottlenecks to changing GPU computation, combining operators, and adjusting execution scheduling. A task-based document index directs work toward relevant implementation rules, benchmarks, and correctness checks.

中文简介正在准备中。当前显示英语简介。

  • Finding bottlenecks in Metal inference
  • Adding or modifying Metal kernels
  • Combining operators into one GPU dispatch
alibaba/MNN1.6万2026年10月21日 更新

查看 alibaba 的全部技能

报告此技能的问题