Instructions to use ibm-granite/granite-3.0-8b-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ibm-granite/granite-3.0-8b-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ibm-granite/granite-3.0-8b-instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.0-8b-instruct") model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-3.0-8b-instruct", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.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(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ibm-granite/granite-3.0-8b-instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ibm-granite/granite-3.0-8b-instruct" # 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-3.0-8b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ibm-granite/granite-3.0-8b-instruct
- SGLang
How to use ibm-granite/granite-3.0-8b-instruct 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-3.0-8b-instruct" \ --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-3.0-8b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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-3.0-8b-instruct" \ --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-3.0-8b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use ibm-granite/granite-3.0-8b-instruct with Docker Model Runner:
docker model run hf.co/ibm-granite/granite-3.0-8b-instruct
load granite model with pipeline
I tried to follow your code example and use pipeline to load granite-3.0-8b-instruct, but I'm getting an error that indicates the tokenizer is not initialized.
I debugged the code, and it seems that the tokenizer is missing from TOKENIZER_MAPPING_NAMES that is defined in tokenization_auto.py.
Could advise?
@lenadan Thanks for your interest! Can you share a code snippet of how you loaded the model the model with a pipeline?
Sure. I actually used the code you've prodived:
from transformers import pipeline
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe = pipeline("text-generation", model="/my_local_path/granite-3.0-8b-instruct")
pipe(messages)
model was cloned locally by running: git clone https://huggingface.co/ibm-granite/granite-3.0-8b-instruct.
These are my dependencies:
transformers==4.45.2
ibm_watsonx_ai==1.1.16
And this is the full error I got:
Thanks! This is definitely a gap and we'll work to get a fix into transformers soon. In the meantime, you can manually pass a tokenizer to the pipeline initialization:
from transformers import AutoTokenizer, pipeline
model_id = "/my_local_path/granite-3.0-8b-instruct"
tok = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline(task="text-generation", model=model_id, tokenizer=tok)
print(pipe("Hello world!"))
Interesting aside: It looks like you're pulling the example code from the Use this model drop down. That is actually auto-populated by Huggingface and not something we wrote. Thanks for pointing out this gap!
Thanks, I had no idea this drop down was auto-populated. It would be nice if you could make the basic pipeline API work (without the need to provide the tokenizer), because it will enable users how use pipelines to switch to Granite without changing anything in their code. I'll keep experimenting with Granite and check every once in a while if there's update regarding this issue.
Thanks for your quick reply!
Yes, 100% agree. The simplest fix seems to be adding "tokenizer_class" to the config.json (I've verified it works locally). We'll work to get that set in all of the models. Thanks again for pointing this out!
