在这篇博文中,我们将逐步指导你在消费级 GPU 上使用 LoRA(低秩自适应)和 Unsloth 对 DeepSeek-R1 进行微调。
LangChat是Java生态下企业级AIGC项目解决方案,集成RBAC和AIGC大模型能力,帮助企业快速定制AI知识库、企业AI机器人。
支持的AI大模型:Gitee AI / 阿里通义 / 百度千帆 / DeepSeek / 抖音豆包 / 智谱清言 / 零一万物 / 讯飞星火 / OpenAI / Gemini / Ollama / Azure / Claude 等大模型。
开源地址:
微调像 DeepSeek-R1 这样的大型 AI 模型可能需要大量资源,但使用正确的工具,可以在消费级硬件上进行有效训练。让我们探索如何使用 LoRA(低秩自适应)和 Unsloth 优化 DeepSeek-R1 微调,从而实现更快、更具成本效益的训练。
DeepSeek 的最新 R1 模型正在设定推理性能的新基准,可与专有模型相媲美,同时保持开源。 DeepSeek-R1 的精简版本在 Llama 3 和 Qwen 2.5 上进行了训练,现在已针对使用 Unsloth(一种专为高效模型自适应而设计的框架)进行微调进行了高度优化。⚙
在这篇博文中,我们将逐步指导你在消费级 GPU 上使用 LoRA(低秩自适应)和 Unsloth 对 DeepSeek-R1 进行微调。
DeepSeek-R1 是由 DeepSeek 开发的开源推理模型。它在需要逻辑推理、数学问题解决和实时决策的任务中表现出色。与传统 LLM 不同,DeepSeek-R1 的推理过程透明,适合……
微调是将 DeepSeek-R1 等通用语言模型适应特定任务、行业或数据集的关键步骤。
微调之所以重要,原因如下:
通过微调 DeepSeek-R1,开发人员可以根据其特定用例对其进行定制,从而提高其有效性和可靠性。
微调大规模 AI 模型面临多项挑战。以下是一些最常见的挑战及其解决方案:
a) 计算限制
b) 在小型数据集上过度拟合
c) 训练时间长
d) 灾难性遗忘
e) 微调模型中的偏差
有效应对这些挑战可确保稳健高效的微调过程。
微调大型语言模型 (LLM) 需要大量计算资源。以下是推荐的配置:
确保你拥有 Python 3.8+ 并安装必要的依赖项:
pip install unsloth torch transformers datasets accelerate bitsandbytes
使用 Unsloth,我们可以高效地以 4 位量化加载模型以减少内存使用量:
from unsloth import FastLanguageModel
model_name ="unsloth/DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit"
max_seq_length = 2048
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=model_name,
max_seq_length=max_seq_length,
load_in_4bit=True,
)
微调需要结构化的输入输出对。让我们假设一个用于遵循指令任务的数据集:
{"instruction":"What is the capital of France?","output":"The capital of France is Paris."}
{"instruction":"Solve: 2 + 2","output":"The answer is 4."}
使用 Hugging Face 的数据集库加载数据集:
from datasets import load_dataset
dataset = load_dataset("json", data_files={"train":"train_data.jsonl","test":"test_data.jsonl"})
使用聊天式提示模板格式化数据集:
prompt_template ="""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
"""
def preprocess_function(examples):
inputs = [prompt_template.format(instruction=inst)forinstinexamples["instruction"]]
model_inputs = tokenizer(inputs, max_length=max_seq_length, truncation=True)
returnmodel_inputs
tokenized_dataset = dataset.map(preprocess_function, batched=True)
LoRA 允许通过仅训练模型的特定部分进行微调,从而显著减少内存使用量:
model = FastLanguageModel.get_peft_model(
model,
r=16, # LoRA rank
target_modules=["q_proj","v_proj"], # Fine-tune key attention layers
lora_alpha=32,
lora_dropout=0.05,
bias="none",
use_gradient_checkpointing=True,
)
训练模型。配置训练参数。初始化并开始训练:
from transformers import Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset["train"],
eval_dataset=tokenized_dataset["test"],
tokenizer=tokenizer,
)
trainer.train()
训练后,评估并保存微调后的模型:
# Evaluate the model
eval_results = trainer.evaluate()
print(f"
erplexity: {eval_results['perplexity']}")
# Save the model and tokenizer
model.save_pretrained("./finetuned_deepseek_r1")
tokenizer.save_pretrained("./finetuned_deepseek_r1")
微调后,使用模型进行推理。本地部署llama.cpp,运行:
./llama.cpp/llama-cli \
--model unsloth/DeepSeek-R1-Distill-Llama-8B-GGUF/DeepSeek-R1-Distill-Llama-8B-Q4_K_M.gguf \
--cache-type-k q8_0 \
--threads 16 \
--prompt'<|User|>What is 1+1?<|Assistant|>'\
--n-gpu-layers 20 \
-no-cnv
通过利用 LoRA 和 Unsloth,我们成功地在消费级 GPU 上微调了 DeepSeek-R1,显著降低了内存和计算要求。这使得更快、更易于访问的 AI 模型训练成为可能,而无需昂贵的硬件。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |