LoRA Fine-Tuning: Train AI Models with Minimal Resources
LoRA Fine-Tuning: Train AI Models with Minimal Resources
📖 Keep reading
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-19
DeepSeek vs Qwen: 7 Real-World Use Cases to Help You Decide
2026-08-02
Open WebUI 2026: Run Your Own Private ChatGPT in 15 Minutes (147k Stars)
2026-08-11
Data Drift Detection 2026: Statistical Tests, Thresholds and the Retraining Trigger
2026-08-06
Meilisearch (58,868 Stars) 2026: Lightning-Fast Typo-Tolerant Search Engine - Self-Host in 5 Minutes
2026-07-26
Self-Hosted AI Starter Kit
2026-07-19
DeepSeek R1 Local Deployment: Best Chinese Reasoning Model Setup
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.