Llama 3.2-3B-Instruct PyTorch模型微调最佳实践
引言Meta Llama 3.2多语言大型语言模型聚集(LM)是一个1B和3B大小(文本输入/文本输出)的预训练和指令微调模型聚集。Llama 3.2指令调解的纯文本模型针对多语言对话用例举行了优化,包括智能检索和总结使命。它们在常见的行业基准上优于很多可用的开源和闭源聊天模型。
环境预备
安装Ascend CANN Toolkit和Kernels
安装方法请参考 安装教程或使用以下命令。
登录后复制 # 请替换URL为CANN版本和设备型号对应的URL
# 安装CANN Toolkit
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run
bash Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run --install
# 安装CANN Kernels
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run
bash Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run --install
# 设置环境变量
source /usr/local/Ascend/ascend-toolkit/set_env.sh
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
[*]6.
[*]7.
[*]8.
[*]9.
[*]10.
[*]11.
安装openMind Library以及openMind Hub Client
[*]安装openMind Hub Client
登录后复制 pip install openmind_hub
[*]1.
[*]安装openMind Library,并安装PyTorch框架及其依靠。
登录后复制 pip install openmind
[*]1.
更具体的安装信息请参考openMind官方的 环境安装章节。
安装llama-factory
登录后复制 git clone https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e "."
[*]1.
[*]2.
[*]3.
模型链接和下载
Llama-3.2-3B模型系列由社区开发者在魔乐社区贡献,包括:
[*]Llama-3.2-3B: https://modelers.cn/models/AI-Research/Llama-3.2-3B
[*]Llama-3.2-3B-Instruct: https://modelers.cn/models/AI-Research/Llama-3.2-3B-Instruct
通过Git从魔乐社区下载模型的repo,以Llama-3.2-3B-Instruct为例:
登录后复制 # 首先保证已安装git-lfs(https://git-lfs.com)
git lfs install
git clone https://modelers.cn/AI-Research/Llama-3.2-3B-Instruct.git
[*]1.
[*]2.
[*]3.
模型推理
用户可以使用openMind Library大概LLaMa Factory举行模型推理,以Llama-3.2-3B-Instruct为例,具体如下:
[*]使用openMind Library举行模型推理
新建推理脚本inference_llama3.2_3b_chat.py,推理脚本内容为:
登录后复制 import argparse
import torch
from openmind import pipeline
from openmind_hub import snapshot_download
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default=None,
)
args = parser.parse_args()
return args
def main():
args = parse_args()
if args.model_name_or_path:
model_path = args.model_name_or_path
else:
model_path = snapshot_download("AI-Research/Llama-3.2-3B-Instruct", revision="main", resume_download=True,
ignore_patterns=["*.h5", "*.ot", "*.mspack"])
pipe = pipeline(
"text-generation",
model=model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
)
messages = [
{"role": "system", "content": ""},
{"role": "user", "content": "你是谁"},
]
outputs = pipe(
messages,
max_new_tokens=256,
)
print(outputs["generated_text"][-1])
if __name__ == "__main__":
main()
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
[*]6.
[*]7.
[*]8.
[*]9.
[*]10.
[*]11.
[*]12.
[*]13.
[*]14.
[*]15.
[*]16.
[*]17.
[*]18.
[*]19.
[*]20.
[*]21.
[*]22.
[*]23.
[*]24.
[*]25.
[*]26.
[*]27.
[*]28.
[*]29.
[*]30.
[*]31.
[*]32.
[*]33.
[*]34.
[*]35.
[*]36.
[*]37.
[*]38.
[*]39.
[*]40.
[*]41.
[*]42.
[*]43.
[*]44.
实行推理脚本:
登录后复制 python inference_llama3.2_3b_chat.py
[*]1.
推理结果如下:
https://img-blog.csdnimg.cn/img_convert/97b5477728aab39671949610f9f44ef0.png
[*]使用LLaMa Factory与模型交互
在LLaMa Factory路径下新建examples/inference/llama3.2_3b_chat.yaml推理配置文件,文件内容为:
登录后复制 model_name_or_path: xxx # 当前仅支持本地加载,填写Llama-3.2-3B-Instruct本地权重路径
template: llama3
[*]1.
[*]2.
使用以下命令与模型举行交互:
登录后复制 llamafactory-cli examples/inference/llama3.2_3b_chat.yaml
[*]1.
交互结果如下:
https://img-blog.csdnimg.cn/img_convert/ac58d61a1f662f9783d08b4a061ed823.png
模型微调
数据集
使用Llama-Factory集成的identity数据集。
修改data/identity.json,将{{name}}替换为openmind,{{author}}替换为shengteng。
微调
新建examples/train_lora/llama3.2_3b_lora_sft.yaml 微调配置文件,微调配置文件如下:
登录后复制 ### model
model_name_or_path: xxx/xxx# 预训练模型路径
### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all
### dataset
dataset: identity
template: llama3
cutoff_len: 1024
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: ./saves/llama3.2-3b/lora/sft
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true
### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000
### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500
[*]1.
[*]2.
[*]3.
[*]4.
[*]5.
[*]6.
[*]7.
[*]8.
[*]9.
[*]10.
[*]11.
[*]12.
[*]13.
[*]14.
[*]15.
[*]16.
[*]17.
[*]18.
[*]19.
[*]20.
[*]21.
[*]22.
[*]23.
[*]24.
[*]25.
[*]26.
[*]27.
[*]28.
[*]29.
[*]30.
[*]31.
[*]32.
[*]33.
[*]34.
[*]35.
[*]36.
[*]37.
[*]38.
使用以下命令举行微调:
登录后复制 llamafactory-cli train examples/train_lora/llama3.2_3b_lora_sft.yaml
[*]1.
微调可视化
[*]训练Loss可视化:
https://img-blog.csdnimg.cn/img_convert/6b007e8f8416794c26704aa05b6e9b72.png
微调后推理
模型推理
修改examples/inference/llama3.2_3b_lora_sft.yaml推理配置文件,文件内容为:
登录后复制 model_name_or_path: xxx # 当前仅支持本地加载,填写Llama-3.2-3B-Instruct本地权重路径
adapter_name_or_path: ./saves/llama3.2-3b/lora/sft
template: llama3
[*]1.
[*]2.
[*]3.
使用以下命令举行推理:
登录后复制 llamafactory-cli chat examples/inference/llama3.2_3b_lora_sft.yaml
[*]1.
推理结果:
https://img-blog.csdnimg.cn/img_convert/00008eaf4557e62c2de6561dcd422758.png
结语
应用使能套件openMind在华为全联接大会2024的展示吸引了我们的注意。通过专家们的分享,得以相识魔乐社区,也相识到openMind在此中发挥的技能能力和未来发展。
通过本次微调的实践,更能体会到openMind套件的魅力。它让微调过程变得更加高效和直观,渴望每一位开发者都来尝试它,一起交换经验,更好地提升它的能力。
相干链接:
openMind Library先容:< https://modelers.cn/docs/zh/openmind-library/overview.html>
openMind Hub Client先容:< https://modelers.cn/docs/zh/openmind-hub-client/overview.html>
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]