Triton Operator Code Gen logo

Triton Operator Code Gen

Organization
Ascend
triton-operator-code-gen

根据 Ascend NPU 算子设计文档(或直接需求)生成 Triton kernel 代码。当用户需要实现 Triton 算子、将设计文档转为可执行代码时使用。核心产出:kernel 代码 + 基本正确性测试。关键词:Triton kernel、算子实现、代码生成、code generation。

Overview

PublisherAscend
Repositoryagent-skills
Skill nametriton-operator-code-gen
Stars
54
Forks
4
Bundled files
2
LicenseMulanPSL-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by Ascend on GitHub. Read the source before you install it.

Installation

Install the Triton Operator Code Gen AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/Ascend/agent-skills/tree/master/skills/triton-operator-code-gen
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Ascend/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/triton-operator-code-gen .claude/skills/triton-operator-code-gen
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Triton Operator Code Gen in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Triton Operator Code Gen on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Triton Operator Code Gen is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Triton 算子代码生成

核心原则

计算逻辑 → Tiling 策略 → 代码实现。顺序不可颠倒。

Triton API 路径优先级

  1. tl.tanh / tl.erf / tl.sqrt — 推荐,性能最优
  2. tl.math.tanh / tl.math.erf — 备选
  3. triton.language.extra.ascend.libdevice / triton.language.extra.libdevice — 以上找不到时

自由度:中高(计算逻辑不可错,实现方式灵活)

参考资源加载

阶段必须加载不要加载
设计 Tilinghardware-architecture.mdtemplates.md
生成 Kerneltemplates.mdhardware-architecture.md
查找 API 签名triton-api-reference.md—

MANDATORY:对应阶段前完整阅读上述文件,不设行数限制。 加载触发:需要确认某个 tl/libdevice API 的签名或参数时,完整阅读 triton-api-reference.md。

工作流

1. 理解需求 → 确认计算逻辑

提取数学公式、输入输出规格、约束条件。用伪代码描述计算过程,必须与用户确认。

2. 实现 Tiling 策略

输入依赖:若已有设计文档(design skill 产出),按其 Tiling 策略实现;若无,则在此设计。

核间切分两原则:

  1. grid = 物理核数(get_npu_aicore_num() 或 get_npu_vectorcore_num())
  2. 核内循环处理多任务,每个核自己计算要处理的数据(负载均衡)
  3. 超过物理核数的grid,超出部分循环串行下发,只会增加开销,无法增加并行度
python
core_num = get_npu_aicore_num()
grid = (core_num,)
# kernel 内:
pid = tl.program_id(0)
num_core = tl.num_programs(0)
blocks_per_core = tl.cdiv(total_blocks, num_core)
for block_idx in range(pid * blocks_per_core, min(...)):
    ...

UB 空间:安全 BLOCK_SIZE = (196608 - 32) / (缓冲区数 × dtype大小) × 0.8

3. 生成 Kernel 代码

按算子类型选择模板(详见 templates.md):

算子类型核心类型模板
归约类vector core模板 1
GEMM/注意力AI core模板 2,6
激活/损失/索引/MoE/后处理vector core模板 3-5,7-8
卷积AI core模板 9

4. 生成基本正确性测试

生成一个基本正确性测试(单 shape × 单 dtype 的 smoke test),确保 kernel 可编译运行且结果正确。全面的精度验证由 precision-eval skill 负责。

反模式清单(NEVER)

  • ❌ 不确认计算逻辑就写代码
  • ❌ 忽略 UB 大小(192KB)
  • ❌ 归约不升精度 FP32 / 使用 int64
  • ❌ grid > 65535 / grid ≠ 物理核数
  • ❌ kernel 中用第三方库 / 逐元素计算
  • ❌ 矩阵乘法退化为 Vector Core 逐元素乘加——Cube Core 矩阵吞吐远高于 Vector Core(数十倍)。即使 GEMV(N=1)也必须用 tl.dot + pad B 到 BLOCK_N=16,禁止用 Vector Core 逐行算内积
  • ❌ 在 NPU 使用 GPU 专用参数(num_stages/num_warps/num_ctas)
  • ❌ 用 PyTorch 而非 Triton 实现算子
  • ❌ 不测试算子正确性 / 不在 NPU 上测试
  • ❌ 归约类算子用双 pass 模式(多次 load 同一数据分别算统计量和归一化)——应单 pass:一次 load 后 tl.sum(x, 1) 在 UB 内完成统计和归一化,见模板 1
  • ❌ 矩阵乘法大矩阵不对角线调度——大矩阵(BLOCK_THRESHOLD ≥ 4)用顺序调度会导致 L2 缓存颠簸,应使用对角线调度,见模板 2
  • ❌ for/while 循环内 return / break — Triton IR 结构化控制流不支持 early exit,编译必报错。替代:while + 布尔标志,或 tl.where mask
  • ❌ tensor[i] 索引操作(读取/赋值/切片)— Triton tensor 不支持 Python 风格下标。替代:tl.where 赋值、tl.gather 读取、tl.extract_slice 切片

常见陷阱

陷阱症状解决
计算逻辑错输出不符预期伪代码描述并确认
UB 溢出"ub overflow"减小 BLOCK_SIZE
coreDim 超限"coreDim > UINT16_MAX"增大 BLOCK_SIZE 或设 TRITON_ALL_BLOCKS_PARALLEL=1
精度损失FP16 不准确归约前升 FP32
索引不够D-cache 报错超大 shape 用 int64
归约类算子慢 5x+aiv_scalar > 80%单 pass:一次 load + tl.sum(x, 1) UB 内全计算
大矩阵 L2 缓存颠簸GEMM 吞吐低于预期对角线调度(task_m_idx = block_idx % NUM_BLOCKS_M),见模板 2

严禁 PyTorch 替代

必须使用 Triton kernel 完成核心计算,严禁以下行为:

  • 禁止用 torch.matmul / torch.mm / torch.bmm / torch.einsum 替代 matmul kernel
  • 禁止用 F.conv1d / F.conv2d / F.conv3d / F.conv_transpose* 替代 conv kernel
  • 禁止用 nn.Conv1d / nn.Conv2d / nn.Conv3d / nn.LayerNorm / nn.BatchNorm* / nn.GroupNorm / nn.InstanceNorm 等高阶接口
  • 禁止用 F.layer_norm / F.batch_norm / F.group_norm / F.instance_norm 替代 norm kernel
  • 禁止用 torch.cumsum / torch.cumprod / torch.flip 等直接替代 scan kernel
  • 禁止写了 @triton.jit kernel 但 forward 不调用它 唯一允许的 torch 调用: tensor 分配(empty/zeros/ones)、形状操作(view/reshape/permute/contiguous)、数据搬运(to/npu)

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Triton Operator Code Gen AI skill do?

根据 Ascend NPU 算子设计文档(或直接需求)生成 Triton kernel 代码。当用户需要实现 Triton 算子、将设计文档转为可执行代码时使用。核心产出:kernel 代码 + 基本正确性测试。关键词:Triton kernel、算子实现、代码生成、code generation。

Why use Triton Operator Code Gen on TypingMind?

Because you install it once and use it with any model. Triton Operator Code Gen is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Triton Operator Code Gen in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/Ascend/agent-skills/tree/master/skills/triton-operator-code-gen. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Triton Operator Code Gen?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Triton Operator Code Gen?

As many as you like. As long as a model supports skills, you can use Triton Operator Code Gen with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Triton Operator Code Gen AI skill free?

Yes. It is published on GitHub by Ascend under the MulanPSL-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇