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
GemmaTokenizer does not exist or is not currently imported
ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.
from transformers import AutoTokenizer, AutoModelForCausalLM
from huggingface_hub import login
#from huggingface_hub import snapshot_download
#snapshot_download(repo_id="google/gemma-2b")
login("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")
model_path = "/Users/macbook/.cache/huggingface/hub/models--google--gemma-2b/snapshots/9d067f00def958594aaa16b39a65b07d69ca655b"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
same error for me as well
ValueError Traceback (most recent call last)
in <cell line: 4>()
2 from transformers import AutoTokenizer, AutoModelForCausalLM
3
----> 4 tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b")
5 model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto")
6
/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.
reinstall transformers
pip uninstall transformers
pip install transformers
pip install -U transformers should solve the issue. If not, make sure to create a fresh new env
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b", use_auth_token=True)
model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto")
''''''''
tokenizer not imported in this and below error
already updated tokenizer but got same error
ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.
Hi there! Did you upgrade transformers by doing pip install -U transformer?
did but got the same error as below:
ValueError Traceback (most recent call last)
in <cell line: 5>()
3
4 # tokenizer = GemmaTokenizer.from_preset("google/gemma-2b", use_auth_token=True)
----> 5 tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b", use_auth_token=True)
6 model = AutoModelForCausalLM.from_pretrained("google/gemma-2b", device_map="auto")
/usr/local/lib/python3.10/dist-packages/transformers/models/auto/tokenization_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)
697 Will be passed to the Tokenizer __init__() method. Can be used to set special tokens like
698 bos_token, eos_token, unk_token, sep_token, pad_token, cls_token, mask_token,
--> 699 additional_special_tokens. See parameters in the __init__() for more details.
700
701 Examples:
ValueError: Tokenizer class GemmaTokenizer does not exist or is not currently imported.
I am running in google colab, which has python 3.10
issue resolved, after updating python from 3.10 to 3.11
Hi, just restart the session again but before that,
remove any pip install transformers command and replace it with below line.
!pip -q install git+https://github.com/huggingface/transformers.git
Install other packages like trl or peft seperately using !pip install trl peft
It started working for me..
it worked for me too! thanks
it worked for me too! thanks
Hey! Can you please share your colab code? I'm getting multiple errors, it will be helpful thanks
reinstall transformers
pip uninstall transformers pip install transformers
After reinstallation, the versation of transformers still keep same
transformers-4.30.2
# python
Python 3.7.3
@WQW You need to upgrade it
pip install --upgrade transformers
A simple install will used the cached version of your already installed package
Colab comes with an older transformers version pre-installed. If you install/upgrade the last version, you will need to restart the runtime so it picks the newly installed version
pip install transformers==4.38.2 and it works