基于阿里云*运算的自建 LLM(类 GPT)大型模型 - LLM 入门
最编程
2024-03-15 10:17:37
...
准备工作
接下来将以开源领域比较出名的几个 LLM
为例,跑起来体验一下,开始前做好一些准备工作:
# 安装 git-lfs curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash apt-get install git-lfs
ChatGLM-6B
下载项目:
https://github.com/THUDM/ChatGLM-6B.git # 国内加速 git clone https://ghproxy.com/https://github.com/THUDM/ChatGLM-6B.git # 安装依赖 pip install -r requirements.txt
加载模型:
mkdir -p /mnt/workspace/chatglm-6b git clone https://huggingface.co/THUDM/chatglm-6b /mnt/workspace/chatglm-6b cd /mnt/workspace/chatglm-6b git lfs install git lfs pull
若速度慢,官方[6]提供的手动下载模型文件方案也可参考:
网络异常,图片无法展示
|
运行项目,基于 transformers
快速使用:
网络异常,图片无法展示
|
ChatGLM-6B
提供了 cli&web&api
三种使用方式,使用前请将这三个文件全部修改下模型目录:
- web_demo.py:设置
share=True
可以分享出去 - cli_demo.py
- api.py
比如我演示环境模型目录下载位置是 /mnt/workspace/chatglm-6b
,改动后代码如下:
tokenizer = AutoTokenizer.from_pretrained("/mnt/workspace/chatglm-6b", trust_remote_code=True) model = AutoModel.from_pretrained("/mnt/workspace/chatglm-6b", trust_remote_code=True).half().cuda()
接下来启动运行对应脚本即可体验:
# 以终端为例 python cli_demo.py
MOSS
准备好项目和模型:
git clone https://github.com/OpenLMLab/MOSS.git # 安装依赖 pip install -r requirements.txt
由于硬件问题,我们使用4bit
量化版本的moss-moon-003-sft
模型(默认):
mkdir -p /mnt/workspace/moss-moon-003-sft-int4 git clone https://huggingface.co/fnlp/moss-moon-003-sft-int4 /mnt/workspace/moss-moon-003-sft-int4
然后将moss_cli_demo.py
的 32-34
行代码:
model_path = args.model_name if not os.path.exists(args.model_name): model_path = snapshot_download(args.model_name)
改为:
# model_path = args.model_name # if not os.path.exists(args.model_name): # model_path = snapshot_download(args.model_name) model_path = "/mnt/workspace/moss-moon-003-sft-int4"
然后执行:
python moss_cli_demo.py
如果想使用 web 交互版本:
python moss_web_demo_gradio.py
baichuan-7B
准备好项目和模型:
git clone https://github.com/baichuan-inc/baichuan-7B.git # 国内加速 git clone https://ghproxy.com/https://github.com/baichuan-inc/baichuan-7B.git # 安装依赖 pip install -r requirements.txt # 下载模型 mkdir -p /mnt/workspace/baichuan-7b git clone https://huggingface.co/baichuan-inc/baichuan-7B /mnt/workspace/baichuan-7b cd /mnt/workspace/baichuan-7b git lfs install git lfs pull
运行项目,基于 transformers
快速使用:
pip install accelerate
只能说,勉强能跑:
想要体验对话能力?有大佬已经微调了对应版本,如 baichuan-7b-sft[7] :
mkdir -p /mnt/workspace/baichuan-7b-sft mkdir -p /mnt/workspace/baichuan-7b-sft-offload-dir git clone https://huggingface.co/hiyouga/baichuan-7b-sft /mnt/workspace/baichuan-7b-sft cd /mnt/workspace/baichuan-7b-sft git lfs install git lfs pull git clone https://github.com/hiyouga/LLaMA-Efficient-Tuning python src/cli_demo.py \ --model_name_or_path /mnt/workspace/baichuan-7b \ --checkpoint_dir /mnt/workspace/baichuan-7b-sft \ --prompt_template ziya
执行结果: