minicpm5-finetune-unsloth
Skill này làm gì
Skill này hướng dẫn AI huấn luyện MiniCPM5 bằng unsloth trên một GPU, dùng LoRA hoặc QLoRA 4-bit. Skill dành cho người có một card đồ họa tiêu dùng và muốn tiết kiệm bộ nhớ.
Khi nào nên dùng
- ✓Khi bạn muốn fine-tune MiniCPM5 trên một GPU tiêu dùng có bộ nhớ hạn chế.
- ✓Khi bạn muốn thử QLoRA 4 bit để tiết kiệm bộ nhớ.
- ✓Khi bạn dùng FastLanguageModel của unsloth để huấn luyện.
Liên kết skill
https://github.com/OpenBMB/MiniCPM/blob/main/skills/minicpm5-finetune-unsloth/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Fine-tune MiniCPM5-1B trên card đồ họa 24 GB của tôi bằng QLoRA để nó dùng ít bộ nhớ nhất có thể.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill cài unsloth cùng các phiên bản torch và transformers đã ghim, rồi chạy một script huấn luyện, tải model ở 4 bit hoặc bf16 và huấn luyện adapter LoRA.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Fine-tune MiniCPM5-1B and MiniCPM5-2B with unsloth
Single-GPU LoRA / QLoRA. Heavy custom kernels for memory savings (~2× reduction at 4-bit).
⚠️ Two install-time pins (read this BEFORE running):
transformers==4.57.3— required if vLLM is in the same env (unsloth's vLLM coexistence patch only handles transformers ≤ 4.57.x).torch==2.7.1+torchvision==0.22.1—pip install unslothmay pull a cu13 torch wheel that fails on cu12.x drivers (cuda.is_available()returns False).pip install --force-reinstall "torch==2.7.1" "torchvision==0.22.1" "transformers==4.57.3"
Required input
| Var | Example | Default |
|---|---|---|
BASE_MODEL |
openbmb/MiniCPM5-2B |
required; openbmb/MiniCPM5-1B also works |
DATA |
path to messages-format jsonl | required |
OUTPUT_DIR |
./runs/minicpm5_unsloth |
required |
LOAD_IN_4BIT |
True (QLoRA, lowest VRAM) / False (LoRA bf16) |
False |
Steps
1. Install (once) — pin transformers to 4.57.3
pip install "unsloth>=2026.5"
pip install --force-reinstall "transformers==4.57.3"
🔑
transformers==4.57.3is required if vLLM is in the same env. unsloth's vLLM coexistence patch only handles transformers ≤ 4.57.x; if you skip this pin you'll get adataclasserror at import time.
2. Train — save as train_unsloth.py
import json, os, torch
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "0")
from unsloth import FastLanguageModel
from datasets import Dataset
from trl import SFTTrainer, SFTConfig
BASE = os.environ["BASE_MODEL"]
DATA = os.environ["DATA"]
OUT = os.environ["OUTPUT_DIR"]
# 1. Load with unsloth's fast wrapper
model, tok = FastLanguageModel.from_pretrained(
model_name=BASE,
max_seq_length=2048,
dtype=torch.bfloat16,
load_in_4bit=os.environ.get("LOAD_IN_4BIT", "False") == "True",
full_finetuning=False,
)
# 2. Add LoRA
model = FastLanguageModel.get_peft_model(
model,
r=16,
target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"],
lora_alpha=32,
lora_dropout=0.05,
bias="none",
use_gradient_checkpointing="unsloth",
random_state=42,
)
# 3. Data — unsloth's TRL wrapper expects a `text` column
rows = [json.loads(l) for l in open(DATA, encoding="utf-8") if l.strip()]
ds = Dataset.from_list([
{"text": tok.apply_chat_template(r["messages"], tokenize=False)}
for r in rows
])
if tok.pad_token is None:
tok.pad_token = tok.eos_token
# 4. Train
trainer = SFTTrainer(
model=model,
args=SFTConfig(
output_dir=OUT,
dataset_text_field="text", # 🔑 needed for unsloth's wrapper
num_train_epochs=2,
per_device_train_batch_size=4,
gradient_accumulation_steps=4,
learning_rate=2e-4,
warmup_ratio=0.03,
lr_scheduler_type="cosine",
bf16=True,
max_length=2048,
packing=False,
logging_steps=10,
save_steps=200,
seed=42,
report_to="none",
dataloader_num_workers=2,
),
train_dataset=ds,
processing_class=tok,
)
trainer.train()
trainer.model.save_pretrained(f"{OUT}/adapter_final")
3. Run
BASE_MODEL=openbmb/MiniCPM5-2B \
DATA=/path/to/messages.jsonl \
OUTPUT_DIR=./runs/minicpm5_unsloth \
LOAD_IN_4BIT=False \
python train_unsloth.py
For QLoRA on a consumer GPU (24 GB or less), set LOAD_IN_4BIT=True.
4. Validate
You should see:
==((====))== Unsloth: Fast Llama patching. Transformers: 4.57.3.
🦥 Unsloth: Padding-free auto-enabled, enabling faster training.
trainable params: model-size dependent (about 1% with the recipe below)
{'loss': 4.67, 'epoch': 0.2}
{'loss': 3.52, 'epoch': 1.0}
First iter is slow (~13 s, JIT compile); subsequent iters < 600 ms.
Inference
from unsloth import FastLanguageModel
model, tok = FastLanguageModel.from_pretrained(
model_name="./runs/minicpm5_unsloth", # path to adapter
max_seq_length=4096, dtype=torch.bfloat16, load_in_4bit=False,
)
FastLanguageModel.for_inference(model) # 2× faster generation
inputs = tok.apply_chat_template([{"role":"user","content":"用一句话解释 GQA。"}],
add_generation_prompt=True, enable_thinking=True, return_tensors="pt").to("cuda")
out = model.generate(inputs, max_new_tokens=512, temperature=1.0, top_p=0.95)
print(tok.decode(out[0][inputs.shape[-1]:], skip_special_tokens=True))
Câu hỏi thường gặp
Vì sao các phiên bản cụ thể được ghim?
Skill nói bản vá vLLM của unsloth chỉ hỗ trợ transformers 4.57 trở xuống, và một số bản torch lỗi trên driver CUDA 12.
Tùy chọn 4 bit làm gì?
Đặt LOAD_IN_4BIT thành True sẽ bật QLoRA, cách dùng ít bộ nhớ nhất.
Khác gì so với LoRA thông thường?
LoRA thông thường chạy ở bf16 và tốn nhiều bộ nhớ hơn, còn QLoRA nạp model ở 4 bit để tiết kiệm.
Chi tiết
Mô tả gốc của tác giả
Fine-tune MiniCPM5-1B or MiniCPM5-2B with unsloth for tight-VRAM single-GPU LoRA / QLoRA. Use when the user wants "unsloth", "FastLanguageModel", QLoRA on a 24 GB consumer GPU, or asks for the smallest VRAM footprint.
repo: OpenBMB/MiniCPM
Độ phổ biến
- Sao GitHub
- 12k
- Vị trí xếp hạng
- #11544
- Giấy phép
- Apache-2.0
- Cập nhật
- 21/9/2026
Bình luận
- …
Thêm skill từ kho mã này
minicpm5-deploy-litert
12kGiúp agent chạy các model MiniCPM5 trên máy tính hoặc điện thoại Android bằng runtime LiteRT-LM của Google, chỉ với một file model.
minicpm5-deploy-llama-cpp
12kChỉ cho agent cách chạy model MiniCPM5 bằng llama.cpp trên CPU hoặc card đồ họa gia đình, dùng file GGUF có sẵn.
minicpm5-deploy-lmstudio
12kGiúp agent chạy các model MiniCPM5 trong LM Studio, ứng dụng desktop có cửa sổ chat, và khởi động máy chủ cục bộ kiểu OpenAI của nó.
minicpm5-deploy-mlx
12kGiúp agent chạy các model MiniCPM5 trực tiếp trên máy Mac Apple Silicon bằng framework MLX của Apple, để chat và sinh văn bản cục bộ nhanh.
Skill tương tự
pytorch-patterns
280kGiúp agent của bạn dùng PyTorch cho các mô hình học máy. Áp dụng thực hành tốt nhất để huấn luyện mô hình hiệu quả và đảm bảo kết quả có thể lặp lại.
evm-token-decimals
280kGiúp agent tránh lỗi về số thập phân khi làm việc với các mạng blockchain khác nhau. Đảm bảo giá trị token chính xác trong giao dịch và phép tính.
ito-inference
280kGiúp agent kiểm tra xem các mô hình AI đã sẵn sàng để dùng chưa. Kết nối yêu cầu tài nguyên tính toán với dịch vụ mô hình đã sẵn sàng.