赞
踩
插播:如果你想私有化部署商业版gpt
演示地址:https://chat.apeto.cn
项目介绍:点我直达
# 非必要无需执行
# 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
将脚本放在swift/examples/pytorch/llm/scripts/chatglm3_6b/lora_ddp_ds这个目录下
# 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 \
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。