Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Alibaba MNN MNN Build System

From Leeroopedia


Field Value
Principle Name MNN_Build_System
Category Model_Conversion_Pipeline
Description Build system configuration for MNN tools (converter, quantizer, etc.)
Applies To Compilation of MNN from source

Overview

MNN uses a CMake-based modular build system that allows developers to selectively compile only the components they need. Through a set of feature flags (CMake options), each major subsystem -- including the model converter, quantization tools, training framework, backend support (CUDA, OpenCL, Metal, Vulkan), and higher-level features (LLM, diffusion, OpenCV) -- can be independently enabled or disabled at configure time.

This design is critical for on-device deployment scenarios where binary size matters and unnecessary components should be excluded from the final artifact.

Theory: Feature-Flag-Driven Compilation

Modular Architecture

The MNN build system is organized around a central CMakeLists.txt that defines dozens of boolean options using CMake's option() directive. Each option controls whether a particular subsystem's source files are included in the build and whether its corresponding targets (libraries, executables) are generated.

The key architectural principles are:

  • Additive composition -- The core MNN library is always built. Additional components (backends, tools, converters) are layered on top via feature flags.
  • Conditional dependencies -- Enabling certain features automatically enables their prerequisites. For example, enabling MNN_BUILD_LLM automatically forces MNN_LOW_MEMORY=ON and MNN_SUPPORT_TRANSFORMER_FUSE=ON.
  • Separate vs. unified builds -- The MNN_SEP_BUILD option controls whether backend libraries are compiled as separate shared libraries or linked into a single monolithic MNN shared library.

Converter Build Dependencies

The model converter (MNNConvert) has its own dependency chain:

  • Protobuf is required for parsing Caffe and TensorFlow model formats. MNN can either use its bundled protobuf (MNN_BUILD_PROTOBUFFER=ON, the default) or the system-installed version.
  • TorchScript support is an optional sub-feature within the converter, controlled by MNN_BUILD_TORCH.
  • The converter produces both an executable (MNNConvert) and a shared/static library (MNNConvertDeps) containing all conversion logic.

Build Outputs

A typical converter-focused build produces:

  • MNNConvert -- The CLI executable for model conversion
  • MNNConvertDeps -- A library containing all converter backend logic (ONNX, TF, Caffe, TFLite, Torch parsers and optimizer passes)
  • MNN -- The core inference library (always built)

Cross-Platform Support

The build system supports multiple platforms and architectures:

  • Linux (x86_64, ARM, AArch64, RISC-V)
  • Android (ARM, AArch64, x86)
  • macOS / iOS (framework or static library builds)
  • Windows (MSVC, Clang, GCC/MinGW)
  • OpenHarmony (OHOS)

Platform-specific compiler flags, linker options, and runtime library settings are automatically configured based on the target platform.

Key Build Options

Option Default Description
MNN_BUILD_CONVERTER OFF Build the MNNConvert model converter tool
MNN_BUILD_QUANTOOLS OFF Build quantization tools
MNN_BUILD_TRAIN OFF Build the MNN training framework
MNN_BUILD_TOOLS ON Build C++ tools
MNN_BUILD_DEMO OFF Build demo executables
MNN_BUILD_SHARED_LIBS ON Build shared (.so/.dylib) vs static (.a) libraries
MNN_BUILD_CODEGEN OFF Build code generation support
MNN_BUILD_PROTOBUFFER ON Use bundled protobuf (vs system protobuf)
MNN_BUILD_TORCH OFF Enable TorchScript converter support (within converter)
MNN_BUILD_LLM OFF Build LLM inference library
MNN_BUILD_DIFFUSION OFF Build diffusion model demo
MNN_SEP_BUILD ON Build backends as separate shared libraries
MNN_LOW_MEMORY OFF Support low-memory weight-quantized model inference
MNN_SUPPORT_TRANSFORMER_FUSE OFF Enable transformer operator fusion

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment