Instructions to use mlx-community/gemma-3-4b-it-4bit with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mlx-community/gemma-3-4b-it-4bit with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="mlx-community/gemma-3-4b-it-4bit") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("mlx-community/gemma-3-4b-it-4bit") model = AutoModelForMultimodalLM.from_pretrained("mlx-community/gemma-3-4b-it-4bit") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - MLX
How to use mlx-community/gemma-3-4b-it-4bit with MLX:
# Make sure mlx-vlm is installed # pip install --upgrade mlx-vlm from mlx_vlm import load, generate from mlx_vlm.prompt_utils import apply_chat_template from mlx_vlm.utils import load_config # Load the model model, processor = load("mlx-community/gemma-3-4b-it-4bit") config = load_config("mlx-community/gemma-3-4b-it-4bit") # Prepare input image = ["http://images.cocodataset.org/val2017/000000039769.jpg"] prompt = "Describe this image." # Apply chat template formatted_prompt = apply_chat_template( processor, config, prompt, num_images=1 ) # Generate output output = generate(model, processor, formatted_prompt, image) print(output) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- vLLM
How to use mlx-community/gemma-3-4b-it-4bit with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mlx-community/gemma-3-4b-it-4bit" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mlx-community/gemma-3-4b-it-4bit", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/mlx-community/gemma-3-4b-it-4bit
- SGLang
How to use mlx-community/gemma-3-4b-it-4bit with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "mlx-community/gemma-3-4b-it-4bit" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mlx-community/gemma-3-4b-it-4bit", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "mlx-community/gemma-3-4b-it-4bit" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mlx-community/gemma-3-4b-it-4bit", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use mlx-community/gemma-3-4b-it-4bit with Docker Model Runner:
docker model run hf.co/mlx-community/gemma-3-4b-it-4bit
Took me about 20 minutes of talking to gemini, 4o-mini and grok to get MLX-LM setup maybe it will help someone.
mlx-vlm Setup on macOS (Apple Silicon)
This document outlines the steps taken to successfully install and run the mlx-vlm package on a macOS machine with Apple Silicon, resolving potential build issues related to dependencies like scipy.
Prerequisites
- Homebrew: Package manager for macOS. (https://brew.sh/)
- Python: A recent version of Python 3 (e.g., 3.10+). The log shows Python 3.13 was used.
- Git: For cloning repositories (implied, standard development tool).
- Build Tools & Libraries:
gcc: The GNU Compiler Collection.libomp: OpenMP runtime library.openblas: Optimized BLAS library (required byscipy). Although not explicitly installed in the final successful command sequence, the environment variables set indicate it's a necessary dependency that must be present (likely installed previously or as a dependency).
Setup Steps
Create and Activate Virtual Environment:
It's recommended to use a virtual environment to manage dependencies.# Assuming you have cloned or created a project directory # cd your_project_directory python3 -m venv mlx-vlm source mlx-vlm/bin/activate(Replace
mlx-vlmwith your preferred environment name if desired)Install Dependencies via Homebrew:
Ensuregcc,libomp, andopenblasare installed. If you encounter issues, make sure you have the latest versions or the specific versions required.brew install gcc libomp openblas(The log shows these were already installed, but this command ensures they are present.)
Configure Environment Variables for Build:
This was the crucial step to resolve thescipybuild error related to findingOpenBLAS. These variables tell the build system where to find the compilers and necessary libraries/headers installed by Homebrew.
Note: The specificgccversion (gcc-14) was used. Adjust if your installed version differs.# Specify the GCC compilers from Homebrew export CC=$(brew --prefix gcc)/bin/gcc-14 export CXX=$(brew --prefix gcc)/bin/g++-14 # Set Linker flags to find OpenBLAS and libomp libraries export LDFLAGS="-L$(brew --prefix openblas)/lib -L$(brew --prefix libomp)/lib" # Set C Preprocessor flags to find OpenBLAS and libomp headers export CPPFLAGS="-I$(brew --prefix openblas)/include -I$(brew --prefix libomp)/include" # Set the pkg-config path to help find OpenBLAS configuration export PKG_CONFIG_PATH="$(brew --prefix openblas)/lib/pkgconfig"Using
$(brew --prefix <formula>)makes the paths dynamic based on your Homebrew installation location.Install mlx-vlm:
With the environment configured, install the package using pip.pip install mlx-vlm(This command triggered the download and successful build/installation of
mlx-vlmand its numerous dependencies, includingscipy,mlx,transformers, etc.)
Usage Example
After successful installation, you can run the model inference command:
python -m mlx_vlm.generate --model mlx-community/gemma-3-4b-it-4bit --max-tokens 100 --temperature 0.0 --prompt "Your prompt here" --image your_image.png
(The example command used in the log was:
python -m mlx_vlm.generate --model mlx-community/gemma-3-4b-it-4bit --max-tokens 100 --temperature 0.0 --prompt "How many points to travel Saturday to Saturday" --image mexico-va.png
which successfully fetched the model and generated output.)
Troubleshooting Notes
The primary issue encountered was scipy failing to build due to not finding the `
All together:
brew install gcc libomp
export CC=gcc-14
export CXX=g++-14
export LDFLAGS="-L/opt/homebrew/opt/openblas/lib -L/opt/homebrew/opt/libomp/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openblas/include -I/opt/homebrew/opt/libomp/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openblas/lib/pkgconfig"
pip install mlx-vlm