TwKit
正在载入
TwKit

微調 8B 模型不用 A100 了:一張遊戲顯卡、8GB 顯存,30 分鐘出爐 (原文發表於 2026 年 10 月 12 日) 別再燒幾千美元買雲端 AP… · @swordBluesy · TwKit

@swordBluesy

微調 8B 模型不用 A100 了:一張遊戲顯卡、8GB 顯存,30 分鐘出爐 (原文發表於 2026 年 10 月 12 日) 別再燒幾千美元買雲端 API 的 token 了。微調一個 8B 參數模型,已經不需要 A100 集群,也不需要無底洞的雲端算力帳單。Unsloth 配 Ollama:用 QLoRA 在消費級顯卡上、8GB 以內顯存做本地微調,直接導出 GGUF 格式,一條命令就在本地跑起來。整條流水線 30 分鐘內搭完。 為什麼是這個組合 標準 Hugging Face 流水線在 16GB 或 24GB 顯卡上訓 8B 模型,經常 CUDA 顯存溢出(OOM)直接崩。Unsloth 把 PyTorch 的注意力層和交叉熵損失函數重寫成手工的 Triton 內核,換來四個具體好處: 訓練快 2 倍:繞開 PyTorch autograd 的開銷。 顯存省 70%–80%:QLoRA 訓 Llama 3 8B 或 Mistral 7B,只要 6.6GB 到 8GB 顯存。 零轉換開銷:直接導出量化 GGUF,並自動生成 Ollama 的 Modelfile。 零 API 帳單:微調好的私有模型用 Ollama 在 macOS、Linux 或 Windows 本地跑。 官方資源先存好:Unsloth 的 GitHub 倉庫(http://github.com/unslothai/unsloth)、官方文件(http://unsloth.ai/docs)、Hugging Face 上的 unsloth 官方帳號(有 Llama 3、Mistral、Qwen、Gemma 的 4-bit 預量化基座模型);Ollama 官網下載頁和 GitHub 倉庫(http://github.com/ollama/ollama)。 安裝兩步 環境是裝了 CUDA 的 Linux/WSL2。 第 1 步,裝 Ollama 運行時: curl -fsSL https://ollama.com/install.sh | sh 第 2 步,建獨立的 Python 虛擬環境,裝 Unsloth 和依賴: python3 -m venv finetune-env source finetune-env/bin/activate pip install --upgrade pip pip install 'unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git' pip install --no-deps trl peft accelerate bitsandbytes datasets 實戰全流程:微調 Llama 3 8B 做客服機器人 場景:拿客服數據集微調 Llama 3 8B Instruct,導出 GGUF,再用 Ollama 本地問答。 第 1 步:初始化模型和 LoRA 適配器。 建訓練腳本 http://train.py,直接從 Hugging Face 加載 4-bit 預量化模型,注入 LoRA 參數: import torch from unsloth import FastLanguageModel from datasets import load_dataset from trl import SFTTrainer from transformers import TrainingArguments max_seq_length = 2048 dtype = None # Auto-detect Float16 / Bfloat16 load_in_4bit = True # 1. Load 4-bit base model model, tokenizer = FastLanguageModel.from_pretrained( model_name = 'unsloth/llama-3-8b-Instruct-bnb-4bit', max_seq_length = max_seq_length, dtype = dtype, load_in_4bit = load_in_4bit, ) # 2. Attach LoRA adapters 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 = 16, lora_dropout = 0, bias = 'none', use_gradient_checkpointing = 'unsloth', random_state = 3407, ) 參數讀法:序列長度上限 2048;LoRA 秩 r=16、alpha=16、dropout=0;目標模塊是注意力四件套(q/k/v/o_proj)加 MLP 三件套(gate/up/down_proj);梯度檢查點用 Unsloth 自家實現省顯存。 第 2 步:格式化數據並訓練。 用 Llama 3 的對話模板把「指令—回覆」對拼成訓練文本,數據集示例用 philschmid/dolly-15k-oai-style 的前 1000 條: # 3. Format dataset using Llama 3 template prompt_template = '''<|start_header_id|>system<|end_header_id|> You are a specialized internal support bot.<|eot_id|><|start_header_id|>user<|end_header_id|> {}<|eot_id|><|start_header_id|>assistant<|end_header_id|> {}<|eot_id|>''' def formatting_prompts_func(examples): texts = [] for instruction, output in zip(examples['instruction'], examples['output']): text = prompt_template.format(instruction, output) texts.append(text) return {'text': texts} dataset = load_dataset('philschmid/dolly-15k-oai-style', split='train[:1000]') dataset = http://dataset.map(formatting_prompts_func, batched=True) trainer = SFTTrainer( model = model, tokenizer = tokenizer, train_dataset = dataset, dataset_text_field = 'text', max_seq_length = max_seq_length, dataset_num_proc = 2, packing = False, args = TrainingArguments( per_device_train_batch_size = 2, gradient_accumulation_steps = 4, warmup_steps = 5, max_steps = 60, learning_rate = 2e-4, fp16 = not http://torch.cuda.is_bf16_supported(), bf16 = http://torch.cuda.is_bf16_supported(), logging_steps = 10, optim = 'adamw_8bit', weight_decay = 0.01, lr_scheduler_type = 'linear', seed = 3407, output_dir = 'outputs', ), ) trainer.train() 訓練參數讀法:每卡 batch 2、梯度累積 4 步(等效 batch 8);預熱 5 步、總共 60 步;學習率 2e-4、線性衰減;優化器用 8-bit 的 adamw_8bit 繼續省顯存;權重衰減 0.01;fp16/bf16 按顯卡支持自動二選一。 第 3 步:導出 GGUF。 不用手動編譯 llama.cpp,在 http://train.py 末尾直接導出 4-bit 中等量化 q4_k_m: # 4. Export directly to GGUF http://model.save_pretrained_gguf('custom_model', tokenizer, quantization_method = 'q4_k_m') 跑 python http://train.py,量化好的 GGUF 權重和自動配好的 Modelfile 會一起寫進 ./custom_model 資料夾。 第 4 步:在 Ollama 裡註冊並運行。 # 5. Build and serve inside Ollama ollama create my-assistant -f./custom_model/Modelfile ollama run my-assistant 原文給的示例對話:問「怎麼重置公司 VPN 令牌?」,微調後的模型按內部客服口徑一步步答出 Okta Verify 的操作路徑,並說明新令牌 60 秒內生效——這就是拿自家數據微調的意義:答的是你的流程,不是通用常識。 原文總結:Unsloth+Ollama 是一條自包含的本地微調工作流,15–30 分鐘訓完,甩掉外部 API 依賴,所有私有數據留在自己的基礎設施裡。 S1哥觀點 這篇真正的門檻提示藏在參數裡:60 步、1000 條數據、r=16——這是「讓模型學會你的格式和口徑」,不是「把模型變聰明」。拿它做客服、做固定格式抽取、做內部知識問答,立竿見影;拿它去拼通用能力,趁早死心。還有一句逆耳的:6.6GB 顯存能訓,不代表該在本機訓——數據只要涉及公司內部資料,本地微調的隱私優勢才是這條流水線最大的賣點,速度和省錢都排第二。 原文連結:https://dev.to/signalmatrixai/unsloth-ollama-fine-tune-and-run-local-llms-3p78 原文:@signalmatrixai(由 S1哥 編譯)

曝光 94 · 评论 0 · 点赞 1 · 书签 0

查看原帖