---
title: "Fine-Tuning Gemma Models in Hugging Face"
description: "Gemma 2B and 7B can be fine-tuned cheaply with LoRA via the Hugging Face PEFT library, loading the base model in 4-bit NF4 with bitsandbytes (QLoRA) so it fits on free Colab GPUs."
url: "https://swe.quest/articles/fine-tuning-gemma-models-in-hugging-face-d432d00965e71590"
canonical: "https://huggingface.co/blog/gemma-peft"
source: "Hugging Face"
published: "2024-02-23"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["model-training","llms","python"]
---

# Fine-Tuning Gemma Models in Hugging Face

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2024-02-23, 5 min read, How-to. Original: [https://huggingface.co/blog/gemma-peft](https://huggingface.co/blog/gemma-peft)

## Summary

Gemma 2B and 7B can be fine-tuned cheaply with LoRA via the Hugging Face PEFT library, loading the base model in 4-bit NF4 with bitsandbytes (QLoRA) so it fits on free Colab GPUs. A worked example adapts all linear projection layers (r=8) and trains with TRL's SFTTrainer on an English quotes dataset, teaching the model to emit a "Quote/Author" format in just 10 steps. For Cloud TPUs, transformers now supports PyTorch/XLA FSDP via SPMD, enabled by adding an fsdp\_config that wraps GemmaDecoderLayer with xla\_fsdp\_v2 and gradient checkpointing.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- PyTorch basics
- Hugging Face Transformers
- LLM fine-tuning concepts ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))

## Keywords

Gemma, LoRA, QLoRA, PEFT, PyTorch/XLA, FSDP, bitsandbytes, TRL

## Related posts

- [Welcome Gemma 2 - Google’s new open LLM](https://swe.quest/articles/welcome-gemma-2-google-s-new-open-llm-cda5c001e5cdbd53.md): Hugging Face · 2024-06-27 · Medium · 11 min read · LLMs, Model training, Inference & serving
  Gemma 2 ships in 9B and 27B sizes (base and instruction-tuned) with an 8K context, trained on 8T and 13T tokens respectively.
- [LoRA Explained: Faster, More Efficient Fine-Tuning with Docker](https://swe.quest/articles/lora-explained-faster-more-efficient-fine-tuning-with-docker-c2d6a36ff181eafe.md): Docker · 2025-10-09 · Medium · 8 min read · Model training, LLMs, Privacy
  Gemma 3 270M can be specialized into a compact PII-masking assistant using LoRA, which freezes the base weights and trains only small low-rank adapters, cutting compute and memory needs while avoiding catastrophic forgetting.
- [Welcome Gemma - Google’s new open LLM](https://swe.quest/articles/welcome-gemma-google-s-new-open-llm-a7d68dd269473a81.md): Hugging Face · 2024-02-21 · Medium · 9 min read · LLMs, Inference & serving, Model training, Python
  Google's Gemma open LLM family ships in 2B and 7B sizes, each with base and instruction-tuned variants and an 8K context.
- [Parameter-Efficient Fine-Tuning using 🤗 PEFT](https://swe.quest/articles/parameter-efficient-fine-tuning-using-peft-5c28b9ec4e65b0a1.md): Hugging Face · 2023-02-10 · Medium · 5 min read · Model training, LLMs, Python
  Hugging Face's PEFT library brings parameter-efficient fine-tuning methods (LoRA, Prefix Tuning, Prompt Tuning, P-Tuning) to Transformers and Accelerate.
- [Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL](https://swe.quest/articles/make-llm-fine-tuning-2x-faster-with-unsloth-and-trl-5db8c0b6a40cf74e.md): Hugging Face · 2024-01-10 · Medium · 5 min read · Model training, LLMs, Performance engineering, Python
  Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
