Implementation:Alibaba MNN CMake Build Diffusion
| Field | Value |
|---|---|
| implementation_name | CMake_Build_Diffusion |
| schema_version | 0.3.0 |
| impl_type | API Doc |
| domain | Stable Diffusion Deployment |
| stage | Engine Compilation |
| source_file | CMakeLists.txt (L75, L119-123, L846-852), transformers/diffusion/engine/CMakeLists.txt (L1-30) |
| external_deps | CMake (>= 3.10), C++14 compiler, make or ninja |
| last_updated | 2026-02-10 14:00 GMT |
Summary
This implementation documents the CMake build invocation required to compile MNN with Stable Diffusion inference support. The build produces the diffusion_demo CLI binary and a libMNN shared library containing the diffusion engine, OpenCV image I/O, and optional GPU backend support.
API
cd build
cmake -DMNN_BUILD_DIFFUSION=ON \
-DMNN_BUILD_OPENCV=ON \
-DMNN_IMGCODECS=ON \
[optional flags] \
.. && make -j32
Key Parameters
| CMake Flag | Default | Description |
|---|---|---|
| MNN_BUILD_DIFFUSION | OFF | Master switch: enables diffusion engine and diffusion_demo build |
| MNN_BUILD_OPENCV | OFF | Enables MNN's built-in OpenCV-compatible image API (auto-forced ON by MNN_BUILD_DIFFUSION) |
| MNN_IMGCODECS | OFF | Enables image codec support (JPEG/PNG). Required alongside MNN_BUILD_OPENCV for diffusion |
| MNN_LOW_MEMORY | OFF | Enables low-memory weight loading for quantized models (auto-forced ON by MNN_BUILD_DIFFUSION) |
| MNN_SUPPORT_TRANSFORMER_FUSE | OFF | Enables fused transformer attention kernels (auto-forced ON by MNN_BUILD_DIFFUSION) |
| MNN_OPENCL | OFF | Enables OpenCL GPU backend (Linux, Android) |
| MNN_METAL | OFF | Enables Metal GPU backend (macOS, iOS) |
| MNN_SEP_BUILD | ON | When OFF, links all components into a single libMNN.so |
| MNN_CPU_WEIGHT_DEQUANT_GEMM | OFF | Enables CPU weight dequantization GEMM kernels for quantized models |
Inputs
- The MNN source tree (root directory containing
CMakeLists.txt)
Outputs
- diffusion_demo -- Standalone CLI executable for text-to-image generation
- sana_diffusion_demo -- Standalone CLI executable for Sana diffusion models
- libMNN.so (or
libMNN.dylibon macOS) -- Shared library with diffusion support compiled in - libdiffusion.so (only when
MNN_SEP_BUILD=ONandMNN_BUILD_SHARED_LIBS=ON) -- Separate diffusion shared library
CMake Auto-Forced Dependencies
When MNN_BUILD_DIFFUSION=ON, the following flags are automatically forced (CMakeLists.txt L119-123):
IF (MNN_BUILD_DIFFUSION)
set(MNN_LOW_MEMORY ON CACHE BOOL "<docstring>" FORCE)
set(MNN_SUPPORT_TRANSFORMER_FUSE ON CACHE BOOL "<docstring>" FORCE)
set(MNN_BUILD_OPENCV ON CACHE BOOL "<docstring>" FORCE)
ENDIF()
The diffusion library is conditionally included only when all three prerequisites are met (CMakeLists.txt L846):
IF(MNN_BUILD_DIFFUSION AND MNN_BUILD_OPENCV AND MNN_IMGCODECS)
include(${CMAKE_CURRENT_LIST_DIR}/transformers/diffusion/engine/CMakeLists.txt)
IF(NOT MNN_SEP_BUILD)
list(APPEND MNN_TARGETS diffusion)
list(APPEND MNN_OBJECTS_TO_LINK $<TARGET_OBJECTS:diffusion>)
ENDIF()
ENDIF()
Diffusion Engine Build Targets
From transformers/diffusion/engine/CMakeLists.txt:
# Diffusion library (shared, static, or object depending on build config)
add_library(diffusion ...)
# CLI demo executables
add_executable(diffusion_demo
${CMAKE_CURRENT_LIST_DIR}/diffusion_demo.cpp
)
target_link_libraries(diffusion_demo ${MNN_DEPS})
add_executable(sana_diffusion_demo ${CMAKE_CURRENT_LIST_DIR}/sana_diffusion_demo.cpp)
target_link_libraries(sana_diffusion_demo ${MNN_DEPS})
Usage Examples
Linux build with CPU only:
mkdir build && cd build
cmake -DMNN_BUILD_DIFFUSION=ON \
-DMNN_BUILD_OPENCV=ON \
-DMNN_IMGCODECS=ON \
-DMNN_SEP_BUILD=OFF \
..
make -j32
Linux build with OpenCL GPU acceleration and quantization support:
mkdir build && cd build
cmake -DMNN_BUILD_DIFFUSION=ON \
-DMNN_BUILD_OPENCV=ON \
-DMNN_IMGCODECS=ON \
-DMNN_OPENCL=ON \
-DMNN_SEP_BUILD=OFF \
-DMNN_CPU_WEIGHT_DEQUANT_GEMM=ON \
..
make -j32
macOS build with Metal GPU acceleration:
mkdir build && cd build
cmake -DMNN_BUILD_DIFFUSION=ON \
-DMNN_BUILD_OPENCV=ON \
-DMNN_IMGCODECS=ON \
-DMNN_METAL=ON \
-DMNN_SEP_BUILD=OFF \
..
make -j32
Notes
- The
MNN_IMGCODECSflag is not auto-forced byMNN_BUILD_DIFFUSIONand must be explicitly set. Without it, the build guard at L846 will prevent the diffusion engine from being compiled even ifMNN_BUILD_DIFFUSION=ON. - When
MNN_SEP_BUILD=OFF, the diffusion object files are linked directly into the monolithiclibMNNshared library, simplifying deployment. - The
build_lib.shscript (L339) provides reference build configurations for Android, iOS, macOS, and HarmonyOS platforms.