deepseek moe 已发布版本的总参数规模为160亿,实际激活参数约为28亿。与同门的7b全连接模型相比,在19个基准测试数据集上表现互有优劣,整体性能接近。相较llama 2-7b这类密集模型,deepseek moe 在数学推理和代码生成任务中展现出更优能力。值得注意的是,llama 2-7b 和 deepseek 7b 的每4k token计算量均超过180tflops,而 deepseek moe 仅需74.4tflops,能耗仅为前者的40%左右,显著提升了推理效率。
在 autodl 平台上选择配备双卡3090(单卡24G,共48G显存)的服务器实例,操作系统镜像选择 PyTorch–>2.1.0–>3.10(ubuntu22.04)–>12.1。启动实例后,进入 JupyterLab 界面,并打开终端进行环境配置、模型下载与服务部署。
![[大模型]DeepSeek-MoE-16b-chat FastApi 部署调用](https://img.php.cn/upload/article/001/503/042/176351908939063.jpg)
执行以下命令更换 pip 源并安装必要依赖:
# 开启学术加速以提升GitHub访问速度 source /etc/network_turbo <h1>升级pip</h1><p>python -m pip install --upgrade pip</p><h1>更换为清华源加速包安装</h1><p>pip config set global.index-url <a href="https://www.php.cn/link/a6455ffc4e47fd737db213366771ec0e">https://www.php.cn/link/a6455ffc4e47fd737db213366771ec0e</a></p><h1>安装核心库</h1><p>pip install modelscope transformers sentencepiece accelerate fastapi uvicorn requests streamlit transformers_stream_generator</p><h1>安装Flash Attention加速组件(可选)</h1><p>pip install <a href="https://www.php.cn/link/0e7adb08b43a589df528d2bdd69b6b03">https://www.php.cn/link/0e7adb08b43a589df528d2bdd69b6b03</a>
使用 modelscope 提供的 snapshot_download 方法下载 DeepSeek-MoE-16b-chat 模型。参数说明:第一个参数为 HuggingFace 或 ModelScope 上的模型标识名,cache_dir 指定本地保存路径。
在 /root/autodl-tmp 目录下创建 download.py 文件,内容如下图所示,请确保保存后再运行:
import torch
from modelscope import snapshot_download, AutoModel, AutoTokenizer
import os</p><p>model_dir = snapshot_download('deepseek-ai/deepseek-moe-16b-chat', cache_dir='/root/autodl-tmp', revision='master')运行命令开始下载:
python /root/autodl-tmp/download.py
模型文件约30GB,预计耗时10~20分钟完成下载。
在 /root/autodl-tmp 下新建 api.py 文件,填入以下完整代码(含详细注释),请务必保存。
from fastapi import FastAPI, Request
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
import uvicorn
import json
import datetime
import torch</p><h1>设备配置</h1><p>DEVICE = "cuda"
DEVICE_ID = "0"
CUDA_DEVICE = f"{DEVICE}:{DEVICE_ID}" if DEVICE_ID else DEVICE</p><h1>GPU内存清理函数</h1><p>def torch_gc():
if torch.cuda.is_available():
with torch.cuda.device(CUDA_DEVICE):
torch.cuda.empty_cache()
torch.cuda.ipc_collect()</p><h1>初始化FastAPI应用</h1><p>app = FastAPI()</p><p>@app.post("/")
async def create_item(request: Request):
global model, tokenizer
json_post_raw = await request.json()
json_post = json.dumps(json_post_raw)
json_post_list = json.loads(json_post)</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">prompt = json_post_list.get('prompt')
max_length = json_post_list.get('max_length')
# 构建对话历史
messages = [
{"role": "user", "content": prompt}
]
# 编码输入
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
# 生成输出
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=max_length)
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
now = datetime.datetime.now()
time = now.strftime("%Y-%m-%d %H:%M:%S")
# 返回响应结构
answer = {
"response": result,
"status": 200,
"time": time
}
# 打印日志
log = "[" + time + "] " + '", prompt:"' + prompt + '", response:"' + repr(result) + '"'
print(log)
torch_gc()
return answerif name == 'main': model_path = '/root/autodl-tmp/deepseek-ai/deepseek-moe-16b-chat'
<code># 加载分词器
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
# 加载模型,启用bfloat16降低显存占用,自动分配GPU资源
model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
torch_dtype=torch.bfloat16,
device_map="auto"
)
# 加载生成配置
model.generation_config = GenerationConfig.from_pretrained(model_path)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
model.eval() # 启用评估模式
# 启动API服务
uvicorn.run(app, host='0.0.0.0', port=6006, workers=1)</code></pre><h4>启动 API 服务</h4><p>在终端执行以下命令运行服务脚本:</p><pre><code class="javascript">cd /root/autodl-tmppython api.py
当看到类似如下日志输出时,表示模型加载成功,服务已就绪:
![[大模型]DeepSeek-MoE-16b-chat FastApi 部署调用](https://img.php.cn/upload/article/001/503/042/176351908921391.jpg)
服务默认监听 6006 端口,支持通过 POST 请求调用。推荐设置 max_length=100,过高易导致显存溢出,过低则可能截断回答。
示例 curl 调用方式:
curl -X POST "<a href="https://www.php.cn/link/6190df6b9dfadcb413f0d0e3b768888a">https://www.php.cn/link/6190df6b9dfadcb413f0d0e3b768888a</a>" \
-H 'Content-Type: application/json' \
-d '{"prompt": "你好,你是谁?","max_length":100}'也可使用 Python 的 requests 库进行调用:
import requests
import json</p><p>def get_completion(prompt, max_length):
headers = {'Content-Type': 'application/json'}
data = {"prompt": prompt, "max_length": max_length}
response = requests.post(url='<a href="https://www.php.cn/link/6190df6b9dfadcb413f0d0e3b768888a">https://www.php.cn/link/6190df6b9dfadcb413f0d0e3b768888a</a>', headers=headers, data=json.dumps(data))
return response.json()['response']</p><p>if <strong>name</strong> == '<strong>main</strong>':
print(get_completion("你好,你是谁?", 100))成功调用后将返回模型应答结果,示例如下:
![[大模型]DeepSeek-MoE-16b-chat FastApi 部署调用](https://img.php.cn/upload/article/001/503/042/176351908938559.jpg)
以上就是[大模型]DeepSeek-MoE-16b-chat FastApi 部署调用的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号