LLaMA-Factory全流程训练模型_llama-factory docker

🤗本文主要讲述在docker下使用LLaMA-Factory训练推理模型。
🫡拉取镜像
首先需要启动docker,然后在终端中输入:
docker run -tid --gpus all -p 8000:8000 --name LLM -e NVIDIA_DRIVER_CAPABILITIES=compute,utility -e NVIDIA_VISIBLE_DEVICES=all --privileged=true ubuntu:20.04
- 这个命令启动了一个 Ubuntu 20.04 容器,使用所有可用的 GPU
- 主机的 8000 端口映射到容器的 8000 端口
- 容器命名为
LLM,以特权模式运行容器
进入容器
docker exec -it LLM /bin/bash

🥰但现在还不行,我们只将GPU映射到了docker里,还没有安装驱动。
wget https://developer.download.nvidia.com/compute/cuda/12.6.2/local_installers/cuda_12.6.2_560.35.03_linux.run
然后运行程序
sh cuda_12.6.2_560.35.03_linux.run
随后会生成一些指引,默认安装就行。
root@82c2f2b69781:/home# ls /usr/local/ | grep cudacudacuda-12.6root@82c2f2b69781:/home# nvcc -Vbash: nvcc: command not found
- 这说明系统的
PATH环境变量没有包含/usr/local/cuda-12.6/bin
编辑环境变量vim ~/.bashrc 加入下面两行:export PATH=/usr/local/cuda-12.6/bin:$PATHexport LD_LIBRARY_PATH=/usr/local/cuda-12.6/lib64:$LD_LIBRARY_PATH然后重新运行一下就生效了:source ~/.bashrc
验证成功 ~
root@82c2f2b69781:/home# echo $PATH/usr/local/cuda-12.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
🤗docker内安装python
docker拉取的Ubuntu20.04没有任何配置,比如wget等命令需要自己通过apt-get install 安装
Index of /ftp/python/3.10.6/ 这是python源码包的地址(3.10.6为例)
wget https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tgz
tar -zxvf Python-3.10.6.tgzcd Python-3.10.6sudo ./configure # configure 脚本会检查系统环境,并生成 Makefile 文件,以便后续的 make 命令可以正确编译源代码
🤗最后一步:
sudo makesudo make testsudo make install
💥LLaMA-Factory
💫安装:
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.gitcd LLaMA-Factorypip install -e \".[torch,metrics]\"
如果使用昇腾NPU的话,先设置一下环境变量:
export ASCEND_HOME_PATH=/usr/local/Ascend/ascend-toolkit/latest
💫下载模型:
git lfs installgit clone https://www.modelscope.cn/Qwen/Qwen2.5-1.5B-Instruct.git
💫我们在 LLaMA-Factory/examples下创建 train.yaml 文件,这是微调训练模型的配置文件
### modelmodel_name_or_path: /home/Qwen/Qwen2___5-1___5B-Instruct### methodstage: sftdo_train: truefinetuning_type: freeze# lora_target: alldataset: alpaca_zh_demotemplate: qwencutoff_len: 10240max_samples: 1000overwrite_cache: truepreprocessing_num_workers: 16### outputoutput_dir: outputlogging_steps: 10save_steps: 500plot_loss: trueoverwrite_output_dir: true### trainper_device_train_batch_size: 1gradient_accumulation_steps: 2learning_rate: 1.0e-4num_train_epochs: 3.0lr_scheduler_type: cosinewarmup_ratio: 0.1fp16: trueddp_timeout: 180000000### evalval_size: 0.1per_device_eval_batch_size: 1eval_strategy: stepseval_steps: 500
💫使用vim写好后,我们使用 LLaMA-Factory/data/ alpaca_zh_demo.json这个数据集

-
instruction部分描述了任务的具体指令。 input部分通常包含任务所需的输入数据或信息。output部分是模型的输出。
💫开始微调训练
llamafactory-cli train examples/train.yaml

🕛️🕧️🕐️🕜️🕑️🕝️🕒️🕞️🕓️

-
loss :模型在当前批次上的预测结果与实际标签之间的差异。 -
grad_norm:模型参数梯度的范数,反映梯度的大小,用于监控梯度爆炸或梯度消失的问题。 -
learning_rate:学习率是优化器在更新模型参数时使用的步长。 -
epoch:整个训练数据集被模型完整遍历的次数,一个 epoch 包含多个批次(batch)。

训练指标总结
***** train metrics ***** epoch = 3.0 total_flos = 2906404GF train_loss = 1.0846 train_runtime = 0:04:15.80 train_samples_per_second = 10.555 train_steps_per_second = 5.277
-
epoch: 训练的总轮次(3.0 个 epoch)。
-
total_flos: 训练过程中总共计算的浮点运算次数(2906404 亿次浮点运算)。
-
train_loss: 训练过程中的平均损失值(1.0846)。
-
train_runtime: 训练总共花费的时间(4 分 15.80 秒)。
-
train_samples_per_second: 每秒处理的样本数(10.555 个样本/秒)。
-
train_steps_per_second: 每秒处理的批次数(5.277 个批次/秒)。
💫 训练结束 ~

- 这是模型微调后产生的输出文件,包含了训练过程中生成的各种配置、权重、日志和结果
💯这时我们可以加载这个训练后的模型权重来对话:
from transformers import AutoModelForCausalLM, AutoTokenizerimport torch# 我们的模型输出路径model_name_or_path = \"/home/LLaMA-Factory/output\"model = AutoModelForCausalLM.from_pretrained(model_name_or_path)tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)device = \"cuda\" if torch.cuda.is_available() else \"cpu\"model.to(device)prompt = \"列出一个应该在野营应急包中的7件物品。\"inputs = tokenizer(prompt, return_tensors=\"pt\").to(device)with torch.no_grad(): outputs = model.generate(inputs.input_ids, max_length=50)response = tokenizer.decode(outputs[0], skip_special_tokens=True)print(response)
💦输出:

💯评估
Llamafactory 支持mmlu、cmmlu、ceval三种数据集验证。
llamafactory-cli eval --task mmlu --model_name_or_path /home/Qwen/Qwen2___5-1___5B-Instruct --template qwen --batch_size 1 –n_shot 5

💯推理
我们在LLaMA-Factory/examples 目录下新建一个 infer.yaml 文件进行推理,内容:
model_name_or_path: /home/Qwen/Qwen2___5-1___5B-Instructtemplate: qwen do_sample: false
运行:
llamafactory-cli chat infer.yaml



