1 拉取源码

使用以下命令拉取源码,这里以版本1.16.3为例

# clone指定分支 发布版1.16.3
git clone --depth=1 --branch v1.16.3 https://github.com.cnpmjs.org/microsoft/onnxruntime.git

# 拉取子项目
git submodule update --init --recursive

2 编译安装OnnxRuntime

2.1 解读编译流程

拉取完成之后,在参考底下有一个build.sh文件,

#!/bin/bash
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# Get directory this script is in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OS=$(uname -s)

if [ "$OS" = "Darwin" ]; then
    DIR_OS="MacOS"
else
    DIR_OS="Linux"
fi

if [[ "$*" == *"--ios"* ]]; then
    DIR_OS="iOS"
elif [[ "$*" == *"--android"* ]]; then
    DIR_OS="Android"
fi

python3 $DIR/tools/ci_build/build.py --build_dir $DIR/build/$DIR_OS "$@"

这个build.sh脚本干的事情只是判断操作系统类型以及获取当前目录,然后把编译参数"$@"原封不动地传给tools/ci_build/build.py这个脚本,我们再来看build.py里面的内容

# Main arguments
    parser.add_argument("--build_dir", required=True, help="Path to the build directory.")
    parser.add_argument(
        "--config",
        nargs="+",
        default=["Debug"],
        choices=["Debug", "MinSizeRel", "Release", "RelWithDebInfo"],
        help="Configuration(s) to build.",
    )
    parser.add_argument("--update", action="store_true", help="Update makefiles.")
    parser.add_argument("--build", action="store_true", help="Build.")
    parser.add_argument(
        "--clean", action="store_true", help="Run 'cmake --build --target clean' for the selected config/s."
    )
    parser.add_argument(
        "--parallel",
        nargs="?",
        const="0",
        default="1",
        type=int,
        help="Use parallel build. The optional value specifies the maximum number of parallel jobs. "
        "If the optional value is 0 or unspecified, it is interpreted as the number of CPUs.",
    )
    ......

这个脚本里面内置了一些命令行参数,我们在使用编译参数时可以重点查看每个参数的help信息就可以知道每个参数的作用。

解析完参数之后通过generate_build_tree这个函数构建cmake项目信息,然后通过build_targets函数构建项目。

2.2 编译

我们只需要执行build.sh这个脚本,并传入相关的参数就可以编译OnnxRuntime

cpu版本示例编译命令

./build.sh --skip_tests --config Release --build_shared_lib --parallel
./build.sh --skip_tests --config RelWithDebInfo --build_shared_lib --parallel
./build.sh --skip_tests --config MinSizeRel --build_shared_lib --parallel

cuda tensorrt版本示例编译命令

./build.sh --build_shared_lib --config Release --use_cuda --cudnn_home /usr/local/cuda/ --cuda_home /usr/local/cuda  --use_tensorrt --tensorrt_home /usr/lib/x86_64-linux-gnu/

编译完成并且成功之后会出现以下信息:

[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/providers/cpu/tensor/upsample_op_test.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/providers/cpu/tensor/where_op_test.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/providers/internal_testing/internal_testing_ep_static_kernels.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/providers/internal_testing/internal_testing_execution_provider.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/providers/internal_testing/internal_testing_partitioning_tests.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/providers/internal_testing/internal_testing_tests.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/quantization/quantization_test.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_test_all.dir/home/huangwang/third/onnxruntime/onnxruntime_build/onnxruntime/onnxruntime/test/unittest_main/test_main.cc.o
[100%] Linking CXX executable onnxruntime_test_all
[100%] Built target onnxruntime_test_all
2023-12-20 01:47:03,385 util.run [DEBUG] - Subprocess completed. Return code: 0
2023-12-20 01:47:03,386 build [INFO] - Build complete

2.3 安装

编译完成之后,进入./build/Linux/Release目录安装即可,执行以下命令

cd ./build/Linux/Release
make install

安装完成之后会出现以下信息:

Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/include/onnxruntime/cpu_provider_factory.h
-- Installing: /usr/local/lib/libonnxruntime_providers_shared.so
-- Installing: /usr/local/lib/libonnxruntime.so.1.16.3
-- Installing: /usr/local/lib/libonnxruntime.so
-- Installing: /usr/local/include/onnxruntime/onnxruntime_c_api.h
-- Installing: /usr/local/include/onnxruntime/onnxruntime_cxx_api.h
-- Installing: /usr/local/include/onnxruntime/onnxruntime_float16.h
-- Installing: /usr/local/include/onnxruntime/onnxruntime_cxx_inline.h
-- Installing: /usr/local/include/onnxruntime/onnxruntime_session_options_config_keys.h
-- Installing: /usr/local/include/onnxruntime/onnxruntime_run_options_config_keys.h
-- Installing: /usr/local/bin/onnx_test_runner
-- Installing: /usr/local/lib/pkgconfig/libonnxruntime.pc
-- Installing: /usr/local/lib/cmake/onnxruntime/onnxruntimeTargets.cmake
-- Installing: /usr/local/lib/cmake/onnxruntime/onnxruntimeTargets-release.cmake
-- Installing: /usr/local/lib/cmake/onnxruntime/onnxruntimeConfig.cmake
-- Installing: /usr/local/lib/cmake/onnxruntime/onnxruntimeConfigVersion.cmake

安装完成之后,include目录为

/usr/local/include

lib目录为

/usr/local/lib

参考