Use custom or fine-tuned FastVLM models
mainexport the model](../model_export#export-vlm). Ensure the custom model files are placed in app/FastVLM/model after clearing any existing models in that directory.repository·main·Indexed 27 days ago
https://github.com/apple/ml-fastvlmFastVLM provides efficient vision encoding for Vision Language Models (VLMs) using the FastViTHD hybrid vision encoder to reduce encoding time and token output for high-resolution images. It supports high-performance inference on servers and Apple Silicon devices (iOS 18.2+ and macOS 15.2+). The project includes pretrained models in 0.5B, 1.5B, and 7B sizes, tools for exporting models to MLX format via coremltools, and a llava package for managing multimodal conversations and image processing.
export the model](../model_export#export-vlm). Ensure the custom model files are placed in app/FastVLM/model after clearing any existing models in that directory.Convert a FastVLM checkpoint to the MLX format using mlx_vlm.convert. You can export only the LLM component and optionally apply quantization.
# Standard export (LLM only)
python -m mlx_vlm.convert --hf-path /path/to/fastvlm-checkpoint \
--mlx-path /path/to/exported-fastvlm \
--only-llm
# Export with 8-bit quantization
python -m mlx_vlm.convert --hf-path /path/to/fastvlm-checkpoint \
--mlx-path /path/to/exported-fastvlm \
--only-llm \
-q \
--q-bits 8To set up the FastVLM environment, create a new Conda environment with Python 3.10 and install the package in editable mode.
conda create -n fastvlm python=3.10
conda activate fastvlm
pip install -e .To ensure all states needed for auto-inference (especially for third-party libraries like mlx-vlm) are available, you must export the vision encoder using coremltools and patch the checkpoint. This saves additional metadata to the model checkpoint directory.
python export_vision_encoder.py --model-path /path/to/fastvlm-checkpointYou can download all available pretrained checkpoints using the provided shell script. The files will be saved to the checkpoints directory.
bash get_models.shFastVLM is designed for on-device visual question answering. To run the application:
get_pretrained_mlx_model.sh script.Compatibility Requirements:
Features:
To run FastVLM on Apple Silicon, PyTorch checkpoints must be exported to a compatible format. Detailed instructions and code are located in the model_export/ subfolder.
Pre-converted models are available for convenience:
fastvlm_0.5b_stage3 (FP16)fastvlm_1.5b_stage3 (INT8)fastvlm_7b_stage3 (INT4)app/ subfolder.Use the get_pretrained_mlx_model.sh script to download pretrained FastVLM models for use in the app. The script downloads the model from the web and places it in the correct directory.
Available model sizes:
Important: Before downloading or copying a new model, ensure you clear the existing model in app/FastVLM/model.
FastVLM requires a specific patch for mlx-vlm to support inference. Follow these steps to clone, checkout the compatible commit, apply the patch, and install in editable mode:
mlx-vlm repository.1884b551bc741f26b2d54d68fa89d4e934b9a3de.fastvlm_mlx-vlm.patch file located in the parent directory.git clone https://github.com/Blaizzy/mlx-vlm.git
cd mlx-vlm
git checkout 1884b551bc741f26b2d54d68fa89d4e934b9a3de
git apply ../fastvlm_mlx-vlm.patch
pip install -e .If you encounter the error ValueError: Received parameters not in model: language_model.lm_head.weight. during conversion, it is likely caused by an incorrect tie_word_embeddings value in the LLaVA model's config.json.
Solution: Manually update the tie_word_embeddings value in your config.json to the correct setting for your model architecture.
Use the predict.py script to run inference on a PyTorch checkpoint by providing the model path, an image file, and a text prompt.
python predict.py --model-path /path/to/checkpoint-dir \
--image-file /path/to/image.png \
--prompt "Describe the image."