microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.07k stars 2.84k forks source link

[Build] reduce the size of the shared library #21544

Open saad-koukous opened 1 month ago

saad-koukous commented 1 month ago

Describe the issue

I successfully built a shared library, but the size of the .so file is 14 MB, which is too big for me. How can I reduce it? onnx v1.18.0

Urgency

No response

Target platform

beagleBone Black rev3 armv7 gcc 8.3

Build script

ARMCC_FLAGS="-march=armv7-a -mfpu=neon-vfpv3 -funsafe-math-optimizations -mfp16-format=ieee" ARMCC_PREFIX=/home/saad/toolchains/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/arm-linux-gnueabihf- cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \ -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \ -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \ -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \ -DCMAKE_SYSTEM_NAME=Linux \ -DCMAKE_SYSTEM_PROCESSOR=armv7 \ -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \ -Donnxruntime_BUILD_SHARED_LIB=ON \ -Donnxruntime_BUILD_UNIT_TESTS=OFF \ -Donnxruntime_MINIMAL_BUILD_CUSTOM_OPS=ON \ -DCMAKE_BUILD_TYPE=MinSizeRel \ -Donnxruntime_ENABLE_CPUINFO=OFF \ ../cmake make -j6

Error / output

no error

Visual Studio Version

No response

GCC / Compiler Version

No response

skottmckay commented 1 month ago

Create a config based on your model to limit the operators and types to the required ones.

https://github.com/microsoft/onnxruntime/blob/main/docs/Reduced_Operator_Kernel_build.md

saad-koukous commented 1 month ago

Thank you for your response. After generating the required operators, how can I use them in the build script I previously provided? Generated from ONNX model/s:

edgchen1 commented 4 weeks ago

To do a reduced ops build, it's easiest to use build.py.

Otherwise, if you want to directly use CMake, you'll need to also run reduce_op_kernels.py (see the doc linked above) and define onnxruntime_REDUCED_OPS_BUILD=ON.

Here's how build.py calls reduce_op_kernels.py: https://github.com/microsoft/onnxruntime/blob/154084efaa1a9d01f12825b7274410aa69dce0de/tools/ci_build/build.py#L2713