Implementation:Mlc ai Mlc llm Package
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Mobile_Deployment |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete tool for packaging compiled model libraries with platform-specific bindings into deployable mobile bundles provided by MLC-LLM.
Description
The package function in python/mlc_llm/interface/package.py is the top-level Python entry point for the MLC-LLM mobile packaging pipeline. It reads the mlc-package-config.json configuration file, compiles model libraries for the specified device target, validates the compiled libraries, builds the platform-specific bindings, and produces a complete output directory ready for integration into an iOS or Android application project.
The function orchestrates four major sub-operations:
build_model_library-- Iterates over each model entry in the configuration, downloads model artifacts from Hugging Face if needed, JIT-compiles model libraries for the target device, optionally copies model weights to the bundle directory, and generates themlc-app-config.jsonruntime configuration file.validate_model_lib-- Combines all compiled model libraries into a single static archive and validates that each model's TVM FFI symbols are present in the global symbol table.build_android_binding-- (Android only) Moves the model library to the build directory, invokesprepare_libs.pyto build the JNI bindings, and assembles themlc4jGradle package with source files, build configuration, and runtime assets.build_iphone_binding-- (iOS only) Invokesprepare_libs.shto build the static libraries and copies them to the output directory.
Usage
Use the package function when:
- Building a complete mobile deployment package from a
mlc-package-config.jsonconfiguration - Automating the end-to-end compilation and packaging pipeline in CI/CD
- Producing the output artifacts needed to integrate MLC-LLM into an Xcode or Android Studio project
Code Reference
Source Location
- Repository: MLC-LLM
- File:
python/mlc_llm/interface/package.py(Lines 323-373)
Signature
def package(
package_config_path: Path,
mlc_llm_source_dir: Path,
output: Path,
) -> None:
"""Python entrypoint of package.
Parameters
----------
package_config_path : Path
Path to the mlc-package-config.json file.
mlc_llm_source_dir : Path
Path to the MLC-LLM source directory.
output : Path
Path to the output directory for compiled libraries,
bundled weights, and platform bindings.
"""
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
package_config_path |
Path | Yes | Path to the mlc-package-config.json file specifying device, models, and compilation options
|
mlc_llm_source_dir |
Path | Yes | Path to the root of the MLC-LLM source tree, used to locate platform-specific build scripts (ios/prepare_libs.sh, android/mlc4j/prepare_libs.py)
|
output |
Path | Yes | Path to the output directory where compiled libraries, bundled weights, and platform bindings will be written |
Outputs
| Name | Type | Description |
|---|---|---|
output/bundle/mlc-app-config.json |
JSON file | Runtime configuration mapping model IDs to compiled library names and model URLs or local paths |
output/lib/libmodel_iphone.a |
Static library | Combined static archive of all compiled model libraries for iOS (when device is iphone)
|
output/lib/libmodel_android.a |
Static library | Combined static archive of all compiled model libraries for Android (when device is android)
|
output/lib/*.a (iOS) |
Static libraries | MLC-LLM and TVM runtime static libraries for iOS |
output/lib/mlc4j/ (Android) |
Directory | Complete mlc4j Gradle package containing JNI source, build configuration, compiled shared libraries, and runtime assets |
output/bundle/<model_id>/ |
Directory | Model weight files for models with bundle_weight: true
|
Usage Examples
Basic Usage (Python API)
from pathlib import Path
from mlc_llm.interface.package import package
package(
package_config_path=Path("ios/MLCChat/mlc-package-config.json"),
mlc_llm_source_dir=Path("/path/to/mlc-llm"),
output=Path("dist"),
)
Command-Line Usage
# Package for iOS
mlc_llm package \
--package-config ios/MLCChat/mlc-package-config.json \
--mlc-llm-source-dir /path/to/mlc-llm \
--output dist/
# Package for Android
mlc_llm package \
--package-config android/MLCChat/mlc-package-config.json \
--mlc-llm-source-dir /path/to/mlc-llm \
--output dist/
Integration with Xcode (iOS)
# After running mlc_llm package, copy the output into the Xcode project
cp dist/lib/*.a ios/MLCChat/lib/
cp -r dist/bundle/ ios/MLCChat/bundle/
# Open the Xcode project and build
open ios/MLCChat.xcodeproj
Integration with Android Studio
# After running mlc_llm package, the mlc4j library is ready
# Copy the mlc4j output into the Android project dependencies
cp -r dist/lib/mlc4j/ android/MLCChat/mlc4j/
# Build the Android project
cd android/MLCChat && ./gradlew assembleRelease