Instructions to use mistral-experimental/pixtral-12b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mistral-experimental/pixtral-12b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="mistral-experimental/pixtral-12b") 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("mistral-experimental/pixtral-12b") model = AutoModelForMultimodalLM.from_pretrained("mistral-experimental/pixtral-12b") 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]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use mistral-experimental/pixtral-12b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "mistral-experimental/pixtral-12b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "mistral-experimental/pixtral-12b", "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/mistral-experimental/pixtral-12b
- SGLang
How to use mistral-experimental/pixtral-12b 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 "mistral-experimental/pixtral-12b" \ --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": "mistral-experimental/pixtral-12b", "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 "mistral-experimental/pixtral-12b" \ --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": "mistral-experimental/pixtral-12b", "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 mistral-experimental/pixtral-12b with Docker Model Runner:
docker model run hf.co/mistral-experimental/pixtral-12b
How do I load the model quantized?
I'm getting CUDA OOM issues and it complains about tensors being on different devices when I set a device map to include CPU. Is there an easy way I can load the model with 8-bit precision?
If I try this:
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = LlavaForConditionalGeneration.from_pretrained(".", device_map="auto", quantization_config=quantization_config)
I get this error:
Input type (torch.cuda.FloatTensor) and weight type (torch.cuda.HalfTensor) should be the same
Also happens when I configure AutoProcessor with a quantization_config...
@treehugg3 can you show the whole inference script? Looks like you didn't cast inputs to fp16 before inference which is the dtype used by BnB by default
Thank you for helping. Here is the script I used:
from transformers import LlavaForConditionalGeneration, AutoProcessor, BitsAndBytesConfig
from PIL import Image
model_id="../pixtral-12b"
image_url ="https://m.media-amazon.com/images/I/81tPIA63TrL._AC_SL1500_.jpg"
prompt = "Describe the picture in one sentence."
IMG_URLS = [
"https://picsum.photos/id/237/400/300",
"https://picsum.photos/id/231/200/300",
"https://picsum.photos/id/27/500/500",
"https://picsum.photos/id/17/150/600",
]
PROMPT = "<s>[INST]Describe the pictures.\n[IMG][IMG][IMG][IMG][/INST]"
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = LlavaForConditionalGeneration.from_pretrained(model_id, device_map="auto", quantization_config=quantization_config)
processor = AutoProcessor.from_pretrained(model_id)
inputs = processor(images=IMG_URLS, text=PROMPT, return_tensors="pt").to("cuda")
generate_ids = model.generate(**inputs, max_new_tokens=500, torch_dtype="auto")
output = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print(output)
It turns out that by creating a new virtualenv, this code now runs properly. There must have been some strange package version combination that caused it to fail in the other one. Really sorry for bothering, I spent a long time trying to figure out why it wasn't working, but it works great now!
Yeah, this has to be casted manually as follows:
inputs = processor(images=IMG_URLS, text=PROMPT, return_tensors="pt").to("cuda", dtype=torch.float16)