Views
No views yet
1from unsloth import FastModel
2
3model, tokenizer = FastModel.from_pretrained(
4 "bradduy/Any2AnyModels",
5 max_seq_length=2048,
6 load_in_4bit=True,
7)
8FastModel.for_inference(model)
9
10messages = [
11 {"role": "user", "content": "Explain how renewable energy helps developing communities"}
12]
13
14inputs = tokenizer.apply_chat_template(
15 messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
16).to("cuda")
17
18outputs = model.generate(
19 input_ids=inputs,
20 max_new_tokens=512,
21 temperature=0.7,
22 do_sample=True,
23)
24print(tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True))1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3
4base_model = AutoModelForCausalLM.from_pretrained(
5 "google/gemma-4-e4b-it",
6 device_map="auto",
7 load_in_4bit=True,
8)
9model = PeftModel.from_pretrained(base_model, "bradduy/Any2AnyModels")
10tokenizer = AutoTokenizer.from_pretrained("bradduy/Any2AnyModels")| Parameter | Value |
|---|---|
| Base Model | google/gemma-4-e4b-it (4B params) |
| Quantization | 4-bit QLoRA via bitsandbytes |
| LoRA Rank | 64 |
| LoRA Alpha | 64 |
| RSLoRA | Enabled (rank-stabilized scaling) |
| Learning Rate | 7e-5 |
| LR Scheduler | Cosine |
| Epochs | 5 |
| Dataset Size | 10,000 samples |
| Effective Batch Size | 8 (1 × 8 grad accumulation) |
| Weight Decay | 0.01 |
| Warmup Steps | 50 |
| Total Steps | 6,250 |
| Max Seq Length | 2048 |
| Optimizer | AdamW 8-bit |
| Seed | 3407 |
| Response Masking | train_on_responses_only enabled |
role: "model", not "assistant")| Exp | LoRA r | Epochs | Samples | LR | Train Loss | Key Finding |
|---|---|---|---|---|---|---|
| 01 | 16 | 0.13 | 3k | 2e-4 | 2.916 | Baseline |
| 02 | 32 | 0.24 | 5k | 2e-4 | 1.725 | Higher rank helps (+41%) |
| 03 | 64+RSLoRA | 0.20 | 10k | 2e-4 | 1.460 | RSLoRA + more data (+50%) |
| 04 | 64+RSLoRA | 0.40 | 20k | 1e-4 | ~1.05 | Lower LR improves convergence |
| 05 | 128+RSLoRA | 0.40 | 20k | 5e-5 | 1.134 | r=128 slower than r=64 |
| 06 | 64+RSLoRA | 3 | 10k | 1e-4 | ~0.30 | Multi-epoch is transformative |
| 07 | 128+RSLoRA | 3 | 10k | 1e-4 | ~0.59 | r=64 > r=128 for multi-epoch |
| 08 | 64+RSLoRA | 5 | 10k | 7e-5 | 0.0115 | 5 epochs = 99.6% reduction |
Epoch 1: loss ~0.90 (learning the patterns)
Epoch 2: loss ~0.60 (reinforcing knowledge)
Epoch 3: loss ~0.30 (deep memorization)
Epoch 4: loss ~0.10 (fine polishing)
Epoch 5: loss ~0.01 (near-perfect fitting)train_on_responses_only is essential — masks user/system tokens so the model only learns from responses1from unsloth import FastModel
2from trl import SFTTrainer, SFTConfig
3from unsloth.chat_templates import get_chat_template, train_on_responses_only
4
5# 1. Load 4-bit quantized model
6model, tokenizer = FastModel.from_pretrained(
7 "unsloth/gemma-4-E4B-it-unsloth-bnb-4bit",
8 max_seq_length=2048, load_in_4bit=True,
9)
10
11# 2. Apply LoRA adapters (r=64, RSLoRA)
12model = FastModel.get_peft_model(model,
13 finetune_vision_layers=False, finetune_language_layers=True,
14 finetune_attention_modules=True, finetune_mlp_modules=True,
15 r=64, lora_alpha=64, lora_dropout=0, bias="none",
16 random_state=3407, use_rslora=True,
17)
18
19# 3. Setup Gemma 4 chat template
20tokenizer = get_chat_template(tokenizer, chat_template="gemma-4")
21
22# 4. Train with response-only masking
23trainer = SFTTrainer(model=model, tokenizer=tokenizer, train_dataset=dataset,
24 args=SFTConfig(
25 per_device_train_batch_size=1, gradient_accumulation_steps=8,
26 learning_rate=7e-5, num_train_epochs=5, lr_scheduler_type="cosine",
27 warmup_steps=50, weight_decay=0.01, optim="adamw_8bit",
28 save_strategy="steps", save_steps=250, save_total_limit=3,
29 ),
30)
31trainer = train_on_responses_only(trainer,
32 instruction_part="<|turn>user\n", response_part="<|turn>model\n",
33)
34trainer.train()1git clone https://github.com/bradduy/Any2AnyModels
2cd Any2AnyModels
3pip install unsloth
4
5python scripts/train.py \
6 --model unsloth/gemma-4-E4B-it-unsloth-bnb-4bit \
7 --load-4bit --lora-rank 64 --use-rslora \
8 --dataset mlabonne/FineTome-100k --max-samples 10000 \
9 --num-epochs 5 --learning-rate 7e-5 --grad-accum 8 \
10 --weight-decay 0.01 --warmup-steps 50 --scheduler cosine \
11 --save-steps 250 --save-total-limit 3