【训练营】基于 CTest + gtest 的测试体系搭建与工程化集成#120
【训练营】基于 CTest + gtest 的测试体系搭建与工程化集成#120luoyueyuguang wants to merge 3 commits intoInfiniTensor:masterfrom
Conversation
|
1.请不要用 merge 操作,统一使用 rebase 同步最新修改; |
CMakeLists.txt
Outdated
| add_subdirectory(third_party/glog) | ||
| add_compile_definitions(GLOG_USE_GLOG_EXPORT=1) | ||
| include_directories(${glog_SOURCE_DIR}/src) | ||
| include_directories(${glog_BINARY_DIR}/glog) |
CMakeLists.txt
Outdated
| file(GLOB_RECURSE SRC ${PROJECT_SOURCE_DIR}/infini_train/src/*.cc) | ||
| list(FILTER SRC EXCLUDE REGEX ".*kernels/cpu/.*") | ||
| list(FILTER SRC EXCLUDE REGEX ".*runtime/cuda/.*") | ||
| list(FILTER SRC EXCLUDE REGEX ".*ccl/cuda/.*") |
There was a problem hiding this comment.
这里的逻辑应该是
if(NOT USE_CUDA)
list(FILTER SRC EXCLUDE REGEX ".*runtime/cuda/.*")
list(FILTER SRC EXCLUDE REGEX ".*ccl/cuda/.*")
endif()
否则会出现编译错误
| @@ -0,0 +1,161 @@ | |||
| # Autograd tests | |||
There was a problem hiding this comment.
这版的风险是新增测试的接入成本和漏配风险都比较高。新增一个普通 autograd CPU 测试,至少要手动写:
add_autograd_test(...)
add_test(...)
set_tests_properties(... LABELS ...)
建议简化。例如把功能整合到一个函数里,新增测试只调用一次;或者考虑使用gtest_discover_tests,不用再手工写 add_test() 和 set_tests_properties()
- Add infini_train_add_test CMake macro for simplified test registration - Integrate gtest_discover_tests for automatic test case discovery - Refactor all test directories to use unified macro (autograd, optimizer, hook, slow, lora) - Reduce test CMakeLists.txt code by 68% - Add LoRA tests (12 test cases) - Delete TEST_REPORT.md - Test labels: cpu/cuda/distributed/slow for flexible test execution - Add shared test_macros.cmake in tests/common/ BREAKING CHANGE: Test registration now uses macro instead of manual add_test() Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
|
修改了CMakeLists.txt,添加了infini_train_add_test宏,可使用以下方式注册tests |
tests/common/test_macros.cmake
Outdated
| ) | ||
|
|
||
| # 5. 链接项目库(whole-archive 方式解决静态库符号依赖) | ||
| target_link_libraries(${ARG_TEST_NAME} PRIVATE |
There was a problem hiding this comment.
测试宏只链接了 infini_train 和 infini_train_cpu_kernels,CUDA 测试可能有问题,建议复用框架的 link_infini_train_exe 方法
tests/autograd/CMakeLists.txt
Outdated
| # 重构版本:使用 infini_train_add_test 宏简化配置 | ||
| # | ||
| # 新增测试只需 1 行: | ||
| # infini_train_add_test(test_name SOURCES test_name.cc LABELS cpu) |
There was a problem hiding this comment.
简化很多了!当前这个宏指定了一个测试源码文件对应一个独立executable,这样做短期简单,但后面测试一多,target 数量、链接次数、配置重复都会一起膨胀。
建议先定义一个测试 binary,把 gtest、主库、include、link 规则这些公共配置一次性配好,再把若干测试源码挂进去。如果以后某些测试确实需要单独 target,也还可以单独建立。参考pytorch https://github.com/pytorch/pytorch/blob/main/test/cpp/jit/CMakeLists.txt
https://github.com/pytorch/pytorch/blob/main/test/cpp/api/CMakeLists.txt
function(infini_train_add_test_binary TARGET_NAME)
# 类似infini_train_add_test 逻辑
cmake_parse_arguments(ARG "" "" "LABELS" ${ARGN})
add_executable(${TARGET_NAME})
link_infini_train_exe(${TARGET_NAME})
target_link_libraries(${TARGET_NAME} PRIVATE GTest::gtest GTest::gtest_main)
target_include_directories(${TARGET_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../common
)
include(GoogleTest)
gtest_discover_tests(${TARGET_NAME}
PROPERTIES LABELS "${ARG_LABELS}"
)
endfunction()
function(infini_train_add_test_sources TARGET_NAME)
cmake_parse_arguments(ARG "" "" "SOURCES" ${ARGN})
if(NOT TARGET ${TARGET_NAME})
message(FATAL_ERROR "Unknown test target: ${TARGET_NAME}")
endif()
target_sources(${TARGET_NAME} PRIVATE ${ARG_SOURCES})
endfunction()使用方式参考:
set(AUTOGRAD_TEST_SOURCES
test_autograd_elementwise_forward.cc
test_autograd_elementwise_backward.cc
test_autograd_linear.cc
)
infini_train_add_test_binary(test_autograd LABELS "cpu;cuda")
infini_train_add_test_sources(test_autograd SOURCES ${AUTOGRAD_TEST_SOURCES})
No description provided.