Skip to content

Commit

Permalink
Add cpp test of libtorch tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqwang committed Oct 10, 2020
1 parent 4495c13 commit 067b2f1
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (c) 2020, Zhiqiang Wang. All Rights Reserved.
# GH actions

name: Nightly

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
Test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.6, 3.7]
os: [ubuntu-latest]

steps:
- name: Clone repository
uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: 'x64'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install PyTorch Nightly
run: |
pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
- name: Build TorchVision Cpp Nightly
run: |
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
cd ..
git clone https://github.com/pytorch/vision.git vision
cd vision
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch
make -j4
sudo make install
- name: Test libtorch tracing
run: |
python -m test.tracing.trace_model
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TORCH_PATH/lib/
cd test/tracing
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch
make
mv ../yolov5s.torchscript.pt ./
echo ">> Test libtorch tracing"
./test_tracing
57 changes: 57 additions & 0 deletions .github/workflows/stable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2020, Zhiqiang Wang. All Rights Reserved.
# GH actions

name: Stable

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
Test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.6, 3.7]
os: [ubuntu-latest]

steps:
- name: Clone repository
uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
architecture: 'x64'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install PyTorch 1.6
run: |
pip install torch==1.6.0+cpu torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
- name: Build TorchVision Cpp
run: |
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
cd ..
git clone https://github.com/pytorch/vision.git vision
cd vision
git checkout release/0.7
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch
make -j4
sudo make install
- name: Test libtorch tracing
run: |
python -m test.tracing.trace_model
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TORCH_PATH/lib/
cd test/tracing
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch
make
mv ../yolov5s.torchscript.pt ./
echo ">> Test libtorch tracing"
./test_tracing
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Cython
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.2
pillow
PyYAML>=5.3
scipy>=1.4.1
tensorboard>=2.2
tqdm>=4.41.0
Empty file added test/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions test/tracing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(test_tracing)

find_package(Torch REQUIRED)
find_package(TorchVision REQUIRED)

# This due to some headers importing Python.h
find_package(Python3 COMPONENTS Development)

add_executable(${CMAKE_PROJECT_NAME} test_tracing.cpp)
target_compile_features(test_tracing PUBLIC cxx_range_for)

target_link_libraries(
${CMAKE_PROJECT_NAME}
${TORCH_LIBRARIES}
TorchVision::TorchVision
Python3::Python
)

# set C++14 to compile
set_property(TARGET test_tracing PROPERTY CXX_STANDARD 14)
56 changes: 56 additions & 0 deletions test/tracing/test_tracing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <ATen/ATen.h>
#include <torch/script.h>
#include <torch/torch.h>
#include <torchvision/cpu/vision_cpu.h>
#include <torchvision/ROIPool.h>
#include <torchvision/nms.h>


int main() {
torch::DeviceType device_type;
device_type = torch::kCPU;

torch::jit::script::Module module;
try {
std::cout << "Loading model" << std::endl;
// Deserialize the ScriptModule from a file using torch::jit::load().
module = torch::jit::load("yolov5s.torchscript.pt");
std::cout << "Model loaded" << std::endl;
} catch (const torch::Error& e) {
std::cout << "error loading the model" << std::endl;
return -1;
} catch (const std::exception& e) {
std::cout << "Other error: " << e.what() << std::endl;
return -1;
}

// TorchScript models require a List[IValue] as input
std::vector<torch::jit::IValue> inputs;

// Demonet accepts a List[Tensor] as main input
torch::Tensor images = torch::rand({1, 3, 416, 352});

inputs.push_back(images);
auto output = module.forward(inputs);

std::cout << "ok" << std::endl;
std::cout << "output" << output << std::endl;

if (torch::cuda::is_available()) {
// Move traced model to GPU
module.to(torch::kCUDA);

// Add GPU inputs
inputs.clear();

torch::TensorOptions options = torch::TensorOptions{torch::kCUDA};
images = images.to(torch::kCUDA);

inputs.push_back(images);
auto output = module.forward(inputs);

std::cout << "ok" << std::endl;
std::cout << "output" << output << std::endl;
}
return 0;
}
14 changes: 14 additions & 0 deletions test/tracing/trace_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import torch

from hubconf import yolov5


if __name__ == "__main__":

model = yolov5(
cfg_path='./models/yolov5s.yaml',
)
model.eval()

traced_model = torch.jit.script(model)
traced_model.save("./test/tracing/yolov5s.torchscript.pt")

0 comments on commit 067b2f1

Please sign in to comment.