Instructions to use ibm-granite/granite-3.1-2b-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ibm-granite/granite-3.1-2b-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ibm-granite/granite-3.1-2b-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.1-2b-instruct") model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-3.1-2b-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.1-2b-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.1-2b-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.1-2b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ibm-granite/granite-3.1-2b-instruct
- SGLang
How to use ibm-granite/granite-3.1-2b-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.1-2b-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.1-2b-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.1-2b-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.1-2b-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use ibm-granite/granite-3.1-2b-instruct with Docker Model Runner:
docker model run hf.co/ibm-granite/granite-3.1-2b-instruct
Exception: data did not match any variant of untagged enum ModelWrapper at line 244953 column 3
#4
by eknath2k - opened
granite model: granite-3.1-8b-instruct
tokenizer version: tokenizers 0.19.1 py311h6640629_0 conda-forge
Error traceback:
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 268, in
main()
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 243, in main
init_llm(p.configfile)
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 193, in init_llm
setup(config)
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 97, in setup
tokenizer = AutoTokenizer.from_pretrained(model_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/models/auto/tokenization_auto.py", line 897, in from_pretrained
return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2271, in from_pretrained
return cls._from_pretrained(
^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2505, in _from_pretrained
tokenizer = cls(*init_inputs, **init_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/models/gpt2/tokenization_gpt2_fast.py", line 99, in init
super().init(
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/tokenization_utils_fast.py", line 115, in init
fast_tokenizer = TokenizerFast.from_file(fast_tokenizer_file)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Exception: data did not match any variant of untagged enum ModelWrapper at line 244953 column 3
(askpenv) [eknath@cccxl008 /dccstor/askpower/eknath/askpbot2]$python runAnyllm.py -c ./newconfig.yaml
Initializing LLM .. from config file: ./newconfig.yaml...Traceback (most recent call last):
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 269, in
main()
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 244, in main
init_llm(p.configfile)
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 193, in init_llm
setup(config)
File "/dccstor/askpower/eknath/askpbot2/runAnyllm.py", line 97, in setup
tokenizer = AutoTokenizer.from_pretrained(model_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/models/auto/tokenization_auto.py", line 897, in from_pretrained
return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2271, in from_pretrained
return cls._from_pretrained(
^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2505, in _from_pretrained
tokenizer = cls(*init_inputs, **init_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/models/gpt2/tokenization_gpt2_fast.py", line 99, in init
super().init(
File "/dccstor/askpower/conda_askp/envs/askpenv/lib/python3.11/site-packages/transformers/tokenization_utils_fast.py", line 115, in init
fast_tokenizer = TokenizerFast.from_file(fast_tokenizer_file)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Exception: data did not match any variant of untagged enum ModelWrapper at line 244953 column 3
Any help would be appreciated