天津商城网站设计公司,绍兴网站建设seo,福州互联网公司排行榜,网站设计与网站制作简介
紧接前文#xff1a;
万字长文细说ChatGPT的前世今生Llama 2实战(上篇):本地部署(附代码)
上篇主要介绍Llama2的基本情况和基于官方模型实测Llama2在中英上的效果#xff0c;包括单轮和多轮对话。今天这篇小作文作为Llama2的下篇#xff0c;主要介绍如何用中文语料对…简介
紧接前文
万字长文细说ChatGPT的前世今生Llama 2实战(上篇):本地部署(附代码)
上篇主要介绍Llama2的基本情况和基于官方模型实测Llama2在中英上的效果包括单轮和多轮对话。今天这篇小作文作为Llama2的下篇主要介绍如何用中文语料对Llama 2的基座模型(7B版)进行微调并实测微调后模型的效果。本文实验完整代码请找小编索取。后续将实战Code Llama感兴趣的小伙伴可以关注下
数据准备
基座模型下载可以参考Llama 2实战(上篇):本地部署(附代码)中的模型下载部分。官方Repo: llama-recipes里面提到支持的默认数据格式有三种grammar_dataset, alpaca_datasetand和samsum_dataset。为了节约时间选择alpaca格式的中文SFT数据集alpaca格式的数据示例如下
[{instruction: 根据给定的坐标确定最近的机场。,input: 40.728157, -73.794853,output: 距给定坐标最近的机场是纽约市的拉瓜迪亚机场 (LGA)。},{instruction: 输出不同种类水果的列表,input: ,output: 1. 苹果\n2. 香蕉\n3. 橘子\n4. 芒果\n5. 草莓\n6. 葡萄\n7. 蓝莓\n8. 樱桃\n9. 猕猴桃\n10. 甜瓜\n11.菠萝\n12. 李子\n13.桃子},{instruction: 找出字串中隐藏的信息,input: 业余咖啡,output: 隐藏的消息是“咖啡因爱好者”。}]本次实验具体选择的数据集是:GuanacoDataset/guanaco_non_chat-utf8.json。完成数据下载之后将guanaco_non_chat-utf8.json放到 llama-recipes/ft_datasets目录下并重命名为 alpaca_data.json。训练时将训练集指定为 alpaca_dataset即可llama-recipes 将会自动找到这个文件。至此SFT微调数据集准备完成。
中文微调
微调训练脚本使用llama_finetuning.py脚本具体如下
# 单机, 多卡
export CUDA_VISIBLE_DEVICES1,2,3
torchrun --nnodes 1 --nproc_per_node 3 llama_finetuning.py \--enable_fsdp \--use_peft \--peft_method lora \--model_name /home/model_zoo/LLM/llama2/Llama-2-7b-hf/ \--dataset alpaca_dataset \--pure_bf16 \--batch_size_training 50 \--num_epochs 2 \--output_dir /home/LLM/llama-recipes/PEFT/model微调结束后在/home/LLM/llama-recipes/PEFT/model生成adapter_config.json和adapter_model.bin文件这就是微调后的参数结果。
模型inference
使用如下脚本加载微调后的模型进行inference
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time : 2023/09/03 10:15
# Author : 卖热干面的小女孩
# File : inference_multi_gpus_one_node.py
# 联系方式 : 《微-心-公-众-号 小窗幽记机器学习》import pdb
import torch
from transformers import LlamaForCausalLM, LlamaTokenizer
from transformers import GenerationConfig
from peft import PeftModel, PeftConfigpeft_model_id ./PEFT/model/
temperature 0.0
max_new_tokens 256
config PeftConfig.from_pretrained(peft_model_id)
model LlamaForCausalLM.from_pretrained(config.base_model_name_or_path)
model PeftModel.from_pretrained(model, peft_model_id)
tokenizer LlamaTokenizer.from_pretrained(config.base_model_name_or_path)device cuda:2model model.to(device)
model.eval()
# test_prompt ### Instruction: \n你谁 \n### Response:\n
# test_prompt ### Instruction: \n你叫做爱坤擅长舞蹈、篮球、武术篮球、舞蹈篮球喜欢Rap能唱会跳偶像是马保国\n### Response:\n
# test_prompt ### Instruction: 根据以下信息回答问题。爱坤的偶像是谁直接给出答案即可不要输出其他\n### Input: \n你叫做爱坤擅长舞蹈、篮球、武术篮球、舞蹈篮球喜欢Rap能唱会跳偶像是马保国\n ### Response:\n
# test_prompt ### Instruction: 根据以下信息爱坤擅长舞蹈、篮球、武术篮球、舞蹈篮球喜欢Rap能唱会跳偶像是马保国 回答问题。爱坤的偶像是谁直接给出答案即可。\n### Response:\n
test_prompt ### Instruction: 根据以下信息爱坤擅长舞蹈、篮球、武术篮球、舞蹈篮球喜欢Rap能唱会跳偶像是马保国 回答问题。爱坤崇拜谁直接给出答案即可。\n### Response:\n
inputs tokenizer(test_prompt, return_tensorspt)
model_input tokenizer(test_prompt, return_tensorspt).to(device)input_ids model_input[input_ids].to(device)
generation_config GenerationConfig(temperaturetemperature,top_p0.75,top_k40,num_beams4,repetition_penalty2.0
)with torch.no_grad():generation_output model.generate(input_idsmodel_input[input_ids],generation_configgeneration_config,return_dict_in_generateTrue,output_scoresTrue,max_new_tokensmax_new_tokens,)
s generation_output.sequences[0]
output tokenizer.decode(s)
print(output, output)输出结果如下
output s ### Instruction: 根据以下信息爱坤擅长舞蹈、篮球、武术篮球、舞蹈篮球喜欢Rap能唱会跳偶像是马保国 回答问题。爱坤崇拜谁直接给出答案即可。
### Response:
爱坤崇拜马保国/s可以看出经过中文语料的简单微调可以较好地支持中文问答。
模型部署
上述方式以脚本方式加载模型并做inference。那么如果想要向外提供服务可以使用alpaca-lora提供的脚本部署Web服务。这里为了减缓模型的重复回复添加了 repeatition penalty CUDA_VISIBLE_DEVICES1 python3 generate.py --base_model /home/model_zoo/LLM/llama2/Llama-2-7b-hf/ --lora_weights /home/Repository/LLM/llama-recipes/PEFT/model