Files
myWiki/concepts/kv-injection.md

36 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "KV Injection"
created: 2026-06-28
updated: 2026-06-28
type: concept
tags: [speculative-decoding, feature-transfer, draft-architecture]
sources: [DFlash, DSpark]
---
# KV Injection
KV 注入KV Injection是 [[DFlash]](及继承其骨干的 [[DSpark]])中使用的目标模型上下文特征注入技术。草稿模型在 prefill 阶段从目标模型的选定层提取隐藏状态,经过投影后注入草稿模型的每一层。
## 机制
**上下文特征提取**
$$H_{ctx} = \text{RMSNorm}\left(W_c [H^{(l_1)}; ...; H^{(l_m)}]\right)$$
其中 $W_c \in \mathbb{R}^{d \times md}$ 是共享投影矩阵,$\{l_1, ..., l_m\}$ 是选定的目标模型层。
**层内注入**:在草稿模型的每一层的 key 和 value 计算中,将注入的上下文特征沿序列维度拼接到草稿块表示之前:
$$K_i = [W_i^K H_{ctx}; W_i^K H_d], \quad V_i = [W_i^V H_{ctx}; W_i^V H_d]$$
块内所有位置双向注意力到彼此及注入的目标上下文。
## 效果
KV 注入使得草稿模型能利用目标模型的**丰富上下文表示**,大幅提升预测质量。这是并行草稿器能在位置 1 上超过浅层自回归草稿器的关键——目标模型的深层语义表示直接流向草稿模型。
## 参考
- [[DFlash]]
- [[DSpark]]
- [[parallel-drafting|并行草稿Parallel Drafting]]