网址:https://gitclone.com/aiit/chat/
网址:互链高科
进入LLaMA-Efficient-Tuning根目录后,有一个data目录,创建一个自己的训练数据文件,录入内容格式如下:
[{"instruction":"阅读下列短文,从每题所给的四个选项《A、 B、 C和D)中。选出最佳选项。",
"input":"sdfdsg",
"output:"A"}]训练数据文件配置到dataset_info.json中
vim run.sh
Model_path="model_path"
save_Model_dir="save_model"
dataset="qwentest"CUDA_VISIBLE_DEVICES=0
torchrun --nproc_per_node 1 src/train_bash.py \
--model_name_or_path ${Model_path} \
--stage sft \--do_train \
--dataset ${dataset} \
--dataset_dir data \
--finetuning_type lora \
--output_dir ${save_Model_dir} \
--overwrite_cache \
--per_device_train_batch_size 4 \
--gradient_accumulation_steps 1 \
--lr_scheduler_type cosine \
--logging_steps 10 \
--save_steps 1000 \--learning_rate 1e-3 \
--num_train_epochs 10.0 \
--plot_loss \--lora_target c_attn \
--template chatml \
--fp16
3、执行nohup ./finetune_llm.sh &
from transformers import AutoTokenizer,AutoModelForCausalLM, AutoConfig
from peft import PeftModelfrom transformers.generation import GenerationConfig
import osmodel_path = "原始模型"
ckpt_path = "lora微调后的模型"save_pre_name = os.path.basename(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)base_model = AutoModelForCausaLLM.from_pretrained(model_path,device_map={"":0},trust_remote_code=True)
base_model.generation_config = GenerationConfig.from_pretrained(model_path, trust_remote_code=True)
lora_model = PeftModel.from_pretrained(base_model, ckpt_path)
lora_model.to('cuda:4')
lora_model.eval()input = ""
line_format=input.replace("######","\n")llm_question="ddsfdsfddsfsdg\nA. 非常满意\nB. 满意\nC. 不满意\nD. 非常不满意\n答案:".format(line_format)
inputs = tokenizer(llm_question, return_tensors='pt')
inputs = inputs.to(lora_model.device)
pred = lora_model.generate(**inputs, max_new_tokens=500)
predict_txt = tokenizer.decode(pred.cpu()[0], skip_special_tokens=True).replace("\n", " ")
print(predict_txt)
进入LLaMA-Efficient-Tuning根目录后,有一个data目录,创建一个自己的训练数据文件,录入内容格式如下:
[{"instruction":"阅读下列短文,从每题所给的四个选项《A、 B、 C和D)中。选出最佳选项。",
"input":"sdfdsg",
"output:"A"}]训练数据文件配置到dataset_info.json中
{
"alpaca_zh":{
"filename":"yourpath"
},
"youtrainname":
{
"filename":"yourpath"
}
}
其中alpaca_zh不参加训练,但是在模型导出时,若不配会报错
Model_path="model_path"
dataset="datasetname"
save_Model_dir="save_model"
CUDA_VISIBLE_DEVICES=0,1,2
torchrun --nproc_per_node 3 src/train_bash.py \
--model_name_or_path ${Model_path} \
--stage sft \
--do_train \
--dataset ${dataset} \
--finetuning_type lora \
--output_dir ${save_Model_dir} \
--overwrite_cache \
--per_device_train_batch_size 10 \
--gradient_accumulation_steps 1 \
--lr_scheduler_type cosine \
--logging_steps 10 \
--save_steps 200 \
--learning_rate 5e-5 \
--num_train_epochs 10.0 \
--plot_loss \
--fp16
model_path="basemodelpath"
python export_model.py \
--model_name_or_path ${model_path} \
--finetuning_type lora \
--checkpoint_dir lora_model_path
--output_dir final_model_path
训练完的模型调用(未进行lora模型导出合并时使用)
from transformers import AutoTokenizer,AutoModelForCausalLM, AutoConfig
from peft import PeftModelimport os
model_path = "原始模型"
ckpt_path = "lora微调后的模型"save_pre_name = os.path.basename(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)base_model= AutoModel.from_pretrained(model_path, trust_remote_code=True)
lora_model = PeftModel.from_pretrained(base_model, ckpt_path)
lora_model.to('cuda:4')
lora_model.eval()input = ""
line_format=input.replace("######","\n")llm_question="ddsfdsfddsfsdg\nA. 非常满意\nB. 满意\nC. 不满意\nD. 非常不满意\n答案:".format(line_format)
predict_txt, history= lora_model.chat(tokenizer, llm_question, history=[])
print(predict_txt)
若进行了模型导出时应用
tokenizer=AutoTokenizer.from_pretrained(model_path,trust_remote_code=True)
model = AutoModel.from_pretrained(model_path,trust_remote_code=True,device="cuda:0")
llm_question="你好"
predict,history = my_model.chat(tokenizer, llm_question, history=[])