Skip to content

Commit

Permalink
docker, ci, new build
Browse files Browse the repository at this point in the history
  • Loading branch information
xmba15 committed Jul 28, 2022
1 parent 4361284 commit deae84b
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 125 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Build

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

jobs:
test-production:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v3

- name: Setup python
uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Install development dependencies
run: |
pip install pre-commit==2.20.0
- name: Apply pre-commit
run: |
pre-commit run --all-files
- name: Setup environment
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends software-properties-common build-essential
bash ./scripts/install_latest_cmake.bash
bash ./scripts/install_onnx_runtime.bash
bash ./scripts/install_apps_dependencies.bash
- name: Test building
run: |
make apps -j`nproc`
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ set(onnxruntime_INCLUDE_DIRS

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

find_package(OpenMP QUIET)
find_package(CUDA QUIET)

if(CUDA_FOUND AND USE_GPU)
add_definitions(-DENABLE_GPU=1)
else()
add_definitions(-DENABLE_GPU=0)
endif()


add_compile_options(
"$<$<CONFIG:DEBUG>:-DDEBUG>"
"$<$<CONFIG:Debug>:-DENABLE_DEBUG=1>"
"$<$<CONFIG:Release>:-DENABLE_DEBUG=0>"
)

add_subdirectory(src)
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
BUILD_EXAMPLES=OFF
BUILD_TYPE=Release
CMAKE_ARGS:=$(CMAKE_ARGS)
USE_GPU=OFF

default:
@mkdir -p build
@cd build && cmake .. -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) \
-DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
-DUSE_GPU=$(USE_GPU) \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
$(CMAKE_ARGS)
@cd build && make

gpu_default:
@make default USE_GPU=ON

debug:
@make default BUILD_TYPE=Debug

apps:
@make default BUILD_EXAMPLES=ON

gpu_apps:
@make apps USE_GPU=ON

debug_apps:
@make debug BUILD_EXAMPLES=ON

Expand Down
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![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)

# small c++ library to quickly use [onnxruntime](https://github.com/microsoft/onnxruntime) to deploy deep learning models

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.
Expand All @@ -14,20 +16,61 @@ Hope that they both are helpful for your work.
- build onnxruntime from source with the following script

```bash
sudo bash ./scripts/install_onnx_runtime.sh
# onnxruntime needs newer cmake version to build
bash ./scripts/install_latest_cmake.bash


bash ./scripts/install_onnx_runtime.bash

# dependencies to build apps
bash ./scripts/install_apps_dependencies.bash
```

## How to build

---

- CPU

```bash
make default

# build examples
make apps
```

- GPU with CUDA

```bash
make gpu_default

make gpu_apps
```

### :whale: How to Run with Docker

- CPU

```bash
# build
docker build -f ./dockerfiles/ubuntu2004_gpu.dockerfile -t onnx_runtime .

# run
docker run -it --rm -v `pwd`:/workspace onnx_runtime
```

- GPU with CUDA

```bash
# build
# change the cuda version to match your local cuda version before build the docker

docker build -f ./dockerfiles/ubuntu2004_gpu.dockerfile -t onnx_runtime_gpu .

# run
docker run -it --rm --gpus all -v `pwd`:/workspace onnx_runtime_gpu
```

## How to test apps

---
Expand Down
27 changes: 27 additions & 0 deletions dockerfiles/ubuntu2004.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /build
COPY ./scripts .

RUN apt-get update && \
apt-get install -y --no-install-recommends \
sudo \
gnupg2 \
lsb-release \
build-essential \
software-properties-common \
cmake \
git \
tmux && \
bash install_latest_cmake.bash && \
bash install_onnx_runtime.bash && \
bash install_apps_dependencies.bash && \
rm -rf /build && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

WORKDIR /workspace

ENTRYPOINT ["/bin/bash"]
26 changes: 26 additions & 0 deletions dockerfiles/ubuntu2004_gpu.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM nvidia/cuda:11.4.0-cudnn8-devel-ubuntu20.04

ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /build

RUN apt-get update && \
apt-get install -y --no-install-recommends \
sudo \
gnupg2 \
lsb-release \
build-essential \
software-properties-common \
cmake \
git \
tmux && \
bash install_latest_cmake.bash && \
bash install_onnx_runtime.bash && \
bash install_apps_dependencies.bash && \
rm -rf /build && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

WORKDIR /workspace

ENTRYPOINT ["/bin/bash"]
5 changes: 1 addition & 4 deletions include/ort_utility/Utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
#include <utility>
#include <vector>

#ifdef DEBUG
#define ENABLE_DEBUG 1
#if ENABLE_DEBUG
#include <iostream>
#else
#define ENABLE_DEBUG 0
#endif

namespace
Expand Down
18 changes: 18 additions & 0 deletions scripts/get_cuda_environment_variables.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

function command_exists
{
type "$1" &> /dev/null;
}

if ! command_exists nvcc; then
echo "Cuda package not found"
else
if [ ! "$(dpkg -S cudnn)" ];then
echo "cudnn not found"
else
export CUDA_HOME=`whereis cuda | awk '{print $2}'`
export CUDA_VERSION=`nvcc --version | grep release | awk '{print $6}' | cut -c 2-4`
export CUDNN_HOME=$(dirname `whereis cudnn.h | awk '{print $2}'`)
fi
fi
5 changes: 5 additions & 0 deletions scripts/install_apps_dependencies.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

sudo -l

sudo apt-get install -y --no-install-recommends libopencv-dev
30 changes: 30 additions & 0 deletions scripts/install_latest_cmake.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

readonly CMAKE_VERSION_TO_INSTALL="3.18.0"

function command_exists
{
type "$1" &> /dev/null;
}

function version_greater_equal
{
printf '%s\n%s\n' "$2" "$1" | sort -V -C
}

if command_exists cmake; then
readonly CMAKE_VERSION=`cmake --version | head -n1 | cut -d" " -f3`
if version_greater_equal "${CMAKE_VERSION}" "${CMAKE_VERSION_TO_INSTALL}"; then
exit 0
fi
fi

echo "Need cmake ${CMAKE_VERSION_TO_INSTALL} or above. Install now..."

sudo -l

sudo apt-get update
sudo apt-get -y purge cmake
sudo apt-get -y --no-install-recommends install python3-pip
python3 -m pip install cmake==$CMAKE_VERSION_TO_INSTALL
exec bash
16 changes: 0 additions & 16 deletions scripts/install_latest_cmake.sh

This file was deleted.

29 changes: 29 additions & 0 deletions scripts/install_onnx_runtime.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# reference: https://github.com/microsoft/onnxruntime#installation

readonly CURRENT_DIR=$(dirname $(realpath $0))

sudo -l

sudo apt-get update
sudo apt install -y --no-install-recommends zlib1g-dev

readonly ONNXRUNTIME_VERSION="v1.10.0"
git clone --recursive -b ${ONNXRUNTIME_VERSION} https://github.com/Microsoft/onnxruntime
cd onnxruntime

INSTALL_PREFIX="/usr/local"
BUILDTYPE=Release
BUILDARGS="--config ${BUILDTYPE}"
BUILDARGS="${BUILDARGS} --build_shared_lib --skip_tests"
BUILDARGS="${BUILDARGS} --parallel"
BUILDARGS="${BUILDARGS} --cmake_extra_defines CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX}"

source $CURRENT_DIR/get_cuda_environment_variables.bash
if [ ! -z "$CUDA_HOME" -a ! -z "$CUDA_VERSION" -a ! -z "$CUDNN_HOME" ]; then
BUILDARGS="${BUILDARGS} --use_cuda --cuda_version=${CUDA_VERSION} --cuda_home=${CUDA_HOME} --cudnn_home=${CUDNN_HOME}"
fi
./build.sh ${BUILDARGS}
cd ./build/Linux/${BUILDTYPE}
sudo make install
Loading

0 comments on commit deae84b

Please sign in to comment.