LoRA Fine-Tuning: Train AI Models with Minimal Resources
LoRA Fine-Tuning: Train AI Models with Minimal Resources
💡 What You Will Learn
LoRA Fine-Tuning: Train AI Models with Minimal Resources
LoRAWhat Is
pip install torch transformers datasets accelerate peft
[
{
"instruction": "Translate to English",
"input": "",
"output": "The weather is nice today."
},
{
"instruction": "",
"input": "",
"output": "โฆโฆ"
}
]
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model
import torch
# 4bit
model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2.5-7B-Instruct",
load_in_4bit=True,
torch_dtype=torch.float16
)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
# ConfigurationLoRA
lora_config = LoraConfig(
r=8, #
lora_alpha=32, #
target_modules=["q_proj", "v_proj"], #
lora_dropout=0.05,
bias="none"
)
model = get_peft_model(model, lora_config)
print(f": {model.num_parameters(only_trainable=True)/1e6:.2f}M")
# Parameter1400.1%
from transformers import TrainingArguments, Trainer
training_args = TrainingArguments(
output_dir="./lora-output",
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
num_train_epochs=3,
learning_rate=2e-4,
fp16=True,
save_strategy="epoch"
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset
)
trainer.train()
from peft import PeftModel
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
lora_model = PeftModel.from_pretrained(base_model, "./lora-output/checkpoint-xxx")
merged = lora_model.merge_and_unload()
merged.save_pretrained("./final-model")
|:----|:-------:|:--------:| | 1.5B | 12GB | 4GB | | 7B | 24GB | 6GB | | 13B | 48GB | 12GB | | 70B | 320GB | 48GB |
Related Articles
2026-07-24
Free AI Workflow Automation Tools 2026 No Code
2026-07-26
AI for Business Analysts 2026
2026-07-19
Unsloth Fine-Tuning Guide: Train a Custom LLM with Your Own Data
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only โ no paid placements.
