README.md 10.5 KB
Newer Older
1
2
3
4
<div align="center">

# LightOp KernelPilot

5
**基于 Humanize 的 LightOp/DCU 算子开发与优化 skill 包。**
6
7
8

</div>

9
10
11
12
13
LightOp KernelPilot 是面向 LightOp DCU 算子库的 KernelPilot 工作流改造版。
它保留了自主算子开发循环里真正有用的部分:明确算子语义、正确性参考、
workload 分布、benchmark 证据、profile digest、尝试记录、优化记录,以及
带 review gate 的迭代。

14
15
## Skills

16
| Skill | 作用 |
17
| --- | --- |
18
19
20
21
22
23
24
| [`lightop-kernel-agent-loop`](humanize/skills/humanize-kernel-agent-loop/) | 主循环。用于新增或优化 LightOp 算子:恢复 `K/R/W/E`,检查 wrapper/binding/kernel/test/config/benchmark,实现 HIP/ROCm 改动,build、test、benchmark、profile、tune,并启动 Humanize RLCR。 |
| [`lightop-kernel-knowledge`](knowledge/SKILL.md) | 证据检索。优先查本地 LightOp 源码,其次查 ROCm/DCU 上游和官方文档,最后才把 bundled CUDA PR corpus 当作跨平台灵感。 |
| [`dcu-profiler-report`](humanize/skills/ncu-report/) | 性能剖析。把 `hipprof`、ROCm/DTK profiler、benchmark log 和可选 AMDGPU ISA 证据整理成可复现 digest,并给出一个明确的下一步 LightOp 修改。 |

磁盘上的目录名 `humanize-kernel-agent-loop``ncu-report` 是为了兼容上游
Humanize installer;真正暴露给 agent 的 skill 名称以上表 frontmatter 为准。

whlwhlwhl's avatar
whlwhlwhl committed
25
26
27
如果只是想人工阅读这三个 skill 的中文说明,可以看
[`docs/lightop-skills.zh-CN.md`](docs/lightop-skills.zh-CN.md)

28
29
30
31
32
33
34
35
36
37
38
39
40
41
## 手动安装

如果没有 Claude 或 Codex CLI installer,可以直接安装这三个 LightOp/DCU
skills:

```bash
./install-lightop-skills-manual.sh --target both
```

只安装 Claude:

```bash
./install-lightop-skills-manual.sh --target claude
```
42

43
只安装 Codex:
44

45
46
47
```bash
./install-lightop-skills-manual.sh --target codex
```
48

49
默认路径:
50
51

```text
52
53
Claude: ~/.claude/skills
Codex:  ${CODEX_HOME:-~/.codex}/skills
54
55
```

56
57
58
59
60
61
62
63
64
65
66
67
68
脚本会:

- symlink `lightop-kernel-knowledge`
- symlink `dcu-profiler-report`
- hydrate `lightop-kernel-agent-loop` 中的 `{{HUMANIZE_RUNTIME_ROOT}}`
  `{{KERNELPILOT_ROOT}}`
- 可选安装 `knowledge/requirements.txt`

## 请求格式

一个清晰的请求最好包含:算子名、正确性参考、workload、执行环境、目标
DCU/gfx arch、baseline、benchmark 方法、成功阈值。

whlwhlwhl's avatar
whlwhlwhl committed
69
prompt示例:
70
71

```text
whlwhlwhl's avatar
whlwhlwhl committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@lightop-kernel-agent-loop
- 宿主机 LightOp 路径:/path/to/lightop
- 验证 Docker:<lightop-container>
- 容器内 LightOp 路径:/workspace/lightop

任务:添加 1-pass layer norm 算子。
- 正确性参考:PyTorch layer_norm。
- 性能目标:最高有效带宽达到 1.1 TB/s。
- 所有 build/test/benchmark/profile 必须在容器内 /workspace/lightop 执行。
- 使用命令格式:
  docker exec <lightop-container> bash -lc 'cd /workspace/lightop && <command>'
- 缺少 shape、dtype、API、是否返回 mean/rstd 时,先从现有 LightOp layernorm/rmsnorm
  测试和 benchmark 推断,无法推断再问。

必须先创建:
- .humanize/lightop-agent/refined-plan.md
- .humanize/lightop-agent/research-digest.md
- .humanize/lightop-agent/attempt-ledger.md
并启动或记录 Humanize loop state。

授权范围:
- 允许修改该 LightOp 仓库内与本任务相关的源码、测试、benchmark、必要 config。
- 允许创建/写入 .humanize/lightop-agent/ 记录文件。
- 允许执行上述 docker exec 命令进行 build、test、benchmark、profiling。
- 允许执行 python setup.py install、hipprof/rocprof。
- 不允许删除 build/。
- 不允许 git reset、清理仓库、force push、删除大目录。
- 不需要 git commit/push。
100
101
102
103
104
```

## LightOp 接入位置

主循环会在一个 LightOp checkout 中工作。该目录通常包含:
105
106
107
108
109
110
111
112
113
114
115

```text
setup.py
setup_torch29.py
lightop/__init__.py
lightop/csrc/export.cpp
lightop/csrc/<family>/
lightop/config*.py
test/
```

116
117
新增算子时,agent 通常会检查或修改:

whlwhlwhl's avatar
whlwhlwhl committed
118
119
- `lightop/csrc/<family>/`:HIP/C++ kernel 和 launcher,源码使用 `.cu`,不要手写
  `.hip` 算子文件;`.hip` 如出现视作编译自动生成产物
120
121
122
123
124
125
126
127
- `lightop/csrc/export.cpp``m.def(...)` binding
- `lightop/<op>.py`:Python wrapper
- `lightop/__init__.py`:公开 API export
- `setup.py`:只有新增 `csrc` family 需要 glob 覆盖时才改
- `test/test_<op>.py`:正确性测试
- benchmark 脚本:性能测试
- `lightop/config*.py`:需要 shape/gfx-aware dispatch 时才改

whlwhlwhl's avatar
whlwhlwhl committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
所有改动都要符合 LightOp 现有开发规范。agent 写代码前必须先找最近的同 family
实现作为参照,沿用它的目录、文件命名、C++ namespace/include/launch helper、
wrapper 参数校验、`export.cpp` binding、config/dispatcher、test 和 benchmark 风格。
不要引入无关依赖、外部项目目录结构、批量格式化、生成源码、无关 operator family
改动,除非用户明确要求并在计划里说明原因。交付前需要列出修改文件对应参考了哪些
LightOp 本地文件,并确认没有手写 `.hip` 源码、没有无关改动、`test/` 下没有多个
最终任务测试入口。

最终验证脚本必须在 `test/` 下。新增算子需要添加 `test/test_<算子名>.py`;优化已有算子
时使用用户指定的测试文件,没有指定时再推断或创建 `test/test_<算子名>.py`。每个任务
`test/` 下只保留这一个正式测试入口;其它 benchmark、candidate test、parse/sweep
脚本都放 `.humanize/lightop-agent/`。测试脚本先做精度验证,再做性能测试;性能测试固定
10 轮 warmup、100 轮 timed iterations,报告平均耗时 us 和有效带宽。最终验证结果用
简短表格呈现。

143
144
145
146
147
LightOp KernelPilot 的 build 规则固定为:

```bash
python setup.py install
```
148

whlwhlwhl's avatar
whlwhlwhl committed
149
150
151
152
153
154
如果用户指定 Docker 容器,编译也必须进容器执行:

```bash
docker exec <container> bash -lc 'cd <container-lightop> && python setup.py install'
```

155
无论 PyTorch 版本是什么,都不切到 `setup_torch29.py`。正常调优循环中也不删除
whlwhlwhl's avatar
whlwhlwhl committed
156
157
158
`build/`,也不删除 `build/bdist.*``build/lib.*``build/temp.*` 这些
`python setup.py install` 正常生成的子目录;它们要保留复用,以免每轮重新全量编译。
只有用户明确要求 clean build,或证明 cache 损坏时才清理。
159
160
161

## DCU Profiling

162
`dcu-profiler-report` 使用 SourceFind DCU 性能分析工具指南作为一阶段官方参考:
163
164
165
166
167

```text
https://developer.sourcefind.cn/gitbook//dcu_developer/DeveloperGuide/dcu_programming/DCU_programming_chapter3_7.html
```

168
默认一阶段 profiling:
169
170
171

```bash
cd /path/to/lightop
whlwhlwhl's avatar
whlwhlwhl committed
172
173
174
175
176
bash /path/to/lightop-skiils/humanize/scripts/measure-device-bandwidth.sh \
  --docker <container> \
  --workdir <container-lightop> \
  --hip-visible-devices <idle-card> \
  --output .humanize/lightop-agent/device-bandwidth.txt
177
178
179
180
181
182
183
mkdir -p .humanize/lightop-agent/profile-artifacts/v000_baseline
python test/test_<op>.py 2>&1 \
  | tee .humanize/lightop-agent/profile-artifacts/v000_baseline/benchmark.log
hipprof python test/test_<op>.py 2>&1 \
  | tee .humanize/lightop-agent/profile-artifacts/v000_baseline/hipprof.txt
```

whlwhlwhl's avatar
whlwhlwhl committed
184
185
186
187
188
189
190
开始优化前必须先测当前选中卡的实际读写/拷贝带宽,作为后续算子有效带宽目标的参照。

优化循环里,每个正确性通过的 candidate 在普通 benchmark 后都要补一轮
`hipprof --pmc`,用于查看 cache 行为、LDS/bank conflict、occupancy/resource
压力,并把结果转成下一步明确的 kernel edit;如果当前 DTK 不支持某个 counter,
要记录实际命令和报错,不能用猜测替代。

191
`hipprof` 和 benchmark log 不够解释问题时,可以进一步使用:
192

193
194
195
196
197
198
199
200
201
202
203
204
```text
rocprof
rocprofv3
rocprof-compute
AMDGPU ISA / code-object inspection
```

profile digest 不是只写一句 “memory-bound”。它必须给出:

```text
测到什么信号 -> 可能机制是什么 -> 为什么其他假设较弱 -> 下一步具体改哪里
```
205

206
207
208
209
## Build And Test 默认规则

build、install、test、benchmark、profiling 必须在同一个环境里完成。如果目标环境是
Docker,优先使用可重复的非交互命令:
210
211
212
213
214

```bash
docker exec <container> bash -lc 'cd /workspace/lightop && <command>'
```

215
在该环境的 LightOp root 下先探测版本和设备:
216
217
218
219
220
221
222
223
224
225

```bash
python - <<'PY'
import torch
print("torch:", torch.__version__)
print("hip:", torch.version.hip)
print("device:", torch.cuda.get_device_name(0))
print("gcn:", torch.cuda.get_device_properties(0).gcnArchName)
PY
hipcc --version
226
227
228
229
230
```

然后 build:

```bash
231
PYTORCH_ROCM_ARCH='gfx928;gfx936;gfx938' python setup.py install
232
233
234
235
236
```

再做 import smoke test:

```bash
237
238
239
240
241
242
243
python - <<'PY'
import torch, lightop
print("lightop:", getattr(lightop, "__file__", "unknown"))
print("gcn:", torch.cuda.get_device_properties(0).gcnArchName)
PY
```

244
优先跑最窄的目标测试:
245
246
247
248
249
250

```bash
cd test
python test_<op>.py
```

251
252
253
254
255
256
257
258
259
260
261
benchmark 必须包含 warmup,并且在计时区间前后显式使用
`torch.cuda.synchronize()` 或 HIP 同步。不能只凭 Python wall-clock 异步计时宣布
加速。

一个 LightOp 算子改动不能只靠 build 通过就算完成。完成条件至少包括:

- install 成功
- import smoke test 成功
- targeted correctness test 成功
- benchmark 和 baseline 对比完成
- 当结果接近阈值或异常时,有 profiler 证据
262

263
## 常规安装
264

265
从当前 checkout 安装 Humanize skill pack:
266
267
268
269
270
271

```bash
cd /path/to/kernel-pilot
./humanize/scripts/install-skill.sh --target codex --kernelpilot-root "$PWD"
```

272
Claude Code 用户可以使用:
273
274
275
276
277

```bash
./humanize/scripts/install-skills-claude.sh --kernelpilot-root "$PWD"
```

278
安装后相关 skill 名称是:
279
280
281
282
283

```text
lightop-kernel-agent-loop
lightop-kernel-knowledge
dcu-profiler-report
whlwhlwhl's avatar
whlwhlwhl committed
284
285
286
triton-kernel-agent-loop
triton-kernel-knowledge
triton-dcu-profiler-report
287
288
```

whlwhlwhl's avatar
whlwhlwhl committed
289
290
291
292
293
For Triton/DCU usage notes, see [`docs/triton-skills.md`](docs/triton-skills.md)
or the Chinese version [`docs/triton-skills.zh-CN.md`](docs/triton-skills.zh-CN.md).
Triton skills 支持 vLLM/SGLang 的 framework mode,也支持用户直接指定某个
Triton Python 文件的 direct-file mode。

294
## 证据规则
295

296
297
298
299
300
301
302
- 本地 LightOp 源码、测试、配置和 benchmark 是第一优先级证据。
- ROCm/DCU 官方文档和上游源码是第二优先级证据。
- bundled CUDA PR corpus 只能作为跨平台灵感,除非已经明确翻译并在 DCU 上验证。
- 复制或改写外部源码时,必须记录来源路径/URL、commit 或 version、
  license/notice,以及优化收益。
- profile digest 最后必须给出一个具体的下一步修改;如果 profiling 不可执行,
  要说明原因。