| @@ -1,62 +0,0 @@ | |||
| 1 | ---- | ||
| 2 | -name: pypto-aicpu-perf-coding | ||
| 3 | -description: PyPTO 昇腾 AICPU / Control-Flow / Schedule 热路径高性能编码原则与评审清单。涵盖禁 STL 与热路径堆分配、显式指针类型、引用传参、平凡默构与结构体布局、忌整结构初始化、Host 以存代算、循环外提、Host 严禁随意 rtMemcpy、惰性 bump、flat 小池、hit 零拷贝等。当用户修改 machine 设备侧调度/schedule、写 AICPU 热路径、做 ctrl-flow CPU 性能优化、评审 DeviceTask/ItemPool/AOT/slab/CF cache/device_sche 相关改动,或提到 AICPU 高性能编码、禁容器、平凡初始化时使用。 | ||
| 4 | ---- | ||
| 5 | - | ||
| 6 | -# AICPU 热路径高性能编码 | ||
| 7 | - | ||
| 8 | -面向 `framework/src/machine/` 下 **AICPU Control-Flow 与 Schedule 调度热路径**(含 `device_sche*`;非 AICore kernel)。 | ||
| 9 | -Host encode/UT 可用 STL,但仍须遵守「Host 严禁随意新增 rtMemcpy」等 Host 侧条款。 | ||
| 10 | - | ||
| 11 | -详细原则见:[references/coding-principles.md](references/coding-principles.md)(**必读**后再改代码或下评审结论)。 | ||
| 12 | - | ||
| 13 | -## 何时使用 | ||
| 14 | - | ||
| 15 | -- 修改 / 新增:`device_*`、`device_sche*`、`dev_workspace`、`item_pool`、`aot_binary`、`spsc_queue`、CF cache、stitch/task build | ||
| 16 | -- 用户提到:AICPU 性能、ctrl cpu、schedule、禁容器、平凡初始化、热路径、`MakeDynDeviceTask` | ||
| 17 | -- Code review:machine 设备侧 / Host launcher 拷贝是否引入回归 | ||
| 18 | - | ||
| 19 | -**不要**用本 skill 替代:workspace OOM(`pypto-machine-workspace`)、精度(`pypto-precision-*`)、Pass 分析(`pypto-pass-*`)。 | ||
| 20 | - | ||
| 21 | -## Agent 工作流 | ||
| 22 | - | ||
| 23 | -1. **读原则**:打开 `references/coding-principles.md`,至少覆盖 §2.1–§2.5、§2.9–§2.11。 | ||
| 24 | -2. **划定范围**:CF / Schedule 热路径?Host launcher?二者约束不同(见原则 Host 条款)。 | ||
| 25 | -3. **对照清单**(命中即改或书面例外): | ||
| 26 | - - [ ] 无热路径 STL 容器 / 业务 `new` | ||
| 27 | - - [ ] 地址用显式指针类型,不用 `uint64_t` 长期冒充地址 | ||
| 28 | - - [ ] 局部对象优先引用传参,避免无必要取址 | ||
| 29 | - - [ ] 热点结构体 trivial 默构 + 布局热冷分离 / 少洞 | ||
| 30 | - - [ ] 无整结构 memset;Shell/Fill/bump;按 `usedSize` 拷 | ||
| 31 | - - [ ] 能 Host 判断的 if 不放到 AICPU(以存代算 / encode 旗标) | ||
| 32 | - - [ ] 循环边界与循环不变量已外提 | ||
| 33 | - - [ ] Host 无新增直连 `rtMemcpy` H2D;必须拷贝则 `NormalizedRtMemcpy` / RELAXED | ||
| 34 | - - [ ] Schedule 路径与 CF 路径一并检查,不只看 stitch | ||
| 35 | -4. **落地或评审**:优先 `ItemPool` / 自研 `Vector` / 定长数组 / 引用接口;输出违规点与建议。 | ||
| 36 | -5. **验证**:相关 UT;必要时上板 `PERF_EVT_*`。 | ||
| 37 | - 注意:**CSE 仅 Host 生成 CF 代码时关注,AICPU 运行时编码不涉及、运行时也感知不到。** | ||
| 38 | - | ||
| 39 | -## 硬约束(摘要) | ||
| 40 | - | ||
| 41 | -| 必须 | 禁止 | | ||
| 42 | -|---|---| | ||
| 43 | -| 定长数组 / `Vector`+workspace / `ItemPool` / `SPSC` | 热路径 STL、业务 `new/delete` | | ||
| 44 | -| 显式 `T*`;局部按引用传递 | `uint64_t` 当万能地址;无必要 `&local` | | ||
| 45 | -| trivial 默构 + 合理布局 | 默构清大数组;热点结构塞冷字段 | | ||
| 46 | -| Host 预计算;循环外提 | AICPU 重复 if/算本可 Host 完成的事 | | ||
| 47 | -| 必须 H2D 时用 `NormalizedRtMemcpy` | Host 随意新增直连 `rtMemcpy` | | ||
| 48 | - | ||
| 49 | -违反 §2.1–§2.5、§2.9、§2.11:**即使功能正确,也视为回归。** | ||
| 50 | - | ||
| 51 | -## 参考代码 | ||
| 52 | - | ||
| 53 | -```text | ||
| 54 | -framework/src/machine/utils/dynamic/item_pool.h | ||
| 55 | -framework/src/machine/utils/dynamic/vector.h | ||
| 56 | -framework/src/machine/utils/dynamic/spsc_queue.h | ||
| 57 | -framework/src/machine/utils/dynamic/device_task.h | ||
| 58 | -framework/src/machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h | ||
| 59 | -framework/src/machine/device/dynamic/aot_binary.h | ||
| 60 | -framework/src/machine/device/dynamic/device_sche.cpp | ||
| 61 | -framework/src/machine/runtime/memory_utils/memory_pool.cpp # NormalizedRtMemcpy | ||
| 62 | -``` | ||
| @@ -0,0 +1,153 @@ | |||||||||||||
| 1 | +--- | ||||||||||||
| 2 | +name: pypto-machine-code-review | ||||||||||||
| 3 | +description: PyPTO machine 侧(framework/src/machine)代码检视与行级 review 意见提交。覆盖 CANN weak 符号保护、CRC/哈希算法正确性、AOT/cache 碰撞、内存对齐、弱符号空指针、离线打包 bundle、AICPU 热路径高性能编码(禁 STL/堆分配、显式指针、平凡默构、以存代算、循环外提、Host 严禁随意 rtMemcpy)等 machine 侧 C++ 改动的检视清单,并通过 GitCode API 将检视意见提交到 PR 的具体代码行(diff_comment)。当用户提到 review PR、检视代码、代码评审、machine code review、行级评论、diff_comment、提交检视意见、AICPU 性能、禁容器、平凡初始化、热路径、ctrl cpu、schedule 时使用。触发词:review PR、检视代码、代码评审、machine code review、行级评论、diff_comment、提交检视意见、AICPU 高性能编码、禁容器、平凡初始化、热路径。 | ||||||||||||
| 4 | +--- | ||||||||||||
| 5 | + | ||||||||||||
| 6 | +# PyPTO Machine Code Review | ||||||||||||
| 7 | + | ||||||||||||
| 8 | +对 `framework/src/machine/` 下 C++ 改动进行代码检视(含 AICPU 热路径性能原则),并将检视意见通过 GitCode API 提交到 PR 的**具体代码行**(diff_comment,显示在 diff 视图对应行旁)。 | ||||||||||||
| 9 | + | ||||||||||||
| 10 | +## 何时使用 | ||||||||||||
| 11 | + | ||||||||||||
| 12 | +- 用户要求 review / 检视某个 PR 的 machine 侧代码 | ||||||||||||
| 13 | +- 用户要求把检视意见"提到代码上"/"提到代码位置"(行级 diff_comment,而非 PR 级普通评论) | ||||||||||||
| 14 | +- 用户要求审查 `framework/src/machine/` 下 C++ 改动(bundle/launcher/runner/device/aot_binary/device_sche 等) | ||||||||||||
| 15 | +- 用户修改 / 新增 AICPU Control-Flow / Schedule 热路径代码(`device_*`、`device_sche*`、`dev_workspace`、`item_pool`、`aot_binary`、`spsc_queue`、CF cache、stitch/task build) | ||||||||||||
| 16 | +- 用户提到 AICPU 性能、禁容器、平凡初始化、热路径、ctrl cpu、schedule、`MakeDynDeviceTask` | ||||||||||||
| 17 | + | ||||||||||||
| 18 | +**不要**用本 skill 替代: | ||||||||||||
| 19 | +- workspace 内存诊断 → `pypto-machine-workspace` | ||||||||||||
| 20 | +- 精度问题 → `pypto-precision-overall` | ||||||||||||
| 21 | +- Pass 模块分析 → `pypto-pass-*` | ||||||||||||
| 22 | + | ||||||||||||
| 23 | +## Agent 工作流 | ||||||||||||
| 24 | + | ||||||||||||
| 25 | +### 阶段 1:获取 PR diff | ||||||||||||
| 26 | + | ||||||||||||
| 27 | +1. **确定 PR**:用户提供 PR 号(如 `4974`)或分支名。 | ||||||||||||
| 28 | +2. **拉取分支**: | ||||||||||||
| 29 | + ```bash | ||||||||||||
| 30 | + git fetch https://gitcode.com/<author>/pypto.git <branch> -q | ||||||||||||
| 31 | + PR_HEAD=$(git rev-parse FETCH_HEAD) | ||||||||||||
| 32 | + ``` | ||||||||||||
| 33 | +3. **确定 base**:`BASE=$(git merge-base FETCH_HEAD upstream/master)` | ||||||||||||
| 34 | +4. **聚焦 machine 侧**:排除 docs/tests 噪声 | ||||||||||||
| 35 | + ```bash | ||||||||||||
| 36 | + git diff --stat $BASE..$PR_HEAD -- 'framework/src/machine/*' 'framework/include/machine/*' | ||||||||||||
| 37 | + ``` | ||||||||||||
| 38 | +5. **逐文件读 diff**:对每个改动文件 `git diff $BASE..$PR_HEAD -- <file>`,定位问题行。 | ||||||||||||
| 39 | + | ||||||||||||
| 40 | +### 阶段 2:对照检视清单 | ||||||||||||
| 41 | + | ||||||||||||
| 42 | +读 [references/review-checklist.md](references/review-checklist.md)(**必读**后再下评审结论),至少覆盖: | ||||||||||||
| 43 | +- §1 算法正确性(CRC/哈希多项式、整数溢出) | ||||||||||||
| 44 | +- §2 内存安全(weak 符号空指针、对齐、越界) | ||||||||||||
| 45 | +- §3 缓存/哈希碰撞(AOT cache、registry 去重键) | ||||||||||||
| 46 | +- §4 范围纪律(无关改动混入、死代码删除) | ||||||||||||
| 47 | +- §5 bundle 专项(format/header/TLV/CRC/对齐) | ||||||||||||
| 48 | +- §6 AICPU 热路径性能(禁 STL/堆分配、显式指针、平凡默构、以存代算、循环外提、Host 严禁随意 rtMemcpy) | ||||||||||||
| 49 | + | ||||||||||||
| 50 | +**若 §6 命中**(PR 涉及 AICPU 热路径),额外读 [references/perf-rules.md](references/perf-rules.md)(详细原则,至少覆盖 §2.1–§2.5、§2.9–§2.11)。 | ||||||||||||
| 51 | + | ||||||||||||
| 52 | +**同时读** [references/memory-rules.md](references/memory-rules.md)(内存规则),将其与内置清单合并执行。该文件表格为空时跳过;非空时按相同流程命中记录。 | ||||||||||||
| 53 | + | ||||||||||||
| 54 | +命中即记录:`{文件, 行号, 级别(Blocker/Major/Minor), 问题描述, 建议}`。 | ||||||||||||
| 55 | + | ||||||||||||
| 56 | +### 阶段 2.5:用户确认(强制) | ||||||||||||
| 57 | + | ||||||||||||
| 58 | +**提交前必须向用户展示全部检视意见并获确认,禁止跳过。** | ||||||||||||
| 59 | + | ||||||||||||
| 60 | +1. 以表格形式展示所有命中意见: | ||||||||||||
| 61 | + | ||||||||||||
| 62 | + | # | 级别 | 文件:行 | 问题摘要 | 建议修复 | | ||||||||||||
| 63 | + |---|---|---|---|---| | ||||||||||||
| 64 | + | ||||||||||||
| 65 | +2. 使用 `question` 工具询问用户,提供以下选项: | ||||||||||||
| 66 | + - **全部提交**:提交全部意见到 PR 代码行 | ||||||||||||
| 67 | + - **选择性提交**:用户指定提交哪些(按编号) | ||||||||||||
| 68 | + - **修改后提交**:用户指出需修改的意见,Agent 修改后重新确认 | ||||||||||||
| 69 | + - **不提交**:仅输出报告,不提交到 GitCode | ||||||||||||
| 70 | + | ||||||||||||
| 71 | +3. 用户确认前**禁止调用** `submit_review_comments.py` 或 curl POST。GitCode API 不支持 PATCH/DELETE 更新或删除评论,提交后无法撤回,因此确认环节不可省略。 | ||||||||||||
| 72 | + | ||||||||||||
| 73 | +4. 若用户选择"选择性提交",仅提交用户指定的编号对应意见。 | ||||||||||||
| 74 | + | ||||||||||||
| 75 | +### 阶段 3:提交行级 diff_comment | ||||||||||||
| 76 | + | ||||||||||||
| 77 | +**关键**:GitCode 行级评论必须用 `position` 字段(不是 `line`/`side`/`diff_side`),否则降级为 PR 级普通评论。 | ||||||||||||
| 78 | + | ||||||||||||
| 79 | +读 [references/gitcode-review-api.md](references/gitcode-review-api.md)(**必读**,含字段对照表和踩坑记录),然后: | ||||||||||||
| 80 | + | ||||||||||||
| 81 | +1. **验证 token**:从 git remote 提取 | ||||||||||||
| 82 | + ```bash | ||||||||||||
| 83 | + TOKEN=$(git remote get-url origin | sed -nE 's|.*oauth2:([^@]+)@.*|\1|p') | ||||||||||||
| 84 | + curl -s -o /dev/null -w "%{http_code}" "https://gitcode.com/api/v5/user?access_token=${TOKEN}" | ||||||||||||
| 85 | + # 200 = 可用 | ||||||||||||
| 86 | + ``` | ||||||||||||
| 87 | +2. **确认行号**:行号是 PR head 文件中的绝对行号(`git show $PR_HEAD:<file> | grep -n <pattern>`)。 | ||||||||||||
| 88 | +3. **提交**(二选一): | ||||||||||||
| 89 | + | ||||||||||||
| 90 | + **方式 A:脚本批量提交**(推荐,4 条以上) | ||||||||||||
| 91 | + ```bash | ||||||||||||
| 92 | + python3 .agents/skills/pypto-machine-code-review/scripts/submit_review_comments.py \ | ||||||||||||
| 93 | + --pr 4974 --token "$TOKEN" \ | ||||||||||||
🟠 High Priority SKILL.md 第 92-93 行将 token 作为命令行参数传递: 建议:移除 Suggestions
![]() ![]() Incorrect? | |||||||||||||
| 94 | + --comment path:position:body \ | ||||||||||||
| 95 | + --comment path2:pos2:body2 | ||||||||||||
| 96 | + ``` | ||||||||||||
| 97 | + 见 [scripts/submit_review_comments.py](scripts/submit_review_comments.py)。 | ||||||||||||
| 98 | + | ||||||||||||
| 99 | + **方式 B:curl 单条提交** | ||||||||||||
| 100 | + ```bash | ||||||||||||
| 101 | + curl -s -H "PRIVATE-TOKEN: ${TOKEN}" -H "Content-Type: application/json" \ | ||||||||||||
| 102 | + -X POST "https://gitcode.com/api/v5/repos/cann/pypto/pulls/<PR>/comments" \ | ||||||||||||
| 103 | + -d "$(jq -n --arg b "$BODY" --arg p "$PATH" --argjson pos $LINE \ | ||||||||||||
| 104 | + '{body:$b, path:$p, position:$pos, need_to_resolve:true}')" | ||||||||||||
| 105 | + ``` | ||||||||||||
| 106 | + | ||||||||||||
| 107 | +4. **验证**:确认 `comment_type=diff_comment` 且 `diff_position` 非空 | ||||||||||||
| 108 | + ```bash | ||||||||||||
| 109 | + curl -s -H "PRIVATE-TOKEN: ${TOKEN}" \ | ||||||||||||
| 110 | + "https://gitcode.com/api/v5/repos/cann/pypto/pulls/<PR>/comments?per_page=50" | \ | ||||||||||||
| 111 | + python3 -c "import sys,json; [print(c['id'],c.get('comment_type'),c.get('diff_position')) for c in json.load(sys.stdin)]" | ||||||||||||
| 112 | + ``` | ||||||||||||
| 113 | + | ||||||||||||
| 114 | +### 阶段 4:报告 | ||||||||||||
| 115 | + | ||||||||||||
| 116 | +输出检视意见汇总表(文件:行 / 级别 / 问题 / comment_id),并给出 PR 链接。 | ||||||||||||
| 117 | + | ||||||||||||
| 118 | +## 硬约束 | ||||||||||||
| 119 | + | ||||||||||||
| 120 | +### GitCode 行级评论 | ||||||||||||
| 121 | + | ||||||||||||
| 122 | +| 必须 | 禁止 | | ||||||||||||
| 123 | +|---|---| | ||||||||||||
| 124 | +| **提交前用户确认(阶段 2.5)** | **未确认直接提交**(GitCode 不支持删除/更新) | | ||||||||||||
| 125 | +| 行级评论用 `position` 字段 | 用 `line`/`side`/`diff_side`(降级为普通评论) | | ||||||||||||
| 126 | +| 行号取 PR head 绝对行号 | 用 base 行号或 diff 相对行号 | | ||||||||||||
| 127 | +| 验证 `comment_type=diff_comment` | 假定 201 = 行级评论(可能只是 pr_comment) | | ||||||||||||
| 128 | +| body 含级别标签(Blocker/Major/Minor) | 无级别裸意见 | | ||||||||||||
| 129 | +| 聚焦 machine 侧 C++ | 把 docs/tests 改动当检视对象 | | ||||||||||||
| 130 | +| 先读 checklist 再下结论 | 凭直觉/记忆评审 | | ||||||||||||
| 131 | +| 意见需有代码证据支撑 | 沿用模式未验证即判定(假阳性) | | ||||||||||||
| 132 | + | ||||||||||||
| 133 | +### AICPU 热路径性能(§6 命中时) | ||||||||||||
| 134 | + | ||||||||||||
| 135 | +| 必须 | 禁止 | | ||||||||||||
| 136 | +|---|---| | ||||||||||||
| 137 | +| 定长数组 / `Vector`+workspace / `ItemPool` / `SPSC` | 热路径 STL、业务 `new/delete` | | ||||||||||||
| 138 | +| 显式 `T*`;局部按引用传递 | `uint64_t` 当万能地址;无必要 `&local` | | ||||||||||||
| 139 | +| trivial 默构 + 合理布局 | 默构清大数组;热点结构塞冷字段 | | ||||||||||||
| 140 | +| Host 预计算;循环外提 | AICPU 重复 if/算本可 Host 完成的事 | | ||||||||||||
| 141 | +| 必须 H2D 时用 `NormalizedRtMemcpy` | Host 随意新增直连 `rtMemcpy` | | ||||||||||||
| 142 | + | ||||||||||||
| 143 | +违反 §6 / `perf-rules.md` §2.1–§2.5、§2.9、§2.11:**即使功能正确,也视为回归。** | ||||||||||||
| 144 | + | ||||||||||||
| 145 | +## 参考文件 | ||||||||||||
| 146 | + | ||||||||||||
| 147 | +| File | Purpose | Load Timing | | ||||||||||||
| 148 | +|------|---------|-------------| | ||||||||||||
| 149 | +| [references/review-checklist.md](references/review-checklist.md) | machine 侧 C++ 检视清单(6 大类,含 AICPU 热路径性能) | 阶段 2 开始前必读 | | ||||||||||||
| 150 | +| [references/perf-rules.md](references/perf-rules.md) | AICPU 热路径性能规则(15 条) | §6 命中时必读 | | ||||||||||||
| 151 | +| [references/memory-rules.md](references/memory-rules.md) | 内存规则(跨核数据一致性 U4–U7 等) | 阶段 2 与内置清单合并执行 | | ||||||||||||
| 152 | +| [references/gitcode-review-api.md](references/gitcode-review-api.md) | GitCode 行级评论 API 字段对照与踩坑 | 阶段 3 提交前必读 | | ||||||||||||
| 153 | +| [scripts/submit_review_comments.py](scripts/submit_review_comments.py) | 批量提交 diff_comment 脚本 | 阶段 3 方式 A | | ||||||||||||
| @@ -0,0 +1,179 @@ | |||
| 1 | +# GitCode 行级评论 API(diff_comment) | ||
| 2 | + | ||
| 3 | +通过 GitCode API 将检视意见提交到 PR 的**具体代码行**,显示在 diff 视图对应行旁(而非 PR 级普通评论)。 | ||
| 4 | + | ||
| 5 | +## 认证 | ||
| 6 | + | ||
| 7 | +从 git remote 提取 token(无需额外配置): | ||
| 8 | +```bash | ||
| 9 | +TOKEN=$(git remote get-url origin | sed -nE 's|.*oauth2:([^@]+)@.*|\1|p') | ||
| 10 | +``` | ||
| 11 | + | ||
| 12 | +三种认证方式均可: | ||
| 13 | +- `PRIVATE-TOKEN: <token>`(header) | ||
| 14 | +- `Authorization: Bearer <token>`(header) | ||
| 15 | +- `?access_token=<token>`(query param) | ||
| 16 | + | ||
| 17 | +验证: | ||
| 18 | +```bash | ||
| 19 | +curl -s -o /dev/null -w "%{http_code}" "https://gitcode.com/api/v5/user?access_token=${TOKEN}" | ||
| 20 | +# 200 = 可用 | ||
| 21 | +``` | ||
| 22 | + | ||
| 23 | +--- | ||
| 24 | + | ||
| 25 | +## 核心 API | ||
| 26 | + | ||
| 27 | +### POST 行级评论 | ||
| 28 | + | ||
| 29 | +``` | ||
| 30 | +POST /api/v5/repos/:owner/:repo/pulls/:number/comments | ||
| 31 | +``` | ||
| 32 | + | ||
| 33 | +**正确请求格式**(关键:`position` 字段,不是 `line`/`side`/`diff_side`): | ||
| 34 | + | ||
| 35 | +```json | ||
| 36 | +{ | ||
| 37 | + "body": "检视意见内容", | ||
| 38 | + "path": "framework/src/machine/runtime/bundle/kernel_bundle_crc32.h", | ||
| 39 | + "position": 31, | ||
| 40 | + "need_to_resolve": true | ||
| 41 | +} | ||
| 42 | +``` | ||
| 43 | + | ||
| 44 | +**成功响应特征**(`comment_type` 必须是 `diff_comment`): | ||
| 45 | + | ||
| 46 | +```json | ||
| 47 | +{ | ||
| 48 | + "id": 182608753, | ||
| 49 | + "comment_type": "diff_comment", | ||
| 50 | + "diff_position": { | ||
| 51 | + "start_new_line": 31, | ||
| 52 | + "end_new_line": 31, | ||
| 53 | + "position_type": "text" | ||
| 54 | + }, | ||
| 55 | + "body": "...", | ||
| 56 | + "resolved": false, | ||
| 57 | + "created_at": "...", | ||
| 58 | + "user": {...} | ||
| 59 | +} | ||
| 60 | +``` | ||
| 61 | + | ||
| 62 | +> ⚠️ POST 响应体只返回 `{id, body, note_id}` 三个字段,**不含** `comment_type`/`diff_position`。 | ||
| 63 | +> 必须通过 GET 列表验证是否真正成为 diff_comment(见下方"验证")。 | ||
| 64 | + | ||
| 65 | +### curl 示例 | ||
| 66 | + | ||
| 67 | +```bash | ||
| 68 | +curl -s -H "PRIVATE-TOKEN: ${TOKEN}" -H "Content-Type: application/json" \ | ||
| 69 | + -X POST "https://gitcode.com/api/v5/repos/cann/pypto/pulls/4974/comments" \ | ||
| 70 | + -d "$(jq -n --arg b "$BODY" --arg p "$PATH" --argjson pos $LINE \ | ||
| 71 | + '{body:$b, path:$p, position:$pos, need_to_resolve:true}')" | ||
| 72 | +``` | ||
| 73 | + | ||
| 74 | +### 验证是否成为 diff_comment | ||
| 75 | + | ||
| 76 | +``` | ||
| 77 | +GET /api/v5/repos/:owner/:repo/pulls/:number/comments?per_page=50 | ||
| 78 | +``` | ||
| 79 | + | ||
| 80 | +```bash | ||
| 81 | +curl -s -H "PRIVATE-TOKEN: ${TOKEN}" \ | ||
| 82 | + "https://gitcode.com/api/v5/repos/cann/pypto/pulls/4974/comments?per_page=50" | \ | ||
| 83 | + python3 -c " | ||
| 84 | +import sys, json | ||
| 85 | +for c in json.load(sys.stdin): | ||
| 86 | + if c.get('comment_type') == 'diff_comment': | ||
| 87 | + dp = c.get('diff_position', {}) | ||
| 88 | + print(f\"id={c['id']} line={dp.get('start_new_line')} body={c['body'][:40]}...\") | ||
| 89 | +" | ||
| 90 | +``` | ||
| 91 | + | ||
| 92 | +**判定标准**: | ||
| 93 | +- `comment_type == "diff_comment"` 且 `diff_position` 非空 → ✅ 行级评论 | ||
| 94 | +- `comment_type == "pr_comment"` 或 `diff_position` 为 None → ❌ 降级为 PR 级普通评论 | ||
| 95 | + | ||
| 96 | +--- | ||
| 97 | + | ||
| 98 | +## 字段对照表(踩坑记录) | ||
| 99 | + | ||
| 100 | +| 字段 | 是否生效 | 结果 | 说明 | | ||
| 101 | +|------|---------|------|------| | ||
| 102 | +| **`position`** | ✅ **正确** | `diff_comment` + `diff_position` | **唯一能产生行级评论的字段** | | ||
| 103 | +| `line` | ❌ 无效 | `pr_comment`(降级) | POST 返回 201 但不关联到行 | | ||
| 104 | +| `side` (`RIGHT`/`LEFT`) | ❌ 无效 | `pr_comment`(降级) | Gitea/GitHub 字段,GitCode 不支持 | | ||
| 105 | +| `diff_side` (`new`/`old`) | ❌ 无效 | `pr_comment`(降级) | GitHub 字段,GitCode 不支持 | | ||
| 106 | +| `commit_id` | ❌ 无效 | 不影响 comment_type | 传了也不报错但无作用 | | ||
| 107 | +| `need_to_resolve` | ✅ 可选 | 设置 resolved 状态 | true = 标记需解决 | | ||
| 108 | + | ||
| 109 | +**结论**:只用 `body` + `path` + `position` + `need_to_resolve` 四个字段。 | ||
| 110 | + | ||
| 111 | +--- | ||
| 112 | + | ||
| 113 | +## 其他相关 API | ||
| 114 | + | ||
| 115 | +### GET PR 信息 | ||
| 116 | + | ||
| 117 | +``` | ||
| 118 | +GET /api/v5/repos/:owner/:repo/pulls/:number | ||
| 119 | +``` | ||
| 120 | +获取 `head.sha`、`base.sha`、关联 issue number。 | ||
| 121 | + | ||
| 122 | +### GET PR 关联 issue | ||
| 123 | + | ||
| 124 | +``` | ||
| 125 | +GET /api/v5/repos/:owner/:repo/pulls/:number/issues | ||
| 126 | +``` | ||
| 127 | +返回 `[{number, id, ...}]`,用于 issue 级评论提交。 | ||
| 128 | + | ||
| 129 | +### POST issue 级评论(PR 级普通评论,非行级) | ||
| 130 | + | ||
| 131 | +``` | ||
| 132 | +POST /api/v5/repos/:owner/:repo/issues/:issue_number/comments | ||
| 133 | +``` | ||
| 134 | +```json | ||
| 135 | +{"body": "整体检视意见汇总..."} | ||
| 136 | +``` | ||
| 137 | +用于提交整体 review 总结(非行级)。 | ||
| 138 | + | ||
| 139 | +### DELETE 评论 | ||
| 140 | + | ||
| 141 | +``` | ||
| 142 | +DELETE /api/v5/repos/:owner/:repo/pulls/comments/:id | ||
| 143 | +``` | ||
| 144 | +> ⚠️ GitCode API 不支持 DELETE(返回 405 Method Not Allowed)。测试评论无法通过 API 删除,需在网页手动删除。 | ||
| 145 | + | ||
| 146 | +### PATCH 更新评论 | ||
| 147 | + | ||
| 148 | +``` | ||
| 149 | +PATCH /api/v5/repos/:owner/:repo/pulls/comments/:id | ||
| 150 | +``` | ||
| 151 | +> ⚠️ 同样不支持(405)。提交前确保内容正确。 | ||
| 152 | + | ||
| 153 | +--- | ||
| 154 | + | ||
| 155 | +## 常见错误 | ||
| 156 | + | ||
| 157 | +| 现象 | 原因 | 修复 | | ||
| 158 | +|------|------|------| | ||
| 159 | +| POST 返回 201 但评论不在 diff 行上 | 用了 `line`/`side` 而非 `position` | 改用 `position` 字段 | | ||
| 160 | +| `comment_type` 为 `pr_comment` | 同上 | 同上 | | ||
| 161 | +| 404 Not Found | PR number 错误或无权限 | 确认 owner=cann repo=pypto | | ||
| 162 | +| 401 Unauthorized | token 无效或过期 | 重新从 git remote 提取 | | ||
| 163 | +| 行号偏移 | 用了 base 行号或 diff 相对行号 | 用 `git show $PR_HEAD:<file> \| grep -n` 取 head 绝对行号 | | ||
| 164 | + | ||
| 165 | +--- | ||
| 166 | + | ||
| 167 | +## 行号获取方法 | ||
| 168 | + | ||
| 169 | +行号必须是 **PR head 文件中的绝对行号**(不是 base 行号,不是 diff hunk 中的相对行号): | ||
| 170 | + | ||
| 171 | +```bash | ||
| 172 | +PR_HEAD=$(git rev-parse FETCH_HEAD) | ||
| 173 | + | ||
| 174 | +# 方法 1:grep 关键模式 | ||
| 175 | +git show "${PR_HEAD}:framework/src/machine/runtime/bundle/kernel_bundle_crc32.h" | grep -n "0xEDB88420" | ||
| 176 | + | ||
| 177 | +# 方法 2:带上下文查看 | ||
| 178 | +git show "${PR_HEAD}:<file>" | sed -n '28,35p' | cat -n | ||
| 179 | +``` | ||
| @@ -0,0 +1,58 @@ | |||
| 1 | +# 内存规则 | ||
| 2 | + | ||
| 3 | +> 本文件录入机器侧内存相关的检视规则,补充内置清单(`review-checklist.md`)。 | ||
| 4 | +> 规则格式见下方模板,直接在对应分类下追加即可。Agent 在阶段 2 会将本文件与内置清单合并执行。 | ||
| 5 | + | ||
| 6 | +## 使用方法 | ||
| 7 | + | ||
| 8 | +1. 在对应分类下按模板追加规则行 | ||
| 9 | +2. `适用路径` 填 glob 模式(如 `framework/src/machine/runtime/bundle/*`),留空表示全 machine 适用 | ||
| 10 | +3. `关键词` 填代码中可用于 grep 定位的标识(如函数名、宏、类名),留空表示靠人工判断 | ||
| 11 | +4. Agent 读到本文件后,在阶段 2 对照检查;命中规则时与内置清单同等处理 | ||
| 12 | + | ||
| 13 | +## 规则模板 | ||
| 14 | + | ||
| 15 | +``` | ||
| 16 | +| U# | 检查项 | 级别 | 适用路径 | 关键词 | 典型问题/判定标准 | | ||
| 17 | +``` | ||
| 18 | + | ||
| 19 | +- **级别**:Blocker / Major / Minor(定义见 `review-checklist.md`) | ||
| 20 | +- **适用路径**:glob 模式,如 `framework/src/machine/device/dynamic/*`;留空 = 全 machine | ||
| 21 | +- **关键词**:grep 关键字,如 `AOTCodePoolManager`、`rtMemcpy`;留空 = 人工判断 | ||
| 22 | + | ||
| 23 | +--- | ||
| 24 | + | ||
| 25 | +## 算法正确性 | ||
| 26 | + | ||
| 27 | +| U# | 检查项 | 级别 | 适用路径 | 关键词 | 典型问题/判定标准 | | ||
| 28 | +|----|--------|------|----------|--------|-------------------| | ||
| 29 | +| | | | | | | | ||
| 30 | + | ||
| 31 | +## 跨核数据一致性安全(AICPU↔AICore) | ||
| 32 | + | ||
| 33 | +> U4–U7 四条规则共同覆盖 AICPU 写入、AICore 读取的共享数据一致性问题的完整链路: | ||
| 34 | +> - **U4** 管写入可见性顺序(`__sync_synchronize` 屏障) | ||
| 35 | +> - **U5** 管分配时 cacheline 对齐(dcci 刷新安全基础) | ||
| 36 | +> - **U6** 管后续新增访问时 dcci 地址覆盖性 | ||
| 37 | +> - **U7** 管 slab 注册 size 与 alloc size 一致(U5/U6 的前置保障) | ||
| 38 | +> | ||
| 39 | +> 四条规则层层递进:U7 保证对象大小正确 → U5 保证对象地址对齐 → U6 保证 dcci 覆盖新增字段 → U4 保证写入顺序可见。任一环节断裂都会导致 AICore 读到脏数据。 | ||
| 40 | + | ||
| 41 | +| U# | 检查项 | 级别 | 适用路径 | 关键词 | 典型问题/判定标准 | | ||
| 42 | +|----|--------|------|----------|--------|-------------------| | ||
| 43 | +| U4 | AICPU 侧跨核共享内存写后需 `__sync_synchronize()` 屏障 | Blocker | `framework/src/machine/device/dynamic/*` | `__sync_synchronize`、`SetReadyQueue`、`TryBatchSendTask`、`CloseFastPathReg`、`ResetShakeBuf` | AICPU 写入 ready queue / shake buffer / MAINBASE 寄存器后,在**下发任务 / 关闭寄存器 / 读取打印缓冲**之前必须有 `__sync_synchronize()`,否则其他核可能读到旧值导致任务丢失或 stop 失败。判定标准:存在「跨核共享写 → 后续依赖该写入的操作」序列但中间无屏障即违规。三类典型场景:(1) 写 ready queue → `TryBatchSendTask` 前;(2) 写 MAINBASE reg → `CloseFastPathReg` 前;(3) 写 shake buffer → 读取前(`ENABLE_AICORE_PRINT` 路径)。**以上函数和场景仅为已知示例,并非穷举——Agent 必须实际分析 PR 改动代码中是否存在新的「跨核共享写 → 依赖该写入的后续操作」序列遗漏屏障,不可仅依赖关键词列表做命中判断。** | | ||
| 44 | +| U5 | AICPU→AICore 跨核共享数据结构须 cacheline 对齐(dcci 刷新安全) | Blocker | `framework/src/machine/device/dynamic/*`、`framework/src/machine/utils/dynamic/*` | `slab`、`dcci`、`devtask`、`DevTaskPtr`、`cceBinary`、`cacheline`、`align` | AICPU 写入、AICore 通过 dcci 读取的共享数据结构(如 devtask 对象)必须 **cacheline 对齐**,否则字段跨 cacheline 边界时 dcci 只刷新起始 cacheline,其余 cacheline 未刷新 → AICore 读到脏数据(指针为 0 或旧值)→ 前几轮正常、后续轮次报 AICPU 异常。判定标准:(1) slab / pool 分配的跨核共享对象地址是否 cacheline 对齐(通常 64B);(2) 对象内被 dcci 读取的字段(指针、flag)是否可能跨越 cacheline 边界;(3) 是否有 `alignas`/对齐分配保证。典型场景:slab 分配 devtask 对象未对齐 → cceBinary 指针跨 cacheline → dcci 未刷新 → AICore 读到 0。**来源:issue #2610(FA 正向算子前两轮正常第三轮 AICPU 异常),修复 PR #4728/#4762。上述场景仅为已知案例——Agent 必须实际分析 PR 改动中所有 AICPU 写、AICore dcci 读的共享数据结构是否 cacheline 对齐,不可仅依赖关键词列表做命中判断。** | | ||
| 45 | +| U6 | AICore 新增读取 AICPU 写入数据时须验证 dcci 地址覆盖该数据 | Blocker | `framework/src/machine/device/dynamic/*`、`framework/include/machine/device/dynamic/*` | `dcci`、`DCCI`、`aicore_entry`、`cceBinary`、`DevTaskPtr`、`cache invalid` | 当 PR 在 AICore 侧**新增**对 AICPU 写入数据的访问时,必须检查上下文中已有的 dcci(cache invalidate)操作地址范围是否覆盖该新数据,保证两者在同一 cacheline 内或 dcci 地址已显式包含新数据地址。判定标准:(1) 定位新增读取的 AICPU 写入字段的地址 `addr`;(2) 找到上下文中该读取之前最近的 dcci 操作,检查其地址 `dcci_addr` 和刷新范围是否覆盖 `addr`(即 `addr ∈ [dcci_addr, dcci_addr + cacheline_size)`);(3) 若新数据地址与已有 dcci 地址不在同一 cacheline,则必须新增 dcci 操作显式刷新该地址,否则 AICore 会读到未刷新的脏数据。典型场景:已有 dcci 刷新了 devtask 对象起始 cacheline(含旧字段),新增读取同对象内偏移较大的字段(如 cceBinary 指针)落在另一个 cacheline → 未被 dcci 覆盖 → 读到 0 或旧值。**该规则关注「已有 dcci 是否覆盖新增字段」,与 U5(对象本身 cacheline 对齐)互补:U5 管分配时对齐,U6 管后续新增访问时的 dcci 覆盖性。Agent 必须对 PR 中每处 AICore 侧新增读取 AICPU 写入数据的位置逐一验证 dcci 覆盖性,不可仅依赖关键词列表做命中判断。** | | ||
| 46 | +| U7 | 新增 slab 类型时注册 obj 大小须与实际 alloc size 一致 | Blocker | `framework/src/machine/device/dynamic/*`、`framework/src/machine/utils/dynamic/*`、`framework/include/machine/device/dynamic/*` | `WsAicpuSlabMemType`、`slab`、`SlabAddCache`、`SlabAlloc`、`objSize`、`COHERENT_SLAB_MEM_TYPE_BUTT` | 当 PR 新增 `WsAicpuSlabMemType` 枚举值(或新增 slab 注册)时,必须验证 slab 注册时声明的 `objSize` 与实际 `SlabAlloc` / `SlabAddCache` 调用时传入的 size 一致。判定标准:(1) 新增枚举值须插在 `COHERENT_SLAB_MEM_TYPE_BUTT` 之前(coherent 类型之前的 slab 参与 cacheline 对齐保证,见 U5);(2) 找到该类型的 slab 注册点(`SlabAddCache(type, objSize, ...)`),记录声明的 `objSize`;(3) grep 所有 `SlabAlloc(type, ...)` / `SlabAlloc<type>(...)` 调用点,逐一核对传入 size 与注册 `objSize` 一致;(4) 若 obj 内含 cacheline 敏感字段(被 dcci 读取),`objSize` 还须 ≥ cacheline(64B)或为 cacheline 整数倍。典型问题:注册 `objSize=48` 但实际 alloc `sizeof(DevTask)=72` → slab 返回 48B 槽位 → 对象截断 → 字段越界读脏数据。**该规则是 U5/U6 的前置保障:objSize 错误会导致 slab 分配截断或跨对象污染,进而引发 cacheline 对齐和 dcci 覆盖问题。Agent 必须对 PR 中每个新增/修改的 slab 类型逐一核对注册 size 与 alloc size,不可仅检查枚举定义。** | | ||
| 47 | + | ||
| 48 | +--- | ||
| 49 | + | ||
| 50 | +## 录入示例 | ||
| 51 | + | ||
| 52 | +以下为示例,展示填写格式(可删除): | ||
| 53 | + | ||
| 54 | +| U# | 检查项 | 级别 | 适用路径 | 关键词 | 典型问题/判定标准 | | ||
| 55 | +|----|--------|------|----------|--------|-------------------| | ||
| 56 | +| U1 | AOT cache 复用需验证 shape 无关 | Blocker | `framework/src/machine/device/dynamic/*` | `AOTCodePoolManager` | 若 CF binary 随 shape 变化,同 hash 复用会导致用错 code | | ||
| 57 | +| U2 | 禁止在 DeviceRunner 析构中新增 rtMemcpy | Major | `framework/src/machine/runtime/runner/*` | `~DeviceRunner` | 析构与 torch_npu context 释放竞争 | | ||
| 58 | +| U3 | bundle TLV value 必须 4KB 对齐 | Major | `framework/src/machine/runtime/bundle/*` | `valueOffset` | `offset % 4096 != 0` 导致设备 page copy 失败 | | ||
| @@ -174,6 +174,28 @@ Device 侧核内拷贝另论;本条约束的是 **Host runtime / launcher** | |||
| 174 | 174 | ||
| 175 | 热头文件少拉日志;dump 默认关;性能计数与业务解耦。 | 175 | 热头文件少拉日志;dump 默认关;性能计数与业务解耦。 |
| 176 | 176 | ||
| 177 | +#### 2.14.1 耗时 debug 操作的宏隔离 | ||
| 178 | + | ||
| 179 | +新增耗时 debug 操作(dump 文件、遍历 op/tensor 写 CSV、`std::ofstream`/`std::mutex`/`std::string` 等)时,必须根据其运行场景选择正确的宏隔离: | ||
| 180 | + | ||
| 181 | +| 场景 | 宏 | 效果 | 典型用法 | | ||
| 182 | +|---|---|---|---| | ||
| 183 | +| 仅 Host 仿真运行(不需要 device 侧编译) | `DEV_IF_NONDEVICE` | device 侧编译期排除整个代码块,零开销 | `DEV_IF_NONDEVICE { DumpRootMemory(...); }` | | ||
| 184 | +| 需要在 device 侧运行但仅 debug 模式生效 | `DEV_IF_DEBUG` | release 模式编译期排除,debug 模式保留 | `DEV_IF_DEBUG { DumpTensorRange(...); }` | | ||
| 185 | + | ||
| 186 | +**判定流程**: | ||
| 187 | +1. 该 debug 操作是否需要在 device 侧(AICPU 运行时)执行? | ||
| 188 | + - **否**(仅 Host 仿真/编译期 dump)→ 用 `DEV_IF_NONDEVICE` 隔离 | ||
| 189 | + - **是**(device 运行时也需要 dump)→ 进入步骤 2 | ||
| 190 | +2. 该操作是否耗时(文件 IO、STL 容器、遍历 op/tensor)? | ||
| 191 | + - **是** → 用 `DEV_IF_DEBUG` 隔离,确保 release 模式不执行 | ||
| 192 | + - **否**(仅读一个 flag/计数器)→ 可不加宏,但须有 `if (!enabled) return` 运行时守卫 | ||
| 193 | + | ||
| 194 | +**禁止**: | ||
| 195 | +- 在 device 侧也会编译的函数中直接调用 debug 函数而不加任何宏隔离(即使被调函数有 `#else` 空实现,调用本身仍生成无用开销) | ||
| 196 | +- 用 `#ifndef __DEVICE__` 替代 `DEV_IF_NONDEVICE`(`DEV_IF_NONDEVICE` 是框架标准分流宏,语义更清晰且可能附带额外检查) | ||
| 197 | +- debug 操作只加运行时 `if (!DumpEnabled()) return` 守卫但不加编译期宏隔离(release 模式下仍编译 STL/文件 IO 代码,增加二进制体积和 icache 压力) | ||
| 198 | + | ||
| 177 | --- | 199 | --- |
| 178 | 200 | ||
| 179 | ### 2.15 无锁仅用于真 SPSC | 201 | ### 2.15 无锁仅用于真 SPSC |
| @@ -195,6 +217,7 @@ Device 侧核内拷贝另论;本条约束的是 **Host runtime / launcher** | |||
| 195 | | 循环边界与不变量外提 | 循环条件里重复算 `B`、体内重复加载 | | 217 | | 循环边界与不变量外提 | 循环条件里重复算 `B`、体内重复加载 | |
| 196 | | Host 慎拷;必须时 `NormalizedRtMemcpy` | Host 随意新增直连 `rtMemcpy` H2D | | 218 | | Host 慎拷;必须时 `NormalizedRtMemcpy` | Host 随意新增直连 `rtMemcpy` H2D | |
| 197 | | Schedule + CF 一并审视 | 只优化 stitch、忽略 `device_sche*` | | 219 | | Schedule + CF 一并审视 | 只优化 stitch、忽略 `device_sche*` | |
| 220 | +| debug 操作按场景加 `DEV_IF_NONDEVICE`/`DEV_IF_DEBUG` 宏隔离 | device 侧函数中裸调 debug 函数仅靠 `#else` 空实现 | | ||
| 198 | 221 | ||
| 199 | --- | 222 | --- |
| 200 | 223 | ||
| @@ -0,0 +1,120 @@ | |||
| 1 | +# Machine 侧 C++ 检视清单 | ||
| 2 | + | ||
| 3 | +面向 `framework/src/machine/` 下 C++ 改动。命中即记录 `{文件, 行号, 级别, 问题, 建议}`。 | ||
| 4 | + | ||
| 5 | +级别定义: | ||
| 6 | +- **Blocker**:必须修复才能合入(crash 风险、精度错误、数据损坏) | ||
| 7 | +- **Major**:应修复(潜在隐患、设计缺陷、范围越界) | ||
| 8 | +- **Minor**:建议修复(风格、文档、可维护性) | ||
| 9 | + | ||
| 10 | +--- | ||
| 11 | + | ||
| 12 | +## §1 算法正确性 | ||
| 13 | + | ||
| 14 | +| # | 检查项 | 级别 | 典型问题 | | ||
| 15 | +|---|---|---|---| | ||
| 16 | +| 1.1 | CRC/哈希多项式与标准一致 | Blocker | `0xEDB88420` 应为 `0xEDB88320`(IEEE-802.3 CRC32) | | ||
| 17 | +| 1.2 | 注释声称的算法名与实现一致 | Major | 注释写"Standard IEEE-802.3"但多项式写错 | | ||
| 18 | +| 1.3 | 整数运算无溢出 | Blocker | `nelm * BytesOf(dtype)` 当 nelm 很大时溢出 | | ||
| 19 | +| 1.4 | 除法/移位无截断误差 | Major | `BitsOf(t)/8` 对 sub-byte 类型(INT4)截断,与 `BytesOf` 不等价 | | ||
| 20 | +| 1.5 | 边界条件(off-by-one、空输入) | Blocker | `len==0` 时仍解引用、`size()==0` 时除零 | | ||
| 21 | + | ||
| 22 | +验证方法:对照标准算法实现或头文件中的定义(如 `framework/include/tilefwk/data_type.h` 的 `BytesOf` vs `DataSizeOf`)。 | ||
| 23 | + | ||
| 24 | +--- | ||
| 25 | + | ||
| 26 | +## §2 内存安全 | ||
| 27 | + | ||
| 28 | +| # | 检查项 | 级别 | 典型问题 | | ||
| 29 | +|---|---|---|---| | ||
| 30 | +| 2.1 | weak 符号调用前判空 | Blocker | `AdxDataDumpServerUnInit()` 直接调用,未判 `if (!func)` | | ||
| 31 | +| 2.2 | 对齐保证与注释一致 | Major | 注释称"4KB aligned"但用 `std::vector`(只保证 16 字节) | | ||
| 32 | +| 2.3 | 缓冲区大小校验 | Blocker | `memcpy_s` 目标缓冲区小于源、TLV `offset+length > totalSize` | | ||
| 33 | +| 2.4 | 空指针解引用 | Blocker | `reinterpret_cast<T*>(nullptr)` 后直接访问 | | ||
| 34 | +| 2.5 | use-after-free / 悬垂指针 | Blocker | 返回局部变量地址、lambda 捕获引用后异步使用 | | ||
| 35 | +| 2.6 | 堆分配在热路径 | Major | AICPU 热路径 `new`/STL(见 §6 / `perf-rules.md` §2.1) | | ||
| 36 | + | ||
| 37 | +验证方法:grep `__attribute__((weak))` 确认所有 weak 符号调用点;检查 `AlignedCopy` 类函数的对齐实现。 | ||
| 38 | + | ||
| 39 | +--- | ||
| 40 | + | ||
| 41 | +## §3 缓存/哈希碰撞 | ||
| 42 | + | ||
| 43 | +| # | 检查项 | 级别 | 典型问题 | | ||
| 44 | +|---|---|---|---| | ||
| 45 | +| 3.1 | cache key 是否包含运行时 shape | Blocker | `KernelBundleRegistry::CacheByHash` 用 hashKey 去重,new IR 不同 shape 相同 hash → 静默返回错误 bundle | | ||
| 46 | +| 3.2 | hash 算法是否含动态维度具体值 | Major | `ComputeHash` 只哈希 IR 结构,动态维度保持符号 `-1`,不同 shape 相同 hash | | ||
| 47 | +| 3.3 | AOT cache 复用是否安全 | Blocker | `AOTCodePoolManager::FindEntry` 按 hashKey 命中,若 CF binary 是 shape 相关则用错 code | | ||
| 48 | +| 3.4 | 文档/注释与去重逻辑一致 | Major | header 写"v1: single static-shape snapshot"但 registry 按 hash 去重多 shape | | ||
| 49 | + | ||
| 50 | +验证方法:同进程跑两个不同 shape,观察第二次是否全 0 / 精度错误;dump `ComputeHash` 的 hash 字符串对比。 | ||
| 51 | + | ||
| 52 | +--- | ||
| 53 | + | ||
| 54 | +## §4 范围纪律 | ||
| 55 | + | ||
| 56 | +| # | 检查项 | 级别 | 典型问题 | | ||
| 57 | +|---|---|---|---| | ||
| 58 | +| 4.1 | 无关改动混入功能 PR | Major | bundle PR 混入 HubMix 重构、文档删除、测试删除 | | ||
| 59 | +| 4.2 | 删除已有保护逻辑 | Blocker | 删除 weak 符号判空、删除边界检查 | | ||
| 60 | +| 4.3 | 无注释的魔法数字 | Minor | 硬编码 `4096`、`0xFF` 无解释 | | ||
| 61 | +| 4.4 | 函数名与行为不符 | Major | `AlignedCopy` 不保证对齐 | | ||
| 62 | +| 4.5 | 全局变量无锁说明 | Minor | `g_aicpuSoOverride` 注释称"no locking needed"但未说明约束 | | ||
| 63 | + | ||
| 64 | +验证方法:`git diff --stat` 看文件范围是否与 PR 标题匹配;逐 hunk 判断"这行改动是否直接服务于 PR 主题"。 | ||
| 65 | + | ||
| 66 | +--- | ||
| 67 | + | ||
| 68 | +## §5 Bundle / 离线打包专项 | ||
| 69 | + | ||
| 70 | +当 PR 涉及 `framework/src/machine/runtime/bundle/` 时额外检查: | ||
| 71 | + | ||
| 72 | +| # | 检查项 | 级别 | 典型问题 | | ||
| 73 | +|---|---|---|---| | ||
| 74 | +| 5.1 | CRC32 多项式 | Blocker | `0xEDB88420` 应为 `0xEDB88320` | | ||
| 75 | +| 5.2 | BundleHeader/TlvHeader 大小断言 | Major | `static_assert(sizeof(BundleHeader)==64)` 缺失 | | ||
| 76 | +| 5.3 | TLV value 4KB 对齐 | Major | `valueOffset % 4096 != 0` | | ||
| 77 | +| 5.4 | 未知 TLV type 跳过而非报错 | Minor | `default: break;` 静默丢弃 | | ||
| 78 | +| 5.5 | magic number 字节序假设 | Minor | 注释写"little-endian"但未校验平台 | | ||
| 79 | +| 5.6 | version 向前兼容 | Major | `hdr.version > kBundleVersion` 直接拒绝旧版无降级 | | ||
| 80 | +| 5.7 | devProgram 对齐实现 | Major | `AlignedCopy` 用 `std::vector` 不保证 4KB | | ||
| 81 | +| 5.8 | registry hashKey 去重碰撞 | Blocker | 见 §3.1 | | ||
| 82 | +| 5.9 | pack/load target 循环依赖 | Major | pack hook 和 load ABI 在同一 target | | ||
| 83 | +| 5.10 | standalone .so 双 singleton | Major | 与普通 pypto stack 共存时 Meyers singleton 分裂 | | ||
| 84 | + | ||
| 85 | +验证方法:读 `kernel_bundle_format.h` 的常量定义、`kernel_bundle_crc32.h` 的多项式、`kernel_bundle_packer.cpp` 的布局计算、`kernel_bundle_registry.cpp` 的去重键。 | ||
| 86 | + | ||
| 87 | +--- | ||
| 88 | + | ||
| 89 | +## §6 AICPU 热路径性能 | ||
| 90 | + | ||
| 91 | +当 PR 涉及 `framework/src/machine/` 下 AICPU Control-Flow / Schedule 热路径(`device_*`、`device_sche*`、`dev_workspace`、`item_pool`、`aot_binary`、`spsc_queue`、CF cache、stitch/task build)时额外检查。 | ||
| 92 | + | ||
| 93 | +详细原则见 [references/perf-rules.md](perf-rules.md)(**必读**后再下评审结论),至少覆盖 §2.1–§2.5、§2.9–§2.11。 | ||
| 94 | + | ||
| 95 | +| # | 检查项 | 级别 | 典型问题 | | ||
| 96 | +|---|---|---|---| | ||
| 97 | +| 6.1 | 热路径无 STL 容器 / 业务 `new` | Blocker | `std::vector`/`std::map`/`new` 在 AICPU 热循环(原则 §2.1) | | ||
| 98 | +| 6.2 | 地址用显式指针类型 | Major | `uint64_t` 长期冒充地址(原则 §2.2) | | ||
| 99 | +| 6.3 | 局部对象优先引用传参 | Minor | 无必要 `&local` 指针传参(原则 §2.3) | | ||
| 100 | +| 6.4 | 热点结构体 trivial 默构 + 布局纪律 | Major | 非平凡默构放大成无用写;热点结构塞冷字段(原则 §2.4) | | ||
| 101 | +| 6.5 | 无整结构 memset | Major | `memset(整个 DeviceTask, 0, sizeof(...))`,应 `InitShell` 只清壳字段(原则 §2.5) | | ||
| 102 | +| 6.6 | 能 Host 判断的 if 不放到 AICPU | Major | 未「以存代算」——encode 置 flag,runtime 读(原则 §2.9) | | ||
| 103 | +| 6.7 | 循环边界与不变量外提 | Major | 循环条件里重复算 `B`、体内重复加载(原则 §2.10) | | ||
| 104 | +| 6.8 | Host 无新增直连 `rtMemcpy` H2D | Blocker | 必须拷贝则 `NormalizedRtMemcpy` / RELAXED(原则 §2.11) | | ||
| 105 | +| 6.9 | Schedule 路径与 CF 路径一并检查 | Major | 只优化 stitch、忽略 `device_sche*` | | ||
| 106 | +| 6.10 | 耗时 debug 操作加宏隔离 | Major | 仅 Host 仿真的 dump 用 `DEV_IF_NONDEVICE`;device 侧需要的耗時 debug 用 `DEV_IF_DEBUG`;禁止裸调 debug 函数仅靠 `#else` 空实现(原则 §2.14.1) | | ||
| 107 | + | ||
| 108 | +验证方法:读 [references/perf-rules.md](perf-rules.md) 原则逐条对照;grep `std::vector|std::map|new |malloc` 在 `framework/src/machine/device/dynamic/*` 热路径文件中是否出现。 | ||
| 109 | + | ||
| 110 | +参考代码: | ||
| 111 | +```text | ||
| 112 | +framework/src/machine/utils/dynamic/item_pool.h | ||
| 113 | +framework/src/machine/utils/dynamic/vector.h | ||
| 114 | +framework/src/machine/utils/dynamic/spsc_queue.h | ||
| 115 | +framework/src/machine/utils/dynamic/device_task.h | ||
| 116 | +framework/src/machine/utils/dynamic/dev_encode_program_ctrlflow_cache.h | ||
| 117 | +framework/src/machine/device/dynamic/aot_binary.h | ||
| 118 | +framework/src/machine/device/dynamic/device_sche.cpp | ||
| 119 | +framework/src/machine/runtime/memory_utils/memory_pool.cpp # NormalizedRtMemcpy | ||
| 120 | +``` | ||
| @@ -0,0 +1,256 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | +# Copyright (c) Huawei Technologies Co., Ltd. 2024-2026. All rights reserved. | ||
| 4 | +"""批量提交 GitCode PR 行级 diff_comment(检视意见显示在代码行旁)。 | ||
| 5 | + | ||
| 6 | +用法: | ||
| 7 | + # 方式 1: 命令行逐条指定 | ||
| 8 | + TOKEN=$(git remote get-url origin | sed -nE 's|.*oauth2:([^@]+)@.*|\\1|p') | ||
| 9 | + python3 submit_review_comments.py --pr 4974 --token "$TOKEN" \\ | ||
| 10 | + --comment "framework/src/a.h:31:**Blocker**: CRC32 多项式错误" \\ | ||
| 11 | + --comment "framework/src/b.cpp:37:**Major**: 碰撞风险" | ||
| 12 | + | ||
| 13 | + # 方式 2: 从 JSON 文件读取 | ||
| 14 | + python3 submit_review_comments.py --pr 4974 --token "$TOKEN" \\ | ||
| 15 | + --file comments.json | ||
| 16 | + | ||
| 17 | +comments.json 格式: | ||
| 18 | + [ | ||
| 19 | + {"path": "framework/src/a.h", "position": 31, "body": "...", "level": "Blocker"}, | ||
| 20 | + {"path": "framework/src/b.cpp", "position": 37, "body": "...", "level": "Major"} | ||
| 21 | + ] | ||
| 22 | +""" | ||
| 23 | + | ||
| 24 | +from __future__ import annotations | ||
| 25 | + | ||
| 26 | +import argparse | ||
| 27 | +from dataclasses import dataclass | ||
| 28 | +import json | ||
| 29 | +import subprocess | ||
| 30 | +import sys | ||
| 31 | +from typing import Optional | ||
| 32 | +import urllib.error | ||
| 33 | +import urllib.request | ||
| 34 | + | ||
| 35 | +API_BASE = "https://gitcode.com/api/v5" | ||
| 36 | +OWNER = "cann" | ||
| 37 | +REPO = "pypto" | ||
| 38 | + | ||
| 39 | + | ||
| 40 | +@dataclass | ||
| 41 | +class ReviewComment: | ||
| 42 | + path: str | ||
| 43 | + position: int | ||
| 44 | + body: str | ||
| 45 | + level: str = "" | ||
| 46 | + | ||
| 47 | + def to_payload(self) -> dict: | ||
| 48 | + return { | ||
| 49 | + "body": self.body, | ||
| 50 | + "path": self.path, | ||
| 51 | + "position": self.position, | ||
| 52 | + "need_to_resolve": True, | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + | ||
| 56 | +def extract_token_from_git() -> Optional[str]: | ||
| 57 | + """从 git remote URL 提取 oauth2 token。""" | ||
| 58 | + try: | ||
| 59 | + url = subprocess.check_output( | ||
| 60 | + ["git", "remote", "get-url", "origin"], | ||
| 61 | + stderr=subprocess.DEVNULL, | ||
| 62 | + text=True, | ||
| 63 | + ).strip() | ||
| 64 | + except (subprocess.CalledProcessError, FileNotFoundError): | ||
| 65 | + return None | ||
| 66 | + if "oauth2:" not in url: | ||
| 67 | + return None | ||
| 68 | + return url.split("oauth2:", 1)[1].split("@", 1)[0] | ||
| 69 | + | ||
| 70 | + | ||
| 71 | +def api_request(token: str, method: str, path: str, payload: Optional[dict] = None) -> tuple[int, dict]: | ||
| 72 | + """执行 GitCode API 请求,返回 (http_code, response_dict)。""" | ||
| 73 | + url = f"{API_BASE}/repos/{OWNER}/{REPO}/{path}" | ||
| 74 | + data = json.dumps(payload).encode() if payload else None | ||
| 75 | + req = urllib.request.Request(url, data=data, method=method) | ||
| 76 | + req.add_header("PRIVATE-TOKEN", token) | ||
| 77 | + req.add_header("Content-Type", "application/json") | ||
| 78 | + try: | ||
| 79 | + with urllib.request.urlopen(req, timeout=30) as resp: | ||
| 80 | + body = resp.read().decode() | ||
| 81 | + return resp.status, json.loads(body) if body else {} | ||
| 82 | + except urllib.error.HTTPError as e: | ||
| 83 | + body = e.read().decode() | ||
| 84 | + try: | ||
| 85 | + return e.code, json.loads(body) | ||
| 86 | + except json.JSONDecodeError: | ||
| 87 | + return e.code, {"raw": body} | ||
| 88 | + except urllib.error.URLError as e: | ||
| 89 | + return -1, {"error": str(e)} | ||
| 90 | + | ||
| 91 | + | ||
| 92 | +def verify_token(token: str) -> bool: | ||
| 93 | + """验证 token 是否可用。""" | ||
| 94 | + url = f"{API_BASE}/user?access_token={token}" | ||
| 95 | + try: | ||
| 96 | + with urllib.request.urlopen(url, timeout=10) as resp: | ||
| 97 | + return resp.status == 200 | ||
| 98 | + except (urllib.error.HTTPError, urllib.error.URLError): | ||
| 99 | + return False | ||
| 100 | + | ||
| 101 | + | ||
| 102 | +def submit_comment(token: str, pr_number: int, comment: ReviewComment) -> dict: | ||
| 103 | + """提交一条行级 diff_comment,返回结果 dict。""" | ||
| 104 | + code, resp = api_request( | ||
| 105 | + token, "POST", | ||
| 106 | + f"pulls/{pr_number}/comments", | ||
| 107 | + comment.to_payload(), | ||
| 108 | + ) | ||
| 109 | + result = { | ||
| 110 | + "path": comment.path, | ||
| 111 | + "position": comment.position, | ||
| 112 | + "level": comment.level, | ||
| 113 | + "body_preview": comment.body[:60], | ||
| 114 | + "http_code": code, | ||
| 115 | + "comment_id": resp.get("id", resp.get("note_id")), | ||
| 116 | + "comment_type": resp.get("comment_type", "?"), | ||
| 117 | + "status": "ok" if code in (200, 201) else f"fail({code})", | ||
| 118 | + } | ||
| 119 | + if code not in (200, 201): | ||
| 120 | + result["error"] = resp.get("error_message") or resp.get("error") or str(resp)[:200] | ||
| 121 | + return result | ||
| 122 | + | ||
| 123 | + | ||
| 124 | +def verify_diff_comment(token: str, pr_number: int, comments: list[ReviewComment]) -> list[dict]: | ||
| 125 | + """验证评论是否真正成为 diff_comment。 | ||
| 126 | + | ||
| 127 | + POST 响应只返回 {id(hash), body, note_id},不含 comment_type/diff_position。 | ||
| 128 | + GET 列表的 id 是数字格式,与 POST 返回的 hash id 不匹配。 | ||
| 129 | + GET 列表也不含 path 字段,因此用 position + body 前缀匹配。 | ||
| 130 | + 默认 per_page=20 不够,需拉取足够页。 | ||
| 131 | + """ | ||
| 132 | + results = [] | ||
| 133 | + for page in range(1, 10): | ||
| 134 | + code, resp = api_request(token, "GET", f"pulls/{pr_number}/comments?per_page=100&page={page}", None) | ||
| 135 | + if code != 200 or not isinstance(resp, list) or len(resp) == 0: | ||
| 136 | + break | ||
| 137 | + target_by_pos = {} | ||
| 138 | + for c in comments: | ||
| 139 | + target_by_pos.setdefault(c.position, []).append(c.body[:30]) | ||
| 140 | + for c in resp: | ||
| 141 | + dp = c.get("diff_position") or {} | ||
| 142 | + line = dp.get("start_new_line") | ||
| 143 | + if line in target_by_pos: | ||
| 144 | + body_prefix = (c.get("body") or "")[:30] | ||
| 145 | + if any(body_prefix.startswith(prefix) or prefix in body_prefix for prefix in target_by_pos[line]): | ||
| 146 | + results.append({ | ||
| 147 | + "id": c.get("id"), | ||
| 148 | + "comment_type": c.get("comment_type"), | ||
| 149 | + "diff_position": dp, | ||
| 150 | + "body_preview": (c.get("body") or "")[:60], | ||
| 151 | + "is_diff_comment": c.get("comment_type") == "diff_comment" and dp is not None, | ||
| 152 | + }) | ||
| 153 | + if len(results) >= len(comments): | ||
| 154 | + break | ||
| 155 | + return results | ||
| 156 | + | ||
| 157 | + | ||
| 158 | +def parse_cli_comment(spec: str) -> ReviewComment: | ||
| 159 | + """解析 'path:position:body' 格式的命令行参数。""" | ||
| 160 | + parts = spec.split(":", 2) | ||
| 161 | + if len(parts) != 3: | ||
| 162 | + raise ValueError(f"invalid --comment format, expected 'path:position:body', got: {spec}") | ||
| 163 | + path, pos_str, body = parts | ||
| 164 | + position = int(pos_str) | ||
| 165 | + level = "" | ||
| 166 | + for tag in ("Blocker", "Major", "Minor"): | ||
| 167 | + if body.startswith(tag): | ||
| 168 | + level = tag | ||
| 169 | + break | ||
| 170 | + return ReviewComment(path=path, position=position, body=body, level=level) | ||
| 171 | + | ||
| 172 | + | ||
| 173 | +def load_comments_from_file(filepath: str) -> list[ReviewComment]: | ||
| 174 | + """从 JSON 文件加载评论列表。""" | ||
| 175 | + with open(filepath, "r", encoding="utf-8") as f: | ||
| 176 | + data = json.load(f) | ||
| 177 | + comments = [] | ||
| 178 | + for item in data: | ||
| 179 | + comments.append(ReviewComment( | ||
| 180 | + path=item["path"], | ||
| 181 | + position=int(item["position"]), | ||
| 182 | + body=item["body"], | ||
| 183 | + level=item.get("level", ""), | ||
| 184 | + )) | ||
| 185 | + return comments | ||
| 186 | + | ||
| 187 | + | ||
| 188 | +def main() -> int: | ||
| 189 | + parser = argparse.ArgumentParser( | ||
| 190 | + description="批量提交 GitCode PR 行级 diff_comment(检视意见显示在代码行旁)" | ||
| 191 | + ) | ||
| 192 | + parser.add_argument("--pr", type=int, required=True, help="PR number") | ||
| 193 | + parser.add_argument("--token", type=str, default=None, help="GitCode API token (默认从 git remote 提取)") | ||
| 194 | + parser.add_argument("--comment", action="append", default=[], help="单条评论: 'path:position:body'") | ||
| 195 | + parser.add_argument("--file", type=str, default=None, help="从 JSON 文件读取评论列表") | ||
| 196 | + parser.add_argument("--no-verify", action="store_true", help="跳过 diff_comment 验证") | ||
| 197 | + args = parser.parse_args() | ||
| 198 | + | ||
| 199 | + token = args.token or extract_token_from_git() | ||
| 200 | + if not token: | ||
| 201 | + print("ERROR: 无法获取 token,请用 --token 指定或确保 git remote origin 含 oauth2 token", file=sys.stderr) | ||
| 202 | + return 1 | ||
| 203 | + | ||
| 204 | + if not verify_token(token): | ||
| 205 | + print("ERROR: token 验证失败 (401)", file=sys.stderr) | ||
| 206 | + return 1 | ||
| 207 | + | ||
| 208 | + comments: list[ReviewComment] = [] | ||
| 209 | + for spec in args.comment: | ||
| 210 | + try: | ||
| 211 | + comments.append(parse_cli_comment(spec)) | ||
| 212 | + except ValueError as e: | ||
| 213 | + print(f"ERROR: {e}", file=sys.stderr) | ||
| 214 | + return 1 | ||
| 215 | + if args.file: | ||
| 216 | + comments.extend(load_comments_from_file(args.file)) | ||
| 217 | + | ||
| 218 | + if not comments: | ||
| 219 | + print("ERROR: 未提供评论 (--comment 或 --file)", file=sys.stderr) | ||
| 220 | + return 1 | ||
| 221 | + | ||
| 222 | + print(f"=== 提交 {len(comments)} 条行级 diff_comment 到 PR {args.pr} ===\n") | ||
| 223 | + | ||
| 224 | + success = 0 | ||
| 225 | + for i, c in enumerate(comments, 1): | ||
| 226 | + result = submit_comment(args.token or token, args.pr, c) | ||
| 227 | + if result["status"] == "ok": | ||
| 228 | + success += 1 | ||
| 229 | + print(f" [{i}/{len(comments)}] {c.path}:{c.position} ({c.level or 'N/A'})") | ||
| 230 | + print(f" {result['status']} id={result['comment_id']} type={result['comment_type']}") | ||
| 231 | + | ||
| 232 | + print(f"\n提交完成: {success}/{len(comments)} 成功\n") | ||
| 233 | + | ||
| 234 | + if not args.no_verify: | ||
| 235 | + print("=== 验证 diff_comment ===\n") | ||
| 236 | + verifications = verify_diff_comment(token, args.pr, comments) | ||
| 237 | + if not verifications: | ||
| 238 | + print(" ⚠️ 未找到已提交的评论(可能需要等待或翻页)") | ||
| 239 | + diff_count = 0 | ||
| 240 | + for v in verifications: | ||
| 241 | + is_diff = v["is_diff_comment"] | ||
| 242 | + if is_diff: | ||
| 243 | + diff_count += 1 | ||
| 244 | + dp = v.get("diff_position") or {} | ||
| 245 | + line = dp.get("start_new_line", "?") | ||
| 246 | + mark = "✅" if is_diff else "❌ pr_comment" | ||
| 247 | + print(f" id={v['id']} type={v['comment_type']} line={line} {mark}") | ||
| 248 | + print(f"\n验证完成: {diff_count}/{len(verifications)} 为 diff_comment(行级评论)") | ||
| 249 | + | ||
| 250 | + pr_url = f"https://gitcode.com/{OWNER}/{REPO}/merge_requests/{args.pr}/files" | ||
| 251 | + print(f"\nPR diff 视图: {pr_url}") | ||
| 252 | + return 0 if success == len(comments) else 1 | ||
| 253 | + | ||
| 254 | + | ||
| 255 | +if __name__ == "__main__": | ||
| 256 | + sys.exit(main()) | ||
| @@ -48,6 +48,7 @@ | |||
| 48 | | 分析编译期 Pass 模块 | skill `pypto-pass-module-analyzer` / `pypto-pass-workflow-analyzer` | | 48 | | 分析编译期 Pass 模块 | skill `pypto-pass-module-analyzer` / `pypto-pass-workflow-analyzer` | |
| 49 | | 排查 workspace 内存问题 | skill `pypto-memory-overlap-detector` / `pypto-machine-workspace` | | 49 | | 排查 workspace 内存问题 | skill `pypto-memory-overlap-detector` / `pypto-machine-workspace` | |
| 50 | | 调试算子精度问题 | skill `pypto-precision-overall` | | 50 | | 调试算子精度问题 | skill `pypto-precision-overall` | |
| 51 | -| AICPU / CF 热路径高性能编码与评审 | skill `pypto-aicpu-perf-coding` | | 51 | +| AICPU / CF 热路径高性能编码与评审 | skill `pypto-machine-code-review` | |
🟡 Medium Priority 本 diff 删除了
这是 diff 的单侧变更(重命名 skill)未同步更新关联文档导致的发散:用户/Agent 访问 建议:将 ![]() ![]() Incorrect? | |||
| 52 | | 安装 / 修复环境 | skill `pypto-environment-setup` | | 52 | | 安装 / 修复环境 | skill `pypto-environment-setup` | |
| 53 | | 创建 / 修复 PR、创建 Issue | skill `pypto-pr-creator` / `pypto-pr-fixer` / `pypto-issue-creator` | | 53 | | 创建 / 修复 PR、创建 Issue | skill `pypto-pr-creator` / `pypto-pr-fixer` / `pypto-issue-creator` | |
| 54 | +| 检视 machine 侧代码、提交行级 review 意见 | skill `pypto-machine-code-review` | | ||


🟠 High Priority
SKILL.md 第 84 行的 token 校验命令将 token 拼接到 URL 查询字符串中:
curl -s ... "https://gitcode.com/api/v5/user?access_[REDACTED] 中的查询参数会被 Web 服务器访问日志、反向代理日志记录,也可能被 shell 历史文件(.bash_history)捕获。Agent 执行此命令时,token 存在泄露风险。同一文档第 101 行的 curl POST 示例已正确使用 header 方式(-H "PRIVATE-[REDACTED] header 认证完全可用,查询参数方式无必要。建议:将 token 校验命令改为使用 header 认证,与文档中 POST 示例保持一致:
curl -s -o /dev/null -w "%{http_code}" -H "PRIVATE-[REDACTED] "https://gitcode.com/api/v5/user"