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
torch.cuda.OutOfMemoryError
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 128.00 MiB (GPU 0; 23.65 GiB total capacity; 5.93 GiB already allocated; 122.56 MiB free; 5.93 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
@ybelkada
~/nlp/lm-evaluation-harness$ python lm_eval/main.py --model=hf --model_args pretrained=google/gemma-2b,load_in_4bit=True --tasks wikitext --batch_size 1
is going OOM not sure what's going on.
same
~/nlp/lm-evaluation-harness$ python lm_eval/main.py --model=hf --model_args pretrained=google/gemma-2b --tasks wikitext --batch_size 1
same with gemma-7b:
File "/home/vincent/miniconda3/envs/pt2.1.0/lib/python3.11/site-packages/transformers/models/gemma/modeling_gemma.py", line 1088, in forward
logits = logits.float()
^^^^^^^^^^^^^^
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 7.81 GiB. GPU 0 has a total capacity of 23.67 GiB of which 6.05 GiB is free. Including non-PyTorch memory, this process has 15.88 GiB memory in use. Of the allocated memory 13.06 GiB is allocated by PyTorch, and 2.52 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
I reduced the max_length but there is still issues with gemma-7b (and gemma-2b is much higher than phi-2)
hf (pretrained=google/gemma-7b,max_length=512), gen_kwargs: (None), limit: None, num_fewshot: None, batch_size: 1
| Tasks | Version | Filter | n-shot | Metric | Value | Stderr | |
|---|---|---|---|---|---|---|---|
| wikitext | 2 | none | None | word_perplexity | 42455038.3994 | ± | N/A |
| none | None | byte_perplexity | 26.6969 | ± | N/A | ||
| none | None | bits_per_byte | 4.7386 | ± | N/A |
hf (pretrained=google/gemma-7b-it,max_length=512), gen_kwargs: (None), limit: None, num_fewshot: None, batch_size: 1
| Tasks | Version | Filter | n-shot | Metric | Value | Stderr | |
|---|---|---|---|---|---|---|---|
| wikitext | 2 | none | None | word_perplexity | 1795.5652 | ± | N/A |
| none | None | byte_perplexity | 4.0602 | ± | N/A | ||
| none | None | bits_per_byte | 2.0216 | ± | N/A |
hf (pretrained=google/gemma-7b,max_length=256), gen_kwargs: (None), limit: None, num_fewshot: None, batch_size: 1
| Tasks | Version | Filter | n-shot | Metric | Value | Stderr | |
|---|---|---|---|---|---|---|---|
| wikitext | 2 | none | None | word_perplexity | 41037962.2523 | ± | N/A |
| none | None | byte_perplexity | 26.5280 | ± | N/A | ||
| none | None | bits_per_byte | 4.7294 | ± | N/A |
hf (pretrained=google/gemma-2b,max_length=512), gen_kwargs: (None), limit: None, num_fewshot: None, batch_size: 1
| Tasks | Version | Filter | n-shot | Metric | Value | Stderr | |
|---|---|---|---|---|---|---|---|
| wikitext | 2 | none | None | word_perplexity | 55.9289 | ± | N/A |
| none | None | byte_perplexity | 2.1223 | ± | N/A | ||
| none | None | bits_per_byte | 1.0857 | ± | N/A |
hf (pretrained=google/gemma-2b-it,max_length=512), gen_kwargs: (None), limit: None, num_fewshot: None, batch_size: 1
| Tasks | Version | Filter | n-shot | Metric | Value | Stderr | |
|---|---|---|---|---|---|---|---|
| wikitext | 2 | none | None | word_perplexity | 242.5852 | ± | N/A |
| none | None | byte_perplexity | 2.7924 | ± | N/A | ||
| none | None | bits_per_byte | 1.4815 | ± | N/A |
Actually, I faced OOM problem when using the DPO trainer for funetuning Gemma-2-2b-it, with 40G memory GPU and batchsize=2. Interesting.
Hi,
Apologies for the delay.
I have successfully reproduced the issue. To resolve it, please enable gradient checkpointing and reduce the batch size and sequence length. For more details, kindly refer to this gist file.
Thank you.
Yeah, that's annoying error.
Hi @vpakarinen ,
Please follow the provided gist file for solving the error, if you still facing an issue. Could you please let us know.
Thank you.