当前位置: 代码网 > 科技>人工智能>智能机器人 > 本地搭建LLaMA-Factory环境进行大模型调优

本地搭建LLaMA-Factory环境进行大模型调优

2024年08月01日 智能机器人 我要评论
本文只是简单对 LLaMA Factory 在本地调优的流程进行了简单的介绍,调优完成之后将模型到处为 GGUF 格式并用 ollama 运行,具体的调优参数还要参考 LLaMA Factory 官方网站,不得不吐槽一下,文档确实不太完善,得看源代码。

llama factory

llama factory快速对大模型进行快速调优,本文看一下如何本地搭建环境并调优,本文使用 modelscope 社区中的模型,模型在国内,下载速度非常友好。

  1. 下载最新代码
## llama factory官方
git pull https://github.com/hiyouga/llama-factory

  1. 编译 docker 并运行
    我添加了use_modelscope_hub=1,代表从 modelscope 拉模型,所以训练时候需要使用 modelscope 的 id。docker 文件稍微修改一下,添加 python 镜像,否则打包非常慢。
from nvcr.io/nvidia/pytorch:24.01-py3

workdir /app

copy requirements.txt /app/
run pip install -i https://mirrors.aliyun.com/pypi/simple -r requirements.txt

copy . /app/
run pip install -i https://mirrors.aliyun.com/pypi/simple -e .[metrics,bitsandbytes,qwen]

volume [ "/root/.cache/huggingface/", "/app/data", "/app/output" ]
expose 7860

cmd [ "llamafactory-cli", "webui" ]

docker build -f ./dockerfile -t llama-factory:latest .
docker run --runtime=nvidia --gpus all \
-v ./hf_cache:/root/.cache/huggingface/ \
-v ./data:/app/data \
-v ./examples:/app/examples \
-v ./output:/app/output \
-e cuda_visible_devices=0 \
-e use_modelscope_hub=1 \
-p 7860:7860 \
--shm-size 32g \
--name llama_factory \
-d llama-factory:latest
  1. 模型训练
    测试中使用了 llam3模型,修改配置文件的模型 id,examples/lora_single_gpu/llama3_lora_sft.yaml,从“model_name_or_path: meta-llama/meta-llama-3-8b-instruct” 改为 “model_name_or_path: llm-research/meta-llama-3-8b-instruct”
cuda_visible_devices=0 llamafactory-cli train examples/lora_single_gpu/llama3_lora_sft.yaml

  1. 模型推理
    使用训练好的模型进行推理,修改模型 id,从“model_name_or_path: meta-llama/meta-llama-3-8b-instruct” 改为 “model_name_or_path: llm-research/meta-llama-3-8b-instruct”
cuda_visible_devices=0 llamafactory-cli chat examples/inference/llama3_lora_sft.yaml
  1. 导出模型
    将lora 调优的adapter 于原始模型进行合并导出。
cuda_visible_devices=0 llamafactory-cli export examples/merge_lora/llama3_lora_sft.yaml
  1. 转换为 gguf
    将导出的模型转换为 gguf,gguf 可以通过 ollama 运行。
## 下载 llamafile

git clone https://github.com/ggerganov/llama.cpp.git

## 安装依赖
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r llama.cpp/requirements.txt

##转换模型
 python llama.cpp/convert-hf-to-gguf.py /app/models/llama3_lora_sft/   --outfile test-llama3.gguf   --outtype q8_0
  1. 导入 ollama
  • 创建模型文件,创建模型文件testmodel。
from ./test-llama3.gguf
template "{{ if .system }}<|start_header_id|>system<|end_header_id|> {{ .system }}<|eot_id|>{{ end }}{{ if .prompt }}<|start_header_id|>user<|end_header_id|> {{ .prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|> {{ .response }}<|eot_id|>"
  • 创建模型文件
ollama create testllama3 -f testmodel
  • 执行文件
 ollama run  testllama3

总结

本文只是简单对 llama factory 在本地调优的流程进行了简单的介绍,调优完成之后将模型到处为 gguf 格式并用 ollama 运行,具体的调优参数还要参考 llama factory 官方网站,不得不吐槽一下,文档确实不太完善,得看源代码。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com