Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 5 min read How-to mediumAdept

Fine-Tuning Gemma Models in Hugging Face

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.

Assumes: PyTorch basicsHugging Face TransformersLLM fine-tuning concepts

Related posts

  • Welcome Gemma 2 - Google’s new open LLM

    Hugging Face mediumAdept

    Gemma 2 ships in 9B and 27B sizes (base and instruction-tuned) with an 8K context, trained on 8T and 13T tokens respectively. Architecturally it interleaves 4096-token sliding-window attention with global attention on alternating layers, applies tanh logit soft-capping (50.0 for attention, 30.0 for final logits), pre-trains the 9B via knowledge distillation, uses on-policy distillation in post-training, and merges models with WARP. Practical notes: soft-capping is incompatible with Flash Attention/SDPA, so fine-tune with eager attention, and run the 27B instruct model in bfloat16 because float16 gives erratic output. Covers Transformers usage (about 18 GB for the 9B model, and 4-bit loading of the 27B in about 18 GB), QLoRA fine-tuning with TRL, and TGI deployment.

  • LoRA Explained: Faster, More Efficient Fine-Tuning with Docker

    Docker mediumAdept

    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. The walkthrough covers formatting supervised examples with the model's chat template so training data matches inference input, configuring PEFT adapters (r=16, alpha=32 on q_proj/v_proj), training with TRL's SFTConfig via Unsloth, and merging adapters into a standalone 16-bit checkpoint. The merged model can then be packaged and shared through Docker Model Runner and Docker Hub.

  • Welcome Gemma - Google’s new open LLM

    Hugging Face mediumAdept

    Google's Gemma open LLM family ships in 2B and 7B sizes, each with base and instruction-tuned variants and an 8K context. On the Open LLM Leaderboard Gemma 7B scores 63.75, comparable to Mistral 7B, while Gemma 2B trails Phi-2. Hugging Face supports it through Transformers 4.38, including bfloat16 inference in about 18 GB, 4-bit loading in about 9 GB, and a ~4x speedup with torch.compile. Other integrations include JAX/Flax weights, Vertex AI/GKE and Inference Endpoints deployment via Text Generation Inference with an OpenAI-compatible Messages API, and a QLoRA fine-tuning recipe with TRL that takes about 9 hours on one A10G.

  • Parameter-Efficient Fine-Tuning using 🤗 PEFT

    Hugging Face mediumAdept

    Hugging Face's PEFT library brings parameter-efficient fine-tuning methods (LoRA, Prefix Tuning, Prompt Tuning, P-Tuning) to Transformers and Accelerate. Most pretrained weights stay frozen and only a small set of extra parameters is trained, which gives performance comparable to full fine-tuning at a fraction of the compute and storage cost. A LoRA config on mt0-large trains about 0.19% of the parameters, and the saved adapters are only a few MB (19MB for T0_3B) instead of 40GB per-task checkpoints. Example use cases include tuning T0_3B on 11GB consumer GPUs with DeepSpeed offload, INT8 tuning of OPT-6.7b in Colab with bitsandbytes, and Stable Diffusion Dreambooth, plus code for training, saving and loading adapters.

  • Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL

    Hugging Face mediumAdept

    Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation. Because no approximations are made, it claims 0% accuracy loss relative to standard QLoRA. Across 59 benchmark runs on A100 and T4 GPUs, it ran 1.5–3.9x faster than Hugging Face transformers and used up to 74% less VRAM. Code examples show loading pre-quantized 4-bit models with FastLanguageModel, attaching LoRA adapters, and training with TRL's SFTTrainer or DPOTrainer.

The quest for software engineering knowledge.

Look:
Layout: