Instructions to use google/gemma-2b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-2b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="google/gemma-2b")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b") model = AutoModelForCausalLM.from_pretrained("google/gemma-2b") - llama-cpp-python
How to use google/gemma-2b with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="google/gemma-2b", filename="gemma-2b.gguf", )
output = llm( "Once upon a time,", max_tokens=512, echo=True ) print(output)
- Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- llama.cpp
How to use google/gemma-2b 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-2b # Run inference directly in the terminal: llama-cli -hf google/gemma-2b
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama-server -hf google/gemma-2b # Run inference directly in the terminal: llama-cli -hf google/gemma-2b
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-2b # Run inference directly in the terminal: ./llama-cli -hf google/gemma-2b
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-2b # Run inference directly in the terminal: ./build/bin/llama-cli -hf google/gemma-2b
Use Docker
docker model run hf.co/google/gemma-2b
- LM Studio
- Jan
- vLLM
How to use google/gemma-2b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-2b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-2b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/google/gemma-2b
- SGLang
How to use google/gemma-2b 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-2b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-2b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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-2b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-2b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Ollama
How to use google/gemma-2b with Ollama:
ollama run hf.co/google/gemma-2b
- Unsloth Studio new
How to use google/gemma-2b 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-2b 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-2b 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-2b to start chatting
- Docker Model Runner
How to use google/gemma-2b with Docker Model Runner:
docker model run hf.co/google/gemma-2b
- Lemonade
How to use google/gemma-2b with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull google/gemma-2b
Run and chat with the model
lemonade run user.gemma-2b-{{QUANT_TAG}}List all available models
lemonade list
pretraining Gemma for domain dataset
Hi team,
I would like to pretrain Gemma model with my domain dataset. I wanted to train gemma model with my domain data. But I wanted to train all the parameters not using LoRA.
1.a : does tokenizer learns/add any new tokens(domain specific words which is not present in the training tokenizer) during continued pre-training?
can you please provide any example article to fine-tune full parameters?
Hello! Sorry for the delay.
I'm not sure what you mean by new tokens; you shouldn't need to use any new tokens when finetuning, and you are welcome to use any formatting template you'd like; see our own formatting we use if you'd like, especially as those are natively supported by our tokenizer.
The Zephyr 7B published their finetuning setup here: https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1, and other guides such as https://ai.google.dev/gemma/docs/jax_finetune, https://lightning.ai/lightning-ai/studios/understanding-using-and-finetuning-gemma, https://www.kaggle.com/code/lucamassaron/fine-tune-gemma-7b-it-for-sentiment-analysis exist as well.
@suryabhupa @ybelkada
token which I was referring to "domain specific word(technical word) which is not present in the training tokenizer".
I think, I have used the wrong term in the title and above description. Now, I have changed it.
Basically, I wanted to train the Gemma model with my domain data(like I want to continue to train the Gemma model with my domain data). I wanted to train all parameters in the Gemma model.
Like Google team trained the base Gemma model. I would like to take the base Gemma model and continue to train it with my domain data.
Did you use delimiter <bos> and <eos> during training the base Gemma model?
Hi @Iamexperimenting
Thanks ! I will let @suryabhupa reply here whenever he can as I am not familiar with the Gemma training procedure
Hello! Yes, when constructing batches, I'd recommend having sequences in your pretraining set-up that have and tokens in the right places to delimit sequences, but also to properly construct the attention masks. We also use and tokens when doing so. You should experiment with how exactly you pack your examples into a single batch, I'd recommend checking out the T5, GPT, or PaLM paper for some details on how they did it. You do not need to add any extra tokens here.
@suryabhupa : Based on my understanding GPT 2 inserts <|endoftext|> between documents and then chunk it to have same size (Please correct me here if otherwise). Curious to know how <bos> and <eos> has been set up for Gemma. Lets say seq_len is 3 and we have two documents:
- Hello India
- Hello how are you doing
Will it be done like this
- First add special tokens to entire text corpus like this:
<bos>Hello India<eos><bos>Hello how are you doing<eos> - Split it based on seq len :
<bos>Hello India<eos><bos>Hello- how are you
- doing
<eos>