即墨今天新闻大事,墨子学院seo,有什么做设计的兼职网站,wordpress 多域名多站点ubuntu 部署 ChatGLM-6B 完整流程 模型量化 Nvidia 初环境与设备环境准备克隆模型代码部署 ChatGLM-6B完整代码 ChatGLM-6B 是一个开源的、支持中英双语的对话语言模型#xff0c;基于 General Language Model (GLM) 架构#xff0c;具有 62 亿参数。结合模型量化技术#x… 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 (英伟达 就行)
以下是一些推荐的消费级显卡 Nvidia GeForce RTX 3080: RTX 3080 是一款性能出色的显卡适用于高质量游戏和深度学习任务。它提供了强大的图形性能和 CUDA 核心能够满足许多高性能计算需求。 AMD Radeon RX 6800 XT: 如果你对 AMD 的显卡感兴趣RX 6800 XT 是一款强大的选择。它具有出色的游戏性能和计算能力适用于多种应用场景。 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
关于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,historyhistory,max_lengthmax_length if max_length else 2048,top_ptop_p if top_p else 0.7,temperaturetemperature 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_codeTrue)model AutoModel.from_pretrained(/home/ai/dev/model/chatglm2-6b-32k, trust_remote_codeTrue).half().cuda()model.eval()uvicorn.run(app, host0.0.0.0, port8000, workers1)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_codeTrue)
model AutoModel.from_pretrained(/home/ai/dev/model/chatglm2-6b-32k, trust_remote_codeTrue).half().cuda()
model model.eval()Override Chatbot.postprocessdef 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] fprecode classlanguage-{items[-1]}else:lines[i] fbr/code/preelse: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] brlinetext .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_lengthmax_length, top_ptop_p,temperaturetemperature):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 aligncenterChatGLM/h1)chatbot gr.Chatbot()with gr.Row():with gr.Column(scale4):with gr.Column(scale12):user_input gr.Textbox(show_labelFalse, placeholderInput..., lines10).style(containerFalse)with gr.Column(min_width32, scale1):submitBtn gr.Button(Submit, variantprimary)with gr.Column(scale1):emptyBtn gr.Button(Clear History)max_length gr.Slider(0, 4096, value2048, step1.0, labelMaximum length, interactiveTrue)top_p gr.Slider(0, 1, value0.7, step0.01, labelTop P, interactiveTrue)temperature gr.Slider(0, 1, value0.95, step0.01, labelTemperature, interactiveTrue)history gr.State([])submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history],show_progressTrue)submitBtn.click(reset_user_input, [], [user_input])emptyBtn.click(reset_state, outputs[chatbot, history], show_progressTrue)demo.queue().launch(server_name0.0.0.0, shareFalse, inbrowserTrue)