Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions HONOR_CODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Honor Code

本仓库(ntops-forge + ntops-copilot)为 2026 春季启元人工智能大赛 **九齿 .skill 创新挑战赛道** 参赛作品。

本人(于鸿伟,GitHub ID:hongwei-2026)声明:

1. `skills/ntops-forge`、`skills/ntops-copilot` 及 `scripts/` 中的工作流、脚本与文档由本人独立编写。
2. 内核代码模式参考 InfiniTensor 公开发布的 `ninetoothed-examples`,已在 `REFERENCE.md` 中注明。
3. 未抄袭其他参赛选手作品;未使用未经授权的商业代码;仓库不含密钥或隐藏答案。
4. 后续向指定仓库提交的 PR 将附本 HONOR_CODE 与 REFERENCE。

签署日期:2026-06-08
105 changes: 105 additions & 0 deletions PR_DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
## 1. Skill 名称、赛题编号和小组名称

- **Skill 名称**:ntops-forge(主)+ ntops-copilot(辅)
- **赛题编号**:T3-1-1
- **小组名称**:于鸿伟

**独立 skill 仓库**:https://github.com/hongwei-2026/qiyuanbisai
**Commit**:https://github.com/hongwei-2026/qiyuanbisai/commit/750db19

## 2. 适用任务范围与不适用范围

**适用**:ntops elementwise 算子(silu/add/gelu/relu/mul);2026-spring PR 规范;GPU + editable ntops。

**不适用**:Triton `@triton.jit`;norm/attention 需先读 reference;无 CUDA。

## 3. 安装与使用方式

```bash
source /root/miniconda3/bin/activate base
pip install pytest && pip install -e /path/to/ntops
python skills/competition/ntops-forge/scripts/forge.py gate --ntops-root /path/to/ntops
```

Cursor:将 `skills/competition/ntops-forge/` 链到 `.cursor/skills/ntops-forge/`。

## 4. 自测运行记录

| 记录 | 路径 |
|------|------|
| GPU 测试报告 | `skills/competition/ntops-forge/docs/GPU_Test_Report.md` |
| A/B 报告 | `skills/competition/ntops-forge/docs/AB_Report.md` |
| 四类自测案例 | `skills/competition/ntops-forge/docs/selftests/`(ST1–ST4) |
| ST2/ST3 结果 | `skills/competition/ntops-forge/docs/st2_st3_gpu_results.md` |
| 审计日志 | 独立仓库 `docs/forge_runs.jsonl`、`docs/ab_runs.csv` |

**环境**:AutoDL · RTX 4080 · `pip install -e /root/work/ntops` · 工作目录 `/root/work/skill`

**ST1–ST4 摘要**:

| 编号 | 类型 | 状态 |
|------|------|------|
| ST1 | 逐元素/广播(silu/add/gelu/relu/mul) | ✅ `forge gate` 五算子全通过 |
| ST2 | 归约/分块(softmax) | ✅ `test_softmax.py` 8 passed |
| ST3 | 布局 stride(max_pool2d) | ✅ `test_max_pool2d.py` 62 passed, 54 skipped |
| ST4 | 性能/诊断 | ✅ A/B + silu benchmark + fix_cards |

## 5. 自测结果(有 skill vs 无 skill)

见 `skills/competition/ntops-forge/docs/AB_Report.md`:

| 指标 | Baseline(无 skill) | Treatment(有 skill) |
|------|---------------------|----------------------|
| preflight 通过率 | 0% | 100% |
| pytest 通过率 | 未跑通 | 100% |
| 平均步骤 | 6 | 1 |
| 人工介入 | 4 次 | 0 次 |
| 平均耗时 | ~1200s | ~7s/算子 |

**GPU forge gate(五算子)**:

| 算子 | pytest | 耗时 |
|------|--------|------|
| silu | 8 passed | 6.7s |
| add | 8 passed | 6.9s |
| gelu | 8 passed, 8 skipped | 7.0s |
| relu | 16 passed | 10.9s |
| mul | 8 passed | 12.5s |

**GATE OK: all operators passed**

## 6. HONOR_CODE 与 REFERENCE

- 本 PR 根目录 [`HONOR_CODE.md`](HONOR_CODE.md)
- 本 PR 根目录 [`REFERENCE.md`](REFERENCE.md)

## 7. Proposal 与报告

| 材料 | 位置 |
|------|------|
| Proposal | 独立仓库 `docs/Proposal.md` |
| 自测计划 | 独立仓库 `docs/SelfTestPlan.md` |
| 中期报告 | 独立仓库 `docs/MidTermReport.md`(PDF:`于鸿伟_九齿skill创新挑战_中期报告.pdf`) |
| 初赛 zip | `submission-initial-20260608.zip` |

---

## pytest output(CONTRIBUTING 要求)

### ninetoothed 仓库 pytest

本 PR **仅新增** `skills/competition/`,**未修改** `src/ninetoothed/`。上游 `master` 已在 NVIDIA self-hosted runner 通过 pytest([#158](https://github.com/InfiniTensor/ninetoothed/pull/158))。

### ntops skill 验收(GPU · RTX 4080 · 2026-06-08)

```text
$ python skills/competition/ntops-forge/scripts/forge.py gate --ntops-root /root/work/ntops
GATE OK: all operators passed (silu, add, gelu, relu, mul)

$ cd /root/work/ntops && pytest tests/test_softmax.py -q
8 passed

$ cd /root/work/ntops && pytest tests/test_max_pool2d.py -q
62 passed, 54 skipped in ~175s
# 54 skipped: upstream Invalid padding(非失败)
```
25 changes: 25 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Reference

## 1. NineToothed 官方示例(内核模式)

| 项目 | 链接 | 使用内容 |
|------|------|----------|
| ninetoothed-examples | https://github.com/InfiniTensor/ninetoothed-examples | `ops/ninetoothed/kernels/add.py`、`silu.py` 的 arrangement/application/make 结构;用于 `scaffold_kernel.py` 模板设计与 `reference.md` |
| 许可证 | Apache-2.0 | 见上游 LICENSE |

**使用方式**:仅借鉴文件结构与 API 用法;本仓库脚本生成的是带 TODO 的骨架,非复制粘贴完整实现。

## 2. 文档

| 资源 | 链接 |
|------|------|
| NineToothed 文档 | https://ninetoothed.org/ |
| ntops | https://github.com/InfiniTensor/ntops |
| 大赛指南 | 2026 春季启元人工智能大赛指南(主办方发布) |

## 3. 无外部代码的组件

- `scripts/preflight.py` — 本人编写(Python ast 标准库)
- `scripts/pack_submission.py` — 本人编写
- `skills/ntops-forge/SKILL.md`、`skills/ntops-copilot/SKILL.md` — 本人编写
- `scripts/forge.py` 及流水线脚本 — 本人编写
13 changes: 13 additions & 0 deletions skills/competition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 2026 Spring · NineToothed .skill Competition Submissions

## ntops-forge(于鸿伟 / hongwei-2026 · T3-1-1)

- 主 skill:`ntops-forge/`
- 辅 skill:`ntops-copilot/`
- 独立仓库:https://github.com/hongwei-2026/qiyuanbisai
- Commit:https://github.com/hongwei-2026/qiyuanbisai/commit/e7b32bb

```bash
# 在 ntops 环境验收(脚本在 ntops-forge/scripts/)
python skills/competition/ntops-forge/scripts/forge.py gate --ntops-root /path/to/ntops
```
188 changes: 188 additions & 0 deletions skills/competition/ntops-copilot/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
---
name: ntops-copilot
description: >-
NineToothed ntops operator copilot: one-command run_task, env doctor, ntops-native
premake+element_wise scaffolds, formula cookbook, preflight, pytest and contest PR.
Use for ntops, NineToothed, 九齿, 算子开发, premake, element_wise, CUDA pytest,
2026-spring PR, InfiniCore.
---

# ntops-copilot

让 Agent **更快、更准** 完成 ntops 九齿算子开发。核心不是堆文档,而是:

1. **对齐 ntops 真实写法**(`premake` + `element_wise.arrangement`)
2. **一条命令开工**(`run_task.py`)
3. **任务卡驱动公式注入**(YAML `formula_hint` → 骨架里已写好 `application()`)
4. **闭环验证**(`verify_task` / `compare_ref` / `register_op`)
5. **公式速查 + 自检护栏**(少返工、少写成 Triton)

## 何时启用

`ntops`、`NineToothed`、`九齿`、`算子`、`premake`、`kernels/`、`pytest`、`2026-spring` PR。

## 30 秒上手(人类/Agent 都适用)

```bash
# 0) 环境自检(GPU 机先 source conda)
python scripts/doctor.py

# 1) 一条命令:读任务卡 -> 注入公式 -> 生成 kernel/torch -> preflight
python scripts/run_task.py --task silu --ntops-root /path/to/ntops --contest-id T1-1-X

# 2) 一键完工:验证 + 记 A/B(推荐演示/提交)
python scripts/run_task.py --task add --ntops-root /path/to/ntops --finish

# 3) 新算子:自动注册 + 全链路验证
python scripts/run_task.py --task gelu --ntops-root /path/to/ntops --force --register --verify

# 4) 交 PR 前严格自检
python scripts/preflight.py src/ntops/kernels/silu.py --kernel --strict
python scripts/compare_ref.py src/ntops/kernels/silu.py --ref src/ntops/kernels/silu.py
```

GPU 云(如 SeetaCloud)常见要先:
```bash
source /root/miniconda3/bin/activate base
```

## ntops 真实范式(必须优先)

ntops **不是** ninetoothed-examples 那种顶层 `ninetoothed.make(...)`。

标准结构:

```python
from ntops.kernels.element_wise import arrangement

def application(input, output):
output = ... # noqa: F841

def premake(ndim, dtype=None, block_size=None):
arrangement_ = functools.partial(arrangement, block_size=block_size)
tensors = (Tensor(ndim, dtype=dtype), Tensor(ndim, dtype=dtype))
return arrangement_, application, tensors
```

torch 封装:

```python
kernel = _cached_make(ntops.kernels.<op>.premake, input.ndim)
kernel(input, output)
```

生成骨架时用:`--style ntops`(默认)。

## 包结构

| 目录 | 内容 |
|------|------|
| `SKILL.md` | 本文件 |
| `tasks/` | 任务卡 YAML |
| `examples/` | `add_walkthrough.md` |
| `references/` | 查阅索引 |
| `tests/` | 验证说明 |

双 Skill 边界见 `docs/DualSkillGuide.md`。

## 标准工作流

### Step 0 — doctor

```bash
python scripts/doctor.py
```

确认:`ninetoothed`、`torch`、`CUDA`(跑 pytest 需要)、`ntops` 是否就绪。

### Step 1 — 选任务

读 `tasks/task_<op>.yaml`,或让用户给算子名 + 公式。

### Step 2 — run_task(推荐)

```bash
python scripts/run_task.py --task <op> --ntops-root <ntops> --contest-id <赛题号>
```

自动生成:
- `src/ntops/kernels/<op>.py`
- `src/ntops/torch/<op>.py`(若给了 ntops-root)

### Step 3 — 只改 application()

查 `formulas.md`,对照 `reference_kernel` 链接。**不要改 premake 结构。**

### Step 4 — 注册 + 验证

```bash
python scripts/register_op.py --name <op> --ntops-root <ntops>
python scripts/verify_task.py --name <op> --ntops-root <ntops> --pytest
python scripts/preflight.py src/ntops/kernels/<op>.py --kernel --strict
```

有官方参考实现时,用 `compare_ref.py` 对比 `application()` 是否对齐。

### Step 5 — PR

- 分支:`2026-spring-hongwei-2026-<赛题号>`
- 标题:`[2026春季][赛题号] hongwei-2026`
- 模板:`templates/PR_DESCRIPTION.md`

## 硬性原则

1. **正确性 > 性能**
2. **禁止 Triton 交卷**:发现 `@triton.jit` 必须改 NineToothed
3. **禁止抄袭**:更新 `REFERENCE.md`
4. **preflight 不过禁止 PR**

## 常见错误

| 现象 | 处理 |
|------|------|
| `can't open file '/root/scripts/...'` | 先 `cd /root/work/skill`(脚本不在 `$HOME`) |
| `conv2d` 等全量 pytest 失败 | **不要** `pytest tests/` 全跑;用任务卡 `pytest_file` |
| 写了 examples 风格 `make` | 改用 `--style ntops` 重新 scaffold |
| pytest 全 skipped | 无 CUDA;换 GPU 机并 `doctor` 确认 |
| `python3 not found` | 用 conda:`source .../activate base && python` |
| application 公式错 | 查 `formulas.md` + `compare_ref.py` |

## v0.5 改进

| 能力 | 说明 |
|------|------|
| **精准 pytest** | 任务卡 `pytest_file`,避免 `-k add` 误匹配 |
| **`--finish`** | `run_task` 一步:scaffold → verify → record_run |
| **`list_tasks`** | 列出全部任务卡与对应测试文件 |

## v0.4 创新点(可演示)

| 能力 | 脚本 | 价值 |
|------|------|------|
| **任务卡→代码** | `run_task` + `formula_hint` | Agent 不必猜公式,少幻觉 |
| **语义对照** | `compare_ref.py` | AST 对比 `application()` 与官方实现 |
| **闭环验收** | `verify_task.py` | preflight + 注册 + pytest 一步完成 |
| **可测 A/B** | `record_run.py` + `eval_ab.py` | 跑完自动记 CSV,报告可复现 |

## 脚本

| 脚本 | 作用 |
|------|------|
| `run_task.py` | **一键任务流**(`--finish` / `--register` / `--verify`) |
| `list_tasks.py` | 列出任务卡与 pytest 文件 |
| `verify_task.py` | 注册检查 + preflight + 可选 pytest |
| `register_op.py` | 幂等写入 `__init__.py` |
| `compare_ref.py` | `application()` 与参考实现对齐检查 |
| `record_run.py` | 记录 A/B 跑数到 CSV |
| `doctor.py` | 环境/CUDA/依赖自检 |
| `scaffold_kernel.py` | 生成 kernel(`--formula` 注入) |
| `scaffold_torch.py` | 生成 torch 封装 |
| `preflight.py` | 拦截 Triton/结构/TODO(`--strict`) |
| `eval_ab.py` | A/B 评测汇总 |

## 资源

- `formulas.md` — 公式速查
- `reference.md` — API 与目录
- `examples.md` — silu 端到端
- `tasks/*.yaml` — 任务卡
7 changes: 7 additions & 0 deletions skills/competition/ntops-copilot/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "ntops-copilot",
"version": "0.5.0",
"stage": "initial-round",
"author": "hongwei-2026",
"description": "NineToothed ntops operator development copilot skill."
}
Loading