| 1 | # MLX-VLM |
| 2 | |
| 3 | MLX-VLM is a package for inference and fine-tuning of Vision Language Models (VLMs) on your Mac using MLX. |
| 4 | |
| 5 | ## Table of Contents |
| 6 | - [Installation](#installation) |
| 7 | - [Usage](#usage) |
| 8 | - [Command Line Interface (CLI)](#command-line-interface-cli) |
| 9 | - [Chat UI with Gradio](#chat-ui-with-gradio) |
| 10 | - [Python Script](#python-script) |
| 11 | - [Multi-Image Chat Support](#multi-image-chat-support) |
| 12 | - [Supported Models](#supported-models) |
| 13 | - [Usage Examples](#usage-examples) |
| 14 | - [Fine-tuning](#fine-tuning) |
| 15 | |
| 16 | ## Installation |
| 17 | |
| 18 | The easiest way to get started is to install the `mlx-vlm` package using pip: |
| 19 | |
| 20 | ```sh |
| 21 | pip install mlx-vlm |
| 22 | ``` |
| 23 | |
| 24 | ## Usage |
| 25 | |
| 26 | ### Command Line Interface (CLI) |
| 27 | |
| 28 | Generate output from a model using the CLI: |
| 29 | |
| 30 | ```sh |
| 31 | python -m mlx_vlm.generate --model mlx-community/Qwen2-VL-2B-Instruct-4bit --max-tokens 100 --temp 0.0 --image http://images.cocodataset.org/val2017/000000039769.jpg |
| 32 | ``` |
| 33 | |
| 34 | ### Chat UI with Gradio |
| 35 | |
| 36 | Launch a chat interface using Gradio: |
| 37 | |
| 38 | ```sh |
| 39 | python -m mlx_vlm.chat_ui --model mlx-community/Qwen2-VL-2B-Instruct-4bit |
| 40 | ``` |
| 41 | |
| 42 | ### Python Script |
| 43 | |
| 44 | Here's an example of how to use MLX-VLM in a Python script: |
| 45 | |
| 46 | ```python |
| 47 | import mlx.core as mx |
| 48 | from mlx_vlm import load, generate |
| 49 | from mlx_vlm.prompt_utils import apply_chat_template |
| 50 | from mlx_vlm.utils import load_config |
| 51 | |
| 52 | # Load the model |
| 53 | model_path = "mlx-community/Qwen2-VL-2B-Instruct-4bit" |
| 54 | model, processor = load(model_path) |
| 55 | config = load_config(model_path) |
| 56 | |
| 57 | # Prepare input |
| 58 | image = ["http://images.cocodataset.org/val2017/000000039769.jpg"] |
| 59 | prompt = "Describe this image." |
| 60 | |
| 61 | # Apply chat template |
| 62 | formatted_prompt = apply_chat_template( |
| 63 | processor, config, prompt, num_images=len(image) |
| 64 | ) |
| 65 | |
| 66 | # Generate output |
| 67 | output = generate(model, processor, image, formatted_prompt, verbose=False) |
| 68 | print(output) |
| 69 | ``` |
| 70 | |
| 71 | ## Multi-Image Chat Support |
| 72 | |
| 73 | MLX-VLM supports analyzing multiple images simultaneously with select models. This feature enables more complex visual reasoning tasks and comprehensive analysis across multiple images in a single conversation. |
| 74 | |
| 75 | ### Supported Models |
| 76 | |
| 77 | The following models support multi-image chat: |
| 78 | |
| 79 | 1. Idefics 2 |
| 80 | 2. LLaVA (Interleave) |
| 81 | 3. Qwen2-VL |
| 82 | 4. Phi3-Vision |
| 83 | 5. Pixtral |
| 84 | |
| 85 | ### Usage Examples |
| 86 | |
| 87 | #### Python Script |
| 88 | |
| 89 | ```python |
| 90 | from mlx_vlm import load, generate |
| 91 | from mlx_vlm.prompt_utils import apply_chat_template |
| 92 | from mlx_vlm.utils import load_config |
| 93 | |
| 94 | model_path = "mlx-community/Qwen2-VL-2B-Instruct-4bit" |
| 95 | model, processor = load(model_path) |
| 96 | config = load_config(model_path) |
| 97 | |
| 98 | images = ["path/to/image1.jpg", "path/to/image2.jpg"] |
| 99 | prompt = "Compare these two images." |
| 100 | |
| 101 | formatted_prompt = apply_chat_template( |
| 102 | processor, config, prompt, num_images=len(images) |
| 103 | ) |
| 104 | |
| 105 | output = generate(model, processor, images, formatted_prompt, verbose=False) |
| 106 | print(output) |
| 107 | ``` |
| 108 | |
| 109 | #### Command Line |
| 110 | |
| 111 | ```sh |
| 112 | python -m mlx_vlm.generate --model mlx-community/Qwen2-VL-2B-Instruct-4bit --max-tokens 100 --prompt "Compare these images" --image path/to/image1.jpg path/to/image2.jpg |
| 113 | ``` |
| 114 | |
| 115 | These examples demonstrate how to use multiple images with MLX-VLM for more complex visual reasoning tasks. |
| 116 | |
| 117 | # Fine-tuning |
| 118 | |
| 119 | MLX-VLM supports fine-tuning models with LoRA and QLoRA. |
| 120 | |
| 121 | ## LoRA & QLoRA |
| 122 | |
| 123 | To learn more about LoRA, please refer to the [LoRA.md](./mlx_vlm/LORA.MD) file. |