[Gemma 2]Using With LocalGemma
Copyright 2024 Google LLC.
Getting Started with Gemma 2 and Local Gemma
Gemma is a family of lightweight, state-of-the-art open-source language models from Google. Built from the same research and technology used to create the Gemini models, Gemma models are text-to-text, decoder-only large language models (LLMs), available in English, with open weights, pre-trained variants, and instruction-tuned variants. Gemma models are well-suited for various text-generation tasks, including question-answering, summarization, and reasoning. Their relatively small size makes it possible to deploy them in environments with limited resources such as a laptop, desktop, or your cloud infrastructure, democratizing access to state-of-the-art AI models and helping foster innovation for everyone.
Local Gemma provides an easy and fast way to run Gemma-2 locally from your CLI or via a Python library. The Huggingface's Transformers and bitsandbytes libraries serve as the foundation for it.
In this notebook, you will learn how to run Gemma 2 models using Local Gemma in a Google Colab environment. You'll install the necessary packages, set up the model, and run a sample prompt.
Setup
Select the Colab runtime
To complete this tutorial, you'll need to have a Colab runtime with sufficient resources to run the Gemma model. In this case, you can use a T4 GPU:
- In the upper-right of the Colab window, select ▾ (Additional connection options).
- Select Change runtime type.
- Under Hardware accelerator, select T4 GPU.
Gemma setup
Before you dive into the tutorial, let's get you set up with Gemma:
- Hugging Face Account: If you don't already have one, you can create a free Hugging Face account by clicking here.
- Gemma Model Access: Head over to the Gemma model page and accept the usage conditions.
- Colab with Gemma Power: For this tutorial, you'll need a Colab runtime with enough resources to handle the Gemma 2B model. Choose an appropriate runtime when starting your Colab session.
- Hugging Face Token: Generate a Hugging Face access (preferably
writepermission) token by clicking here. You'll need this token later in the tutorial.
Once you've completed these steps, you're ready to move on to the next section where you'll set up environment variables in your Colab environment.
Configure your HF token
Add your Hugging Face token to the Colab Secrets manager to securely store it.
- Open your Google Colab notebook and click on the 🔑 Secrets tab in the left panel.

- Create a new secret with the name
HF_TOKEN. - Copy/paste your token key into the Value input box of
HF_TOKEN. - Toggle the button on the left to allow notebook access to the secret.
Install dependencies
You'll need to install a few Python packages and dependencies for Local Gemma and interact with HuggingFace.
Run the following cell to install or upgrade it:
Run Local Gemma directly
If you want to chat with the Gemma-2 through an interactive session, run the local-gemma command directly. If you are running this command for the first time, it will download the model to the cache.
local-gemma command comes with a set of options. You will use --model and --silent here.
| Option | Details |
|---|---|
--model | Size of Gemma 2 instruct model to be used in the application ('2b', '9b', or '27b') or, a Hugging Face repo. Defaults to '9b'. |
--silent | Does NOT print any output except for the model outputs. |
Note:
Input !exit to leave the program and input !new session to reset the conversation.
Use local-gemma to run the 2b version of Gemma 2 in command line.
To know about all the available options for local-gemma, run the following command.
Alternatively, you can pass a prompt to generate a single response from the model.
!local-gemma <prompt>
Running the Gemma 2 model using the local_gemma library
You can use Local Gemma in your Python code using the local_gemma library.
You will initialize the LocalGemma2ForCausalLM from the local_gemma library by loading a pre-trained Gemma 2 model from HuggingFace. Here's what each argument of from_pretrained does:
pretrained_model_name_or_path: Path to the model.preset: Sets the optimization strategy for the local model deployment. Defaults to 'auto', which selects the best strategy for your device.
For this example, you will use the 9b version of Gemma 2.
Interacting with the Gemma model
Finally, you can prompt the Gemma 2 model to generate a response.
To do this, you will first tokenize the input prompt by initializing the tokenizer for the selected model(google/gemma-2-9b) and passing the prompt as its argument. To learn more about tokenizer and its arguments, read HuggingFace's Tokenizer documentation.
You can then query the model using the tokenized prompt. The response generated by the model is tokenized. It must be decoded into sentences.
To learn more about the Python usage of Local Gemma, visit Local Gemma's GitHub page.
Conclusion
Congratulations! You've successfully set up the Gemma 2 model using Local Gemma in a Colab environment. You can now experiment with the model, generate text, and explore its capabilities.
Run in Google Colab