Implementation:Alibaba MNN PyMNN Build Setup
Appearance
| Field | Value |
|---|---|
| implementation_name | PyMNN_Build_Setup |
| schema_version | 0.1.0 |
| workflow | Python_Model_Inference |
| implementation_type | API_Doc |
| domain | Deep_Learning_Inference |
| scope | Building and installing the MNN Python package from source or PyPI |
| source_file | pymnn/CMakeLists.txt:L1-235 |
| related_patterns | CMake_Build_Systems, Python_Extension_Modules, Cross_Platform_Compilation |
| last_updated | 2026-02-10 14:00 GMT |
Summary
This implementation covers two approaches for installing MNN's Python bindings: (1) installing a pre-built wheel from PyPI using pip, and (2) building from source using CMake for custom configurations. The build system is defined in pymnn/CMakeLists.txt and produces a native Python extension module (mnnpybridge) that links against the MNN inference engine.
API Signatures
Method 1: pip install (Pre-built Wheel)
pip install MNN
Method 2: Build from Source (CMake)
cd /path/to/MNN
mkdir pymnn_build && cd pymnn_build
cmake -DPYMNN_EXPR_API=ON -DPYMNN_OPENCV_API=ON .. && make
Method 3: setup.py (Development Install)
cd /path/to/MNN/pymnn/pip_package
python setup.py install
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| PYMNN_EXPR_API | CMake option | ON | Enables the expression API binding (MNN.expr module) for tensor manipulation and computation graph operations |
| PYMNN_OPENCV_API | CMake option | ON | Enables the OpenCV-compatible image processing module (MNN.cv) |
| PYMNN_TRAIN_API | CMake option | OFF | Enables the training API for model fine-tuning and custom module training |
| PYMNN_NUMPY_USABLE | CMake option | OFF | Enables interoperability with numpy arrays, allowing Var.read() to return numpy.ndarray |
| PYMNN_LLM_API | CMake option | OFF | Enables the large language model API (MNN.llm) |
| PYMNN_AUDIO_API | CMake option | OFF | Enables the audio processing API (MNN.audio) |
| PYMNN_IMGCODECS | CMake option | OFF | Enables image codec functions (imread, imwrite) for mobile builds |
| MNN_BUILD_SHARED_LIBS | CMake option | ON | Controls whether MNN is built as a shared (.so/.dylib) or static (.a) library |
| MNN_WIN_RUNTIME_MT | CMake option | OFF | On Windows, controls /MT vs /MD runtime linkage |
Inputs
- MNN source tree -- The complete MNN repository containing C++ source, headers, and the pymnn bridge code
- Python development headers -- Required for compiling the native extension module
- CMake 3.4.1+ -- Build system generator
- C++11-compatible compiler -- GCC, Clang, or MSVC
Outputs
- Installed MNN Python package with the following submodules:
- MNN.expr -- Tensor operations, data format conversion, model loading
- MNN.nn -- Module loading, runtime management, neural network layers
- MNN.cv -- OpenCV-compatible image processing (imread, resize, cvtColor, etc.)
- MNN.numpy -- Numpy-compatible array creation and manipulation
Code Example
# After installation, verify the package is working
import MNN
import MNN.expr as expr
import MNN.nn as nn
import MNN.cv as cv
import MNN.numpy as np
# Check basic tensor creation
x = expr.const([1.0, 2.0, 3.0, 4.0], [2, 2], expr.NCHW, expr.float)
print(x.shape) # [2, 2]
print(x.dtype) # dtype.float
Build System Details
The CMake build (pymnn/CMakeLists.txt) performs the following:
- Lines 7-21: Define all optional feature flags as CMake options with their defaults
- Lines 37-41: Create the mnnpybridge library target (shared or static based on MNN_BUILD_SHARED_LIBS)
- Lines 52-103: Set compile definitions based on enabled features (PYMNN_EXPR_API, PYMNN_OPENCV_API, etc.)
- Lines 114-139: Configure platform-specific compiler flags (MSVC flags on Windows, GCC/Clang flags on Unix)
- Lines 149-226: Link against MNN, Python, and optional libraries (numpy, AliNNPython, MNNOpenCV, MNN_LLM, MNNAudio)
# Key CMake options from pymnn/CMakeLists.txt:L7-21
option(PYMNN_EXPR_API "MNN expr API be exposed" ON)
option(PYMNN_NUMPY_USABLE "Build based on numpy" OFF)
option(PYMNN_TRAIN_API "MNN train API be exposed" OFF)
option(PYMNN_OPENCV_API "MNN OpenCV API be exposed" ON)
option(PYMNN_IMGCODECS "MNN IMGCODECS API be exposed" OFF)
option(PYMNN_AUDIO_API "MNN Audio API be exposed" OFF)
option(PYMNN_LLM_API "MNN LLM API be exposed" OFF)
Edge Cases and Limitations
- Mobile platforms: On Android, some features like imread/imwrite are not available by default; enable PYMNN_IMGCODECS to include them
- numpy dependency: PYMNN_NUMPY_USABLE is OFF by default on mobile; Var.read() returns numpy.ndarray only when enabled
- Windows 32-bit: Requires special handling via the BUILD_ARCH=x86 setup.py flag
- GPU backend libraries: Building with CUDA or OpenCL support requires the appropriate SDK to be installed and library paths set
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment