Skip to content

Commit

Permalink
PR: Run C++ torchvision example as part of cmake tests (#2678)
Browse files Browse the repository at this point in the history
* Run C++ torchvision example as part of cmake tests

* Enable C++14 support

* Minor error correction

* Add GPU forward on example

* Use arrow operator
  • Loading branch information
andfoy authored Sep 14, 2020
1 parent ca26022 commit c36dc43
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/cpp/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ add_executable(hello-world main.cpp)
# We now need to link the TorchVision library to our executable.
# We can do that by using the TorchVision::TorchVision target,
# which also adds all the necessary torch dependencies.
target_compile_features(hello-world PUBLIC cxx_range_for)
target_link_libraries(hello-world TorchVision::TorchVision)
set_property(TARGET hello-world PROPERTY CXX_STANDARD 14)
12 changes: 10 additions & 2 deletions examples/cpp/hello_world/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <iostream>

#include <torchvision/models/resnet.h>

int main()
Expand All @@ -11,5 +10,14 @@ int main()
auto in = torch::rand({1, 3, 10, 10});
auto out = model->forward(in);

std::cout << out;
std::cout << out.sizes();

if (torch::cuda::is_available()) {
// Move model and inputs to GPU
model->to(torch::kCUDA);
auto gpu_in = in.to(torch::kCUDA);
auto gpu_out = model->forward(gpu_in);

std::cout << gpu_out.sizes();
}
}
20 changes: 19 additions & 1 deletion packaging/build_cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ popd
python setup.py develop

# Trace, compile and run project that uses Faster-RCNN
cd test/tracing/frcnn
pushd test/tracing/frcnn
mkdir build

# Trace model
Expand All @@ -81,3 +81,21 @@ fi

# Run traced program
./test_frcnn_tracing

# Compile and run the CPP example
popd
cd examples/cpp/hello_world

mkdir build
cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch

if [[ "$OSTYPE" == "msys" ]]; then
"$script_dir/windows/internal/vc_env_helper.bat" "$script_dir/windows/internal/build_cpp_example.bat"
cd Release
else
make
fi

# Run CPP example
./hello-world
3 changes: 3 additions & 0 deletions packaging/windows/internal/build_cpp_example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo on
set CL=/I"C:\Program Files (x86)\torchvision\include"
msbuild "-p:Configuration=Release" hello-world.vcxproj

0 comments on commit c36dc43

Please sign in to comment.