Skip to content

Commit deae84b

Browse files
committed
docker, ci, new build
1 parent 4361284 commit deae84b

17 files changed

+239
-125
lines changed

.github/workflows/build.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: ["master"]
6+
pull_request:
7+
8+
jobs:
9+
test-production:
10+
runs-on: ubuntu-20.04
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Setup python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: "3.x"
19+
20+
- name: Install development dependencies
21+
run: |
22+
pip install pre-commit==2.20.0
23+
24+
- name: Apply pre-commit
25+
run: |
26+
pre-commit run --all-files
27+
28+
- name: Setup environment
29+
run: |
30+
sudo apt-get update
31+
sudo apt-get install -y --no-install-recommends software-properties-common build-essential
32+
bash ./scripts/install_latest_cmake.bash
33+
bash ./scripts/install_onnx_runtime.bash
34+
bash ./scripts/install_apps_dependencies.bash
35+
36+
- name: Test building
37+
run: |
38+
make apps -j`nproc`

CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ set(onnxruntime_INCLUDE_DIRS
1313

1414
find_library(onnxruntime_LIBS NAMES onnxruntime PATHS /usr/local/lib)
1515

16-
find_package(OpenMP QUIET)
16+
find_package(CUDA QUIET)
17+
18+
if(CUDA_FOUND AND USE_GPU)
19+
add_definitions(-DENABLE_GPU=1)
20+
else()
21+
add_definitions(-DENABLE_GPU=0)
22+
endif()
23+
1724

1825
add_compile_options(
19-
"$<$<CONFIG:DEBUG>:-DDEBUG>"
26+
"$<$<CONFIG:Debug>:-DENABLE_DEBUG=1>"
27+
"$<$<CONFIG:Release>:-DENABLE_DEBUG=0>"
2028
)
2129

2230
add_subdirectory(src)

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
BUILD_EXAMPLES=OFF
22
BUILD_TYPE=Release
33
CMAKE_ARGS:=$(CMAKE_ARGS)
4+
USE_GPU=OFF
45

56
default:
67
@mkdir -p build
78
@cd build && cmake .. -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) \
89
-DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
10+
-DUSE_GPU=$(USE_GPU) \
911
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
1012
$(CMAKE_ARGS)
1113
@cd build && make
1214

15+
gpu_default:
16+
@make default USE_GPU=ON
17+
1318
debug:
1419
@make default BUILD_TYPE=Debug
1520

1621
apps:
1722
@make default BUILD_EXAMPLES=ON
1823

24+
gpu_apps:
25+
@make apps USE_GPU=ON
26+
1927
debug_apps:
2028
@make debug BUILD_EXAMPLES=ON
2129

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Build](https://github.com/xmba15/onnx_runtime_cpp/actions/workflows/build.yml/badge.svg)](https://github.com/xmba15/onnx_runtime_cpp/actions/workflows/build.yml)
2+
13
# small c++ library to quickly use [onnxruntime](https://github.com/microsoft/onnxruntime) to deploy deep learning models
24

35
Thanks to [cardboardcode](https://github.com/cardboardcode), we have [the documentation](https://onnx-runtime-cpp.readthedocs.io/en/latest/index.html) for this small library.
@@ -14,20 +16,61 @@ Hope that they both are helpful for your work.
1416
- build onnxruntime from source with the following script
1517

1618
```bash
17-
sudo bash ./scripts/install_onnx_runtime.sh
19+
# onnxruntime needs newer cmake version to build
20+
bash ./scripts/install_latest_cmake.bash
21+
22+
23+
bash ./scripts/install_onnx_runtime.bash
24+
25+
# dependencies to build apps
26+
bash ./scripts/install_apps_dependencies.bash
1827
```
1928

2029
## How to build
2130

2231
---
2332

33+
- CPU
34+
2435
```bash
2536
make default
2637

2738
# build examples
2839
make apps
2940
```
3041

42+
- GPU with CUDA
43+
44+
```bash
45+
make gpu_default
46+
47+
make gpu_apps
48+
```
49+
50+
### :whale: How to Run with Docker
51+
52+
- CPU
53+
54+
```bash
55+
# build
56+
docker build -f ./dockerfiles/ubuntu2004_gpu.dockerfile -t onnx_runtime .
57+
58+
# run
59+
docker run -it --rm -v `pwd`:/workspace onnx_runtime
60+
```
61+
62+
- GPU with CUDA
63+
64+
```bash
65+
# build
66+
# change the cuda version to match your local cuda version before build the docker
67+
68+
docker build -f ./dockerfiles/ubuntu2004_gpu.dockerfile -t onnx_runtime_gpu .
69+
70+
# run
71+
docker run -it --rm --gpus all -v `pwd`:/workspace onnx_runtime_gpu
72+
```
73+
3174
## How to test apps
3275

3376
---

dockerfiles/ubuntu2004.dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM ubuntu:20.04
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
5+
WORKDIR /build
6+
COPY ./scripts .
7+
8+
RUN apt-get update && \
9+
apt-get install -y --no-install-recommends \
10+
sudo \
11+
gnupg2 \
12+
lsb-release \
13+
build-essential \
14+
software-properties-common \
15+
cmake \
16+
git \
17+
tmux && \
18+
bash install_latest_cmake.bash && \
19+
bash install_onnx_runtime.bash && \
20+
bash install_apps_dependencies.bash && \
21+
rm -rf /build && \
22+
apt-get clean && \
23+
rm -rf /var/lib/apt/lists/*
24+
25+
WORKDIR /workspace
26+
27+
ENTRYPOINT ["/bin/bash"]

dockerfiles/ubuntu2004_gpu.dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM nvidia/cuda:11.4.0-cudnn8-devel-ubuntu20.04
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
5+
WORKDIR /build
6+
7+
RUN apt-get update && \
8+
apt-get install -y --no-install-recommends \
9+
sudo \
10+
gnupg2 \
11+
lsb-release \
12+
build-essential \
13+
software-properties-common \
14+
cmake \
15+
git \
16+
tmux && \
17+
bash install_latest_cmake.bash && \
18+
bash install_onnx_runtime.bash && \
19+
bash install_apps_dependencies.bash && \
20+
rm -rf /build && \
21+
apt-get clean && \
22+
rm -rf /var/lib/apt/lists/*
23+
24+
WORKDIR /workspace
25+
26+
ENTRYPOINT ["/bin/bash"]

include/ort_utility/Utility.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
#include <utility>
1818
#include <vector>
1919

20-
#ifdef DEBUG
21-
#define ENABLE_DEBUG 1
20+
#if ENABLE_DEBUG
2221
#include <iostream>
23-
#else
24-
#define ENABLE_DEBUG 0
2522
#endif
2623

2724
namespace
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
function command_exists
4+
{
5+
type "$1" &> /dev/null;
6+
}
7+
8+
if ! command_exists nvcc; then
9+
echo "Cuda package not found"
10+
else
11+
if [ ! "$(dpkg -S cudnn)" ];then
12+
echo "cudnn not found"
13+
else
14+
export CUDA_HOME=`whereis cuda | awk '{print $2}'`
15+
export CUDA_VERSION=`nvcc --version | grep release | awk '{print $6}' | cut -c 2-4`
16+
export CUDNN_HOME=$(dirname `whereis cudnn.h | awk '{print $2}'`)
17+
fi
18+
fi
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
sudo -l
4+
5+
sudo apt-get install -y --no-install-recommends libopencv-dev

scripts/install_latest_cmake.bash

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
readonly CMAKE_VERSION_TO_INSTALL="3.18.0"
4+
5+
function command_exists
6+
{
7+
type "$1" &> /dev/null;
8+
}
9+
10+
function version_greater_equal
11+
{
12+
printf '%s\n%s\n' "$2" "$1" | sort -V -C
13+
}
14+
15+
if command_exists cmake; then
16+
readonly CMAKE_VERSION=`cmake --version | head -n1 | cut -d" " -f3`
17+
if version_greater_equal "${CMAKE_VERSION}" "${CMAKE_VERSION_TO_INSTALL}"; then
18+
exit 0
19+
fi
20+
fi
21+
22+
echo "Need cmake ${CMAKE_VERSION_TO_INSTALL} or above. Install now..."
23+
24+
sudo -l
25+
26+
sudo apt-get update
27+
sudo apt-get -y purge cmake
28+
sudo apt-get -y --no-install-recommends install python3-pip
29+
python3 -m pip install cmake==$CMAKE_VERSION_TO_INSTALL
30+
exec bash

scripts/install_latest_cmake.sh

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

scripts/install_onnx_runtime.bash

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
# reference: https://github.com/microsoft/onnxruntime#installation
4+
5+
readonly CURRENT_DIR=$(dirname $(realpath $0))
6+
7+
sudo -l
8+
9+
sudo apt-get update
10+
sudo apt install -y --no-install-recommends zlib1g-dev
11+
12+
readonly ONNXRUNTIME_VERSION="v1.10.0"
13+
git clone --recursive -b ${ONNXRUNTIME_VERSION} https://github.com/Microsoft/onnxruntime
14+
cd onnxruntime
15+
16+
INSTALL_PREFIX="/usr/local"
17+
BUILDTYPE=Release
18+
BUILDARGS="--config ${BUILDTYPE}"
19+
BUILDARGS="${BUILDARGS} --build_shared_lib --skip_tests"
20+
BUILDARGS="${BUILDARGS} --parallel"
21+
BUILDARGS="${BUILDARGS} --cmake_extra_defines CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}"
22+
23+
source $CURRENT_DIR/get_cuda_environment_variables.bash
24+
if [ ! -z "$CUDA_HOME" -a ! -z "$CUDA_VERSION" -a ! -z "$CUDNN_HOME" ]; then
25+
BUILDARGS="${BUILDARGS} --use_cuda --cuda_version=${CUDA_VERSION} --cuda_home=${CUDA_HOME} --cudnn_home=${CUDNN_HOME}"
26+
fi
27+
./build.sh ${BUILDARGS}
28+
cd ./build/Linux/${BUILDTYPE}
29+
sudo make install

0 commit comments

Comments
 (0)