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
Can't reproduce hellaswag result - getting 42.3% v.s. 71.4 % reported
Hi! hope all is good.
I'm trying to reproduce the hellaswag result obtained through lm-evaluation-harness. Following the discussion from https://huggingface.co/google/gemma-2b/discussions/18, I:
- Pulled lm-evaluation-harness form commit b281b0921b636bc36ad05c0b0b0763bd6dd43463 and set it up in a fresh conda environment
- Ran:
$ python main.py --model hf-causal-experimental --model_args pretrained=google/gemma-2b,dtype=float32 --tasks hellaswag --device cuda:0 --batch_size 1
- Got the following results:
{
"results": {
"hellaswag": {
"acc": 0.34116709818761204,
"acc_stderr": 0.0047313244091332675,
"acc_norm": 0.42342162915753834,
"acc_norm_stderr": 0.004930911515084784
}
},
"versions": {
"hellaswag": 0
},
"config": {
"model": "hf-causal-experimental",
"model_args": "pretrained=google/gemma-2b,dtype=float32",
"num_fewshot": 0,
"batch_size": "1",
"batch_sizes": [],
"device": "cuda:0",
"no_cache": false,
"limit": null,
"bootstrap_iters": 100000,
"description_dict": {}
}
}
hf-causal-experimental (pretrained=google/gemma-2b,dtype=float32), limit: None, provide_description: False, num_fewshot: 0, batch_size: 1
| Task |Version| Metric |Value | |Stderr|
|---------|------:|--------|-----:|---|-----:|
|hellaswag| 0|acc |0.3412|± |0.0047|
| | |acc_norm|0.4234|± |0.0049|
Am I doing something obviously wrong? As can be seen from the output, I'm getting an accuracy of 42.3%. However, the paper reports an accuracy of 71.4% in hellaswag (similar to the 71.77 listed in the open llm leaderboard: https://huggingface.co/spaces/open-llm-leaderboard-old/open_llm_leaderboard).
Thanks in advance!
Taking a deeper look at the results from the open llm leaderboard (https://huggingface.co/spaces/open-llm-leaderboard-old/open_llm_leaderboard, https://huggingface.co/datasets/open-llm-leaderboard-old/details_google__gemma-2b), from those links, if I'm reading this correctly, it seems like the 71.77% accuracy listed in the open llm leaderboard for hellaswag was obtained using 10 few-shot examples per:
...
"harness|hellaswag|10": {
"hashes": {
"hash_examples": "e1768ecb99d7ecf0",
"hash_full_prompts": "0b4c16983130f84f",
"hash_input_tokens": "11490eb47260730b",
"hash_cont_tokens": "6a8516a792e1673e"
},
"truncated": 0,
"non_truncated": 10042,
"padded": 40055,
"non_padded": 113,
"effective_few_shots": 10.0,
"num_truncated_few_shots": 0
},
...
Is this read correct?
Also, as noted above, the paper (https://arxiv.org/pdf/2403.08295) and its hugging face page (https://huggingface.co/google/gemma-2b) list a similar accuracy (71.4%) for hellaswag, but note that it was obtained using 0-shot. Is there any way to possibly replicate the 0-shot results listed there through lm-eval-harness or lighteval?
Thanks in advance