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.

Implementation:Alibaba MNN CMake Build Converter

From Leeroopedia


Field Value
Implementation Name CMake_Build_Converter
Type API Doc
Category Model_Conversion_Pipeline
Source CMakeLists.txt:L51 (MNN_BUILD_CONVERTER option), tools/converter/CMakeLists.txt (converter build)
External Dependencies cmake (>= 3.6), C/C++ compiler, protobuf (bundled or system)

Summary

Building the MNN model converter from source requires enabling the MNN_BUILD_CONVERTER CMake option. This compiles the MNNConvert executable along with all framework-specific converter backends (ONNX, TensorFlow, Caffe, TFLite, and optionally TorchScript).

API

cmake -DMNN_BUILD_CONVERTER=ON .. && make -j4

Build Option Definitions

The following option definitions are taken directly from the root CMakeLists.txt:

# CMakeLists.txt:L51
option(MNN_BUILD_CONVERTER "Build Converter" OFF)

# CMakeLists.txt:L69
option(MNN_BUILD_CODEGEN "Build with codegen" OFF)

# CMakeLists.txt:L71
option(MNN_BUILD_PROTOBUFFER "Build with protobuffer in MNN" ON)

# tools/converter/CMakeLists.txt:L3
option(MNN_BUILD_TORCH "Build Converter support TorchScript." OFF)

Key Parameters

CMake Option Default Description
MNN_BUILD_CONVERTER OFF Master switch to enable building the converter. Must be set to ON.
MNN_BUILD_PROTOBUFFER ON Use bundled protobuf from 3rd_party/protobuf. Set to OFF to use system protobuf.
MNN_BUILD_TORCH OFF Enable TorchScript model support in the converter. Requires libtorch.
MNN_BUILD_SHARED_LIBS ON Build shared libraries. When ON, also builds MNNRevert2Buffer, MNNDump2Json, TestConvertResult.
MNN_BUILD_CODEGEN OFF Enable code generation support.
MNN_SEP_BUILD ON Build MNN backends as separate shared libraries. Auto-disabled for static builds.
CMAKE_BUILD_TYPE (unset) Set to Release for optimized builds or Debug for debugging.

Inputs

  • MNN source tree (the complete repository checkout)
  • CMake >= 3.6
  • C99 / C++11 compatible compiler (C++17 if CUDA + transformer fuse is enabled)

Outputs

  • build/MNNConvert -- The model converter CLI executable
  • build/libMNNConvertDeps.so (or .a) -- Shared/static library containing all converter logic
  • build/libMNN.so (or .a) -- The core MNN inference library
  • When MNN_BUILD_SHARED_LIBS=ON, additional helper executables:
    • build/MNNRevert2Buffer
    • build/MNNDump2Json
    • build/TestConvertResult
    • build/TestPassManager

Step-by-Step Build Instructions

Basic Converter Build

# Clone the repository
git clone https://github.com/alibaba/MNN.git
cd MNN

# Create build directory
mkdir build && cd build

# Configure with converter enabled
cmake -DMNN_BUILD_CONVERTER=ON ..

# Build (use -j to parallelize)
make -j$(nproc)

# Verify the binary exists
./MNNConvert --help

Converter Build with TorchScript Support

cmake -DMNN_BUILD_CONVERTER=ON \
      -DMNN_BUILD_TORCH=ON \
      -DCMAKE_PREFIX_PATH=/path/to/libtorch \
      ..
make -j$(nproc)

Static Library Build

cmake -DMNN_BUILD_CONVERTER=ON \
      -DMNN_BUILD_SHARED_LIBS=OFF \
      -DMNN_WIN_RUNTIME_MT=ON \
      ..
make -j$(nproc)

Note: On Windows with MSVC, when MNN_BUILD_CONVERTER=ON and MNN_BUILD_SHARED_LIBS=OFF, MNN_WIN_RUNTIME_MT must be ON because protobuf does not support the static /MD configuration.

Cross-Compilation for Android

cmake -DMNN_BUILD_CONVERTER=ON \
      -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
      -DANDROID_ABI=arm64-v8a \
      -DMNN_BUILD_FOR_ANDROID_COMMAND=ON \
      ..
make -j$(nproc)

Build Dependency Chain

When MNN_BUILD_CONVERTER=ON, the following dependency chain is activated:

MNNConvert (executable)
  -> MNNConvertDeps (library)
       -> ONNX converter backend
       -> TensorFlow converter backend (requires protobuf)
       -> Caffe converter backend (requires protobuf)
       -> TFLite converter backend
       -> Torch converter backend (optional, requires MNN_BUILD_TORCH=ON)
       -> MNN optimizer passes
       -> Compression utilities
       -> FlatBuffers utilities
  -> MNN (core library)
       -> MNNCore, MNNMath, MNNCV, MNNUtils, MNNTransform
       -> MNN_Express (expression API)

Implicit Dependencies

Enabling MNN_BUILD_CONVERTER triggers the following implicit behavior in the build system:

  • Protobuf is built from source (from 3rd_party/protobuf/cmake) when MNN_BUILD_PROTOBUFFER=ON (the default).
  • The converter subdirectory (tools/converter) is included only when MNN_SKIPBUILD_GEOMETRY is not set, as the converter depends on the geometry module.

Related Pages

Page Connections

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