怎么增加網(wǎng)站訪問(wèn)量網(wǎng)站優(yōu)化培訓(xùn)學(xué)校
1.機(jī)器配置及實(shí)驗(yàn)說(shuō)明
基于前期搭建的雙卡機(jī)器裝機(jī)教程,配置如下:
硬件名稱 | 參數(shù) | 備注 |
---|---|---|
CPU | E5-2680V42 *2(線程28個(gè)) | 無(wú) |
GPU | 2080TI-22G 雙卡 | 魔改卡 |
系統(tǒng) | WSL Unbuntu 22.04.5 LTS | 虛擬機(jī) |
本輪實(shí)驗(yàn)?zāi)康?#xff1a;基于VLLM/Ollama/ktransformers框架完成Deepseek大模型私有化部署,了解如何搭建大模型推理服務(wù)。
2.大模型推理框架介紹及實(shí)戰(zhàn)
2.1 推理框架介紹
目前大模型推理框架主要包含VLLM/SGLang/ollama/ktransformer等框架,如下是各個(gè)框架的介紹:
-
VLLM:是UC Berkeley大佬Ion Stoica組開(kāi)源的大模型推理引擎。其在2023.6.20首次發(fā)布,為了實(shí)現(xiàn)快速推理,經(jīng)過(guò)特別優(yōu)化,具有高服務(wù)吞吐量,使用pagedAttention的有效注意力內(nèi)存管理。連續(xù)批處理和優(yōu)化的CUDA內(nèi)核,此外還支持各種解碼算法、張量并行和流式輸出,支持huggingface模型格式,提供兼容OpenAI的API接口官網(wǎng)鏈接
-
SGLang:是一個(gè)對(duì)標(biāo)vLLM的大語(yǔ)言模型推理框架, 系統(tǒng)的整體架構(gòu)如下圖,分為前端和后端。 前端是對(duì)調(diào)用大語(yǔ)言模型的一些常用操作的抽象,提供一系列原語(yǔ)。 后端是具體的對(duì)推理過(guò)程的優(yōu)化。 SGLang的兩點(diǎn)主要的優(yōu)化是RadixAttention和Structured output。在此基礎(chǔ)之上,作為大模型推理的基礎(chǔ)框架, 后續(xù)也有很多其他的系統(tǒng)優(yōu)化工作是在SGLang框架上的。官網(wǎng)鏈接
-
ollama:是一個(gè)簡(jiǎn)明易用的本地大模型運(yùn)行框架,只需一條命令即可在本地跑大模型。開(kāi)源項(xiàng)目,專注于開(kāi)發(fā)和部署先進(jìn)的大型語(yǔ)言模型(LLM)官網(wǎng)鏈接,支持guff模型格式
-
KTransfomers:是一個(gè)由清華大學(xué)KVAV.AI團(tuán)隊(duì)開(kāi)發(fā)的開(kāi)源項(xiàng)目,旨在優(yōu)化大語(yǔ)言模型(LLM)的推理性能,特別是在有限顯存資源下運(yùn)行大型模型。支持huggingface模型格式和guff的格式。
2.2 VLLM部署Deepseek大模型
為了方便快速部署,本輪實(shí)驗(yàn)采用Deepseek蒸餾模型(DeepSeek-R1-Distill-Qwen-1.5B),可以從huggingface社區(qū)或者modelscope進(jìn)行下載
步驟一:模型下載
- 國(guó)外下載鏈接:https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B/tree/main
- 國(guó)內(nèi)下載鏈接:https://hf-mirror.com/deepseek-ai
- git方式下載:
git clone https://hf-mirror.com/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
步驟二:安裝VLLM
需要注意的是,安裝vllm有比較多的依賴,例如torch環(huán)境、cuda版本等
(1)nvcc -V 版本和nvidia-smi版本保持一致:
驅(qū)動(dòng)報(bào)錯(cuò):
cmake報(bào)錯(cuò):
(2)torch版本依賴:安裝vllm之前,虛擬環(huán)境中一定要有pytorch環(huán)境,否則會(huì)報(bào)錯(cuò),這里建議部署的時(shí)候,新建環(huán)境,python和torch都安裝最新版本,出錯(cuò)概率會(huì)小一些,否則會(huì)報(bào)如下錯(cuò)誤:
```pythonPreparing metadata (setup.py) ... errorerror: subprocess-exited-with-error× python setup.py egg_info did not run successfully.│ exit code: 1╰─> [6 lines of output]Traceback (most recent call last):File "<string>", line 2, in <module>File "<pip-setuptools-caller>", line 34, in <module>File "/tmp/pip-install-sa81j1_y/xformers_5701c77d28094ba6bffdabe8c9ba5779/setup.py", line 24, in <module>import torchModuleNotFoundError: No module named 'torch'
【錯(cuò)誤分析】環(huán)境中未安裝pytorch,按照驅(qū)動(dòng)版本安裝相應(yīng)pytorch版本即可:
有時(shí)候還會(huì)遇到如下錯(cuò)誤:
Please install it with "pip install transformers[torch]'
【錯(cuò)誤解析】python的版本較老,對(duì)于一些新的模型不兼容,建議重建虛擬環(huán)境進(jìn)行最新版本安裝
(3)Xformer版本的依賴
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
torchvision 0.20.1+cu124 requires torch==2.5.1, but you have torch 2.6.0 which is incompatible.
【錯(cuò)誤解析】Xformer版本與torch有強(qiáng)烈依賴關(guān)系,安裝之前需要下載對(duì)應(yīng)版本
對(duì)應(yīng)關(guān)系可以參考xfomers官網(wǎng)鏈接
(4)安裝VLLM
步驟三:配置模型,啟動(dòng)服務(wù)
(1)配置啟動(dòng)模型及配置sh 腳本
#啟動(dòng)時(shí)需要將注釋內(nèi)容刪除,且對(duì)空格比較敏感
python -m vllm.entrypoints.openai.api_server \
--model /mnt/e/NLP/deepseek/DeepSeek-R1-Distill-Qwen-1.5B \ #模型存放絕對(duì)路徑
--served-model-name deepseek-qwen-1.5b \ # 配置的服務(wù)名稱
--dtype=half \ #精度
--tensor-parallel-size 1 \ # 并行tensor
--max-model-len 1000 \ #最大模型長(zhǎng)度
--trust-remote-code \
--gpu-memory-utilization 0.9 #gpu的利用率
(2)啟動(dòng)腳本 sh start.sh
模型占用顯存:
(3)調(diào)用模型服務(wù):
curl -X POST "http://localhost:8000/v1/chat/completions" -H "Content-Type: application/json" --data '{"model": "deepseek-qwen-1.5b","messages": [{"role": "user","content": "你是誰(shuí)?"}]}'
返回結(jié)果
2.3 ollama部署Deepseek大模型
步驟一:下載ollama
(1)網(wǎng)絡(luò)下載:
curl -fsSL https://ollama.com/install.sh -o ollama_install.sh
(2)安裝ollama
步驟二:下載Deepseek模型
ollama run deepseek-r1:1.5b
步驟三:模型測(cè)試
2.4 Ktransformer部署Deepseek大模型
步驟一:克隆倉(cāng)庫(kù)
git clone https://github.com/kvcache-ai/ktransformers.git
步驟二:安裝ktransfomers庫(kù)
pip install KTransformers -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simplesome-package
步驟三:啟動(dòng)模型
ktransformers --type transformers --model_path /mnt/e/NLP/deepseek/DeepSeek-R1-Distill-Qwen-1.5B
步驟四:測(cè)試模型接口
curl -X POST "http://localhost:10002/v1/chat/completions" -H "Content-Type: application/json" --data '{"model": "deepseek-qwen-1.5b","messages": [{"role": "user","content": "你是誰(shuí)?"}]}'
返回結(jié)果: