chatglm3-6b部署及微调

chatglm3-6b部署及微调

插播:如果你想私有化部署商业版gpt
演示地址:https://chat.apeto.cn
项目介绍:点我直达

  • modelscope: https://modelscope.cn/models/ZhipuAI/chatglm3-6b/files
  • github: https://github.com/THUDM/ChatGLM3
  • 镜像: ubuntu20.04-cuda11.7.1-py38-torch2.0.1-tf1.15.5-1.8.1
  • v100 16G现存 单卡

安装

软件依赖

# 非必要无需执行
# pip install --upgrade pip

pip install modelscope>=1.9.0

下载及调用

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

项目下载

mkdir py
git clone https://github.com/modelscope/swift.git
cd swift

安装依赖:

# 已安装忽略
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

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: 你的modelscope平台的token该参数只有在push_to_hub设置为True时才生效.
  • gradient_accumulation_steps 根据显卡性能调整 v100 我这里用的16 高级显卡可以设置32
  • max_length 根据显卡性能调整 v100我这里用的是2048 高级显卡可以调整成4096
  • 剩余其他参数默认即可
# 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 2048 \
    --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 这个目录下进行 记得给脚本权限chmod +x llm/*.py

./scripts/chatglm3_6b/lora_ddp_ds/sft.sh

推理

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

# Experimental environment: v100
PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0 \
python llm_infer.py \
    --model_id_or_path ZhipuAI/chatglm3-6b \
    --model_revision v1.0.2 \
    --sft_type lora \
    --template_type chatglm3 \
    --dtype fp16 \
    # --ckpt_dir "output/chatglm3-6b/vx_xxx/checkpoint-xxx" \
    --eval_human false \
    --dataset damo-agent-mini-zh \
    --max_length 2048 \
    --max_new_tokens 2048 \
    --temperature 0.9 \
    --top_k 20 \
    --top_p 0.9 \
    --do_sample true \
    --merge_lora_and_save false \

常见问题

1.显卡驱动

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
https://pytorch.org/get-started/previous-versions/

# 查看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
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
回答: ChatGLM2-6B是一个AI模型,引用提到了对ChatGLM2-6B进行P-tuning微调的过程。微调是指在已有的预训练模型基础上,使用特定的数据集进行进一步的训练,以提高模型在特定任务上的性能。在这个例子中,ChatGLM2-6B通过本地部署和本地微调的方式被成功跑通了。具体来说,本地部署是指将模型部署到本地环境中进行使用,而本地微调是指在本地环境中使用特定的数据集对模型进行微调训练。引用中提到了在Windows系统上跑通了ChatGLM2-6B。引用则提到了进行微调时可以更换模型路径以及注意微调目前只支持单轮对话的回复。总之,ChatGLM2-6B经过微调可以在特定任务上表现更好。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [ChatGLM2-6B! 我跑通啦!本地部署+微调(windows系统)](https://blog.csdn.net/m0_54515450/article/details/131617081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [ChatGLM2-6BChatGLM-6B 模型介绍及训练自己数据集实战](https://blog.csdn.net/dream_home8407/article/details/130099656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿道apeto

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值