Instructions to use ibm-granite/granite-8b-code-instruct-4k with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ibm-granite/granite-8b-code-instruct-4k with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ibm-granite/granite-8b-code-instruct-4k") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-8b-code-instruct-4k") model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-8b-code-instruct-4k") 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
- vLLM
How to use ibm-granite/granite-8b-code-instruct-4k with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ibm-granite/granite-8b-code-instruct-4k" # 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-8b-code-instruct-4k", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ibm-granite/granite-8b-code-instruct-4k
- SGLang
How to use ibm-granite/granite-8b-code-instruct-4k 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-8b-code-instruct-4k" \ --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-8b-code-instruct-4k", "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-8b-code-instruct-4k" \ --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-8b-code-instruct-4k", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use ibm-granite/granite-8b-code-instruct-4k with Docker Model Runner:
docker model run hf.co/ibm-granite/granite-8b-code-instruct-4k
Input context length
Hello,
I'm just looking for confirmation that I'm understanding things correctly in regards of the input context length of this model.
I'm assuming that it's 4096 due to the hidden layer size seen from the config.json coupled with the model. (https://huggingface.co/ibm-granite/granite-8b-code-instruct/blob/main/config.json) Which has been working for me.
The input context is not directly mentioned outright anywhere I'm use to seeing it being stated. (Near to where one would gain access to it for example.)
If there is something I've missed, let me know. Also cite the info so I can have a look for myself.
Thanks.
I'm finding in their paper that the 8B models were intended for 4096. Half that for the 3B. 8192 for 20B and larger for the granite family. ("Section 3 Model Acrchitecture" - https://arxiv.org/pdf/2405.04324)
yeah, you need to look at context length from this table. Hidden size in the config is a different thing.
Also, the 3b and 8b can be used with infinite length theoretically since it is using RoPE but performance can't be guaranteed since its not trained above 2048 and 4096 (for 3b and 8b).
For the 20b and 34b, since they are trained with absolute position embeddings, you are limited to 8192 tokens.
Thanks for pointing out the addition points you made. And thanks for following up. I think I have what I need for now.