Skip to content

Commit 9e56286

Browse files
authored
Merge branch 'main' into roi-align-compile
2 parents 31a78a9 + c585a51 commit 9e56286

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2996
-416
lines changed

.github/scripts/cmake.sh

+10-31
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::'

.github/scripts/setup-env.sh

-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@ case $(uname) in
2222
;;
2323
esac
2424

25-
if [[ "${OS_TYPE}" == "macos" && $(uname -m) == x86_64 ]]; then
26-
echo '::group::Uninstall system JPEG libraries on macOS'
27-
# The x86 macOS runners, e.g. the GitHub Actions native "macos-12" runner, has some JPEG and PNG libraries
28-
# installed by default that interfere with our build. We uninstall them here and use the one from conda below.
29-
IMAGE_LIBS=$(brew list | grep -E "jpeg|png")
30-
for lib in $IMAGE_LIBS; do
31-
brew uninstall --ignore-dependencies --force "${lib}"
32-
done
33-
echo '::endgroup::'
34-
fi
35-
3625
echo '::group::Create build environment'
3726
# See https://github.com/pytorch/vision/issues/7296 for ffmpeg
3827
conda create \

.github/scripts/unittest.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ eval "$($(which conda) shell.bash hook)" && conda deactivate && conda activate c
99

1010
echo '::group::Install testing utilities'
1111
# TODO: remove the <8 constraint on pytest when https://github.com/pytorch/vision/issues/8238 is closed
12-
pip install --progress-bar=off "pytest<8" pytest-mock pytest-cov expecttest!=0.2.0
12+
pip install --progress-bar=off "pytest<8" pytest-mock pytest-cov expecttest!=0.2.0 requests
1313
echo '::endgroup::'
1414

1515
python test/smoke_test.py

.github/workflows/build-cmake.yml

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ jobs:
4040
strategy:
4141
matrix:
4242
include:
43-
- runner: macos-12
4443
- runner: macos-m1-stable
4544
fail-fast: false
4645
uses: pytorch/test-infra/.github/workflows/macos_job.yml@main

.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
6464
echo '::group::Lint C source'
6565
set +e
66-
./.github/scripts/run-clang-format.py -r torchvision/csrc --clang-format-executable ./clang-format
66+
./.github/scripts/run-clang-format.py -r torchvision/csrc --clang-format-executable ./clang-format --exclude "torchvision/csrc/io/image/cpu/giflib/*"
6767
6868
if [ $? -ne 0 ]; then
6969
git --no-pager diff

.github/workflows/tests.yml

+1-6
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,11 @@ jobs:
5353
- "3.10"
5454
- "3.11"
5555
- "3.12"
56-
runner: ["macos-12"]
57-
include:
58-
- python-version: "3.8"
59-
runner: macos-m1-stable
56+
runner: ["macos-m1-stable"]
6057
fail-fast: false
6158
uses: pytorch/test-infra/.github/workflows/macos_job.yml@main
6259
with:
6360
repository: pytorch/vision
64-
# We need an increased timeout here, since the macos-12 runner is the free one from GH
65-
# and needs roughly 2 hours to just run the test suite
6661
timeout: 240
6762
runner: ${{ matrix.runner }}
6863
test-infra-ref: main

CMakeLists.txt

+1-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ option(WITH_CUDA "Enable CUDA support" OFF)
77
option(WITH_MPS "Enable MPS support" OFF)
88
option(WITH_PNG "Enable features requiring LibPNG." ON)
99
option(WITH_JPEG "Enable features requiring LibJPEG." ON)
10-
option(USE_PYTHON "Link to Python when building" OFF)
1110

1211
if(WITH_CUDA)
1312
enable_language(CUDA)
@@ -33,11 +32,6 @@ if (WITH_JPEG)
3332
find_package(JPEG REQUIRED)
3433
endif()
3534

36-
if (USE_PYTHON)
37-
add_definitions(-DUSE_PYTHON)
38-
find_package(Python3 REQUIRED COMPONENTS Development)
39-
endif()
40-
4135
function(CUDA_CONVERT_FLAGS EXISTING_TARGET)
4236
get_property(old_flags TARGET ${EXISTING_TARGET} PROPERTY INTERFACE_COMPILE_OPTIONS)
4337
if(NOT "${old_flags}" STREQUAL "")
@@ -80,7 +74,7 @@ include(GNUInstallDirs)
8074
include(CMakePackageConfigHelpers)
8175

8276
set(TVCPP torchvision/csrc)
83-
list(APPEND ALLOW_LISTED ${TVCPP} ${TVCPP}/io/image ${TVCPP}/io/image/cpu ${TVCPP}/models ${TVCPP}/ops
77+
list(APPEND ALLOW_LISTED ${TVCPP} ${TVCPP}/io/image ${TVCPP}/io/image/cpu ${TVCPP}/io/image/cpu/giflib ${TVCPP}/models ${TVCPP}/ops
8478
${TVCPP}/ops/autograd ${TVCPP}/ops/cpu ${TVCPP}/io/image/cuda)
8579
if(WITH_CUDA)
8680
list(APPEND ALLOW_LISTED ${TVCPP}/ops/cuda ${TVCPP}/ops/autocast)
@@ -110,10 +104,6 @@ if (WITH_JPEG)
110104
target_link_libraries(${PROJECT_NAME} PRIVATE ${JPEG_LIBRARIES})
111105
endif()
112106

113-
if (USE_PYTHON)
114-
target_link_libraries(${PROJECT_NAME} PRIVATE Python3::Python)
115-
endif()
116-
117107
set_target_properties(${PROJECT_NAME} PROPERTIES
118108
EXPORT_NAME TorchVision
119109
INSTALL_RPATH ${TORCH_INSTALL_PREFIX}/lib)

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ We don't officially support building from source using `pip`, but _if_ you do, y
7474
#### Other development dependencies (some of these are needed to run tests):
7575

7676
```
77-
pip install expecttest flake8 typing mypy pytest pytest-mock scipy
77+
pip install expecttest flake8 typing mypy pytest pytest-mock scipy requests
7878
```
7979

8080
## Development Process

README.md

+8-34
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

cmake/TorchVisionConfig.cmake.in

-8
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,5 @@ if(@WITH_JPEG@)
4646
target_compile_definitions(${PN}::${PN} INTERFACE JPEG_FOUND)
4747
endif()
4848

49-
if (@USE_PYTHON@)
50-
if(NOT TARGET Python3::Python)
51-
find_package(Python3 COMPONENTS Development)
52-
endif()
53-
target_link_libraries(torch INTERFACE Python3::Python)
54-
target_compile_definitions(${PN}::${PN} INTERFACE USE_PYTHON)
55-
endif()
56-
5749
endif()
5850
endif()

docs/source/io.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Images
1919
encode_jpeg
2020
decode_jpeg
2121
write_jpeg
22+
decode_gif
2223
encode_png
2324
decode_png
2425
write_png

examples/cpp/CMakeLists.txt

+18
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

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
**DISCLAIMER**: the `libtorchvision` library includes the torchvision
85+
custom ops as well as most of the C++ torchvision APIs. Those APIs do not come
86+
with any backward-compatibility guarantees and may change from one version to
87+
the next. Only the Python APIs are stable and with backward-compatibility
88+
guarantees. So, if you need stability within a C++ environment, your best bet is
89+
to export the Python APIs via torchscript.
90+
91+
Now that libtorchvision is built and installed we can tell our project to use
92+
and link to it via the `-DUSE_TORCHVISION` flag. We also need to tell CMake
93+
where to find it, just like we did with LibTorch, e.g.:
94+
95+
```bash
96+
cmake .. -DTorch_DIR=$Torch_DIR -DTorchVision_DIR=path/to/libtorchvision -DUSE_TORCHVISION=ON
97+
cmake --build .
98+
```
99+
100+
Now the `run_model` executable should be able to run the
101+
`fasterrcnn_resnet50_fpn.pt` file.

examples/cpp/hello_world/CMakeLists.txt

-20
This file was deleted.

0 commit comments

Comments
 (0)