一、前言
? ??基于真實(shí)生產(chǎn)級項(xiàng)目分享,幫助有需要的同學(xué)快速構(gòu)建完整可交付項(xiàng)目
? ? 項(xiàng)目流程包括(去掉業(yè)務(wù)部分):
- ? 開源模型測試,包括baichuan、qwen、chatglm、bloom
- ? 數(shù)據(jù)爬取及清洗
- ? 模型微調(diào)及評估
- ? 搭建AI交互能力
- ??搭建IM交互能力
- ? 搭建違禁詞識別能力
- ? 優(yōu)化模型推理速度
- ? 增強(qiáng)模型長期記憶能力
二、術(shù)語介紹
? ? 2.1. vLLM
? ? vLLM是一個(gè)開源的大模型推理加速框架,通過PagedAttention高效地管理attention中緩存的張量,實(shí)現(xiàn)了比HuggingFace Transformers高14-24倍的吞吐量。
? ? 2.2. qwen-7b
? ??通義千問-7B(Qwen-7B) 是阿里云研發(fā)的通義千問大模型系列的70億參數(shù)規(guī)模的模型。?
? ? 2.3.Anaconda
? ??Anaconda(官方網(wǎng)站)就是可以便捷獲取包且對包能夠進(jìn)行管理,同時(shí)對環(huán)境可以統(tǒng)一管理的發(fā)行版本。Anaconda包含了conda、Python在內(nèi)的超過180個(gè)科學(xué)包及其依賴項(xiàng)。
三、構(gòu)建環(huán)境
? ? 3.1. 基礎(chǔ)環(huán)境及前置條件
- ?操作系統(tǒng):centos7
- ?Tesla V100-SXM2-32GB? CUDA Version: 12.2
- ?提前下載好qwen-7b-chat模型
? ? ? ? ? 通過以下兩個(gè)地址進(jìn)行下載,優(yōu)先推薦魔搭
? ? ? ? ??https://modelscope.cn/models/qwen/Qwen-7B-Chat/files
?? ? ? ?????????https://huggingface.co/Qwen/Qwen-7B-Chat/tree/main
? ? ? ? ??
? ??3.2.?Anaconda安裝
? ? ? ? 1.? 更新軟件包
? ? ? ? ? ? ? sudo yum upgrade -y
? ? ? ? ?2. 下載Anaconda
? ? ? ? ? ? ?wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
? ? ? ? ?3.?安裝
? ? ? ? ? ? ?默認(rèn)安裝
? ? ? ? ? ? ?bash Anaconda3-2022.10-Linux-x86_64.sh
? ? ? ? ? ? ?-p 指定安裝目錄為/opt/anaconda3
? ? ? ? ? ? ?bash Anaconda3-2022.10-Linux-x86_64.sh -p /opt/anaconda3
? ? ? ? ? 4. 初始化
? ? ? ? ? ? ?source ~/.bashrc
? ? ? ? ??5. 驗(yàn)證安裝結(jié)果
? ? ? ? ? ? ??conda --version
? ? ? ? ? 6. 配置鏡像源
? ? ? ? ? ? ??conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
? ? ? ? ? ? ? conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
? ? ? ? ? ? ? conda config --set show_channel_urls yes
? ? 3.3.?創(chuàng)建虛擬環(huán)境
? ? ? ? 2.3.1.創(chuàng)建新環(huán)境
? ? ? ? ? ? conda create --name vllm python=3.10
? ? ? ? 2.3.2.切換環(huán)境
? ? ? ? ? ? conda activate vllm
? 3.4.?vLLM安裝
? ? ? ? 2.4.1.安裝軟件包
? ? ? ? ? ? ?pip install vllm -i https://pypi.tuna.tsinghua.edu.cn/simple
? ? ? ? ? ? ?pip install tiktoken -i https://pypi.tuna.tsinghua.edu.cn/simple
? ? ? ? ? ? ?ps: vllm版本為0.2.7,tiktoken版本為0.5.2
? ? ? ? 2.4.2.查看已軟件包? ?
? ? ? ? ? ? conda list 或者 pip list?
? ? ? ? ? ? 注意:上述命令必須先切換至vllm虛擬環(huán)境
四、部署服務(wù)
? ? 4.1.?啟動(dòng)vllm服務(wù)
? ? ? ??python -m vllm.entrypoints.api_server ?--model ?/data/model/qwen-7b-chat ?--swap-space 24 --disable-log-requests --trust-remote-code --max-num-seqs 256 --host 0.0.0.0 --port 9000 ?--dtype float16 --max-parallel-loading-workers 1 ?--enforce-eager
? ? ? ? 常用參數(shù):
? ? ? ? ?--model <model_name_or_path>
? ? ? ? ? ?Name or path of the huggingface model to use.
? ? ? ? ?--trust-remote-code
? ? ? ? ? ?Trust remote code from huggingface
? ? ? ??--dtype {auto,half,float16,bfloat16,float,float32}
? ? ? ? ? Data type for model weights and activations.
? ? ? ? ? ? ?? “auto” will use FP16 precision for FP32 and FP16 models, and BF16 precision for BF16 models.
? ? ? ? ? ? ?? “half” for FP16. Recommended for AWQ quantization.
? ? ? ? ? ? ?? “float16” is the same as “half”.
? ? ? ? ? ? ?? “bfloat16” for a balance between precision and range.
? ? ? ? ? ? ? “float” is shorthand for FP32 precision.
? ? ? ? ? ? ? “float32” for FP32 precision
? ? ? ? ?--swap-space <size>
? ? ? ? ? ? CPU swap space size (GiB) per GPU.
? ? ? ? ?--max-num-seqs <sequences>
? ? ? ? ? ? Maximum number of sequences per iteratio
? ? ? ? ??--quantization (-q) {awq,squeezellm,None}
? ? ? ? ? ? Method used to quantize the weights.
五、測試
? ? 5.1.?流式案例文章來源:http://www.zghlxwxcb.cn/news/detail-783530.html
import threading
import requests
import json
class MyThread(threading.Thread):
def run(self):
headers = {"User-Agent": "Stream Test"}
pload = {
"prompt": "<|im_start|>system\n你是一位知名作家,名字叫張三,你擅長寫作.<|im_end|>\n<|im_start|>user\n以中秋為主寫一篇1000字的文章<|im_end|>\n<|im_start|>assistant\n",
"n": 1,
"temperature": 0.35,
"max_tokens": 8192,
"stream": True,
"stop": ["<|im_end|>", "<|im_start|>",]
}
#此處端口9000要與vLLM Server發(fā)布的端口一致
response = requests.post("http://127.0.0.1:9000/generate", headers=headers, json=pload, stream=True)
for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False, delimiter=b"\0"):
if chunk:
now_thread = threading.current_thread()
data = json.loads(chunk.decode("utf-8"))
output = data["text"]
print(f'now thread name: {now_thread.name},output: {output}')
if __name__ == '__main__':
threads = []
for i in range(1, 10, 1):
t = MyThread()
threads.append(t)
# 啟動(dòng)線程
for t in threads:
t.start()
# 等待所有線程完成
for t in threads:
t.join()
五、后續(xù)文章來源地址http://www.zghlxwxcb.cn/news/detail-783530.html
- 支持多輪對話
- 支持高可用
- 兼容復(fù)雜業(yè)務(wù)場景
- 性能優(yōu)化
到了這里,關(guān)于開源模型應(yīng)用落地-qwen-7b-chat與vllm實(shí)現(xiàn)推理加速的正確姿勢(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!