skills/triton/op-task-extractor/SKILL.md
从用户 PyTorch/Python 代码中提取算子实现,构建为 KernelBench 格式的标准化 任务文件。支持两种模式:单 case(单一自包含 .py,get_inputs 返回单组)和 多 case(.py + 同名 .json 配对,get_input_groups 返回多组)。
npx skillsauth add Just-it/AscendOpGenAgent op-task-extractorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
按以下优先级判定输入属于哪种模式(任一命中即定型):
.py 已定义 get_input_groups() 函数 → 多 case 模式.py 同目录存在同名 .json 文件 → 多 case 模式下游 verify.py / benchmark.py 已内建判断(优先 get_input_groups、回落 get_inputs),
禁止将多 case 源降级为单 case 任务文件。
最终生成的文件必须是 单一自包含 Python 文件,仅包含以下 4 个部分:
import 区:只允许 torch / torch.nn / 标准库class Model(nn.Module):包装待优化算子逻辑(含 __init__ 和 forward)def get_inputs():返回 forward() 的输入参数列表def get_init_inputs():返回 __init__() 的初始化参数列表输出 .py + .json 一对文件,两者必须同时复制到工作目录、保持同名同目录关系:
{op_name}.py:含 Model + get_input_groups() + get_init_inputs(),
其中 get_input_groups() 通过 os.path.dirname(__file__) 读取同目录 {op_name}.json{op_name}.json:JSONL 格式,每行一个 case 的输入规格详细格式规范见 @references/kernelbench-format.md
arch 配置(framework=torch、backend=ascend、dsl=triton_ascend 为固定值)Model.forward() 中Model.__init__()get_inputs() 和 get_init_inputs(){工作目录}/{op_name}.py核心原则:原样透传,不改写源码
.py → {工作目录}/{op_name}.py
get_input_groups、get_init_inputs 的任何逻辑.py 缺少 get_init_inputs(必需项),可补一个返回 [] 的实现.json → {工作目录}/{op_name}.json
.py 同目录发现同名 .json.py 同名同目录(不要改名、不要嵌套子目录),保证 os.path.dirname(__file__) 仍能定位get_inputs() 兼容层:下游 verify.py / benchmark.py 已优先调用
get_input_groups(),无需再注入使用本 skill 自带的验证脚本进行静态检查和运行时检查。
验证命令(使用 bash 工具执行):
python3 <skill-path>/scripts/validate_task.py /abs/path/{op_name}.py --json
其中 <skill-path> 为本 skill 所在目录的绝对路径。
验证脚本检查项:
class Model(nn.Module)、forward、get_init_inputs 必需;
输入提供函数 get_inputs 或 get_input_groups 至少存在其一exec → Model(*get_init_inputs()) → 取输入提供方get_input_groups() → 遍历全部 groups,每组依次执行 forward + NaN/Inf + 一致性检查get_inputs() → 单 case 执行cases_tested / cases_passed结果处理:
[VALID] → 验证通过,进入 Step 5[INVALID] → 根据错误信息修复(多 case 模式禁止改成单 case 来"绕过");
最多重试 2 次多 case 模式注意:
__file__,因此 .json 必须已在目标位置cases_passed < cases_tested,说明部分 case 在参考实现下就跑不通,
应反馈给用户而非裁剪 cases⛔ 禁止事项:
验证通过后,必须使用 question 工具将完整的任务文件展示给用户,请求确认。
.json 行数 + 前 2 行示例)算子生成完成,请查看生成代码:
请选择:
- 接受
- <让用户输入修改要求>
具体要求:
question 工具,不能只打印文本让用户选择question 的描述中展示 .py 完整内容;多 case 模式下附 .json 摘要处理回复:
⛔ 禁止事项:
question 工具调用| 约束 | 说明 |
|------|------|
| 自包含 | 单 case:所有依赖函数必须内联;多 case:仅允许依赖同名 .json |
| 可执行 | 单 case:Model(*get_init_inputs())(*get_inputs()) 必须直接运行;多 case:对所有 groups 都必须可运行 |
| 确定性 | 给定相同输入,输出必须一致 |
| 无 NaN/Inf | forward 输出不能包含 NaN 或 Inf |
| 禁止重写 | 原始函数可运行就直接复用,一行都不改;多 case 模式下源 .py 整体原样透传 |
| 返回一致 | 返回类型/形状必须与原始实现一致 |
| 合理输入 | get_inputs 应提供合理大小的输入(不能过小或过大) |
| 模式不可降级 | 含 get_input_groups 或同名 .json 的源必须走多 case 流程 |
用户说:"/path/to/model.py 的 matmul_with_bias 函数有优化空间,shape 信息在 /path/to/config.py"
import torch
import torch.nn as nn
class Model(nn.Module):
def __init__(self, in_features: int, out_features: int):
super(Model, self).__init__()
self.weight = nn.Parameter(torch.randn(out_features, in_features))
self.bias = nn.Parameter(torch.randn(out_features))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.matmul(x, self.weight.t()) + self.bias
def get_inputs():
batch_size = 32
in_features = 1024
return [torch.randn(batch_size, in_features)]
def get_init_inputs():
in_features = 1024
out_features = 512
return [in_features, out_features]
用户说:"/path/to/benchmarks/level430/1_LogicalAnd.py,多 shape 评测"
extractor 自动发现同目录 1_LogicalAnd.json(52 行 JSONL)。
{工作目录}/1_LogicalAnd.py # 复制源文件,不改一行
{工作目录}/1_LogicalAnd.json # 复制源 JSON,不改一行
验证脚本输出:cases_tested: 52, cases_passed: 52。
tools
多 Case 专用 Kernel 分裂 Skill — 在泛用 Kernel 优化完成后,针对不同 Shape/Case 特征 生成专用 Kernel,构建智能调度器,实现性能最大化。失败自动回退到泛用 Kernel。
tools
擅长在 Ascend NPU 平台上编写高效 Triton 算子的性能优化专家。 按照严格的顺序逐步优化 Triton 代码,每次只尝试一个优化点, 确保优化前后功能一致、精度一致。 ⚠️ 只能使用本 skill 规定的优化方式,禁止使用任何超出本 skill 之外的优化方式。
testing
算子代码验证 Skill — 按照标准验证流程验证生成的内核代码。 创建验证项目文件,调用 scripts/verify.py 运行验证,验证通过后 调用 scripts/benchmark.py 进行性能测试并收集结果。
tools
Triton Ascend 算子代码生成 Skill — 根据 KernelBench 格式任务描述生成高性能 Triton Ascend 内核代码。支持首次生成和基于错误反馈的迭代优化。