MOSS模型量化版部署过程

发布时间:2025-12-09 16:20:29 浏览次数:18

文章目录

  • 项目背景
  • 配置环境与准备
  • 部署推理
    • 命令行部署
      • 报错1
      • 报错2:
    • 网页版部署

项目背景

2023年4月21日,复旦大学自然语言处理实验室正式开放MOSS模型,是国内首个插件增强的开源对话大语言模型。MOSS 相关代码、数据、模型参数已在 GitHub 和 Hugging Face 等平台开放,
项目地址:https://github.com/OpenLMLab/MOSS。

MOSS 对硬件要求还是较高,如果想本地部署的话,仍但需要非常大的开销。如果使用A100 或 A800 可以单卡运行,而使用NVIDIA 3090 只能并行多卡运行,GPU 显存至少需要 30GB。但如果想在消费级显卡上进行部署,就只能部署量化后的版本,如下图所示:

具体对应的模型版本如下:

moss-moon-003-sft-int4: 4bit量化版本的moss-moon-003-sft模型,约占用12GB显存即可进行推理。
moss-moon-003-sft-int8: 8bit量化版本的moss-moon-003-sft模型,约占用24GB显存即可进行推理。
moss-moon-003-sft-plugin-int4: 4bit量化版本的moss-moon-003-sft-plugin模型,约占用12GB显存即可进行推理。
moss-moon-003-sft-plugin-int8: 8bit量化版本的moss-moon-003-sft-plugin模型,约占用24GB显存即可进行推理。

由于本次实验所使用的配置如下:
CPU&内存:28核(vCPU)112 GB
操作系统:Ubuntu_64
GPU:NVIDIA Tesla P100
显存:16G

故选择 moss-moon-003-sft-int4 模型进行部署实验。

配置环境与准备

1、登录服务器,下载本仓库内容至服务器,进入MOSS目录

git clone https://github.com/OpenLMLab/MOSS.gitcd MOSS

2、创建python3.8版本的conda环境,并进入

conda create --name moss python=3.8conda activate moss

3、根据 requirements.txt 安装环境依赖

pip install -r requirements.txt

4、安装使用量化模型所需的包(4/8-bit)

pip install triton

5、安装gradio,以便可以使用基于Gradio的网页demo

pip install gradiopip install mdtex2html

其中,mdtex2html 是因为运行时有报错缺少此包,故这里可以提前装好。

6、下载模型文件,这里直接从huggingface上下载到服务器

# 安装 huggingface_hubpip install huggingface_hub# 进入模型文件的指定位置cd /data/sim_chatgpt/

然后使用下面代码,将模型文件安装到指定位置

from huggingface_hub import snapshot_downloadsnapshot_download(repo_id="fnlp/moss-moon-003-sft-int4", cache_dir="./moss-moon-003-sft-int4")

下载完成后,得到model_path为:/data/sim_chatgpt/moss-moon-003-sft-int4/models–fnlp–moss-moon-003-sft-int4/snapshots/4e33058483a36ade9067f7f25f3127cb95386b01

部署推理

这里有两种方式,一种是命令行,在服务器上直接运行;一种是网页版,可以通过链接在浏览器访问。

命令行部署

这里我们要运行 moss_cli_demo.py 文件,在运行前需要修改model_path,如下:

parser.add_argument("--model_name", default="/data/sim_chatgpt/moss-moon-003-sft-int4/models--fnlp--moss-moon-003-sft-int4/snapshots/4e33058483a36ade9067f7f25f3127cb95386b01"

报错1

ValueError:
/data/sim_chatgpt/moss-moon-003-sft-int4/models–fnlp–moss-moon-003-sft-int4/snapshots/4e33058483a36ade9067f7f25f3127cb95386b01
is not a folder containing a .index.json file.

解决办法
将下面代码进行替换(注释掉)

# model = load_checkpoint_and_dispatch(# raw_model, model_path, device_map="auto", no_split_module_classes=["MossBlock"], dtype=torch.float16# )model = MossForCausalLM.from_pretrained(model_path, trust_remote_code=True).half().cuda()

报错2:

运行时报错,如下图所示:

python: /project/lib/Analysis/Utility.cpp:136: bool mlir::supportMMA(mlir::Value, int): Assertion `(version == 1 || version == 2) && “Unexpected MMA layout version found”’ failed.
遇到问题:https://github.com/OpenLMLab/MOSS/issues/149

issue中有人提到:

  • 算力小于70的显卡都不支持Float8 and Float16,P100 P40算力版本都是60+所以暂时只能使用Float32,但是显存又不够。NVDIA V100 NVIDIA TITAN V及其以上显卡可以支持。
  • triton官网说对fp16量化模型支持不完善, p100/40等老显卡都会报如上的错. 需要等他们写入更多老显卡支持。
    另外有人实测V100 32GB可以跑int4量化模型.

解决方法如下
将triton换成auto-gptq,这样就绕过了triton验证.

git clone https://github.com/PanQiWei/AutoGPTQconda create -n moss python==3.10cd MOSSpython setup_env.py --install_auto_gptq

另外,需要注意需要修改两个地方:
1、修改model
2、修改model_path

运行

python moss_cli_demo.py

但我并没有尝试成功,一个原因在于,github链接中没有setup_env.py,只有setup.py,另一个原因是即使按照github链接中提供的安装方式,无法安装成功。

待更…

网页版部署

命令行版本成功后更新网页版部署

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477