Gemma LLM Library
repository·main·Indexed 26 days ago
https://github.com/google-deepmind/gemmaA JAX-based library from Google DeepMind for using and fine-tuning Gemma open-weight Large Language Models. It supports multi-modal and multi-turn capabilities, including a Hackable Diffusion (HD) adapter for hybrid autoregressive-diffusion workflows, Supervised Fine-Tuning (SFT) with LoRA, and specialized data pipelines for tasks such as PubMedQA and Sudoku.
What's inside gemma
- This package provides the data loading, preprocessing, and dataset-specific pipelines required for the Hackable Diffusion (HD) text Supervised Fine-Tuning (SFT) project. It includes shared utilities for text transformation and specialized pipelines for specific tasks like medical Q&A and puzzle solving.
Overview of the HD Package (Hackable Diffusion Adapter)
mainThehdpackage provides core neural network architectures, custom Flax modules, SFT-specific training losses, and checkpoint formatting utilities designed for hybrid autoregressive-diffusion workflows. It is built to adapt the Gemma backbone for diffusion and localized prefilling tasks.Sudoku dataset directory structure and outputs
mainThe Sudoku preprocessing directory contains the following components:
Scripts:
prepare_sudoku_dataset.sh: Configures Kaggle credentials and fetches the dataset.convert_sudoku.py: Preprocesses CSV raw datasets into Bagz files.sudoku_data.py: Handles dataset and transform configuration using Kauldron pipelines.
Generated Files:
sudoku_train.bagz: Converted training records containing source puzzles and reference solutions.sudoku_eval.bagz: Converted evaluation records.
Explore Gemma-related research projects
mainThe repository contains non-official research projects located in the
gemma/research/directory. One such project is:t5gemma: An encoder/decoder Gemma architecture based on Gemma 2.
Run validation tests for the HD package
mainTo validate core architectures, custom layers, losses, and utilities, run the following command:
pytest gemma/diffusion/hackable_diffusion_adapter/hd/...Install the Hackable Diffusion Adapter
mainTo use the Hackable Diffusion Adapter, you must install the
gemmapackage and thejax[cuda13]dependencies. It is recommended to use Python 3.12 and CUDA 13.Warning: Do not mix CUDA 13 with CUDA 12 packages (such as
jax-cuda12-pluginornvidia-nccl-cu12) as this can cause NCCL errors.# Install from PyPI pip install gemma # Or install from source git clone https://github.com/google-deepmind/gemma.git cd gemma pip install . # Install required JAX dependencies pip install -U jax[cuda13]Evaluate PubMedQA datasets
mainUse the
pubmedqa_eval.pymodule to evaluate model performance on the PubMedQA dataset. This includes extracting structured answers and calculating accuracy or BLEU scores.extract_pubmedqa_answer: Extracts the structured answer (yes/no/maybe) from a model response by searching for the marker:"The answer is: {yes|no|maybe}".PubMedQAAccuracy: Computes the accuracy of extracted answers against ground truth.BLEUScore: Computes smoothed sentence-level BLEU scores between generated and ground-truth text usingsacrebleu.
Train the Hackable Diffusion Adapter
mainTraining is performed using
kauldron.main. To prevent compilation hangs and NCCL errors, specific environment variables must be set.Hardware Recommendation: At least 2 A100s for LoRA training, or 8 A100s for full weight updates.
Run these commands from the parent directory of the
gemmadirectory.# PubMedQA Training env XLA_FLAGS="--xla_disable_hlo_passes=constant_folding" \ NCCL_ALGO="Ring" \ NCCL_PROTO="LL128" \ NCCL_NVLS_ENABLE="0" \ NCCL_CUMEM_ENABLE="0" \ python3 -m kauldron.main \ --cfg=gemma/diffusion/hackable_diffusion_adapter/configs/sft_pubmedqa.py \ --cfg.workdir=$(pwd)/xp_dir # Sudoku Training (with LoRA) env XLA_FLAGS="--xla_disable_hlo_passes=constant_folding" \ NCCL_ALGO="Ring" \ NCCL_PROTO="LL128" \ NCCL_NVLS_ENABLE="0" \ NCCL_CUMEM_ENABLE="0" \ python3 -m kauldron.main \ --cfg=gemma/diffusion/hackable_diffusion_adapter/configs/sft_sudoku.py \ --cfg.workdir=$(pwd)/xp_dir # Sudoku Training (full weight updates) env XLA_FLAGS="--xla_disable_hlo_passes=constant_folding" \ NCCL_ALGO="Ring" \ NCCL_PROTO="LL128" \ NCCL_NVLS_ENABLE="0" \ NCCL_CUMEM_ENABLE="0" \ python3 -m kauldron.main \ --cfg=gemma/diffusion/hackable_diffusion_adapter/configs/sft_sudoku_full.py \ --cfg.workdir=$(pwd)/xp_dirConvert PubMedQA raw data to JSONL
mainTheconvert_pubmedqa.pyscript is used to format raw PubMedQA datasets. It applies instruction-tuning turn tokens to the data and outputs the results as line-delimited JSONL files.Evaluate the Hackable Diffusion Adapter
mainEvaluation is an offline process run after training. It loads checkpoints and performs autoregressive (AR) sampling to report metrics like accuracy (Sudoku) or BLEU (PubMedQA).
Run the evaluation command from the parent directory of the
gemmadirectory.env XLA_FLAGS="--xla_disable_hlo_passes=constant_folding" \ XLA_PYTHON_CLIENT_PREALLOCATE="false" \ TF_FORCE_GPU_ALLOW_GROWTH="true" \ python3 -m gemma.diffusion.hackable_diffusion_adapter.eval_main \ --cfg=gemma/diffusion/hackable_diffusion_adapter/configs/sft_sudoku.py \ --task=sudoku \ --step=1000 \ --eval_names=sample_ar_steps64 \ --cfg.workdir=$(pwd)/xp_dir_sudoku_lora \ --cfg.eval_ds.batch_size=2 \ --cfg.aux.eval_num_batches=2 \ --cfg.aux.num_canvases=2Perform model surgery with ModuleInterceptor
mainYou can replace existing modules with their LoRA or quantized versions using a
ModuleInterceptor. This allows you to transform a model's architecture dynamically during a forward pass.# Replace dense layers with LoRA def _replace_dense_by_lora(module: nn.Module) -> nn.Module: if isinstance(module, nn.Dense): return peft.LoRADense(rank=3, wrapped=module) else: return module with ModuleInterceptor(_replace_dense_by_lora): y = model(x)Manually download Gemma model archives
mainTo download Gemma models manually from Kaggle Hub:
- Navigate to the desired model page (e.g., Gemma 4, Gemma 3, etc.).
- Select one of the Flax model variations.
- Click the "Download" button to obtain the model archive.
- Extract the archive.
The extracted archive contains both the model weights and the tokenizer (e.g., a directory for weights and a
tokenizer.modelfile).