马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一、根本模子下载
- 本文的配景是微调一个基于Llama3的中文版模子Llama3-8B-Chinese-Chat,用于中文指定范畴的问答鄙俚使命
1、HuggingFace官网直接下载
- 官网地点:https://huggingface.co/models
- 镜像网址:https://hf-mirror.com/
- 按照如下图所示搜刮必要的根本模子
- 点击Files and versions
- 依次点击下载,将模子文件下载至指定的文件夹内
- 此方法官方网站下载速率大概很慢,发起时间紧的同砚接纳镜像网站下载
2、使用HuggingFace的transformers库下载
- 创建环境变量HF_HOME,设置模子下载地点
- 验证设置是否见效
- huggingface-cli scan-cache
复制代码
- from transformers import AutoModel
- model_name = "shenzhi-wang/Llama3-8B-Chinese-Chat"
- model = AutoModel.from_pretrained(model_name)
复制代码- transformers-cli download Qwen/Qwen2.5-14B --cache-dir ./
复制代码 3、HuggingFace国内镜像下载(旧版现已经弃用)
- # 下载所需的工具包
- pip install -U huggingface_hub
- pip install huggingface-cli
- # 指定国内镜像源
- # Linux
- export HF_ENDPOINT=https://hf-mirror.com
- # Windows
- set HUGGINGFACE_HUB_ENDPOINT=https://hf-mirror.com
- # 执行下载命令
- # --resume-download 是下载中断恢复下载后继续从上次下载中断点继续下载
- # shenzhi-wang/Llama3-8B-Chinese-Chat 是要下载模型的名称
- # E:\LLM 是下载到本地的路径
- huggingface-cli download --resume-download shenzhi-wang/Llama3-8B-Chinese-Chat --local-dir E:\LLM
复制代码 4、下载乐成后最好校验一下下载的模子是否有标题
二、微调工具LLaMA-Factory 框架
1、LLaMA-Factory安装
- git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
- cd LLaMA-Factory
- # "[torch,metrics]"可以删除直接执行pip install -e .
- pip install -e ".[torch,metrics]"
复制代码
- LLaMA-Factory具体先容可移步GitHub:https://github.com/hiyouga/LLaMA-Factory/blob/main/README_zh.md
三、构建微调数据
1、打开LLaMA-Factory项目
2、构建微调json数据
- 在data目次下新建fine_tuning_data文件夹,用来存放微调所需的数据
- 由于业务数据公司要求保密,这里就恣意构造一些数据用于实验,各人可以根据本身的业务需求举行预训练数据的构造
- 在dataset_info.json文件中将刚刚构建的微调数据举行路径设置,留意设置格式要与构建的数据格式逐一对应
- # 新构建的节点
- "fintech": {
- "file_name": "fine_tuning_data/fintech.json",
- "columns": {
- "prompt": "instruction",
- "query": "input",
- "response": "output",
- "history": "history"
- }
- },
- # 默认自带的节点,注意修改路径
- "identity": {
- "file_name": "fine_tuning_data/identity.json"
- },
复制代码
四、启动LLaMA-Factory Web UI举行模子微调
1、启动LLaMA-Factory并设置微调参数
- 在LLaMA-Factory项目标根目次下输入已下指令启动LLaMa-Factory Web UI
- 启动乐成后下令框体现如下
- Web UI界面体现如下
- 设置微调参数
- 点击预览下令,生产微调指令
- llamafactory-cli train \
- --stage sft \
- --do_train True \
- --model_name_or_path E:\LLM\Llama3-8B-Chinese-Chat \
- --preprocessing_num_workers 16 \
- --finetuning_type lora \
- --template llama3 \
- --flash_attn auto \
- --dataset_dir data \
- --dataset fintech,identity \
- --cutoff_len 1024 \
- --learning_rate 0.0002 \
- --num_train_epochs 10.0 \
- --max_samples 1000 \
- --per_device_train_batch_size 2 \
- --gradient_accumulation_steps 8 \
- --lr_scheduler_type cosine \
- --max_grad_norm 1.0 \
- --logging_steps 5 \
- --save_steps 100 \
- --warmup_steps 0 \
- --optim adamw_torch \
- --packing False \
- --report_to none \
- --output_dir saves\LLaMA3-8B-Chinese-Chat\lora\train_2024-06-20-19-55-32 \
- --fp16 True \
- --plot_loss True \
- --ddp_timeout 180000000 \
- --include_num_input_tokens_seen True \
- --lora_rank 8 \
- --lora_alpha 16 \
- --lora_dropout 0 \
- --lora_target all
复制代码
2、开始微调
- 第二种方法启动微调
- 将微调指令构建成build/train_llama3_lora.yaml设置文件,内容如下
- stage: sft
- do_train: True
- model_name_or_path: E:\LLM\LLMLlama3-8B-Chinese-Chat
- preprocessing_num_workers: 16
- finetuning_type: lora
- template: llama3
- flash_attn: auto
- dataset_dir: data
- dataset: fintech,identity
- cutoff_len: 1024
- learning_rate: 0.0002
- num_train_epochs: 10.0
- max_samples: 1000
- per_device_train_batch_size: 2
- gradient_accumulation_steps: 8
- lr_scheduler_type: cosine
- max_grad_norm: 1.0
- logging_steps: 5
- save_steps: 100
- warmup_steps: 0
- optim: adamw_torch
- packing: False
- report_to: none
- output_dir: saves\LLaMA3-8B-Chinese-Chat\lora\train_2024-06-20-19-55-32
- fp16: True
- plot_loss: True
- ddp_timeout: 180000000
- include_num_input_tokens_seen: True
- lora_rank: 8
- lora_alpha: 16
- lora_dropout: 0
- lora_target: all
复制代码
- 在LLaMA-Factory项目标根目次实验以下下令开始微调
- llamafactory-cli train build/train_llama3_lora.yaml
复制代码
- 开始微调,必要耐心等候
- 微调过程中要经常检测一下显卡的运行环境,防止“爆显存”导致微调使命克制
- # Linux
- watch -n 1 nvidia-smi
- # windows
- nvidia-smi.exe -l
复制代码
- 开始微调后,可以观看终端进度条检测微调使命进程
- 还可以在Web UI中观看微调过程中的loss降落图
3、查察微调结果
- 微调完成后,终端和Web UI会有相应的提示
- 微调完成后,在指定的文件夹下可以查察微调对应的模子参数
五、查验微调结果
1、加载验证集举行模子验证
- 在微调乐成后,各人必要根据具体业务环境,将预备好的验证数据集加载进来,用于验证模子的微调环境
- 这里在举行验证的过程中,必要用到jieba、nltk、rouge-chinese这三个NLP常用的工具包,各人如果没有安装,可以在实验验证前举行安装
- pip install jieba nltk rouge-chinese
复制代码 2、加载微调前后的模子举行推理,对比结果
- 这里用到了vllm推理框架,没有安装的同砚也必要安装一下
- 加载预训练模子Llama3-8B-Chinese-Chat
- 加载乐成后,就可以举行对话了
- 这是微调前的模子答复
- 加载微调后的预训练模子Llama3-8B-Chinese-Chat
- 这是微调后的模子答复,从结果可以看出,我们的微调是有用的
3、将训练后的LoRA Adapter和基座模子归并
1、Web UI界面归并
- 根据业务指定好归并参数,点击导出,耐心等候即可
- 归并导出后的模子目次如下
- 如许就可以直接使用微调后的模子了
2、下令行归并
- 将模子归并参数构建成build/merge_llama3_lora.yaml设置文件,内容如下
- ### Note: DO NOT use quantized model or quantization_bit when merging lora adapters
- ### model
- model_name_or_path: E:\LLM\Llama3-8B-Chinese-Chat
- adapter_name_or_path: E:\LLM\LLaMA-Factory\saves\LLaMA3-8B-Chinese-Chat\lora\train_2024-06-20-19-55-32
- template: llama3
- finetuning_type: lora
- ### export
- export_dir: E:\LLM\LLaMA3-8B-Chinese-Chat-merged
- export_size: 4
- export_device: cuda
- export_legacy_format: false
复制代码- llamafactory-cli export build/merge_llama3_lora.yaml
复制代码 六、常用下令总结
- llamafactory-cli train \
- --stage sft \
- --do_train True \
- --model_name_or_path /mnt/workspace/qwen2-5/Qwen2.5-0.5B-Instruct \
- --preprocessing_num_workers 16 \
- --finetuning_type lora \
- --template qwen \
- --flash_attn auto \
- --dataset_dir data \
- --dataset identity \
- --cutoff_len 1024 \
- --learning_rate 0.0002 \
- --num_train_epochs 10.0 \
- --max_samples 1000 \
- --per_device_train_batch_size 2 \
- --gradient_accumulation_steps 8 \
- --lr_scheduler_type cosine \
- --max_grad_norm 1.0 \
- --logging_steps 5 \
- --save_steps 100 \
- --warmup_steps 0 \
- --optim adamw_torch \
- --packing False \
- --report_to none \
- --output_dir saves/Qwen2.5-0.5B-Instruct/lora/train_2024-12-25-19-55-32 \
- --fp16 True \
- --plot_loss True \
- --ddp_timeout 180000000 \
- --include_num_input_tokens_seen True \
- --lora_rank 8 \
- --lora_alpha 16 \
- --lora_dropout 0 \
- --lora_target all
复制代码- llamafactory-cli export \
- --model_name_or_path /mnt/workspace/qwen2-5/Qwen2.5-0.5B-Instruct \
- --adapter_name_or_path saves/Qwen2.5-0.5B-Instruct/lora/train_2024-12-25-19-55-32 \
- --template qwen \
- --export_dir saves/Qwen2.5-0.5B-Instruct/lora/merged
复制代码- llamafactory-cli export \
- --model_name_or_path /mnt/workspace/qwen2-5/Qwen2.5-0.5B-Instruct \
- --adapter_name_or_path saves/Qwen2.5-0.5B-Instruct/lora/train_2024-12-25-19-55-32 \
- --template qwen \
- --export_dir saves/Qwen2.5-0.5B-Instruct/lora/merged
复制代码- llamafactory-cli chat \
- --model_name_or_path saves/Qwen2.5-0.5B-Instruct/lora/merged \
- --template qwen
复制代码- CUDA_VISIBLE_DEVICES=0 llamafactory-cli webchat \
- --model_name_or_path /home/util/muyan/Qwen/Qwen2___5-1___5B-Instruct \
- --template qwen
复制代码- export USE_MODELSCOPE_HUB=1
- CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli webui
复制代码- export USE_MODELSCOPE_HUB=1export CUDA_VISIBLE_DEVICES=0,1nohup llamafactory-cli webui
- >20241119.log 2>&1 &
复制代码 |