Skip to content

StaticLibraries

Ilya Lavrenov edited this page Mar 30, 2022 · 30 revisions

Building OpenVINO static libraries

Contents

Introduction

Building static OpenVINO Runtime libraries allows to additionally reduce the size of a binary when it is used together with conditional compilation. It is possible because not all interface symbols of OpenVINO Runtime libraries are exported to end users during a static build and can be removed by linker. See Static OpenVINO libraries + Conditional compilation for particular models

Configure OpenVINO Runtime in the CMake stage

The default architecture of OpenVINO Runtime assumes that the following components are subject to dynamic loading during execution:

  • (Device) Inference backends (CPU, GPU, MULTI, HETERO, etc.)
  • (Model) Frontends (IR, ONNX, PDPD, etc.)
  • Preprocessing library (to perform preprocessing, e.g. resize and color space conversions)
  • IR v7 reader (used in legacy tests only, if you are not to going to run OpenVINO tests, set -DENABLE_TESTS=OFF which disables IR v7 reader)

With the static OpenVINO Runtime, all these modules should be linked into a final user application and the list of modules/configuration must be known for the CMake configuration stage. To minimize the total binary size, you can explicitly turn OFF unnecessary components. Use CMake Options for Custom Compilation as a reference for OpenVINO CMake configuration.

For example, to enable only IR v11 reading and CPU inference capabilities without G-API preprocessing, use:

cmake -DENABLE_INTEL_MYRIAD_COMMON=OFF \
      -DENABLE_INTEL_GPU=OFF \
      -DENABLE_INTEL_GNA=OFF \
      -DENABLE_HETERO=OFF \
      -DENABLE_MULTI=OFF \
      -DENABLE_TEMPLATE=OFF \
      -DENABLE_IR_V7_READER=OFF \
      -DENABLE_OV_ONNX_FRONTEND=OFF \
      -DENABLE_OV_PADDLE_FRONTEND=OFF \
      -DENABLE_OV_TF_FRONTEND=OFF \
      -DENABLE_GAPI_PREPROCESSING=OFF \
      -DENABLE_INTEL_CPU=ON \
      -DENABLE_OV_IR_FRONTEND=ON

Note: Inference backends located in external repositories can also be used in a static build. Use -DIE_EXTRA_MODULES=<path to external plugin root> to enable them. OpenVINODeveloperPackage.cmake must not be used to build external plugins, only IE_EXTRA_MODULES is a working solution.

Note: The ENABLE_LTO CMake option can also be passed to enable link time optimizations to reduce the binary size. But such property should also be enabled on the target which links with static OpenVINO libraries via set_target_properties(<target_name> PROPERTIES INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)

Build static OpenVINO libraries

To build OpenVINO Runtime in a static mode, you need to specify the additional CMake option:

cmake -DBUILD_SHARED_LIBS=OFF <all other CMake options> <openvino_sources root>

Then, use the usual CMake 'build' command:

cmake --build . --target openvino --config Release -j12

Then, the installation step:

cmake -DCMAKE_INSTALL_PREFIX=<install_root> -P cmake_install.cmake

The OpenVINO runtime is located in <install_root>/runtime/lib

Link static OpenVINO Runtime

Once you build static OpenVINO Runtime libraries and install them, you can use one of the two ways to add them to your project:

CMake interface

Just use CMake's find_package as usual and link openvino::runtime:

find_package(OpenVINO REQUIRED)
target_link_libraries(<application> PRIVATE openvino::runtime)

openvino::runtime transitively adds all other static OpenVINO libraries to a linker command.

Pass libraries to linker directly

If you want to configure your project directly, you need to pass all libraries from <install_root>/runtime/lib to linker command.

Note: Since the proper order of static libraries must be used (dependent library should come before dependency in a linker command), consider useing the following compiler specific flags to link static OpenVINO libraries:

Microsoft Visual Studio compiler:

/WHOLEARCHIVE:<ov_library 0> /WHOLEARCHIVE:<ov_library 1> ...

GCC like compiler:

gcc main.cpp -Wl,--whole-archive <all libraries from <root>/runtime/lib> > -Wl,--no-whole-archive -o a.out

Static OpenVINO libraries + Conditional compilation for particular models

OpenVINO Runtime can be compiled for particular models, as shown in the Conditional compilation for particular models guide. The conditional compilation feature can be paired with static OpenVINO libraries to build even smaller end-user applications in terms of binary size. The following procedure can be used, (based on the detailed Conditional compilation for particular models guide):

  • Build OpenVINO Runtime as usual with the CMake option of -DSELECTIVE_BUILD=COLLECT.
  • Run target applications on target models and target platforms to collect traces.
  • Build the final OpenVINO static Runtime with -DSELECTIVE_BUILD=ON -DSELECTIVE_BUILD_STAT=/path/*.csv -DBUILD_SHARED_LIBS=OFF

Limitations

  • CMake version 3.17 or higher must be used to build static OpenVINO libraries.

  • Supported OSes:

    • Windows x64
    • Linux x64
    • All other OSes may work, but have not been explicitly tested
  • The enabled and tested capabilities of OpenVINO Runtime in a static build:

    • OpenVINO common runtime - work with ov::Model, perform model loading on particular device
    • CPU and GNA inference plugins (GPU and MYRIAD are not enabled)
    • MULTI, HETERO, AUTO, and BATCH inference modes
    • IR, ONNX, PDPD, and TF frontends to read ov::Model
  • Static build support for building static libraries only for OpenVINO Runtime libraries. All other third-party prebuilt dependencies remain in the same format:

    • libGNA is a shared library.
    • TBB is a shared library; to provide your own TBB build from oneTBB source code use export TBBROOT=<tbb_root> before OpenVINO CMake scripts are run.

    Note: The TBB team does not recommend using oneTBB as a static library, see Why onetbb does not like a static library?

  • TBBBind_2_5 is not available on Windows x64 during a static OpenVINO build (see description for ENABLE_TBBBIND_2_5 CMake option here to understand what this library is responsible for). So, capabilities enabled by TBBBind_2_5 are not available. To enable them, build oneTBB from source code and provide the path to built oneTBB artifacts via TBBROOT environment variable before OpenVINO CMake scripts are run.

  • ov::Op::type_info static member is deprecated and not available in static build. Don't use type_info during implementation of your own custom operations, use ov::Op::get_type_info_static() instead.

Clone this wiki locally