Instructions to use google/gemma-7b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-7b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="google/gemma-7b")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b") model = AutoModelForCausalLM.from_pretrained("google/gemma-7b") - llama-cpp-python
How to use google/gemma-7b with llama-cpp-python:
# !pip install llama-cpp-python from llama_cpp import Llama llm = Llama.from_pretrained( repo_id="google/gemma-7b", filename="gemma-7b.gguf", )
output = llm( "Once upon a time,", max_tokens=512, echo=True ) print(output)
- Inference
- Local Apps Settings
- llama.cpp
How to use google/gemma-7b 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 # Run inference directly in the terminal: llama cli -hf google/gemma-7b
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf google/gemma-7b # Run inference directly in the terminal: llama cli -hf google/gemma-7b
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 # Run inference directly in the terminal: ./llama-cli -hf google/gemma-7b
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 # Run inference directly in the terminal: ./build/bin/llama-cli -hf google/gemma-7b
Use Docker
docker model run hf.co/google/gemma-7b
- LM Studio
- Jan
- vLLM
How to use google/gemma-7b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-7b" # 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-7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/google/gemma-7b
- SGLang
How to use google/gemma-7b 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" \ --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-7b", "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-7b" \ --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-7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Ollama
How to use google/gemma-7b with Ollama:
ollama run hf.co/google/gemma-7b
- Unsloth Studio
How to use google/gemma-7b 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 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 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 to start chatting
- Atomic Chat new
- Docker Model Runner
How to use google/gemma-7b with Docker Model Runner:
docker model run hf.co/google/gemma-7b
- Lemonade
How to use google/gemma-7b with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull google/gemma-7b
Run and chat with the model
lemonade run user.gemma-7b-{{QUANT_TAG}}List all available models
lemonade list
Weird Performance Issue with Gemma-7b compared to Gemma-2b with Qlora
Hey,
I've been working with the Gemma-7b with QLoRA. I tried training Gemma-7b and Gemma-2b with Qlora.
Here's the weird part: Gemma-2b performed better than Gemma-7b after the fine tuning. I double-checked my training setup and hyperparameters, and they were the same for both models.
I'm trying to figure out why this is happening. Is it
I tested the models on a few different datasets, and the smaller one still comes out on top. I'm not sure if I'm missing something obvious or if there's some weird with Gemma-7b architecture.
If anyone has an idea of what is happening here I will appreciate their input.
Thanks!
May I know your prompt format for both models?
Could you please try the following prompt format?
def prompt_template(query):
"""
Prompt Template
:param query: User Input Question
:return: Prompt Template with query
"""
template = f"""<start_of_turn>user\n{query}<end_of_turn>\n<start_of_turn>model\n"""
return template
Hey! Surya from the Gemma team -- we haven't tried internal QLoRA finetunes, so I'm not sure how our models perform with it. The difference between the 2B and 7B is also interesting -- we'll try to test both internally and see if we notice anything on our side.
As @shuyuej suggested, can you share some of the outputs you're getting, and confirm if you're using the right prompt template?
Thank you @shuyuej and @suryabhupa for your prompt response
here is the prompt I was using are use the exact same code for both models
# define some variables - model names
model_name = "google/gemma-7b"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type='nf4',
bnb_4bit_compute_dtype=torch.float32,
# load_in_8bit = True,
bnb_4bit_use_double_quant=True,
)
# Load base model
model = AutoModelForCausalLM.from_pretrained(
model_name,
# token=hf_token,
quantization_config=bnb_config,
device_map='auto',
torch_dtype=torch.float32,
config = AutoConfig.from_pretrained(model_name, hidden_activation='gelu_pytorch_tanh')
)
model.config.use_cache = False
model.config.pretraining_tp = 1
tokenizer = AutoTokenizer.from_pretrained(model_name,
# token=hf_token,
trust_remote_code=False)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right" # Fix weird overflow issue with fp16 training
tokenizer.bos_token_id = 1
prompt_format = """### Instruction:
{}
### Question:
{}
### Answer:
{}"""
EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
BOS_TOKEN = tokenizer.bos_token
def formatting_prompts_func(examples):
instructions = ["Given a question, classify it into one of two categories: Yes or No." for i in examples["question"]]
inputs = [record_to_gpt3_instance(_inst) for _inst in examples["question"]]
outputs = examples["answer"]
texts = []
for instruction, input, output in zip(instructions, inputs, outputs):
# Must add EOS_TOKEN, otherwise your generation will go on forever!
text = prompt_format.format(instruction, input, output) + EOS_TOKEN
texts.append(text)
return { "text" : texts}
from datasets import load_dataset
dataset = loaded_dataset_dict['train'] #loaded_dataset_dict("yahma/alpaca-cleaned", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)
Just to give you a context here is the date https://huggingface.co/datasets/social_i_qa I am using as you can see it is a multiple choice question answering and the expected response by the model is of the choices either one A, B, or C
After the fine tuning the Gemma-2b gave me one of the expected choices A, B, or C
But with Gemma-7b it is giving me different behavior here is the output.i am getting
Please use Gemma's specific prompt format during the SFT stage.
https://ai.google.dev/gemma/docs/formatting
Hi guys @shuyuej @suryabhupa I applied your advice by modifying the instruction tuning like the one here: https://ai.google.dev/gemma/docs/formatting
Here is an example from the training data after the formatting:
<bos><start_of_turn>user
Instruction:
Given a context, a question, and three answer choices, select the most appropriate answer.
Context:
Cameron decided to have a barbecue and gathered her friends together.
Question:
How would Others feel as a result?
Options:
A. like attending
B. like staying home
C. a good friend to have
<end_of_turn>
<start_of_turn>model
Answer: A
<end_of_turn><eos>
And here is an example of the formatting for inference
<bos><start_of_turn>user
Instruction:
Given a context, a question, and three answer choices, select the most appropriate answer.
Context:
Tracy didn't go home that evening and resisted Riley's attacks.
Question:
What does Tracy need to do before this?
Options:
A. make a new plan
B. Go home and see Riley
C. Find somewhere to go
<end_of_turn>
<start_of_turn>model
But unfortunately this did not help as the model got biased (and desired) behaviour by just predicting option A here is a sample of the outputs
Answer: A
Answer: A
Answer: A
Answer: A
As mentioned before: Just to give you a context here is the date https://huggingface.co/datasets/social_i_qa I am using as you can see it is a multiple choice question answering and the expected response by the model is of the choices either one A, B, or C
Can you help me with this why I'm getting this behaviour am I doing something wrong??
