vLLM 是一款專為大語言模型推理加速而設計的框架,實現了 KV 緩存內存幾乎零浪費,解決了內存管理瓶頸問題。
更多 vLLM 中文文檔及教程可訪問 →https://vllm.hyper.ai/
*在線運行 vLLM 入門教程:零基礎分步指南
源碼?examples/offline_inference/cpu_offload_lmcache.py
# SPDX-License-Identifier: Apache-2.0"""
該文件演示了 CPU 卸載的示例用法
與 LMCache。
請注意,運行此示例需要 "pip install lmcache"。
在 https://github.com/LMCache/LMCache 中了解有關 LMCache 的更多信息。
"""
import os
import timefrom lmcache.experimental.cache_engine import LMCacheEngineBuilder
from lmcache.integration.vllm.utils import ENGINE_NAMEfrom vllm import LLM, SamplingParams
from vllm.config import KVTransferConfig# 與 LMCache 相關的環境變量
# 在 LMCache 中使用實驗功能
os.environ["LMCache_USE_EXPERIMENTAL"] = "True"
# LMCache 設置為每塊使用256個 token
os.environ["LMCache_CHUNK_SIZE"] = "256"
# 在 LMCache 中啟用本地 CPU 后端
os.environ["LMCache_LOCAL_CPU"] = "True"
# 將本地 CPU 內存限制設置為 5.0 GB
os.environ["LMCache_MAX_LOCAL_CPU_SIZE"] = "5.0"# 此示例腳本以共享前綴運行兩個請求。
shared_prompt = "Hello, how are you?" * 1000
first_prompt = [shared_prompt + "Hello, my name is",
]
second_prompt = [shared_prompt + "Tell me a very long story",
]sampling_params = SamplingParams(temperature=0, top_p=0.95, max_tokens=10)ktc = KVTransferConfig.from_cli('{"kv_connector":"LMCacheConnector", "kv_role":"kv_both"}')
# 將 GPU 內存利用設置為 0.8,用于 40GB 顯存的 A40 GPU。
# 如果您的 GPU 的內存較少,則降低值。
# 請注意,LMCache 目前與塊預填充不兼容。
llm = LLM(model="mistralai/Mistral-7B-Instruct-v0.2",kv_transfer_config=ktc,max_model_len=8000,enable_chunked_prefill=False,gpu_memory_utilization=0.8)outputs = llm.generate(first_prompt, sampling_params)
for output in outputs:generated_text = output.outputs[0].textprint(f"Generated text: {generated_text!r}")
print("First request done.")time.sleep(1)outputs = llm.generate(second_prompt, sampling_params)
for output in outputs:generated_text = output.outputs[0].textprint(f"Generated text: {generated_text!r}")
print("Second request done.")# 清理 LMCache 后端
LMCacheEngineBuilder.destroy(ENGINE_NAME)