当前位置: 首页 > news >正文

ubuntu 部署 ChatGLM-6B 完整流程 模型量化 Nvidia

ubuntu 部署 ChatGLM-6B 完整流程 模型量化 Nvidia

  • 环境与设备
    • 环境准备
    • 克隆模型
    • 代码部署 ChatGLM-6B
    • 完整代码

ChatGLM-6B 是一个开源的、支持中英双语的对话语言模型,基于 General Language Model (GLM) 架构,具有 62 亿参数。结合模型量化技术,用户可以在消费级的显卡上进行本地部署(INT4 量化级别下最低只需 6GB 显存)。 ChatGLM-6B 使用了和 ChatGPT 相似的技术,针对中文问答和对话进行了优化。经过约 1T 标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62 亿参数的 ChatGLM-6B 已经能生成相当符合人类偏好的回答

本篇文章将介绍ubuntu 部署 ChatGLM-6B 完整流程 模型量化 Nvidia GUP

希望能写一些简单的教程和案例分享给需要的人

环境与设备

系统:Ubuntu 22.04.2 LTS (ubuntu 就行)
设备:Nvidia GeForce RTX 4090 (英伟达 就行)

以下是一些推荐的消费级显卡:

  1. Nvidia GeForce RTX 3080: RTX 3080 是一款性能出色的显卡,适用于高质量游戏和深度学习任务。它提供了强大的图形性能和 CUDA 核心,能够满足许多高性能计算需求。

  2. AMD Radeon RX 6800 XT: 如果你对 AMD 的显卡感兴趣,RX 6800 XT 是一款强大的选择。它具有出色的游戏性能和计算能力,适用于多种应用场景。

  3. Nvidia GeForce RTX 3070: RTX 3070 是一款性价比较高的显卡,它在性能和价格之间找到了很好的平衡。它适用于游戏、图形设计和一些中等规模的深度学习任务。

环境准备

在开始之前,确保 Ubuntu 系统已经安装了Python和必要的依赖项。

输入下面命令:判断PIP是否安装

 pip --version

输入
如果没安装,就安装 python3-pip

sudo apt update
sudo apt install python3-pip

安装完成后如下图:

在这里插入图片描述

克隆模型

全部都完成后,我们就可以去下载模型了

去下面这个网站,下载模型

https://huggingface.co/THUDM/chatglm2-6b-32k

在这里插入图片描述

点击克隆后,我们需要使用命令:

git lfs install
git clone https://huggingface.co/THUDM/chatglm2-6b-32k

这个时候,可能会遇到报错:需要安装 git ,还有 git-lfs

在这里插入图片描述

sudo apt install gitsudo apt install git-lfs

这两个都安装完成后,我们再克隆,我这边会到指定的路径克隆,大家自行选择。

克隆成功后,如下图:

在这里插入图片描述

代码部署 ChatGLM-6B

git clone https://github.com/THUDM/ChatGLM-6B.git

在这里插入图片描述

代码克隆下来后,就安装环境 pytorch

PyTorch 是一个开源的机器学习框架,它提供了丰富的工具和库,用于构建和训练各种深度学习模型。它由 Facebook 的人工智能研究院(Facebook AI Research,缩写为FAIR)开发并维护,旨在为研究人员和开发者提供一个灵活、动态的平台来实现各种机器学习任务。

PyTorch 提供了一种动态计算图的机制,这意味着您可以在运行时构建、修改和调整计算图,使其更加灵活和直观。这使得 PyTorch 在实验和原型开发阶段非常有用,因为它能够快速适应不同的数据和模型结构。此外,PyTorch 还具有广泛的神经网络库、优化算法以及用于数据加载和预处理的工具。它也支持 GPU 加速,可以在 NVIDIA CUDA 上利用 GPU 进行高效的计算,加速模型训练过程。总之,PyTorch 是一个受欢迎的机器学习框架,广泛用于深度学习研究、开发和应用。它以其动态计算图、灵活性和易用性而闻名。

直接进入下面网址

https://pytorch.org/

进入页面后,翻到下一页,我这里是ubuntu 所以我这边用预览版最新的 CUDA 12.1
ubuntu

关于CUDA的支持可以通过命令 nvidia-smi 来查看

在这里插入图片描述
我们执行命令

在这里插入图片描述

pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu121

等待安装结束

在这里插入图片描述
都按照完成后,进入项目,我这里项目路径是这个 /home/ai/dev/code

cd /home/ai/dev/code
cd /home/ai/dev/code/ChatGLM-6B

然后安装 环境

pip install -r requirements.txt

等待这些都安装完成后,

api.py 文件中的路径:

将原本的:THUDM/chatglm-6b

更换成:/home/ai/dev/model/chatglm2-6b-32k

/home/ai/dev/model/chatglm2-6b-32k

在这里插入图片描述

执行下面命令:

python3 api.py

在这里插入图片描述
测试一下:

curl -X POST "http://127.0.0.1:8000" \-H 'Content-Type: application/json' \-d '{"prompt": "你好", "history": []}'

在这里插入图片描述

上面是 API 的效果。如果选需要 ui 版本 web_demo.py 这个文件 修改模型路径后,执行:

python3 web_demo.py

修改截图如下:

在这里插入图片描述

方便外网请求的修改地方如下:

在这里插入图片描述

执行结果如下:

在这里插入图片描述

在这里插入图片描述

完整代码

api.py

from fastapi import FastAPI, Request
from transformers import AutoTokenizer, AutoModel
import uvicorn, json, datetime
import torchDEVICE = "cuda"
DEVICE_ID = "0"
CUDA_DEVICE = f"{DEVICE}:{DEVICE_ID}" if DEVICE_ID else DEVICEdef torch_gc():if torch.cuda.is_available():with torch.cuda.device(CUDA_DEVICE):torch.cuda.empty_cache()torch.cuda.ipc_collect()app = FastAPI()@app.post("/")
async def create_item(request: Request):global model, tokenizerjson_post_raw = await request.json()json_post = json.dumps(json_post_raw)json_post_list = json.loads(json_post)prompt = json_post_list.get('prompt')history = json_post_list.get('history')max_length = json_post_list.get('max_length')top_p = json_post_list.get('top_p')temperature = json_post_list.get('temperature')response, history = model.chat(tokenizer,prompt,history=history,max_length=max_length if max_length else 2048,top_p=top_p if top_p else 0.7,temperature=temperature if temperature else 0.95)now = datetime.datetime.now()time = now.strftime("%Y-%m-%d %H:%M:%S")answer = {"response": response,"history": history,"status": 200,"time": time}log = "[" + time + "] " + '", prompt:"' + prompt + '", response:"' + repr(response) + '"'print(log)torch_gc()return answerif __name__ == '__main__':tokenizer = AutoTokenizer.from_pretrained("/home/ai/dev/model/chatglm2-6b-32k", trust_remote_code=True)model = AutoModel.from_pretrained("/home/ai/dev/model/chatglm2-6b-32k", trust_remote_code=True).half().cuda()model.eval()uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)

web_demo.py

from transformers import AutoModel, AutoTokenizer
import gradio as gr
import mdtex2htmltokenizer = AutoTokenizer.from_pretrained("/home/ai/dev/model/chatglm2-6b-32k", trust_remote_code=True)
model = AutoModel.from_pretrained("/home/ai/dev/model/chatglm2-6b-32k", trust_remote_code=True).half().cuda()
model = model.eval()"""Override Chatbot.postprocess"""def postprocess(self, y):if y is None:return []for i, (message, response) in enumerate(y):y[i] = (None if message is None else mdtex2html.convert((message)),None if response is None else mdtex2html.convert(response),)return ygr.Chatbot.postprocess = postprocessdef parse_text(text):"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""lines = text.split("\n")lines = [line for line in lines if line != ""]count = 0for i, line in enumerate(lines):if "```" in line:count += 1items = line.split('`')if count % 2 == 1:lines[i] = f'<pre><code class="language-{items[-1]}">'else:lines[i] = f'<br></code></pre>'else:if i > 0:if count % 2 == 1:line = line.replace("`", "\`")line = line.replace("<", "&lt;")line = line.replace(">", "&gt;")line = line.replace(" ", "&nbsp;")line = line.replace("*", "&ast;")line = line.replace("_", "&lowbar;")line = line.replace("-", "&#45;")line = line.replace(".", "&#46;")line = line.replace("!", "&#33;")line = line.replace("(", "&#40;")line = line.replace(")", "&#41;")line = line.replace("$", "&#36;")lines[i] = "<br>"+linetext = "".join(lines)return textdef predict(input, chatbot, max_length, top_p, temperature, history):chatbot.append((parse_text(input), ""))for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,temperature=temperature):chatbot[-1] = (parse_text(input), parse_text(response))       yield chatbot, historydef reset_user_input():return gr.update(value='')def reset_state():return [], []with gr.Blocks() as demo:gr.HTML("""<h1 align="center">ChatGLM</h1>""")chatbot = gr.Chatbot()with gr.Row():with gr.Column(scale=4):with gr.Column(scale=12):user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(container=False)with gr.Column(min_width=32, scale=1):submitBtn = gr.Button("Submit", variant="primary")with gr.Column(scale=1):emptyBtn = gr.Button("Clear History")max_length = gr.Slider(0, 4096, value=2048, step=1.0, label="Maximum length", interactive=True)top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)history = gr.State([])submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history],show_progress=True)submitBtn.click(reset_user_input, [], [user_input])emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)demo.queue().launch(server_name='0.0.0.0', share=False, inbrowser=True)
http://www.lryc.cn/news/123367.html

相关文章:

  • 【数据分享】2001-2022年我国省市县镇四级的逐月最高气温数据(无需转发/Shp/Excel格式)
  • 线段树-模板-区间查询-区间修改
  • 微服务架构和分布式架构的区别
  • Ajax-概念、Http协议、Ajax请求及其常见问题
  • react 09之状态管理工具1 redux+ react-thunk的使用实现跨组件状态管理与异步操作
  • opencv实战项目 手势识别-实现尺寸缩放效果
  • Netty对HPACK头部压缩的支持
  • C++:替换string中的字符
  • 【ChatGPT】自我救赎
  • 微信小程序(由浅到深)
  • 冒泡排序 简单选择排序 插入排序 快速排序
  • linux文件I/O之 open() 函数用法
  • 用Java操作MySQL数据库
  • SpringBoot启动报错:java: 无法访问org.springframework.boot.SpringApplication
  • Vue3 setup语法糖 解决富文本编辑器上传图片64位码过长问题 quill-image-extend-module
  • 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
  • 论文浅尝 | CI4MRC:基于因果推断去除机器阅读理解中的名字偏差
  • 【校招VIP】测试计划之黑盒测试白盒测试
  • 学习笔记整理-JS-01-语法与变量
  • PHP之PHPExcel
  • Redis系列(一):深入了解Redis数据类型和底层数据结构
  • javaScript:如何获取html中的元素对象
  • 面试总结-webpack/git
  • 深入解析美颜SDK:算法、效果与实现
  • ChatGPT Plus和ChatGPT对比
  • 计算机网络 运输层 TCP连接建立、释放
  • npm run xxx 的时候发生了什么?(以npm run dev举例说明)
  • 图解结构体大小和位域例子
  • 游戏行业实战案例 5 :玩家在线分布
  • TypeScript 关于对【泛型】的定义使用解读