用 LLaMA-Factory 在魔搭微调千问

今天在魔搭上把千问调优跑通了,训练模型现在在 Mac 还不支持,需要用 N 卡才可以,只能弄个N 卡的机器,或者买个云服务器。魔搭可以用几十个小时,但是不太稳定,有的时候会自动停止。

注册账号

直接手机号注册就可以.

找到对应模型

这步可能不需要,随便一个模型,只要启动了 GPU 环境就可以,如果手里有代码,直接启动环境即可。进入模型说明页,通常会有一个测试代码把代码放到 notebook 直接运行接就可以看到结果。我用了Qwen一个最小的模型 0.5B,代码和运行结果如下:

复制代码
from modelscope import AutoModelForCausalLM, AutoTokenizer
device = "cuda" # the device to load the model onto

model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen1.5-0.5B-Chat",
    torch_dtype="auto",
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B-Chat")

prompt = "你好,什么是 Java?"
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(device)

generated_ids = model.generate(
    model_inputs.input_ids,
    max_new_tokens=512
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]

response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)

调优

调优模型需要几步,首先,需要准备数据,我这里就是测试一下,所以就直接用了 LLama Factory 的例子。然后,配置命令行参数进行模型训练。

  1. 安装LLaMA Factory, 通过 notebook 打开安装

    git clone https://github.com/hiyouga/LLaMA-Factory.git
    cd LLaMA-Factory
    pip install -r requirements.txt
    pip install modelscope -U

  2. 运行训练命令
    --model_name_or_path 模型名称要写对
    --dataset 训练数据集名称要写对,这个名称是在/data/dataset_info.json进行配置,直接搜索 example 就可以看到
    训练很快,因为训练数据就两条,就是测试一下。

    CUDA_VISIBLE_DEVICES=0 python src/train_bash.py
    --stage sft
    --do_train
    --model_name_or_path Qwen/Qwen1.5-0.5B-Chat \
    --dataset example
    --template qwen
    --finetuning_type lora
    --lora_target q_proj,v_proj
    --output_dir output
    --overwrite_cache
    --overwrite_output_dir true
    --per_device_train_batch_size 2
    --gradient_accumulation_steps 32
    --lr_scheduler_type cosine
    --logging_steps 10
    --save_steps 1000
    --learning_rate 5e-5
    --num_train_epochs 3.0
    --plot_loss
    --fp16

  3. 合并训练好的模型
    --export_dir Qwen1.5-0.5B-Chat_fine 导出的位置要写对

    CUDA_VISIBLE_DEVICES=0 python src/export_model.py
    --model_name_or_path Qwen/Qwen1.5-0.5B-Chat
    --adapter_name_or_path output
    --template qwen
    --finetuning_type lora
    --export_dir Qwen1.5-0.5B-Chat_fine
    --export_size 2
    --export_legacy_format False

  4. 运行模型
    模型位置要写对,否则会报错。

    from modelscope import AutoModelForCausalLM, AutoTokenizer
    device = "cuda" # the device to load the model onto

    model = AutoModelForCausalLM.from_pretrained(
    "/mnt/workspace/LLaMA-Factory/Qwen1.5-0.5B-Chat_fine",
    torch_dtype="auto",
    device_map="auto"
    )
    tokenizer = AutoTokenizer.from_pretrained("/mnt/workspace/LLaMA-Factory/Qwen1.5-0.5B-Chat_fine")

    prompt = "你好,纽约天怎么样?"
    messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": prompt}
    ]
    text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
    )
    model_inputs = tokenizer([text], return_tensors="pt").to(device)

    generated_ids = model.generate(
    model_inputs.input_ids,
    max_new_tokens=512
    )
    generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
    ]

    response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    print(response)

现在各种开源框架很多,训练起来不复杂,但是如果想训练一个可用的生产模型,还是要花一些时间的,可以比较一下训练前和训练后,模型对纽约天气的回答,大概率出现幻觉。

相关推荐
heroboyluck12 小时前
AI工程师第四课 - 深度学习入门
人工智能·python·深度学习·llama
Token炼金师3 天前
引擎四强:vLLM、SGLang、TensorRT-LLM 与 llama.cpp —— 推理引擎选型对决
人工智能·llm·llama·vllm·tensorrt-llm·sglang
尼米棕熊4 天前
大模型学习8上-推理部署框架llama.cpp与Ollama使用指北
学习·llama
Day(AKA Elin)4 天前
【Day】MTP(Multi Token Prediction)技术学习
python·深度学习·学习·llama
染指11105 天前
50.llama_index-文档分割器(文本)
llama·rag·llamaindex
Scabbards_7 天前
经典大语言模型框架整理(gpt/llama/chatglm/qwen/deepseek)
gpt·语言模型·llama
染指11108 天前
46.llama_index-提示词模板(富提示词模板、jinja2静态和动态模板)
llama·rag·llamaindex
染指111010 天前
45.llama_index-全局配置(Settings)
llama·llamaindex
俊俊谢10 天前
LLaMA-Factory 部署与 DeepSeek-R1-Distill-Qwen 模型乱码问题解决全记录
机器学习·大模型·llama·qwen·llama-factory·deepseek·hugging-face
sunywz10 天前
【AI】基于LLaMA-Factory大模型微调
人工智能·llama