Skip to content

Commit 1644fff

Browse files
authored
Cleanup C++ model example and cmake CI job (#8411)
1 parent d23a6e1 commit 1644fff

File tree

15 files changed

+218
-250
lines changed

15 files changed

+218
-250
lines changed

.github/scripts/cmake.sh

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,10 @@ fi
4646
echo '::group::Prepare CMake builds'
4747
mkdir -p cpp_build
4848

49-
pushd test/tracing/frcnn
50-
python trace_model.py
49+
pushd examples/cpp
50+
python script_model.py
5151
mkdir -p build
52-
mv fasterrcnn_resnet50_fpn.pt build
53-
popd
54-
55-
pushd examples/cpp/hello_world
56-
python trace_model.py
57-
mkdir -p build
58-
mv resnet18.pt build
52+
mv resnet18.pt fasterrcnn_resnet50_fpn.pt build
5953
popd
6054

6155
# This was only needed for the tracing above
@@ -65,6 +59,7 @@ echo '::endgroup::'
6559
echo '::group::Build and install libtorchvision'
6660
pushd cpp_build
6761

62+
6863
# On macOS, CMake is looking for the library (*.dylib) and the header (*.h) separately. By default, it prefers to load
6964
# the header from other packages that install the library. This easily leads to a mismatch if the library installed
7065
# from conda doesn't have the exact same version. Thus, we need to explicitly set CMAKE_FIND_FRAMEWORK=NEVER to force
@@ -85,40 +80,24 @@ fi
8580
popd
8681
echo '::endgroup::'
8782

88-
echo '::group::Build and run project that uses Faster-RCNN'
89-
pushd test/tracing/frcnn/build
90-
91-
cmake .. -DTorch_DIR="${Torch_DIR}" -DWITH_CUDA="${WITH_CUDA}" \
92-
-DCMAKE_PREFIX_PATH="${CONDA_PREFIX}" \
93-
-DCMAKE_FIND_FRAMEWORK=NEVER
94-
if [[ $OS_TYPE == windows ]]; then
95-
"${PACKAGING_DIR}/windows/internal/vc_env_helper.bat" "${PACKAGING_DIR}/windows/internal/build_frcnn.bat" $JOBS
96-
cd Release
97-
cp ../fasterrcnn_resnet50_fpn.pt .
98-
else
99-
make -j$JOBS
100-
fi
101-
102-
./test_frcnn_tracing
103-
104-
popd
105-
echo '::endgroup::'
106-
10783
echo '::group::Build and run C++ example'
108-
pushd examples/cpp/hello_world/build
84+
pushd examples/cpp/build
10985

11086
cmake .. -DTorch_DIR="${Torch_DIR}" \
11187
-DCMAKE_PREFIX_PATH="${CONDA_PREFIX}" \
112-
-DCMAKE_FIND_FRAMEWORK=NEVER
88+
-DCMAKE_FIND_FRAMEWORK=NEVER \
89+
-DUSE_TORCHVISION=ON # Needed for faster-rcnn since it's using torchvision ops like NMS.
11390
if [[ $OS_TYPE == windows ]]; then
11491
"${PACKAGING_DIR}/windows/internal/vc_env_helper.bat" "${PACKAGING_DIR}/windows/internal/build_cpp_example.bat" $JOBS
11592
cd Release
11693
cp ../resnet18.pt .
94+
cp ../fasterrcnn_resnet50_fpn.pt .
11795
else
11896
make -j$JOBS
11997
fi
12098

121-
./hello-world
99+
./run_model resnet18.pt
100+
./run_model fasterrcnn_resnet50_fpn.pt
122101

123102
popd
124103
echo '::endgroup::'

README.md

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,14 @@ python setup.py install
7474

7575
# Using the models on C++
7676

77-
TorchVision provides an example project for how to use the models on C++ using JIT Script.
78-
79-
Installation From source:
80-
81-
```
82-
mkdir build
83-
cd build
84-
# Add -DWITH_CUDA=on support for the CUDA if needed
85-
cmake ..
86-
make
87-
make install
88-
```
89-
90-
Once installed, the library can be accessed in cmake (after properly configuring `CMAKE_PREFIX_PATH`) via the
91-
`TorchVision::TorchVision` target:
92-
93-
```
94-
find_package(TorchVision REQUIRED)
95-
target_link_libraries(my-target PUBLIC TorchVision::TorchVision)
96-
```
97-
98-
The `TorchVision` package will also automatically look for the `Torch` package and add it as a dependency to
99-
`my-target`, so make sure that it is also available to cmake via the `CMAKE_PREFIX_PATH`.
100-
101-
For an example setup, take a look at `examples/cpp/hello_world`.
102-
103-
Python linking is disabled by default when compiling TorchVision with CMake, this allows you to run models without any
104-
Python dependency. In some special cases where TorchVision's operators are used from Python code, you may need to link
105-
to Python. This can be done by passing `-DUSE_PYTHON=on` to CMake.
106-
107-
### TorchVision Operators
108-
109-
In order to get the torchvision operators registered with torch (eg. for the JIT), all you need to do is to ensure that
110-
you `#include <torchvision/vision.h>` in your project.
77+
Refer to [example/cpp](https://github.com/pytorch/vision/tree/main/examples/cpp).
78+
79+
**DISCLAIMER**: the `libtorchvision` library includes the torchvision
80+
custom ops as well as most of the C++ torchvision APIs. Those APIs do not come
81+
with any backward-compatibility guarantees and may change from one version to
82+
the next. Only the Python APIs are stable and with backward-compatibility
83+
guarantees. So, if you need stability within a C++ environment, your best bet is
84+
to export the Python APIs via torchscript.
11185

11286
## Documentation
11387

examples/cpp/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(run_model)
3+
4+
option(USE_TORCHVISION "Whether to link to torchvision" OFF)
5+
6+
find_package(Torch REQUIRED)
7+
if(USE_TORCHVISION)
8+
find_package(TorchVision REQUIRED)
9+
endif()
10+
11+
add_executable(run_model run_model.cpp)
12+
13+
target_link_libraries(run_model "${TORCH_LIBRARIES}")
14+
if(USE_TORCHVISION)
15+
target_link_libraries(run_model TorchVision::TorchVision)
16+
endif()
17+
18+
set_property(TARGET run_model PROPERTY CXX_STANDARD 17)

examples/cpp/README.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
Using torchvision models in C++
2+
===============================
3+
4+
This is a minimal example of getting TorchVision models to work in C++ with
5+
Torchscript. The model is first scripted in Python and exported to a file, and
6+
then loaded in C++. For a similar tutorial, see [this
7+
tutorial](https://pytorch.org/tutorials/advanced/cpp_export.html).
8+
9+
In order to successfully compile this example, make sure you have ``LibTorch``
10+
installed. You can either:
11+
12+
- Install PyTorch normally
13+
- Or download the LibTorch C++ distribution.
14+
15+
In both cases refer [here](https://pytorch.org/get-started/locally/) the
16+
corresponding install or download instructions.
17+
18+
Some torchvision models only depend on PyTorch operators, and can be used in C++
19+
without depending on the torchvision lib. Other models rely on torchvision's C++
20+
operators like NMS, RoiAlign (typically the detection models) and those need to
21+
be linked against the torchvision lib.
22+
23+
We'll first see the simpler case of running a model without the torchvision lib
24+
dependency.
25+
26+
Running a model that doesn't need torchvision lib
27+
-------------------------------------------------
28+
29+
Create a ``build`` directory inside the current one.
30+
31+
```bash
32+
mkdir build
33+
cd build
34+
```
35+
36+
Then run `python ../trace_model.py` which should create a `resnet18.pt` file in
37+
the build directory. This is the scripted model that will be used in the C++
38+
code.
39+
40+
We can now start building with CMake. We have to tell CMake where it can find
41+
the necessary PyTorch resources. If you installed PyTorch normally, you can do:
42+
43+
```bash
44+
TORCH_PATH=$(python -c "import pathlib, torch; print(pathlib.Path(torch.__path__[0]))")
45+
Torch_DIR="${TORCH_PATH}/share/cmake/Torch" # there should be .cmake files in there
46+
47+
cmake .. -DTorch_DIR=$Torch_DIR
48+
```
49+
50+
If instead you downloaded the LibTorch somewhere, you can do:
51+
52+
```bash
53+
cmake .. -DCMAKE_PREFIX_PATH=/path/to/libtorch
54+
```
55+
56+
Then `cmake --build .` and you should now be able to run
57+
58+
```bash
59+
./run_model resnet18.pt
60+
```
61+
62+
If you try to run the model with a model that depends on the torchvision lib, like
63+
`./run_model fasterrcnn_resnet50_fpn.pt`, you should get a runtime error. This is
64+
because the executable wasn't linked against the torchvision lib.
65+
66+
67+
Running a model that needs torchvision lib
68+
------------------------------------------
69+
70+
First, we need to build the torchvision lib. To build the torchvision lib go to
71+
the root of the torchvision project and run:
72+
73+
```bash
74+
mkdir build
75+
cd build
76+
cmake .. -DCMAKE_PREFIX_PATH=/path/to/libtorch # or -DTorch_DIR= if you installed PyTorch normally, see above
77+
cmake --build .
78+
cmake --install .
79+
```
80+
81+
You may want to pass `-DCMAKE_INSTALL_PREFIX=/path/to/libtorchvision` for
82+
cmake to copy/install the files to a specific location (e.g. `$CONDA_PREFIX`).
83+
84+
On Windows, you may also need to pass `-DUSE_PYTHON`. Refer to the corresponding
85+
`CMakeLists.txt` for additional options.
86+
87+
**DISCLAIMER**: the `libtorchvision` library includes the torchvision
88+
custom ops as well as most of the C++ torchvision APIs. Those APIs do not come
89+
with any backward-compatibility guarantees and may change from one version to
90+
the next. Only the Python APIs are stable and with backward-compatibility
91+
guarantees. So, if you need stability within a C++ environment, your best bet is
92+
to export the Python APIs via torchscript.
93+
94+
Now that libtorchvision is built and installed we can tell our project to use
95+
and link to it via the `-DUSE_TORCHVISION` flag. We also need to tell CMake
96+
where to find it, just like we did with LibTorch, e.g.:
97+
98+
```bash
99+
cmake .. -DTorch_DIR=$Torch_DIR -DTorchVision_DIR=path/to/libtorchvision -DUSE_TORCHVISION=ON
100+
cmake --build .
101+
```
102+
103+
Now the `run_model` executable should be able to run the
104+
`fasterrcnn_resnet50_fpn.pt` file.

examples/cpp/hello_world/CMakeLists.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/cpp/hello_world/README.rst

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/cpp/hello_world/main.cpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

examples/cpp/hello_world/trace_model.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)