Skip to content

Commit e77e3cb

Browse files
committed
docs: add test usage guide and rename doc files for consistency
- Add docs/test_usage_guide.md with build/run/write instructions - Rename hook_mechanism.md → hook_mechanism_design.md - Rename lora_usage.md → lora_usage_guide.md - Add googletest as submodule in .gitmodules - Add infini_run tool target in CMakeLists.txt, remove stale comments
1 parent 20b735c commit e77e3cb

6 files changed

Lines changed: 156 additions & 5 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "third_party/eigen"]
88
path = third_party/eigen
99
url = git@github.com:InfiniTensor/eigen-mirror.git
10+
[submodule "third_party/googletest"]
11+
path = third_party/googletest
12+
url = git@github.com:google/googletest.git

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ include_directories(${gflags_SOURCE_DIR}/include)
4040
set(WITH_GFLAGS OFF CACHE BOOL "Disable glog finding system gflags" FORCE)
4141
set(WITH_GTEST OFF CACHE BOOL "Disable glog finding system gtest" FORCE)
4242
add_subdirectory(third_party/glog)
43-
# add_compile_definitions(GLOG_USE_GLOG_EXPORT=1)
4443
include_directories(${glog_SOURCE_DIR}/src)
45-
# include_directories(${glog_BINARY_DIR}/glog)
4644

4745
# eigen
4846
if(USE_OMP)
@@ -210,6 +208,9 @@ add_executable(llama3
210208
)
211209
link_infini_train_exe(llama3)
212210

211+
# Tools
212+
add_subdirectory(tools/infini_run)
213+
set_target_properties(infini_run PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
213214

214215
# Tests
215216
if(BUILD_TEST)
File renamed without changes.
File renamed without changes.

docs/test_usage_guide.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# 测试使用指南
2+
3+
本指南介绍如何在 InfiniTrain 中构建、运行和编写测试。底层设计详见 [test_infrastructure_design.md](test_infrastructure_design.md)[test_lifecycle.md](test_lifecycle.md)
4+
5+
---
6+
7+
## 构建测试
8+
9+
```bash
10+
mkdir build && cd build
11+
cmake -DBUILD_TEST=ON -DUSE_CUDA=ON ..
12+
make -j$(nproc)
13+
```
14+
15+
仅构建 CPU(不含 CUDA):
16+
17+
```bash
18+
cmake -DBUILD_TEST=ON -DUSE_CUDA=OFF ..
19+
```
20+
21+
---
22+
23+
## 运行测试
24+
25+
```bash
26+
# 运行所有测试
27+
ctest --output-on-failure
28+
29+
# 仅运行 CPU 测试
30+
ctest -L cpu --output-on-failure
31+
32+
# 仅运行 CUDA 测试
33+
ctest -L cuda --output-on-failure
34+
35+
# 运行指定测试套件
36+
ctest -R tensor --output-on-failure
37+
38+
# 直接运行测试二进制,使用 GTest 过滤器
39+
./tests/tensor/test_tensor_create_cpu --gtest_filter="CPU/TensorCreateTest.*"
40+
./tests/tensor/test_tensor_create_cuda --gtest_filter="CUDA/TensorCreateTest.*"
41+
```
42+
43+
---
44+
45+
## 编写新测试
46+
47+
### 1. 创建测试文件
48+
49+
`tests/` 下对应子目录中新建文件,例如 `tests/tensor/test_tensor_copy.cc`
50+
51+
```cpp
52+
#include "common/test_utils.h"
53+
54+
class TensorCopyTest : public infini_train::test::InfiniTrainTest {};
55+
56+
TEST_P(TensorCopyTest, CopiesDataCorrectly) {
57+
auto src = createTensor({4}, DataType::kFLOAT32);
58+
FillSequentialTensor(src);
59+
60+
auto dst = createTensor({4}, DataType::kFLOAT32);
61+
// ... 执行拷贝并断言 ...
62+
EXPECT_EQ(dst->Dims(), src->Dims());
63+
}
64+
65+
INFINI_TRAIN_REGISTER_TEST(TensorCopyTest);
66+
```
67+
68+
注意事项:
69+
- 继承 `InfiniTrainTest`(autograd 测试继承 `AutogradTestBase`)。
70+
- 使用 `TEST_P`,设备参数由框架自动注入。
71+
- 文件末尾调用 `INFINI_TRAIN_REGISTER_TEST`,自动实例化 CPU 和 CUDA 两个变体。
72+
73+
### 2. 在 CMakeLists.txt 中注册
74+
75+
在子目录的 `CMakeLists.txt`(例如 `tests/tensor/CMakeLists.txt`)中添加:
76+
77+
```cmake
78+
infini_train_add_test_suite(test_tensor_copy test_tensor_copy.cc)
79+
```
80+
81+
这会生成两个 CTest 目标:`test_tensor_copy_cpu`(标签 `cpu`)和 `test_tensor_copy_cuda`(标签 `cuda`)。
82+
83+
---
84+
85+
## 基类辅助方法
86+
87+
以下方法在 `TEST_P` 体内可直接调用(通过 `this->` 隐式访问):
88+
89+
| 方法 | 说明 |
90+
|---|---|
91+
| `GetDevice()` | 返回当前测试实例的设备(CPU 或 CUDA) |
92+
| `createTensor(shape)` | 在当前设备上创建 `kFLOAT32` 张量 |
93+
| `createTensor(shape, dtype)` | 创建指定数据类型的张量 |
94+
| `createTensor(shape, dtype, requires_grad)` | 创建启用自动微分的张量 |
95+
| `FillSequentialTensor(tensor)` | 用 0, 1, 2, … 填充张量(自动处理 CPU/GPU 传输) |
96+
| `FillConstantTensor(tensor, value)` | 用常量填充张量所有元素 |
97+
98+
---
99+
100+
## 条件执行宏
101+
102+
`TEST_P` 体内使用,跳过不适用的场景:
103+
104+
```cpp
105+
TEST_P(MyTest, 仅CUDA) {
106+
ONLY_CUDA(); // 在 CPU 上跳过
107+
// ...
108+
}
109+
110+
TEST_P(MyTest, 仅CPU) {
111+
ONLY_CPU(); // 在 CUDA 上跳过
112+
// ...
113+
}
114+
115+
TEST_P(MyTest, 需要多卡) {
116+
REQUIRE_MIN_DEVICES(2); // GPU 数量不足时跳过
117+
// ...
118+
}
119+
```
120+
121+
---
122+
123+
## 添加新测试模块
124+
125+
1. 在 `tests/` 下新建子目录,例如 `tests/mymodule/`。
126+
2. 在其中创建 `CMakeLists.txt`,对每个测试文件调用 `infini_train_add_test_suite()`。
127+
3. 在 `tests/CMakeLists.txt` 中添加 `add_subdirectory(mymodule)`。
128+
129+
---
130+
131+
## Autograd 测试
132+
133+
需要预填充输入张量时,继承 `AutogradTestBase`:
134+
135+
```cpp
136+
#include "common/test_utils.h"
137+
138+
class MyOpTest : public infini_train::test::AutogradTestBase {};
139+
140+
TEST_P(MyOpTest, 前向传播) {
141+
// input_ 和 weight_ 已在当前设备上创建并填充好序列值
142+
auto output = MyOp(input_, weight_);
143+
EXPECT_NE(output, nullptr);
144+
}
145+
146+
INFINI_TRAIN_REGISTER_TEST(MyOpTest);
147+
```
148+
149+
`AutogradTestBase` 继承自 `InfiniTrainTest`,预先创建了 `input_``weight_` 张量并填充了序列值。

tests/dtype/test_dtype_dispatch_compile_fail.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ int main() {
2121
// static_assert inside DispatchByTypeMap, failing this translation unit
2222
// before MappedType_t<TypeMap, kFLOAT16> is ever instantiated.
2323
DispatchByTypeMap<LowPrecisionMissingTypeMap, DataType::kFLOAT16>(
24-
DataType::kFLOAT16,
25-
[]<typename T>() { (void)sizeof(T); },
26-
"compile-fail: unregistered low-precision dtype");
24+
DataType::kFLOAT16, []<typename T>() { (void)sizeof(T); }, "compile-fail: unregistered low-precision dtype");
2725
return 0;
2826
}

0 commit comments

Comments
 (0)