Instructions to use google/gemma-7b-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-7b-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="google/gemma-7b-it") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it") model = AutoModelForCausalLM.from_pretrained("google/gemma-7b-it") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - llama-cpp-python
How to use google/gemma-7b-it with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="google/gemma-7b-it", filename="gemma-7b-it.gguf", )
llm.create_chat_completion( messages = [ { "role": "user", "content": "What is the capital of France?" } ] ) - Inference
- Local Apps Settings
- llama.cpp
How to use google/gemma-7b-it with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf google/gemma-7b-it # Run inference directly in the terminal: llama cli -hf google/gemma-7b-it
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf google/gemma-7b-it # Run inference directly in the terminal: llama cli -hf google/gemma-7b-it
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-7b-it # Run inference directly in the terminal: ./llama-cli -hf google/gemma-7b-it
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-7b-it # Run inference directly in the terminal: ./build/bin/llama-cli -hf google/gemma-7b-it
Use Docker
docker model run hf.co/google/gemma-7b-it
- LM Studio
- Jan
- vLLM
How to use google/gemma-7b-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-7b-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-7b-it", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/google/gemma-7b-it
- SGLang
How to use google/gemma-7b-it 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-7b-it" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-7b-it", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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-7b-it" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-7b-it", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Ollama
How to use google/gemma-7b-it with Ollama:
ollama run hf.co/google/gemma-7b-it
- Unsloth Studio
How to use google/gemma-7b-it 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-7b-it 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-7b-it 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-7b-it to start chatting
- Atomic Chat new
- Docker Model Runner
How to use google/gemma-7b-it with Docker Model Runner:
docker model run hf.co/google/gemma-7b-it
- Lemonade
How to use google/gemma-7b-it with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull google/gemma-7b-it
Run and chat with the model
lemonade run user.gemma-7b-it-{{QUANT_TAG}}List all available models
lemonade list
Could not find GemmaForCausalLM neither in <module 'transformers.models.gemma'
[Could not find GemmaForCausalLM neither in <module 'transformers.models.gemma'](Name: transformers
Version: 4.38.1
Summary: State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow
Home-page: https://github.com/huggingface/transformers
Author: The Hugging Face team (past and future) with the help of all our contributors (https://github.com/huggingface/transformers/graphs/contributors)
Author-email: transformers@huggingface.co
License: Apache 2.0 License
Location: C:\Users\Administrator\AppData\Local\NVIDIA\MiniConda\envs\gcn\Lib\site-packages
Requires: filelock, huggingface-hub, numpy, packaging, pyyaml, regex, requests, safetensors, tokenizers, tqdm)
I have already used all the specified versions, but it still doesn't work
Why doesn't it work no matter how I update it? Is everyone else experiencing the same issue?
Hi @chenwei1984
Thanks for the issue - there might be some weird conflict in your environment. Can you try to re-install transformers in a fresh new env?
i sure is new
Hi @chenwei1984 , Could you please provide more details on the issue? Like what are all the steps you have tried along with reproducible code to replicate the error to better understand the issues. Thank you
I'm having the same issue (ricc@ - go/ricc-AIWC-C1-S1-fl ) .
input from https://huggingface.co/google/gemma-2-9b:
# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-9b")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-2-9b",
device_map="auto",
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids, max_new_tokens=32)
print(tokenizer.decode(outputs[0]))
output:
Traceback (most recent call last):
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 701, in getattribute_from_module
return getattribute_from_module(transformers_module, attr)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 705, in getattribute_from_module
raise ValueError(f"Could not find {attr} in {transformers_module}!")
ValueError: Could not find Gemma2ForCausalLM in <module 'transformers' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/__init__.py'>!
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/utils/import_utils.py", line 1603, in _get_module
return importlib.import_module("." + module_name, self.__name__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/generation/utils.py", line 119, in <module>
from accelerate.hooks import AlignDevicesHook, add_hook_to_module
File "/home/ricc/accelerate.py", line 7, in <module>
model = AutoModelForCausalLM.from_pretrained(
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 563, in from_pretrained
model_class = _get_model_class(config, cls._model_mapping)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 384, in _get_model_class
supported_models = model_mapping[type(config)]
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 735, in __getitem__
return self._load_attr_from_module(model_type, model_name)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 749, in _load_attr_from_module
return getattribute_from_module(self._modules[module_name], attr)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 703, in getattribute_from_module
raise ValueError(f"Could not find {attr} neither in {module} nor in {transformers_module}!")
ValueError: Could not find Gemma2ForCausalLM neither in <module 'transformers.models.gemma2' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/gemma2/__init__.py'> nor in <module 'transformers' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/__init__.py'>!
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/utils/import_utils.py", line 1603, in _get_module
return importlib.import_module("." + module_name, self.__name__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/gemma2/modeling_gemma2.py", line 37, in <module>
from ...modeling_utils import PreTrainedModel
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/modeling_utils.py", line 46, in <module>
from .generation import GenerationConfig, GenerationMixin
File "<frozen importlib._bootstrap>", line 1039, in _handle_fromlist
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/utils/import_utils.py", line 1593, in __getattr__
module = self._get_module(self._class_to_module[name])
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/utils/import_utils.py", line 1605, in _get_module
raise RuntimeError(
RuntimeError: Failed to import transformers.generation.utils because of the following error (look up to see its traceback):
Could not find Gemma2ForCausalLM neither in <module 'transformers.models.gemma2' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/gemma
2/__init__.py'> nor in <module 'transformers' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/__init__.py'>!
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "accelerate.py", line 7, in <module>
model = AutoModelForCausalLM.from_pretrained(
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 563, in from_pretrained
model_class = _get_model_class(config, cls._model_mapping)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 384, in _get_model_class
supported_models = model_mapping[type(config)]
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 735, in __getitem__
return self._load_attr_from_module(model_type, model_name)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 749, in _load_attr_from_module
return getattribute_from_module(self._modules[module_name], attr)
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/auto/auto_factory.py", line 693, in getattribute_from_module
if hasattr(module, attr):
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/utils/import_utils.py", line 1593, in __getattr__
module = self._get_module(self._class_to_module[name])
File "/home/ricc/.venv/lib/python3.8/site-packages/transformers/utils/import_utils.py", line 1605, in _get_module
raise RuntimeError(
RuntimeError: Failed to import transformers.models.gemma2.modeling_gemma2 because of the following error (look up to see its traceback):
Failed to import transformers.generation.utils because of the following error (look up to see its traceback):
Could not find Gemma2ForCausalLM neither in <module 'transformers.models.gemma2' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/models/gemma
2/__init__.py'> nor in <module 'transformers' from '/home/ricc/.venv/lib/python3.8/site-packages/transformers/__init__.py'>!
@palladius , It seems, there is version incompatibility issue between the installed libraries in your system to run this Gemma model. You are using older python version 3.8 to run the Gemma model. Please try running the model again after installing the Python version 3.10 and let us know if the issue still persists.