11# 测试体系设计
22
3+ ** 核心思路:** 测试屏蔽平台差异,底层实例化不同平台,特化需单独处理的测试。
4+
35## 1. 架构
46
57```
@@ -33,8 +35,16 @@ tests/
3335| ` InfiniTrainTest ` | 通用参数化测试 | ` GetDevice() ` , ` createTensor(shape, dtype, requires_grad) ` |
3436| ` AutogradTestBase ` | Autograd 测试 | ` createTensor(shape, value) ` 自动 ` requires_grad=true ` + 顺序填充 |
3537
38+ ** 为什么需要 AutogradTestBase?**
39+
40+ - 所有 autograd 测试都需要 ` requires_grad=true `
41+ - 所有 autograd 测试都需要填充数据
42+ - 前向/反向传播测试必须有输入数据才能验证结果。` AutogradTestBase ` 把 ` FillSequentialTensor ` 内置了,避免每个测试都手动调用
43+
3644### 跳过特定平台
3745
46+ 这些宏函数涉及到了具体平台,用来针对性检验或跳过某些测试样例。
47+
3848在个别测试内部按需跳过:
3949
4050``` cpp
@@ -71,7 +81,7 @@ ctest --output-on-failure
7181# 只运行 CPU 测试
7282ctest -L cpu --output-on-failure
7383
74- # 只运行 CUDA 测试(分布式测试也在此标签下,不满足条件时自动 skip)
84+ # 只运行 CUDA 测试
7585ctest -L cuda --output-on-failure
7686
7787# 运行单个测试二进制(看完整 GTest 输出)
@@ -119,7 +129,7 @@ TEST_P(FooBasicTest, CUDAOnlyFeature) {
119129INFINI_TRAIN_REGISTER_TEST(FooBasicTest);
120130```
121131
122- 基类选择:
132+ ** 基类选择(或创建):**
123133
124134| 场景 | 基类 |
125135|------|------|
@@ -149,44 +159,7 @@ add_subdirectory(foo)
149159
150160** 生成的 CTest target:** ` test_foo_cpu ` 、` test_foo_cuda ` ,可通过 ` ctest -L cpu ` 等按标签筛选。
151161
152- ### 3.2 新增编译期负面测试
153-
154- 用于验证某段代码必须编译失败(如缺少 dtype 注册时 ` static_assert ` 应触发)。这类测试不走 GTest,而是用 CMake 的 ` try_compile ` 在 configure 阶段验证。
155-
156- 现有示例:` tests/dtype/test_dtype_dispatch_compile_fail.cc ` — 验证未注册的 dtype 在 dispatch 时编译失败。
157-
158- CMakeLists.txt 写法(参考 ` tests/dtype/CMakeLists.txt ` ):
159-
160- ``` cmake
161- set(COMPILE_FAIL_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_xxx_compile_fail.cc)
162-
163- try_compile(UNEXPECTEDLY_SUCCEEDED
164- ${CMAKE_BINARY_DIR}/CMakeFiles/try_compile_xxx
165- SOURCES ${COMPILE_FAIL_SOURCE}
166- CMAKE_FLAGS
167- "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
168- "-DCMAKE_CXX_STANDARD_REQUIRED=ON"
169- "-DCMAKE_CXX_EXTENSIONS=OFF"
170- "-DCMAKE_CXX_FLAGS=-I${PROJECT_SOURCE_DIR}"
171- OUTPUT_VARIABLE TRY_COMPILE_OUTPUT
172- )
173-
174- if(UNEXPECTEDLY_SUCCEEDED)
175- message(FATAL_ERROR
176- "compile-fail test unexpectedly succeeded.\n"
177- "Source: ${COMPILE_FAIL_SOURCE}\n"
178- "Output:\n${TRY_COMPILE_OUTPUT}")
179- endif()
180-
181- add_custom_target(test_xxx_compile_fail
182- COMMAND ${CMAKE_COMMAND} -E echo "compile-fail check passed."
183- VERBATIM
184- )
185- ```
186-
187- 编译期负面测试的源文件用手写 ` main() ` ,不依赖 GTest。它在 ` cmake ` 阶段就完成验证,不会生成可执行文件。
188-
189- ### 3.3 在已有目录新增测试文件
162+ ### 3.2 在已有目录新增测试文件
190163
191164所有使用 ` file(GLOB ...) ` 的目录(autograd、tensor、optimizer、hook、lora):
192165
@@ -196,23 +169,23 @@ add_custom_target(test_xxx_compile_fail
196169
197170无需修改任何 CMakeLists.txt。
198171
199- ### 3.4 工具函数速查
172+ ### 3.3 工具函数速查
200173
201174` test_utils.h ` 提供的常用工具:
202175
203176| 函数 / 宏 | 用途 |
204177| -----------| ------|
205178| ` GetDevice() ` | 返回当前参数化的 ` Device ` (基类方法) |
206179| ` createTensor(shape, dtype, requires_grad) ` | 在当前设备创建 tensor(` InfiniTrainTest ` 基类方法) |
207- | ` FillSequentialTensor(tensor, start) ` | 填充递增值,自动处理 CUDA tensor(先填 CPU 再 copy) |
180+ | ` FillSequentialTensor(tensor, start) ` | 填充递增值,自动处理 Device tensor(先填 CPU 再 copy) |
208181| ` SKIP_CPU() ` | 跳过 CPU 实例 |
209182| ` ONLY_CPU() ` | 只在 CPU 实例运行 |
210183| ` ONLY_CUDA() ` | 只在 CUDA 实例运行 |
211184| ` REQUIRE_MIN_DEVICES(n) ` | 加速器设备不足时 skip |
212185
213- ## 4. 扩展新设备平台(如沐曦 MUSA )
186+ ## 4. 扩展新设备平台(以沐曦 MACA 为例 )
214187
215- 当前测试体系围绕 CPU / CUDA 两种设备参数化。如果需要支持新平台(以沐曦 MUSA 为例),需要改动以下几处:
188+ 当前测试体系围绕 CPU / CUDA 两种设备参数化。如果需要支持新平台(以沐曦 MACA 为例),需要改动以下几处:
216189
217190### 4.1 框架层:注册新设备类型
218191
@@ -222,7 +195,7 @@ add_custom_target(test_xxx_compile_fail
222195enum class DeviceType : int8_t {
223196 kCPU = 0,
224197 kCUDA = 1,
225- kMUSA = 2, // 新增
198+ kMACA = 2, // 新增
226199};
227200```
228201
@@ -231,54 +204,51 @@ enum class DeviceType : int8_t {
2312041. 新增运行时检测函数和 `CudaDeviceTypes` 的对称版本:
232205
233206```cpp
234- #ifdef USE_MUSA
235- inline int GetMusaDeviceCount () { /* musaGetDeviceCount ... */ }
207+ #ifdef USE_MACA
208+ inline int GetMacaDeviceCount () { /* macaGetDeviceCount ... */ }
236209#else
237- inline int GetMusaDeviceCount () { return 0; }
210+ inline int GetMacaDeviceCount () { return 0; }
238211#endif
239- inline bool HasMusaRuntime () { return GetMusaDeviceCount () > 0; }
212+ inline bool HasMacaRuntime () { return GetMacaDeviceCount () > 0; }
240213
241- inline std::vector<Device::DeviceType> MusaDeviceTypes () {
242- if (HasMusaRuntime ()) {
243- return {Device::DeviceType::kMUSA };
214+ inline std::vector<Device::DeviceType> MacaDeviceTypes () {
215+ if (HasMacaRuntime ()) {
216+ return {Device::DeviceType::kMACA };
244217 }
245- LOG(INFO) << "No MUSA runtime found, skipping MUSA tests.";
218+ LOG(INFO) << "No MACA runtime found, skipping MACA tests.";
246219 return {};
247220}
248221```
249222
250- 2 . 新增 ` ONLY_MUSA ()` 宏:
223+ 2 . 新增 ` ONLY_MACA ()` 宏:
251224
252225``` cpp
253- #define ONLY_MUSA () \
254- do { if (GetParam() != infini_train::Device::DeviceType::kMUSA ) { GTEST_SKIP() << "MUSA -only test"; } } while (0)
226+ #define ONLY_MACA () \
227+ do { if (GetParam() != infini_train::Device::DeviceType::kMACA ) { GTEST_SKIP() << "MACA -only test"; } } while (0)
255228```
256229
257- ### 4.3 注册宏:新增 MUSA 实例
230+ ### 4.3 注册宏:新增 MACA 实例
258231
259232```cpp
260233#define INFINI_TRAIN_REGISTER_TEST(TestName) \
261234 INSTANTIATE_TEST_SUITE_P(CPU, TestName, \
262235 ::testing::Values(infini_train::Device::DeviceType::kCPU)); \
263236 INSTANTIATE_TEST_SUITE_P(CUDA, TestName, \
264237 ::testing::ValuesIn(infini_train::test::CudaDeviceTypes())); \
265- INSTANTIATE_TEST_SUITE_P(MUSA , TestName, \
266- ::testing::ValuesIn(infini_train::test::MusaDeviceTypes ()))
238+ INSTANTIATE_TEST_SUITE_P(MACA , TestName, \
239+ ::testing::ValuesIn(infini_train::test::MacaDeviceTypes ()))
267240```
268241
269242### 4.4 CMake 层:`test_macros.cmake`
270243
271- 将默认 label 列表从 `cpu cuda` 扩展为 `cpu cuda musa`(在 `infini_train_add_test_suite` 宏的第 122 行)。
272-
273- filter 模式会自动推导为 `MUSA /* `,无需手动添加分支。
244+ 将默认 label 列表从 `cpu cuda` 扩展为 `cpu cuda maca`
274245
275246### 4.5 检查清单
276247
277248| 步骤 | 文件 | 改动 |
278249|------|------|------|
279- | 1 | `device.h` | `DeviceType` 枚举新增 `kMUSA` |
280- | 2 | `test_utils.h` | 新增 `GetMusaDeviceCount()` / `HasMusaRuntime()` / `MusaDeviceTypes()` / `ONLY_MUSA()` |
281- | 3 | `test_utils.h` | `FillSequentialTensor` 新增 MUSA 路径 |
282- | 4 | `test_utils.h` | `INFINI_TRAIN_REGISTER_TEST` 新增 MUSA 实例 |
283- | 5 | `test_macros.cmake` | `infini_train_add_test_suite` 新增 `musa` label + filter |
284- | 6 | `CMakeLists.txt`(根) | 新增 `USE_MUSA` option + MUSA SDK 查找 + kernel 编译 |
250+ | 1 | `device.h` | `DeviceType` 枚举新增 `kMACA ` |
251+ | 2 | `test_utils.h` | 新增 `GetMacaDeviceCount()` / `HasMacaRuntime()` / `MacaDeviceTypes()` / `ONLY_MACA ()` |
252+ | 3 | `test_utils.h` | `INFINI_TRAIN_REGISTER_TEST ` 新增 MACA 实例 |
253+ | 4 | `test_macros.cmake` | 将默认 label 列表扩展为 `cpu cuda maca` |
254+ | 5 | `CMakeLists.txt`(根) | 新增 `USE_MACA ` option + MACA SDK 查找 + kernel 编译 |
0 commit comments