Instructions to use google/gemma-7b-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-7b-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="google/gemma-7b-it") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it") model = AutoModelForCausalLM.from_pretrained("google/gemma-7b-it") 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]:])) - llama-cpp-python
How to use google/gemma-7b-it with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="google/gemma-7b-it", filename="gemma-7b-it.gguf", )
llm.create_chat_completion( messages = [ { "role": "user", "content": "What is the capital of France?" } ] ) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- llama.cpp
How to use google/gemma-7b-it with llama.cpp:
Install from brew
brew install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf google/gemma-7b-it # Run inference directly in the terminal: llama-cli -hf google/gemma-7b-it
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf google/gemma-7b-it # Run inference directly in the terminal: llama-cli -hf google/gemma-7b-it
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf google/gemma-7b-it # Run inference directly in the terminal: ./llama-cli -hf google/gemma-7b-it
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf google/gemma-7b-it # Run inference directly in the terminal: ./build/bin/llama-cli -hf google/gemma-7b-it
Use Docker
docker model run hf.co/google/gemma-7b-it
- LM Studio
- Jan
- vLLM
How to use google/gemma-7b-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-7b-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-7b-it", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/google/gemma-7b-it
- SGLang
How to use google/gemma-7b-it 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 "google/gemma-7b-it" \ --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": "google/gemma-7b-it", "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 "google/gemma-7b-it" \ --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": "google/gemma-7b-it", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Ollama
How to use google/gemma-7b-it with Ollama:
ollama run hf.co/google/gemma-7b-it
- Unsloth Studio new
How to use google/gemma-7b-it with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for google/gemma-7b-it to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for google/gemma-7b-it to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for google/gemma-7b-it to start chatting
- Docker Model Runner
How to use google/gemma-7b-it with Docker Model Runner:
docker model run hf.co/google/gemma-7b-it
- Lemonade
How to use google/gemma-7b-it with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull google/gemma-7b-it
Run and chat with the model
lemonade run user.gemma-7b-it-{{QUANT_TAG}}List all available models
lemonade list
Bug in logits for BOS token.
For the 7b - it model, both this variant and the new one there is bug in the logits of the first BOS token after inference.
It you look at the output it gives all the weight to token id 2 which is the BOS token itself, in result you'll get zero probabilities for other tokens.
I validated the logits of the next tokens, and they seem to be fine.
You can easily verify it by looking at logits values/ soft max values of the tokenized empty string(the tokenizer will add BOS by default).
It might be a training issue of the base model(wrong token input for prediction).
Hi Izarel, can you share some code to help debug this? that would really help
A reproducible colab would be perfect if possible, even with a couple of examples!
Here is a link to colab - unfortunately it crashes when I try to load the model there - but it should work on NVIDIA GPU/MPS:
https://colab.research.google.com/drive/1ADRUjfO8k8H8OfDeprRrSr1g1hDH6f33?usp=sharing
Here is a screenshot:
Thanks, can you give me a couple days to look into this? Will try and figure out what's going on
Hey Izarel,
You took the time out to put together a colab so I want to be sure you get a proper explanation from me.
I'm currently unsure if this bug is in the weights, in the HF framework, somewhere else in the env like pytorch, or even deeper (though unlikely). I'm trying to reproduce this on flax to see if it persists. If its not there then we know its likely somewhere in the HF Framework or weights on Hf Hub.
I briefly tried reproducing on colab but was getting OOM memories like you were getting, If I can't try this there, It'll take me over a week to get back to you as I' m away from my home and most relevant my GPU desktop.
As someone that's just trying to run a model I apologize for the delay. I'll get back to you with an answer though.
Cheers
No problem - I appreciate the help!
I've been able to repro it both on MPS and on Cuda- though I loaded the model only via hugging face.
Just getting back on this - are there any updates on the matter?
No rush - just wanted to make sure I'm not missing a potential fix.
Thanks again, really appreciate the effort!
Hey! Sorry for the delay on my end. I wasn't able to isolate whether this was a weights issue, framework issue, or quantization issue with what is currently available. However with the Gemma 2 release there is going to be updates for all three as well as some extra checks.
https://blog.google/technology/developers/gemini-gemma-developer-updates-may-2024/
Will a June timeline be alright?
Thanks for the investigation!
Unfortunately, I'm purely using the model for research analysis - I had to compute a metric which depends on the outputs of the model, including the logprobs of the first token.
If there is anything you suggest me to check on my side, I'd be more than willing to do the investigation as well.
I'm specifically interested in this variant since it has a lot of existing evaluations on downstream tasks which I had been using.
However - I do understand that it's not a top priority since a new model version is coming.
I did manage to get results on the 2B-it variant, so I have some measurements for the paper anyway (;
