Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

4.9 KB
Raw Blame

Selective Build

Introduction

Selective build or conditional compilation can significantly reduce OpenVINO™ binaries size by excluding unnecessary components for particular model's inference.

In-code conditional compilation

Two families of macros let CPU plugin code participate in conditional compilation. They are the in-code counterpart of the build workflow described below: the COLLECT build records which branches are actually exercised, and the selective build then drops the rest.

OV_SWITCH / OV_CASE — runtime element-type dispatch that the build system can prune. Defined in the shared conditional-compilation component, openvino/cc/selective_build.h. Inside the CPU plugin they are pulled in via selective_build.h. Use them for per-precision dispatch instead of a manual if/else/switch on the element type — a manual chain compiles in every type unconditionally and defeats selective build.

OV_SWITCH(intel_cpu, OpNameExecute, ctx, precision,
          OV_CASE(ov::element::f32, float),
          OV_CASE(ov::element::bf16, ov::bfloat16),
          OV_CASE(ov::element::i32, int32_t))

OV_CPU_INSTANCE_* — architecture/backend guards for executor implementation lists, defined in src/utils/arch_macros.h: OV_CPU_INSTANCE_COMMON, OV_CPU_INSTANCE_X64, OV_CPU_INSTANCE_ARM64, OV_CPU_INSTANCE_ACL, OV_CPU_INSTANCE_ACL32, OV_CPU_INSTANCE_ACL64, OV_CPU_INSTANCE_DNNL, OV_CPU_INSTANCE_DNNL_X64, OV_CPU_INSTANCE_DNNL_ARM64, OV_CPU_INSTANCE_MLAS_X64, OV_CPU_INSTANCE_MLAS_ARM64, OV_CPU_INSTANCE_RISCV64, OV_CPU_INSTANCE_KLEIDIAI. Each wraps an ExecutorImplementation so code for unavailable platforms is compiled away — see executors/README.md for the full macro-to-condition table and how they're used in getImplementations().

For the OpenVINO-wide conditional-compilation guide see docs/dev/conditional_compilation.md.

Workflow

Onednn path in OpenVINO:

./src/plugins/intel_cpu/thirdparty/onednn/

The selective build carries on under the instruction of:

./src/common/conditional_compilation/CMakeLists.txt

First build

The first build collects the information and generate a CSV file indicating which components are going to be included and which are going to be excluded in the selected build.

  • For the first build, config cmake with option "-DENABLE_PROFILING_ITT=FULL -DSELECTIVE_BUILD=COLLECT"
cd ./build

cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_BUILD_TYPE=Release \
-DENABLE_CPU_DEBUG_CAPS=ON \
-DENABLE_PROFILING_ITT=FULL \
-DSELECTIVE_BUILD=COLLECT \
-DCMAKE_INSTALL_PREFIX=`pwd`/install \
-DCMAKE_INSTALL_RPATH=`pwd`/install/runtime/3rdparty/tbb/lib:`pwd`/install/runtime/lib/intel64

cmake --build . --config Release -j 8

Generate statistics file (CSV):

  • Need to build manually to get libIntelSEAPI.so with the following step first.
# in ./build
cd ./thirdparty/itt_collector && make
  • Then generate statistics file (in .csv format).
cd ../../../bin/intel64/Release/

python ../../../thirdparty/itt_collector/runtool/sea_runtool.py \
--bindir ./lib -o models_statistics_dir ! \
./benchmark_app -niter 1 -nireq 1 \
-m <model_path>

Second build

The second build takes in the information in the statistics file and only build the components mentioned in the CSV file.

The generated CSV file could be found in folder:

openvino/bin/intel64/Release/

This time, config cmake with option "-DSELECTIVE_BUILD=ON"

cd ../../../build

cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DENABLE_CPU_DEBUG_CAPS=ON \
-DENABLE_DEBUG_CAPS=ON \
-DSELECTIVE_BUILD=ON \
-DSELECTIVE_BUILD_STAT=<CSV_PATH> \
-DPython3_EXECUTABLE=/usr/bin/python3.7 \
-DCMAKE_INSTALL_PREFIX=`pwd`/install \
-DCMAKE_INSTALL_RPATH=`pwd`/install/runtime/3rdparty/tbb/lib:`pwd`/install/runtime/lib/intel64

cmake --build . --config Release -j 8

As shown in

./src/common/conditional_compilation/CMakeLists.txt

CSV file works as a SELECTIVE_BUILD_STAT and is first interpreted into a c++ head file:

./build/src/common/conditional_compilation/conditional_compilation_gen.h

It is generated by 'ccheader.py' and can indicate if selective build analyze process (first build) works correctly.

Test selective compile with benchmark

cd ./bin/intel64/Release

./benchmark_app -niter 1 -nireq 1 \
-m <model_path> -d CPU 

Reference

https://github.com/openvinotoolkit/openvino/blob/master/docs/dev/conditional_compilation.md

https://github.com/openvinotoolkit/openvino/commit/ec48fcb29bef94cca480573110a598afc3515019#

https://github.com/openvinotoolkit/openvino/commit/1f96ddfbf9030e765aa0ca0a6aafb1ae2b8c3e98#diff-de6f7126342889fe9d0b505b0ac391db2275400488d5bab9194c2542a4f4d1fe