Skip to content

Commit 825181e

Browse files
goldsboroughfacebook-github-bot
authored andcommitted
Rewrite C++ API tests in gtest (pytorch#11953)
Summary: This PR is a large codemod to rewrite all C++ API tests with GoogleTest (gtest) instead of Catch. You can largely trust me to have correctly code-modded the tests, so it's not required to review every of the 2000+ changed lines. However, additional things I changed were: 1. Moved the cmake parts for these tests into their own `CMakeLists.txt` under `test/cpp/api` and calling `add_subdirectory` from `torch/CMakeLists.txt` 2. Fixing DataParallel tests which weren't being compiled because `USE_CUDA` wasn't correctly being set at all. 3. Updated README ezyang ebetica Pull Request resolved: pytorch#11953 Differential Revision: D9998883 Pulled By: goldsborough fbshipit-source-id: affe3f320b0ca63e7e0019926a59076bb943db80
1 parent d0db23e commit 825181e

27 files changed

+2132
-2160
lines changed

test/cpp/api/CMakeLists.txt

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
set(TORCH_API_TEST_DIR "${TORCH_ROOT}/test/cpp/api")
2+
set(TORCH_API_TEST_SOURCES
3+
${TORCH_API_TEST_DIR}/any.cpp
4+
${TORCH_API_TEST_DIR}/cursor.cpp
5+
${TORCH_API_TEST_DIR}/expanding-array.cpp
6+
${TORCH_API_TEST_DIR}/integration.cpp
7+
${TORCH_API_TEST_DIR}/jit.cpp
8+
${TORCH_API_TEST_DIR}/main.cpp
9+
${TORCH_API_TEST_DIR}/memory.cpp
10+
${TORCH_API_TEST_DIR}/misc.cpp
11+
${TORCH_API_TEST_DIR}/module.cpp
12+
${TORCH_API_TEST_DIR}/modules.cpp
13+
${TORCH_API_TEST_DIR}/optim.cpp
14+
${TORCH_API_TEST_DIR}/ordered-dict.cpp
15+
${TORCH_API_TEST_DIR}/rnn.cpp
16+
${TORCH_API_TEST_DIR}/sequential.cpp
17+
${TORCH_API_TEST_DIR}/serialize.cpp
18+
${TORCH_API_TEST_DIR}/static.cpp
19+
${TORCH_API_TEST_DIR}/tensor_cuda.cpp
20+
${TORCH_API_TEST_DIR}/tensor_options_cuda.cpp
21+
${TORCH_API_TEST_DIR}/tensor_options.cpp
22+
${TORCH_API_TEST_DIR}/tensor.cpp
23+
)
24+
25+
if (USE_CUDA)
26+
list(APPEND TORCH_API_TEST_SOURCES ${TORCH_API_TEST_DIR}/parallel.cpp)
27+
endif()
28+
29+
add_executable(test_api ${TORCH_API_TEST_SOURCES})
30+
target_include_directories(test_api PRIVATE ${ATen_CPU_INCLUDE})
31+
target_link_libraries(test_api PRIVATE torch gtest)
32+
33+
if (USE_CUDA)
34+
target_link_libraries(test_api PRIVATE
35+
${CUDA_LIBRARIES}
36+
${CUDA_NVRTC_LIB}
37+
${CUDA_CUDA_LIB}
38+
${TORCH_CUDA_LIBRARIES})
39+
target_compile_definitions(test_api PRIVATE "USE_CUDA")
40+
endif()
41+
42+
if (NOT MSVC)
43+
if (APPLE)
44+
target_compile_options(test_api PRIVATE
45+
# Clang has an unfixed bug leading to spurious missing braces
46+
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
47+
-Wno-missing-braces)
48+
else()
49+
target_compile_options(test_api PRIVATE
50+
# Considered to be flaky. See the discussion at
51+
# https://github.com/pytorch/pytorch/pull/9608
52+
-Wno-maybe-uninitialized
53+
# gcc gives nonsensical warnings about variadic.h
54+
-Wno-unused-but-set-parameter)
55+
endif()
56+
endif()

test/cpp/api/README.md

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
# C++ API Tests
1+
# C++ Frontend Tests
22

3-
In this folder live the tests for PyTorch's C++ API (formerly known as
4-
autogradpp). They use the [Catch2](https://github.com/catchorg/Catch2) test
5-
framework.
3+
In this folder live the tests for PyTorch's C++ Frontend. They use the
4+
[GoogleTest](https://github.com/google/googletest) test framework.
65

76
## CUDA Tests
87

9-
The way we handle CUDA tests is by separating them into a separate `TEST_CASE`
10-
(e.g. we have `optim` and `optim_cuda` test cases in `optim.cpp`), and giving
11-
them the `[cuda]` tag. Then, inside `main.cpp` we detect at runtime whether
12-
CUDA is available. If not, we disable these CUDA tests by appending `~[cuda]`
13-
to the test specifications. The `~` disables the tag.
8+
To make a test runnable only on platforms with CUDA, you should suffix your
9+
test with `_CUDA`, e.g.
1410

15-
One annoying aspect is that Catch only allows filtering on test cases and not
16-
sections. Ideally, one could have a section like `LSTM` inside the `RNN` test
17-
case, and give this section a `[cuda]` tag to only run it when CUDA is
18-
available. Instead, we have to create a whole separate `RNN_cuda` test case and
19-
put all these CUDA sections in there.
11+
```cpp
12+
TEST(MyTestSuite, MyTestCase_CUDA) { }
13+
```
14+
15+
To make it runnable only on platforms with at least two CUDA machines, suffix
16+
it with `_MultiCUDA` instead of `_CUDA`, e.g.
17+
18+
```cpp
19+
TEST(MyTestSuite, MyTestCase_MultiCUDA) { }
20+
```
21+
22+
There is logic in `main.cpp` that detects the availability and number of CUDA
23+
devices and supplies the appropriate negative filters to GoogleTest.
2024

2125
## Integration Tests
2226

2327
Integration tests use the MNIST dataset. You must download it by running the
2428
following command from the PyTorch root folder:
2529

26-
```shell
30+
```sh
2731
$ python tools/download_mnist.py -d test/cpp/api/mnist
2832
```
33+
34+
The required paths will be referenced as `test/cpp/api/mnist/...` in the test
35+
code, so you *must* run the integration tests from the PyTorch root folder.

0 commit comments

Comments
 (0)