xref: /aosp_15_r20/external/pytorch/test/cpp/jit/README.md (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker# JIT C++ Tests
2*da0073e9SAndroid Build Coastguard Worker
3*da0073e9SAndroid Build Coastguard Worker## Adding a new test
4*da0073e9SAndroid Build Coastguard WorkerFirst, create a new test file. Test files should have be placed in this
5*da0073e9SAndroid Build Coastguard Workerdirectory, with a name that starts with `test_`, like `test_foo.cpp`.
6*da0073e9SAndroid Build Coastguard Worker
7*da0073e9SAndroid Build Coastguard WorkerIn general a single test suite
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard WorkerAdd your test file to the `JIT_TEST_SRCS` list in `test/cpp/jit/CMakeLists.txt`.
10*da0073e9SAndroid Build Coastguard Worker
11*da0073e9SAndroid Build Coastguard WorkerA test file may look like:
12*da0073e9SAndroid Build Coastguard Worker```cpp
13*da0073e9SAndroid Build Coastguard Worker#include <gtest/gtest.h>
14*da0073e9SAndroid Build Coastguard Worker
15*da0073e9SAndroid Build Coastguard Workerusing namespace ::torch::jit
16*da0073e9SAndroid Build Coastguard Worker
17*da0073e9SAndroid Build Coastguard WorkerTEST(FooTest, BarBaz) {
18*da0073e9SAndroid Build Coastguard Worker   // ...
19*da0073e9SAndroid Build Coastguard Worker}
20*da0073e9SAndroid Build Coastguard Worker
21*da0073e9SAndroid Build Coastguard Worker// Append '_CUDA' to the test case name will automatically filter it out if CUDA
22*da0073e9SAndroid Build Coastguard Worker// is not compiled.
23*da0073e9SAndroid Build Coastguard WorkerTEST(FooTest, NeedsAGpu_CUDA) {
24*da0073e9SAndroid Build Coastguard Worker   // ...
25*da0073e9SAndroid Build Coastguard Worker}
26*da0073e9SAndroid Build Coastguard Worker
27*da0073e9SAndroid Build Coastguard Worker// Similarly, if only one GPU is detected, tests with `_MultiCUDA` at the end
28*da0073e9SAndroid Build Coastguard Worker// will not be run.
29*da0073e9SAndroid Build Coastguard WorkerTEST(FooTest, NeedsMultipleGpus_MultiCUDA) {
30*da0073e9SAndroid Build Coastguard Worker   // ...
31*da0073e9SAndroid Build Coastguard Worker}
32*da0073e9SAndroid Build Coastguard Worker```
33*da0073e9SAndroid Build Coastguard Worker
34*da0073e9SAndroid Build Coastguard Worker## Building and running the tests
35*da0073e9SAndroid Build Coastguard WorkerThe following commands assume you are in PyTorch root.
36*da0073e9SAndroid Build Coastguard Worker
37*da0073e9SAndroid Build Coastguard Worker```bash
38*da0073e9SAndroid Build Coastguard Worker# ... Build PyTorch from source, e.g.
39*da0073e9SAndroid Build Coastguard Workerpython setup.py develop
40*da0073e9SAndroid Build Coastguard Worker# (re)build just the binary
41*da0073e9SAndroid Build Coastguard Workerninja -C build bin/test_jit
42*da0073e9SAndroid Build Coastguard Worker# run tests
43*da0073e9SAndroid Build Coastguard Workerbuild/bin/test_jit --gtest_filter='glob_style_filter*'
44*da0073e9SAndroid Build Coastguard Worker```
45