Title here
Summary here
假设只重算了位置 1 和 3。它们产生的新 K、V 要写回原来的两处;后续层收到的隐藏状态和残差也要按 1、3 这一相同顺序排列。
在 status=2 的层中,将新计算的 HKVD token 的 KV 更新到旧 KV Cache 中。
# 文件: vllm_blend/vllm/attention/backends/xformers.py
# 位置: XFormersImpl.forward() 第 240-245 行
if status in [2]:
# 获取 HKVD 索引
imp_indices = cache_fuse_metadata["imp_indices"]
# 核心:部分更新
# 只更新 HKVD token 对应位置的 KV
key_old[imp_indices] = key # key 只包含 HKVD token
value_old[imp_indices] = value # value 只包含 HKVD token
# 使用融合后的 KV 进行后续计算
key = key_old
value = value_old这种部分更新策略非常高效:
在 Check 层(status=1)之后,残差连接也需要过滤:
# 文件: vllm_blend/vllm/model_executor/models/llama.py
# 位置: LlamaDecoderLayer.forward() 第 264-265 行
if status == 1:
# 过滤残差,只保留 HKVD token 对应的部分
residual = residual[cache_fuse_metadata["imp_indices"]]在 LlamaModel 中,位置编码也需要过滤:
# 文件: vllm_blend/vllm/model_executor/models/llama.py
# 位置: LlamaModel.forward() 第 373-376 行
if temp_status == 1:
# 过滤位置编码,只保留 HKVD token 的位置
positions = positions[self.cache_fuse_metadata["imp_indices"]]下一层用紧凑张量继续前向,注意力后端仍通过原序列位置访问整段 KV。位置向量、隐藏状态和残差同步选取,才会保持 token 对应关系。