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
Tokenizer issue on Colab
Hi I'm trying to run it on Colab using the Transformers Package as instructed in the Model Card
but I'm getting this error
ValueError Traceback (most recent call last)
in <cell line: 3>()
1 from transformers import AutoTokenizer, AutoModelForCausalLM
2
----> 3 tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b")
4 model = AutoModelForCausalLM.from_pretrained("google/gemma-2b")
5
/usr/local/lib/python3.10/dist-packages/transformers/models/auto/tokenization_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
809 tokenizer_class.register_for_auto_class()
810 return tokenizer_class.from_pretrained(
--> 811 pretrained_model_name_or_path, *inputs, trust_remote_code=trust_remote_code, **kwargs
812 )
813 elif config_tokenizer_class is not None:
ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.
Hi there! Could you make sure to pip install the latest transformers version please?
pip install -U transformers
This will update the transformers to the latest version and then you can download it
pip install -U transformers
This will update the transformers to the latest version and then you can download it
Nope. Still the same.
I've tried the following ways to update the Transformers:
!pip install -U transformers huggingface_hub -q
!pip install git+https://github.com/huggingface/transformers -q
On both I'm getting this:
ValueError Traceback (most recent call last)
in <cell line: 3>()
1 from transformers import AutoTokenizer, AutoModelForCausalLM
2
----> 3 tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b")
ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.
Hi there! Could you make sure to pip install the latest transformers version please?
Sorry. Still the same issue. I've tried the following ways to update the Transformers:
!pip install -U transformers huggingface_hub -q
!pip install git+https://github.com/huggingface/transformers -q
Hi there!
I fixed this error by using the demo code of the GPU one. I think the CPU demo code has an error about these three lines:
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(**input_text, return_tensors="pt")
outputs = model.generate(input_ids)
which should be updated to :
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)
Hi there!
By running this code:
input_ids = tokenizer(**input_text, return_tensors="pt").to("cuda")
I got this error:
TypeError: GemmaTokenizerFast(name_or_path='google/gemma-2b', vocab_size=256000, model_max_length=1000000000000000019884624838656, is_fast=True, padding_side='left', truncation_side='right', special_tokens={'bos_token': '', 'eos_token': '', 'unk_token': '', 'pad_token': ''}, clean_up_tokenization_spaces=False), added_tokens_decoder={
0: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
1: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
2: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
3: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
} argument after ** must be a mapping, not str
@arslankas Did you restart the session after updating the transformers ? Just restart the session after updating the transformers and it must run fine
@arslankas Did you restart the session after updating the transformers ? Just restart the session after updating the transformers and it must run fine
Oops. Thank you. After restarting the session I ran into another error:
TypeError Traceback (most recent call last)
in <cell line: 10>()
8
9 input_text = "Write me a poem about Machine Learning."
---> 10 input_ids = tokenizer(**input_text, return_tensors="pt")
11
12 outputs = model.generate(input_ids)
TypeError: GemmaTokenizerFast(name_or_path='google/gemma-2b', vocab_size=256000, model_max_length=1000000000000000019884624838656, is_fast=True, padding_side='left', truncation_side='right', special_tokens={'bos_token': '', 'eos_token': '', 'unk_token': '', 'pad_token': ''}, clean_up_tokenization_spaces=False), added_tokens_decoder={
0: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
1: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
2: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
3: AddedToken("", rstrip=False, lstrip=False, single_word=False, normalized=False, special=True),
} argument after ** must be a mapping, not str
@arslankas
I think you can just try to replace your code:
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(**input_text, return_tensors="pt")
outputs = model.generate(input_ids)
to :
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)
It works for me.
@junchenfu yah that's the fix for that issue. You need to pass in the tensor to the model.generate()
@arslankas
I think you can just try to replace your code:
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(**input_text, return_tensors="pt")
outputs = model.generate(input_ids)
to :
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)It works for me.
Thank you it works :) :)
@arslankas
I think you can just try to replace your code:
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(**input_text, return_tensors="pt")
outputs = model.generate(input_ids)
to :
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)It works for me.
Is there a way to increase the response? All I get is just 3,4 words as output
Edit: After increasing max_new_tokens=150 the model keeps loading on....kinda stuck at this point
Hi all! Closing this discussion as the tokenization issue is resolved. Feel free to begin a new discussion if you want to discuss ways to speed up generation.
same issue, but on my windows labtop
ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.
Hi there, please make sure to upgrade the transformers version to the latest with pip install -U transformers
The official huggingface TGI Docker image at version 1.4.2 (released yesterday) is also hitting this (ghcr.io/huggingface/text-generation-inference:1.4.2)
(I poked the GitHub as well: https://github.com/huggingface/text-generation-inference/issues/1590#issuecomment-1960042694)