Skip to content

Commit 6e1b29a

Browse files
committed
fix skill.md and update SumTest.cpp
1 parent a0af9ab commit 6e1b29a

File tree

2 files changed

+352
-55
lines changed

2 files changed

+352
-55
lines changed

.claude/skills/compatibility-testing/SKILL.md

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static void write_abs_result_to_file(FileManerger* file, const at::Tensor& resul
9494

9595
注意:
9696
- 第一个测试用例调用 `file.createFile()` 创建文件,后续用例调用 `file.openAppend()` 追加
97+
- 每个用例输出前须写入**用例名标签**,输出末尾须追加 `"\n"` 换行,使每个用例占独立一行(详见"输出格式"章节)
9798
- 对于多 dtype 支持的算子,需按 `result.scalar_type()` 分发到对应的 `data_ptr<T>()` 类型
9899

99100
## Shape 覆盖要求
@@ -140,49 +141,74 @@ static void write_abs_result_to_file(FileManerger* file, const at::Tensor& resul
140141

141142
```cpp
142143
TEST_F(SomeOpTest, InvalidInputHandling) {
144+
auto file_name = g_custom_param.get();
145+
FileManerger file(file_name);
146+
file.openAppend();
147+
file << "InvalidInputHandling ";
143148
try {
144149
at::Tensor result = at::some_op(invalid_tensor);
145150
// 未抛异常 — 正常记录结果
146-
auto file_name = g_custom_param.get();
147-
FileManerger file(file_name);
148-
file.openAppend();
149151
write_someop_result_to_file(&file, result);
150-
file.saveFile();
151-
} catch (const c10::Error& e) {
152-
// ATen/c10 层异常
153-
auto file_name = g_custom_param.get();
154-
FileManerger file(file_name);
155-
file.openAppend();
156-
file << "c10::Error: " << e.what();
157-
file.saveFile();
158152
} catch (const std::exception& e) {
159-
auto file_name = g_custom_param.get();
160-
FileManerger file(file_name);
161-
file.openAppend();
162-
file << "exception: " << e.what();
163-
file.saveFile();
153+
file << "exception ";
164154
}
155+
file << "\n";
156+
file.saveFile();
165157
}
166158
```
167159
168-
> 捕获时优先匹配 `c10::Error`(ATen 的标准异常类型),再兜底 `std::exception`。异常信息写入输出文件后可直接 diff,两框架的异常消息不要求完全一致,但**是否抛异常**须一致。
160+
**注意事项**:
161+
- **不要使用 `c10::Error` 作为 catch 类型** — 在 Paddle 兼容层中 `c10::Error` 是 `C10ErrorType` 枚举常量(定义在 `c10/util/Exception.h`),不是异常类。统一使用 `std::exception` 捕获即可。
162+
- **不要输出 `e.what()`** — 两个框架的异常消息文本不同,会产生大量无意义的 diff。仅记录 `"exception "` 标记是否抛出异常。
163+
- 两框架的对比重点是**是否抛异常**须一致,异常消息内容不要求匹配。
169164
170165
## 输出格式
171166
172-
输出文件采用空格分隔的纯文本,按以下字段顺序逐 tensor 追加
167+
输出文件采用空格分隔的纯文本,**每个用例独占一行**,格式如下
173168
174169
```
175-
<ndim> <numel> [<size_0> <size_1> ...] <val_0> <val_1> ...
170+
<TestCaseName> <ndim> <numel> [<size_0> <size_1> ...] <val_0> <val_1> ...\n
176171
```
177172
178-
示例(一个 shape 为 `{2, 3}` 的 float tensor):
173+
- **用例名标签**:每行以用例名(如 `SumAllElements`)开头,与后续数据以空格分隔
174+
- **换行分隔**:每个用例输出末尾追加 `"\n"`,使 diff 能逐行定位到具体出错的用例
175+
176+
示例(SumTest 的部分输出):
177+
```
178+
SumAllElements 0 1 21.000000
179+
SumWithDtype 7 0 1 21.000000
180+
SumAlongDim0 1 3 3 5.000000 7.000000 9.000000
181+
SumInt32 0 1 100
179182
```
180-
2 6 2 3 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000
183+
184+
代码示例:
185+
```cpp
186+
TEST_F(SumTest, SumAllElements) {
187+
auto file_name = g_custom_param.get();
188+
FileManerger file(file_name);
189+
file.createFile(); // 第一个用例
190+
file << "SumAllElements "; // 用例名标签
191+
at::Tensor result = at::sum(test_tensor);
192+
write_sum_result_to_file(&file, result);
193+
file << "\n"; // 换行分隔
194+
file.saveFile();
195+
}
196+
197+
TEST_F(SumTest, SumWithDtype) {
198+
auto file_name = g_custom_param.get();
199+
FileManerger file(file_name);
200+
file.openAppend(); // 后续用例
201+
file << "SumWithDtype "; // 用例名标签
202+
// ...
203+
file << "\n";
204+
file.saveFile();
205+
}
181206
```
182207

183208
注意事项:
184209
- 浮点值通过 `std::to_string()` 序列化,精度为 6 位有效数字
185-
- 不同测试用例的输出依次追加到同一文件中,以换行或空格分隔,顺序由 GTest 的用例注册顺序决定
210+
- 用例名标签使得 diff 输出直接可读,无需逐字节计数来定位差异
211+
- 不同测试用例的输出依次追加到同一文件中,顺序由 GTest 的用例注册顺序决定
186212

187213
## 调试手段
188214

@@ -254,8 +280,10 @@ EXPECT_TRUE(result.is_contiguous()) << "Non-contiguous result from reshape";
254280

255281
**输出**
256282
- [ ] `*` 第一个用例使用 `createFile()`,后续使用 `openAppend()`
283+
- [ ] `*` 每个用例输出前写入用例名标签,末尾追加 `"\n"` 换行
257284
- [ ] `*` 通过 `write_<op>_result_to_file()` 统一输出
258285
- [ ] 多 dtype 场景按 `scalar_type()` 分发 `data_ptr<T>()`
286+
- [ ] 异常捕获统一使用 `std::exception`(不要用 `c10::Error`),不输出 `e.what()`
259287

260288
## 输出文件路径
261289

0 commit comments

Comments
 (0)