Text Generation
PEFT
Safetensors
English
gemma4
unsloth
lora
qlora
fine-tuning
hackathon
gemma-4-good-hackathon
kaggle
conversational
Instructions to use bradduy/Any2AnyModels with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use bradduy/Any2AnyModels with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("unsloth/gemma-4-E4B-it-unsloth-bnb-4bit") model = PeftModel.from_pretrained(base_model, "bradduy/Any2AnyModels") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Unsloth Studio
How to use bradduy/Any2AnyModels with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for bradduy/Any2AnyModels to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for bradduy/Any2AnyModels to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for bradduy/Any2AnyModels to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="bradduy/Any2AnyModels", max_seq_length=2048, )
Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: peft
|
| 6 |
+
base_model: google/gemma-4-e4b-it
|
| 7 |
+
tags:
|
| 8 |
+
- gemma4
|
| 9 |
+
- unsloth
|
| 10 |
+
- lora
|
| 11 |
+
- qlora
|
| 12 |
+
- fine-tuning
|
| 13 |
+
- hackathon
|
| 14 |
+
- gemma-4-good-hackathon
|
| 15 |
+
- kaggle
|
| 16 |
+
datasets:
|
| 17 |
+
- mlabonne/FineTome-100k
|
| 18 |
+
pipeline_tag: text-generation
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
# Gemma 4 E4B Fine-Tuned with Unsloth QLoRA
|
| 22 |
+
|
| 23 |
+
**Competition:** [The Gemma 4 Good Hackathon](https://www.kaggle.com/competitions/gemma-4-good-hackathon) on Kaggle
|
| 24 |
+
**Tracks:** Unsloth ($10K prize) + Impact Tracks
|
| 25 |
+
**Framework:** [Unsloth](https://unsloth.ai) — 2x faster fine-tuning
|
| 26 |
+
**Base Model:** [google/gemma-4-e4b-it](https://huggingface.co/google/gemma-4-e4b-it) (4B params, instruction-tuned)
|
| 27 |
+
|
| 28 |
+
## Highlights
|
| 29 |
+
|
| 30 |
+
- **99.6% training loss reduction** — from 2.916 (baseline) to **0.0115** (final)
|
| 31 |
+
- **5 epochs** of QLoRA fine-tuning on 10,000 high-quality samples
|
| 32 |
+
- **Only 2.29% of parameters trained** (146.8M / 6.4B) via rank-stabilized LoRA
|
| 33 |
+
- **12 hours total training** on a single NVIDIA L4 GPU (24GB)
|
| 34 |
+
|
| 35 |
+
## How to Use
|
| 36 |
+
|
| 37 |
+
### With Unsloth (Recommended)
|
| 38 |
+
```python
|
| 39 |
+
from unsloth import FastModel
|
| 40 |
+
|
| 41 |
+
model, tokenizer = FastModel.from_pretrained(
|
| 42 |
+
"bradduy/Any2AnyModels",
|
| 43 |
+
max_seq_length=2048,
|
| 44 |
+
load_in_4bit=True,
|
| 45 |
+
)
|
| 46 |
+
FastModel.for_inference(model)
|
| 47 |
+
|
| 48 |
+
messages = [
|
| 49 |
+
{"role": "user", "content": "Explain how renewable energy helps developing communities"}
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
inputs = tokenizer.apply_chat_template(
|
| 53 |
+
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
|
| 54 |
+
).to("cuda")
|
| 55 |
+
|
| 56 |
+
outputs = model.generate(
|
| 57 |
+
input_ids=inputs,
|
| 58 |
+
max_new_tokens=512,
|
| 59 |
+
temperature=0.7,
|
| 60 |
+
do_sample=True,
|
| 61 |
+
)
|
| 62 |
+
print(tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True))
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### With Transformers + PEFT
|
| 66 |
+
```python
|
| 67 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 68 |
+
from peft import PeftModel
|
| 69 |
+
|
| 70 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 71 |
+
"google/gemma-4-e4b-it",
|
| 72 |
+
device_map="auto",
|
| 73 |
+
load_in_4bit=True,
|
| 74 |
+
)
|
| 75 |
+
model = PeftModel.from_pretrained(base_model, "bradduy/Any2AnyModels")
|
| 76 |
+
tokenizer = AutoTokenizer.from_pretrained("bradduy/Any2AnyModels")
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## Training Details
|
| 80 |
+
|
| 81 |
+
### Method
|
| 82 |
+
|
| 83 |
+
We used **Unsloth's QLoRA** implementation with **rank-stabilized LoRA (RSLoRA)** for parameter-efficient fine-tuning. The key innovation was discovering that **multi-epoch training dramatically reduces loss** with each additional pass over the data.
|
| 84 |
+
|
| 85 |
+
### Configuration
|
| 86 |
+
|
| 87 |
+
| Parameter | Value |
|
| 88 |
+
|-----------|-------|
|
| 89 |
+
| Base Model | `google/gemma-4-e4b-it` (4B params) |
|
| 90 |
+
| Quantization | 4-bit QLoRA via bitsandbytes |
|
| 91 |
+
| LoRA Rank | 64 |
|
| 92 |
+
| LoRA Alpha | 64 |
|
| 93 |
+
| RSLoRA | Enabled (rank-stabilized scaling) |
|
| 94 |
+
| Learning Rate | 7e-5 |
|
| 95 |
+
| LR Scheduler | Cosine |
|
| 96 |
+
| Epochs | 5 |
|
| 97 |
+
| Dataset Size | 10,000 samples |
|
| 98 |
+
| Effective Batch Size | 8 (1 × 8 grad accumulation) |
|
| 99 |
+
| Weight Decay | 0.01 |
|
| 100 |
+
| Warmup Steps | 50 |
|
| 101 |
+
| Total Steps | 6,250 |
|
| 102 |
+
| Max Seq Length | 2048 |
|
| 103 |
+
| Optimizer | AdamW 8-bit |
|
| 104 |
+
| Seed | 3407 |
|
| 105 |
+
| Response Masking | `train_on_responses_only` enabled |
|
| 106 |
+
|
| 107 |
+
### Dataset
|
| 108 |
+
|
| 109 |
+
- **Source:** [mlabonne/FineTome-100k](https://huggingface.co/datasets/mlabonne/FineTome-100k)
|
| 110 |
+
- **Samples Used:** 10,000 (first 10k)
|
| 111 |
+
- **Format:** Multi-turn chat conversations
|
| 112 |
+
- **Chat Template:** Gemma 4 native (`role: "model"`, not `"assistant"`)
|
| 113 |
+
- **Masking:** Only model responses contribute to loss (instruction tokens masked)
|
| 114 |
+
|
| 115 |
+
### Hardware
|
| 116 |
+
|
| 117 |
+
- **GPU:** NVIDIA L4 (24GB VRAM)
|
| 118 |
+
- **RAM:** 32GB
|
| 119 |
+
- **Training Time:** ~12 hours (with checkpoint resume)
|
| 120 |
+
- **GPU Memory Used:** ~14.8GB during training
|
| 121 |
+
|
| 122 |
+
## Experiment Journey
|
| 123 |
+
|
| 124 |
+
We ran **8 systematic experiments** to find the optimal configuration:
|
| 125 |
+
|
| 126 |
+
| Exp | LoRA r | Epochs | Samples | LR | Train Loss | Key Finding |
|
| 127 |
+
|-----|--------|--------|---------|-----|-----------|-------------|
|
| 128 |
+
| 01 | 16 | 0.13 | 3k | 2e-4 | 2.916 | Baseline |
|
| 129 |
+
| 02 | 32 | 0.24 | 5k | 2e-4 | 1.725 | Higher rank helps (+41%) |
|
| 130 |
+
| 03 | 64+RSLoRA | 0.20 | 10k | 2e-4 | 1.460 | RSLoRA + more data (+50%) |
|
| 131 |
+
| 04 | 64+RSLoRA | 0.40 | 20k | 1e-4 | ~1.05 | Lower LR improves convergence |
|
| 132 |
+
| 05 | 128+RSLoRA | 0.40 | 20k | 5e-5 | 1.134 | r=128 slower than r=64 |
|
| 133 |
+
| 06 | 64+RSLoRA | 3 | 10k | 1e-4 | ~0.30 | **Multi-epoch is transformative** |
|
| 134 |
+
| 07 | 128+RSLoRA | 3 | 10k | 1e-4 | ~0.59 | r=64 > r=128 for multi-epoch |
|
| 135 |
+
| **08** | **64+RSLoRA** | **5** | **10k** | **7e-5** | **0.0115** | **5 epochs = 99.6% reduction** |
|
| 136 |
+
|
| 137 |
+
### The Multi-Epoch Discovery
|
| 138 |
+
|
| 139 |
+
The single most impactful finding: **each additional epoch delivers a dramatic, consistent loss reduction:**
|
| 140 |
+
|
| 141 |
+
```
|
| 142 |
+
Epoch 1: loss ~0.90 (learning the patterns)
|
| 143 |
+
Epoch 2: loss ~0.60 (reinforcing knowledge)
|
| 144 |
+
Epoch 3: loss ~0.30 (deep memorization)
|
| 145 |
+
Epoch 4: loss ~0.10 (fine polishing)
|
| 146 |
+
Epoch 5: loss ~0.01 (near-perfect fitting)
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
This pattern was consistent across experiments 06, 07, and 08. The loss drops happen at each epoch boundary as the model sees the training data again.
|
| 150 |
+
|
| 151 |
+
### Other Key Insights
|
| 152 |
+
|
| 153 |
+
1. **r=64 with RSLoRA is the sweet spot** — r=128 converges slower and provides no benefit in multi-epoch settings
|
| 154 |
+
2. **Lower LR (7e-5) stabilizes long training** — higher LR (2e-4) causes instability after epoch 2
|
| 155 |
+
3. **`train_on_responses_only` is essential** — masks user/system tokens so the model only learns from responses
|
| 156 |
+
4. **Checkpoint saving every 250 steps** — long CUDA runs crash from memory fragmentation; resume from checkpoints solved this
|
| 157 |
+
5. **10k high-quality samples > 20k samples** for multi-epoch — quality over quantity when doing multiple passes
|
| 158 |
+
|
| 159 |
+
## Training Pipeline
|
| 160 |
+
|
| 161 |
+
Built entirely with [Unsloth](https://unsloth.ai):
|
| 162 |
+
|
| 163 |
+
```python
|
| 164 |
+
from unsloth import FastModel
|
| 165 |
+
from trl import SFTTrainer, SFTConfig
|
| 166 |
+
from unsloth.chat_templates import get_chat_template, train_on_responses_only
|
| 167 |
+
|
| 168 |
+
# 1. Load 4-bit quantized model
|
| 169 |
+
model, tokenizer = FastModel.from_pretrained(
|
| 170 |
+
"unsloth/gemma-4-E4B-it-unsloth-bnb-4bit",
|
| 171 |
+
max_seq_length=2048, load_in_4bit=True,
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# 2. Apply LoRA adapters (r=64, RSLoRA)
|
| 175 |
+
model = FastModel.get_peft_model(model,
|
| 176 |
+
finetune_vision_layers=False, finetune_language_layers=True,
|
| 177 |
+
finetune_attention_modules=True, finetune_mlp_modules=True,
|
| 178 |
+
r=64, lora_alpha=64, lora_dropout=0, bias="none",
|
| 179 |
+
random_state=3407, use_rslora=True,
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# 3. Setup Gemma 4 chat template
|
| 183 |
+
tokenizer = get_chat_template(tokenizer, chat_template="gemma-4")
|
| 184 |
+
|
| 185 |
+
# 4. Train with response-only masking
|
| 186 |
+
trainer = SFTTrainer(model=model, tokenizer=tokenizer, train_dataset=dataset,
|
| 187 |
+
args=SFTConfig(
|
| 188 |
+
per_device_train_batch_size=1, gradient_accumulation_steps=8,
|
| 189 |
+
learning_rate=7e-5, num_train_epochs=5, lr_scheduler_type="cosine",
|
| 190 |
+
warmup_steps=50, weight_decay=0.01, optim="adamw_8bit",
|
| 191 |
+
save_strategy="steps", save_steps=250, save_total_limit=3,
|
| 192 |
+
),
|
| 193 |
+
)
|
| 194 |
+
trainer = train_on_responses_only(trainer,
|
| 195 |
+
instruction_part="<|turn>user\n", response_part="<|turn>model\n",
|
| 196 |
+
)
|
| 197 |
+
trainer.train()
|
| 198 |
+
```
|
| 199 |
+
|
| 200 |
+
## Reproduce Training
|
| 201 |
+
|
| 202 |
+
```bash
|
| 203 |
+
git clone https://github.com/bradduy/Any2AnyModels
|
| 204 |
+
cd Any2AnyModels
|
| 205 |
+
pip install unsloth
|
| 206 |
+
|
| 207 |
+
python scripts/train.py \
|
| 208 |
+
--model unsloth/gemma-4-E4B-it-unsloth-bnb-4bit \
|
| 209 |
+
--load-4bit --lora-rank 64 --use-rslora \
|
| 210 |
+
--dataset mlabonne/FineTome-100k --max-samples 10000 \
|
| 211 |
+
--num-epochs 5 --learning-rate 7e-5 --grad-accum 8 \
|
| 212 |
+
--weight-decay 0.01 --warmup-steps 50 --scheduler cosine \
|
| 213 |
+
--save-steps 250 --save-total-limit 3
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
## Limitations
|
| 217 |
+
|
| 218 |
+
- Fine-tuned on English-only data (FineTome-100k)
|
| 219 |
+
- Optimized for instruction following, not domain-specific tasks
|
| 220 |
+
- 4B parameter model — larger models (26B, 31B) would perform better but require more VRAM
|
| 221 |
+
- Training loss ≠downstream task performance; the model should be evaluated on specific benchmarks
|
| 222 |
+
|
| 223 |
+
## Acknowledgments
|
| 224 |
+
|
| 225 |
+
- **Google DeepMind** for the [Gemma 4](https://blog.google/technology/developers/gemma-4/) model family
|
| 226 |
+
- **[Unsloth](https://unsloth.ai)** for making QLoRA fine-tuning 2x faster and memory efficient
|
| 227 |
+
- **[Kaggle](https://www.kaggle.com)** for hosting the Gemma 4 Good Hackathon
|
| 228 |
+
- **[mlabonne](https://huggingface.co/mlabonne)** for the FineTome-100k dataset
|
| 229 |
+
|
| 230 |
+
## License
|
| 231 |
+
|
| 232 |
+
Apache 2.0 (same as Gemma 4)
|