chatglm3-6b部署及微调

chatglm3-6b部署及微调

安装

软件依赖

shell 复制代码
pip install --upgrade pip

pip install deepspeed -U

pip install modelscope>=1.9.0

pip install protobuf 'transformers>=4.30.2' cpm_kernels 'torch>=2.0' gradio mdtex2html sentencepiece accelerate

下载及调用

shell 复制代码
from modelscope import AutoTokenizer, AutoModel, snapshot_download
model_dir = snapshot_download("ZhipuAI/chatglm3-6b", revision = "v1.0.2")
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().cuda()
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

微调

数据集: https://modelscope.cn/datasets/damo/MSAgent-Bench/summary

项目: https://github.com/modelscope/swift

项目下载

shell 复制代码
mkdir py
git clone https://github.com/modelscope/swift.git
cd swift

# 多环境设置(可选)
# python -m venv swift
# source swift/bin/activate

安装依赖:

shell 复制代码
# 已安装忽略
pip install ms-swift

# 已安装忽略
pip install modelscope>=1.9.0

# 设置pip全局镜像和安装相关的python包
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
git clone https://github.com/modelscope/swift.git
cd swift
pip install .[llm]
# 下面的脚本需要在此目录下执行
cd examples/pytorch/llm


# 如果你想要使用deepspeed
pip install deepspeed -U


# 如果你想要使用基于auto_gptq的qlora训练. (推荐, 效果优于bnb)
# 使用auto_gptq的模型: qwen-7b-chat-int4, qwen-14b-chat-int4, qwen-7b-chat-int8, qwen-14b-chat-int8
pip install auto_gptq optimum -U


# 如果你想要使用基于bnb的qlora训练.
pip install bitsandbytes -U

脚本sft.sh

将脚本放在swift/examples/pytorch/llm/scripts/chatglm3_6b/lora_ddp_ds这个目录下

  • 单显卡: CUDA_VISIBLE_DEVICES=0
  • 模型ID: model_id_or_path ZhipuAI/chatglm3-6b
  • 模型版本: model_revision v1.0.2
  • dtype: 如果是老显卡比如V100 是不支持bf16的 需要指定为: fp16
  • 模板类型: template_type chatglm3
  • 数据集: dataset damo-agent-mini-zh 这里采用达摩院的agent
  • lora_rank和lora_alpha 注意: lora_alpha一定要是lora_rank 2倍质量最高
  • hub_token: 本地模型不需要填写
  • gradient_accumulation_steps 根据你的服务器性能调整大小 性能不好则值相对较小 v100
  • 剩余其他参数默认即可
shell 复制代码
# v100 16G 单卡
nproc_per_node=1

PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0 \
torchrun \
    --nproc_per_node=$nproc_per_node \
    --master_port 29500 \
    llm_sft.py \
    --model_id_or_path ZhipuAI/chatglm3-6b \
    --model_revision v1.0.2 \
    --sft_type lora \
    --tuner_backend swift \
    --template_type chatglm3 \
    --dtype fp16 \
    --output_dir output \
    --dataset damo-agent-mini-zh \
    --train_dataset_sample -1 \
    --num_train_epochs 1 \
    --max_length 4096 \
    --lora_rank 8 \
    --lora_alpha 16 \
    --lora_dropout_p 0.05 \
    --lora_target_modules AUTO \
    --gradient_checkpointing true \
    --batch_size 1 \
    --weight_decay 0. \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps 16 \
    --max_grad_norm 0.5 \
    --warmup_ratio 0.03 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 2 \
    --logging_steps 10 \
    --push_to_hub false \
    --hub_model_id chatglm3-6b-lora \
    --hub_private_repo true \
    --hub_token '改成你的token' \
    --deepspeed_config_path 'ds_config/zero2.json' \
    --only_save_model true \

运行脚本

注意: 要在 swift/examples/pytorch/llm 这个目录下进行

shell 复制代码
./scripts/chatglm3_6b/lora_ddp_ds/sft.sh

常见问题

1.显卡驱动

shell 复制代码
RuntimeError: The NVIDIA driver on your system is too old (found version 11080). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver.
解决方案

错误提示显卡驱动较老 其实可能是torch版本太高导致的问题 我们用的是2.0.1 请检查你的版本是否是2.0.1

shell 复制代码
# 查看torch版本
python
import torch
print(torch.__version__)

# 查看CUDA版本
nvidia-smi

# 卸载过高的版本
pip uninstall torch

# 访问官方查看对应版本: https://pytorch.org/get-started/previous-versions/  以cuda 11.8 pytorch:2.0.1 举例  
conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.8 -c pytorch -c nvidia

2.编码问题

shell 复制代码
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
解决方案
shell 复制代码
export PYTHONIOENCODING=utf-8
相关推荐
心无旁骛~20 分钟前
python多进程和多线程问题
开发语言·python
星云数灵21 分钟前
使用Anaconda管理Python环境:安装与验证Pandas、NumPy、Matplotlib
开发语言·python·数据分析·pandas·教程·环境配置·anaconda
计算机毕设匠心工作室44 分钟前
【python大数据毕设实战】青少年抑郁症风险数据分析可视化系统、Hadoop、计算机毕业设计、包括数据爬取、数据分析、数据可视化、机器学习
后端·python
计算机毕设小月哥1 小时前
【Hadoop+Spark+python毕设】智能制造生产效能分析与可视化系统、计算机毕业设计、包括数据爬取、Spark、数据分析、数据可视化、Hadoop
后端·python·mysql
计算机毕设小月哥3 小时前
【Hadoop+Spark+python毕设】中风患者数据可视化分析系统、计算机毕业设计、包括数据爬取、Spark、数据分析、数据可视化、Hadoop
后端·python·mysql
Keep_Trying_Go3 小时前
基于Zero-Shot的目标计数算法详解(Open-world Text-specified Object Counting)
人工智能·pytorch·python·算法·多模态·目标统计
计算机毕设匠心工作室3 小时前
【python大数据毕设实战】强迫症特征与影响因素数据分析系统、Hadoop、计算机毕业设计、包括数据爬取、数据分析、数据可视化、机器学习、实战教学
后端·python·mysql
Trouville015 小时前
Pycharm软件初始化设置,字体和shell路径如何设置到最舒服
ide·python·pycharm
高-老师5 小时前
WRF模式与Python融合技术在多领域中的应用及精美绘图
人工智能·python·wrf模式