1.模型部署

激活环境

conda activate qql

 下载模型以及源码

git clone https://github.com/THUDM/ChatGLM-6B
git clone https://huggingface.co/THUDM/chatglm-6b 

下载模型所需的依赖

pip install -r requirements.txt 

修改cli_demo.py,api.py文件,将模型位置修改为自己的模型路径  

# 修改api.py 用于API模式启动。修改cli_demo.py用于命令行方式启动
tokenizer = AutoTokenizer.from_pretrained("/home/user6/Yh/p-tuning/chatglm3/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("/home/user6/Yh/p-tuning/chatglm3/chatglm-6b", trust_remote_code=True).half().cuda()
# 如果要用网页模式启动,需要按上述方法修改修改web_demo.py文件

测试模型是否能正常部署,选择上述三种方法的其中一种即可。我用的是API模式

# api模式
python api.py  
# web模式
pip install gradio
python web_demo.py
# 命令行模式
python cli_demo.py

 大模型部署成功

2.p-tuning 微调

安装依赖

pip install rouge_chinese nltk jieba datasets

此次使用的数据集是ADGEN ,数据集地址

此数据集会根据输入(content)生成一段广告词(summary)

"content": "类型#裤*版型#宽松*风格#性感*图案#线条*裤型#阔腿裤"
"summary": "宽松的阔腿裤这两年真的吸粉不少,明星时尚达人的心头爱。毕竟好穿时尚,谁都能穿出腿长2米的效果宽松的裤腿,当然是遮肉小能手啊。上身随性自然不拘束,面料亲肤舒适贴身体验感棒棒哒。系带部分增加设计看点,还让单品的设计感更强。腿部线条若隐若现的,性感撩人。颜色敲温柔的,与裤子本身所呈现的风格有点反差萌。"

 修改参数

在train.sh文件中修改
PRE_SEQ_LEN=128
LR=2e-2

CUDA_VISIBLE_DEVICES=0 python3 main.py \
    --do_train \
    --train_file /home/user6/Yh/p-tuning/chatglm3/train.json \         # 修改为自己的训练集数据路径
    --validation_file /home/user6/Yh/p-tuning/chatglm3/dev.json \      # 修改为自己的dev.json路径
    --prompt_column content \
    --response_column summary \
    --overwrite_cache \
    --model_name_or_path /home/user6/Yh/p-tuning/chatglm3/chatglm-6b \  # 修改为自己的模型路径
    --output_dir output/adgen-chatglm-6b-pt-$PRE_SEQ_LEN-$LR \
    --overwrite_output_dir \
    --max_source_length 64 \  											# 最大输入长度
    --max_target_length 64 \											# 最大输出长度
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --gradient_accumulation_steps 16 \
    --predict_with_generate \
    --max_steps 1000 \
    --logging_steps 10 \
    --save_steps 1000 \
    --learning_rate $LR \												# 学习率
    --pre_seq_len $PRE_SEQ_LEN \
    --quantization_bit 4
在evaluate.sh文件中修改
PRE_SEQ_LEN=128
CHECKPOINT=adgen-chatglm-6b-pt-128-2e-2
STEP=3000

CUDA_VISIBLE_DEVICES=0 python3 main.py \
    --do_predict \
    --validation_file /home/user6/Yh/p-tuning/chatglm3/dev.json \  			# 修改为自己的文件路径
    --test_file /home/user6/Yh/p-tuning/chatglm3/dev.json \					# 修改为自己的文件路径
    --overwrite_cache \
    --prompt_column content \
    --response_column summary \
    --model_name_or_path /home/user6/Yh/p-tuning/chatglm3/chatglm-6b \     # 修改为自己的模型路径
    --ptuning_checkpoint ./output/$CHECKPOINT/checkpoint-$STEP \
    --output_dir ./output/$CHECKPOINT \
    --overwrite_output_dir \
    --max_source_length 64 \
    --max_target_length 64 \
    --per_device_eval_batch_size 1 \
    --predict_with_generate \
    --pre_seq_len $PRE_SEQ_LEN \
    --quantization_bit 4

进行训练

bash train.sh

 进行推理

bash evaluate.sh

 训练结果

{
    "epoch": 0.28,
    "predict_bleu-4": 6.766246635514018,
    "predict_rouge-1": 26.911804485981307,
    "predict_rouge-2": 5.948947476635514,
    "predict_rouge-l": 23.03057280373832,
    "predict_runtime": 2856.6571,
    "predict_samples": 1070,
    "predict_samples_per_second": 0.375,
    "predict_steps_per_second": 0.375,
    "train_loss": 3.8562335205078124,
    "train_runtime": 5804.5567,
    "train_samples": 114599,
    "train_samples_per_second": 5.513,
    "train_steps_per_second": 0.172
}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐