Instructions to use ibm-granite/granite-vision-4.1-4b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ibm-granite/granite-vision-4.1-4b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="ibm-granite/granite-vision-4.1-4b", trust_remote_code=True) 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)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("ibm-granite/granite-vision-4.1-4b", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("ibm-granite/granite-vision-4.1-4b", trust_remote_code=True, device_map="auto") 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=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ibm-granite/granite-vision-4.1-4b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ibm-granite/granite-vision-4.1-4b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ibm-granite/granite-vision-4.1-4b", "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/ibm-granite/granite-vision-4.1-4b
- SGLang
How to use ibm-granite/granite-vision-4.1-4b 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 "ibm-granite/granite-vision-4.1-4b" \ --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": "ibm-granite/granite-vision-4.1-4b", "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 "ibm-granite/granite-vision-4.1-4b" \ --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": "ibm-granite/granite-vision-4.1-4b", "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 ibm-granite/granite-vision-4.1-4b with Docker Model Runner:
docker model run hf.co/ibm-granite/granite-vision-4.1-4b
Task tags fail when the image placeholder isn't <image>
I'm running this model in llama.cpp with the chat_template.jinja from the repo. The task tags fail before generation.
expand_tags rebuilds the user message from scratch and only keeps the image when the content contains the literal string <image>:
{%- set has_image = "<image>" in text -%}
{%- set img_idx = text.index("<image>") -%}
llama.cpp uses a unique marker for images (<__media_...__>), so has_image is false. The macro returns only the canonical prompt, the marker is gone, and the runtime stops with:
Number of media markers (0) does not match number of loaded media (1)
The macro also calls str.index(). llama.cpp's Jinja engine has no index or find, so forcing <image> doesn't help.
Could the macro replace just the tag instead of rebuilding the message?
{%- macro expand_tags(text) -%}
{%- if "<chart2code>" in text -%}
{{- text.replace("<chart2code>", chart2code_prompt) -}}
{%- elif "<chart2csv>" in text -%}
{{- text.replace("<chart2csv>", chart2csv_prompt) -}}
{%- elif "<chart2summary>" in text -%}
{{- text.replace("<chart2summary>", chart2summary_prompt) -}}
{%- elif "<tables_json>" in text -%}
{{- text.replace("<tables_json>", tables_json_prompt) -}}
{%- elif "<tables_html>" in text -%}
{{- text.replace("<tables_html>", tables_html_prompt) -}}
{%- elif "<tables_otsl>" in text -%}
{{- text.replace("<tables_otsl>", tables_otsl_prompt) -}}
{%- else -%}
{{- text -}}
{%- endif -%}
{%- endmacro -%}
That keeps whatever image placeholder the runtime uses, keeps text around the tag, and drops the str.index() dependency. For <image>\n<tables_html> the output in transformers is the same.
Repro: transformers 5.23.0 with bf16 returns the table. llama.cpp b12383 with the Q8_0 GGUF and F16 mmproj gives the marker error above.
Hi @stornic56 , thanks for raising this. @mrutkows I think this may be the same issue you were discussing with me the other day about the non-<image> placeholders?