Automatic Prefix Caching

部署运行你感兴趣的模型镜像

APC技术,遇到新prompt和老prompt前缀完全相等的,则复用老prompt的KV cache,避免重新计算。

VLLM代码实例:

# set enable_prefix_caching=True to enable APC
llm = LLM(
    model='lmsys/longchat-13b-16k',
    enable_prefix_caching=True
)

 应用场景:

1. 长文档问答。给1个长文档,变换不同的query。则把长文档放到前面,可以复用KV cache。

2. 多轮聊天。多轮对话聊天历史,放到前面,可以复用KV cache。

我的实验:

1. fetch_count>1时,不管是否打开enable_prefix_caching,prompt的KV cache都会被复用。

2. prefill公共前缀较长,且decode output较短时,KV cache复用的威力才能发挥出来。

3. 只要前面有1个字符不同,后面完全相同的部分,也不能被视为公共前缀。因为2个prompt这部分的KV值不相等了。

您可能感兴趣的与本文相关的镜像

Vllm-v0.11.0

Vllm-v0.11.0

Vllm

vLLM是伯克利大学LMSYS组织开源的大语言模型高速推理框架,旨在极大地提升实时场景下的语言模型服务的吞吐与内存使用效率。vLLM是一个快速且易于使用的库,用于 LLM 推理和服务,可以和HuggingFace 无缝集成。vLLM利用了全新的注意力算法「PagedAttention」,有效地管理注意力键和值

Prefix Caching Prefix Caching is a technique to optimize the inference efficiency of generative models. Its core idea is to cache intermediate computation results (KV Cache) of input sequences, avoiding redundant computations and thereby accelerating response times for multiple requests sharing the same prefix. How It Works Prefix Identification: When multiple requests share identical input prefixes (e.g., prompts or initial context), the system caches the intermediate states (KV Cache) corresponding to that prefix. Incremental Computation: For subsequent requests, only the newly added portions (e.g., user-appended input) need computation while reusing cached intermediate results, significantly reducing computational overhead. Enabling Prefix Caching for Service Deployment To enable prefix caching when launching the service, add the parameter enable-prefix-caching. By default, only first-level caching (GPU cache) is enabled. To enable CPU caching, specify the swap-space parameter to allocate CPU cache space (in GB). The size should be set based on available machine memory after model loading. Note: The ERNIE-4.5-VL multimodal model currently does not support prefix caching. For detailed parameter descriptions, refer to the Parameters Documentation. Example launch command: python -m fastdeploy.entrypoints.openai.api_server \ --model "baidu/ERNIE-4.5-21B-A3B-Paddle" \ --port 8180 --engine-worker-queue-port 8181 \ --metrics-port 8182 \ --cache-queue-port 8183 \ --enable-prefix-caching \ --swap-space 50 \ --max-model-len 8192 \ --max-num-seqs 32 Enabling Prefix Caching for Offline Inference Set enable_prefix_caching=True when launching FastDeploy. Enable CPU caching via swap_space based on available machine memory. A test example is provided: demo/offline_prefix_caching_demo.py
07-06
Prefix Caching 是一种优化生成模型推理效率的技术,其核心思想是通过缓存输入序列的中间计算结果(KV Cache),避免重复计算,从而加速共享相同前缀的多个请求的响应时间。 ### 工作原理 1. **前缀识别**:当多个请求共享相同的输入前缀(例如提示或初始上下文)时,系统会缓存与该前缀对应的中间状态(KV Cache)。 2. **增量计算**:对于后续请求,只需计算新增部分(例如用户追加的输入),同时重用缓存的中间结果,显著减少计算开销。 ### 服务部署中启用 Prefix Caching 在启动服务时,添加参数 `enable-prefix-caching` 以启用前缀缓存。默认情况下,仅启用一级缓存(GPU 缓存)。 要启用 CPU 缓存,需通过 `swap-space` 参数分配 CPU 缓存空间(以 GB 为单位)。大小应根据模型加载后的可用机器内存进行设置。 **注意**:ERNIE-4.5-VL 多模态模型目前不支持前缀缓存。 有关参数的详细说明,请参阅[参数文档](https://github.com/PaddlePaddle/FastDeploy/blob/develop/docs/zh_CN/faq/prefix_caching.md)。 **示例启动命令**: ```bash python -m fastdeploy.entrypoints.openai.api_server \ --model "baidu/ERNIE-4.5-21B-A3B-Paddle" \ --port 8180 --engine-worker-queue-port 8181 \ --metrics-port 8182 \ --cache-queue-port 8183 \ --enable-prefix-caching \ --swap-space 50 \ --max-model-len 8192 \ --max-num-seqs 32 ``` ### 离线推理中启用 Prefix Caching 在启动 FastDeploy 时,设置 `enable_prefix_caching=True`。根据可用机器内存,通过 `swap_space` 启用 CPU 缓存。 提供了一个测试示例:`demo/offline_prefix_caching_demo.py` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值