AI Workflows

LLM Fine-Tuning Tutorial for Gemma Model

Learn step-by-step how to fine-tune the Gemma model for specific tasks using Hugging Face tools.

5 min read

Fine-tuning is a critical step in customizing large language models (LLMs) for a specific task. In this tutorial, we'll walk you through the process of fine-tuning the Gemma 327M parameter model, designed for lightweight use cases, using Hugging Face tools and techniques. This guide covers data preparation, fine-tuning, executing batch inference, and deployment to Hugging Face Spaces.

Understanding Fine-Tuning

Fine-tuning involves adapting a pre-trained model to a specific downstream task using a smaller, labeled dataset. Its benefits include:

  • Full ownership and control over the model.
  • Keeping sensitive data local instead of relying on APIs.
  • Enhanced performance for narrowly defined tasks.

The Gemma 327M model is suitable for fine-tuning due to its relatively small size and instruction-following capability, making it ideal for efficient customizations.

Prerequisites for Fine-Tuning

Before fine-tuning your Gemma model, make sure you meet the following requirements:

prerequisites

  • Install Hugging Face Transformers (pip install transformers), Datasets (pip install datasets), Accelerate (pip install accelerate), Hugging Face Hub (pip install huggingface_hub), and Gradio (pip install gradio) libraries.
  • Prepare a labeled dataset in JSON format or another compatible structure.
  • Ensure access to sufficient resources: a GPU with at least 16GB VRAM (e.g., Nvidia T4 or higher). Google Colab is an excellent free option.

Steps to Fine-Tune the Gemma Model

Fine-tuning can be broken into systematic steps:

1. Set Up Your Environment

Install all necessary libraries using the following commands:

Install Python Libraries

pip install transformers datasets accelerate gradio huggingface_hub

Additionally, ensure that your system can detect the GPU for hardware acceleration:

python
import torch
print(torch.cuda.is_available())  # Should return True
print(torch.cuda.get_device_name(0))  # Prints GPU model

2. Load the Base Model and Tokenizer

The Gemma model can be loaded from Hugging Face:

python
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "Gemma-327M"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

3. Prepare the Data

Format the training data by aligning it with the Gemma model’s structure. Here’s an example of how to create LLM-style inputs and outputs:

python
from datasets import load_dataset

# Load dataset
dataset = load_dataset("json", data_files={"train": "train.json", "validation": "validation.json"})

# Example function to format dataset samples
def format_sample(sample):
    input_text = sample["input"]
    output_text = sample["output"]
    return {"input": input_text, "output": output_text}

# Apply formatting
formatted_dataset = dataset.map(format_sample)

4. Fine-Tune the Model

Use the Hugging Face Trainer and Accelerate libraries to fine-tune the Gemma model.

python
from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir="./results",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=8,
    per_device_eval_batch_size=8,
    num_train_epochs=3,
    weight_decay=0.01,
    save_strategy="epoch",
    logging_dir="./logs",
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=formatted_dataset["train"],
    eval_dataset=formatted_dataset["validation"],
    tokenizer=tokenizer
)

trainer.train()

5. Test the Model

Evaluate the model on a separate test dataset.

python
test_results = trainer.evaluate()
print("Results on Test Set:", test_results)

6. Save and Load the Fine-Tuned Model

After success, save the fine-tuned model:

python
model.save_pretrained("fine_tuned_gemma")
tokenizer.save_pretrained("fine_tuned_gemma")

To later load the model:

python
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

finetuned_model = AutoModelForCausalLM.from_pretrained("fine_tuned_gemma")
finetuned_tokenizer = AutoTokenizer.from_pretrained("fine_tuned_gemma")
finetuned_pipeline = pipeline("text-generation", model=finetuned_model, tokenizer=finetuned_tokenizer)

output = finetuned_pipeline("Sample input text")
print(output)

Optimizing Batch Inference

Performing inference in batches can significantly increase processing speed. Here’s an example configuration:

python
from transformers import TextGenerationPipeline

# Batch inference
batch_generator = TextGenerationPipeline(model=finetuned_model, tokenizer=finetuned_tokenizer)

inputs = [
    "Text sample 1",
    "Text sample 2",
    "Text sample 3",
]
batch_size = 2

outputs = batch_generator(inputs, batch_size=batch_size)
print(outputs)

Batch inference can reduce latency and improve processing efficiency for large workloads. Experiment with different batch sizes based on your hardware capacity.

Deploying the Fine-Tuned Model

Once fine-tuned, you can upload your model to the Hugging Face Hub and build a custom demo using Gradio.

Uploading to Hugging Face

Save your Hugging Face authentication token locally for easy uploads:

bash
huggingface-cli login

Make sure the folder path exists and points to a directory containing the saved model, e.g., the folder "fine_tuned_gemma", then run the upload script:

python
from huggingface_hub import HfApi
import os

folder_path = "./fine_tuned_gemma"
if not os.path.isdir(folder_path):
    raise ValueError(f"Provided path: '{folder_path}' is not a directory")

api = HfApi()
api.upload_folder(
    repo_id="your-username/your-finetuned-gemma-model",
    folder_path=folder_path
)

Creating a Gradio-Powered Demo

Write an app using Gradio:

python
import gradio as gr

def predict(input_text):
    result = finetuned_pipeline(input_text)
    return result[0]["generated_text"]

demo = gr.Interface(
    fn=predict, 
    inputs="text", 
    outputs="text", 
    description="Try out our fine-tuned Gemma model for text predictions!"
)

demo.launch()

Host the Gradio app locally or upload it to Hugging Face Spaces for the community to access.

Next Steps for Model Improvement

FAQ

What is fine-tuning a language model?

Fine-tuning involves retraining a pre-trained language model on task-specific data to adapt it to a new use case, leveraging the existing knowledge in the model while tailoring it to your requirements.

Why should I fine-tune the Gemma model?

The Gemma model is small (327M parameters) and lightweight, making it efficient for local computations, quick training times, and deployment in resource-constrained environments.

Can I fine-tune Gemma on Google Colab?

Yes! Ensure you have a GPU runtime enabled in Colab and reduce the batch size to fit into the provided memory. Follow this guide to successfully fine-tune it.

Is the fine-tuned model free to share?

Yes, if you make it public on the Hugging Face Hub. You can also set up private repositories if the data or model is sensitive.


Official reference: Hugging Face documentation.