Python / PyTorch 工业标准参考实现答案 (含测试验证与边界断言)
import torch
import torch.nn.functional as F
import math
def prefix_cache_attention(
q: torch.Tensor,
k_new: torch.Tensor,
v_new: torch.Tensor,
k_cached: torch.Tensor = None,
v_cached: torch.Tensor = None
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Computes Scaled Dot-Product Attention by concatenating newly computed K, V
with cached prefix K, V tensors.
Shapes:
q: (B, H, S_q, D)
k_new, v_new: (B, H, S_new, D)
k_cached, v_cached: (B, H, S_cached, D) or None
"""
# 1. Concatenate cached KV with new KV along sequence dimension (dim=2)
if k_cached is not None and v_cached is not None:
k_total = torch.cat([k_cached, k_new], dim=2)
v_total = torch.cat([v_cached, v_new], dim=2)
else:
k_total = k_new
v_total = v_new
B, H, S_q, D = q.shape
S_total = k_total.shape[2]
# 2. Scaled Dot-Product: (B, H, S_q, D) @ (B, H, D, S_total) -> (B, H, S_q, S_total)
scale = 1.0 / math.sqrt(D)
scores = torch.matmul(q, k_total.transpose(-2, -1)) * scale
# 3. Softmax over all key positions
attn_weights = F.softmax(scores, dim=-1)
# 4. Weighted combination with values: (B, H, S_q, S_total) @ (B, H, S_total, D) -> (B, H, S_q, D)
output = torch.matmul(attn_weights, v_total)
return output, k_total, v_total
# --- Verification & Test Case ---
if __name__ == "__main__":
B, H, D = 2, 4, 32
# Cached prefix turn: 10 tokens
k_cache = torch.randn(B, H, 10, D)
v_cache = torch.randn(B, H, 10, D)
# New prompt: 5 tokens
q = torch.randn(B, H, 5, D)
k_new = torch.randn(B, H, 5, D)
v_new = torch.randn(B, H, 5, D)
out, k_updated, v_updated = prefix_cache_attention(q, k_new, v_new, k_cache, v_cache)
assert out.shape == (B, H, 5, D)
assert k_updated.shape == (B, H, 15, D)
print("Prefix cache attention successfully verified!")