class SelfAttention(torch.nn.Module):
"""
自注意力的逻辑,包含四部分:
+ 从输入计算 QKV,
+ 对 QKV 分头,
+ 从 QKV 计算 O(在`CoreAttention`里面),
+ 从 O 计算输出
"""
def __init__(self, config: ChatGLMConfig, layer_number, device=None):
super(SelfAttention, self).__init__()
# 层的序号
self.layer_number = max(1, layer_number)
# ProjSize:就是没有开启 MQA 情况下的 QKV 的尺寸
# 等于 NHead * HeadSize,和原始的 HidSize 可能有不同·
self.projection_size = config.kv_channels * config.num_attention_heads
# HeadSize = ProjSize // NHead
self.hidden_size_per_attention_head = self.projection_size // config.num_attention_heads
# NHead
self.num_attention_heads_per_partition = config.num_attention_heads
# 控制是否启用MQA
self.multi_query_attention = config.multi_query_attention
# 如果不启用 MQA,QKVSize 就是三倍的 ProjSize
self.qkv_hidden_size = 3 * self.projection_size
if self.multi_query_attention:
# 如果启用了 MQA
# NGroup
self.num_multi_query_groups_per_partition = config.multi_query_group_num
# QKVSize 等于 ProjSize(Q) + 2 * HeadSize * NGroup (KV)
self.qkv_hidden_size = (
self.projection_size + 2 * self.hidden_size_per_attention_head * config.multi_query_group_num
)
# 将输入映射成 QKV 的线性层
self.query_key_value = nn.Linear(config.hidden_size, self.qkv_hidden_size,
bias=config.add_bias_linear or config.add_qkv_bias,
device=device, **_config_to_kwargs(config)
)
# 用于从 QKV 计算 O 的核心模块
self.core_attention = CoreAttention(config, self.layer_number)
# 用于从 O 计算输出的线性层
self.dense = nn.Linear(self.projection_size, config.hidden_size, bias=config.add_bias_linear,
device=device, **_config_to_kwargs(config)
)
def _allocate_memory(self, inference_max_sequence_len, batch_size, device=None, dtype=None):
if self.multi_query_attention:
num_attention_heads = self.num_multi_query_groups_per_partition
else:
num_attention_heads = self.num_attention_heads_per_partition
return torch.empty(
inference_max_sequence_len,
batch_size,
num_attention_heads,
self.hidden_size_per_attention_head,
dtype=dtype,
device=device,
)
def forward(
self, hidden_states, attention_mask, rotary_pos_emb, kv_cache=None, use_cache=True
):
# 输入隐藏状态尺寸为 [SeqLen, BatchSize, HidSize]
# 使用输入计算 QKV
mixed_x_layer = self.query_key_value(hidden_states)
if self.multi_query_attention:
# 如果开启了 MQA,将 QKV 按照最后一维分割
# 得到 Q [SeqLen, BatchSize, ProjSize]
# 和 K/V [SeqLen, BatchSize, NGroup * HeadSize]
(query_layer, key_layer, value_layer) = mixed_x_layer.split(
[
self.num_attention_heads_per_partition * self.hidden_size_per_attention_head,
self.num_multi_query_groups_per_partition * self.hidden_size_per_attention_head,
self.num_multi_query_groups_per_partition * self.hidden_size_per_attention_head,
],
dim=-1,
)
# 对 Q 分头,变形为 [SeqLen, BatchSize, NHead, HeadSize]
query_layer = query_layer.view(
query_layer.size()[:-1] + (self.num_attention_heads_per_partition, self.hidden_size_per_attention_head)
)
# 对 K 分头,变形为 [SeqLen, BatchSize, NGroup, HeadSize]
key_layer = key_layer.view(
key_layer.size()[:-1] + (self.num_multi_query_groups_per_partition, self.hidden_size_per_attention_head)
)
# 对 V 分头,变形为 [SeqLen, BatchSize, NGroup, HeadSize]
value_layer = value_layer.view(
value_layer.size()[:-1]
+ (self.num_multi_query_groups_per_partition, self.hidden_size_per_attention_head)
)
else:
# 变形为 [SeqLen, BatchSize, NHead, 3 * HeadSize]
new_tensor_shape = mixed_x_layer.size()[:-1] + \
(self.num_attention_heads_per_partition,
3 * self.hidden_size_per_attention_head)
mixed_x_layer = mixed_x_layer.view(*new_tensor_shape)
# 将 QKV 最后一维平分三份,得到 Q/K/V
# 尺寸为 [SeqLen, BatchSize, NHead, HeadSize]
(query_layer, key_layer, value_layer) = split_tensor_along_last_dim(mixed_x_layer, 3)
# 应用 ROPE
if rotary_pos_emb is not None:
query_layer = apply_rotary_pos_emb(query_layer, rotary_pos_emb)
key_layer = apply_rotary_pos_emb(key_layer, rotary_pos_emb)
# 如果传入了 KVCache
# 拆分为 KCache 和 VCache
# 每个形状为 [CacheLen, BatchSize, NGroup, HeadSize]
# 分别添加到 K 和 V 前面
if kv_cache is not None:
cache_k, cache_v = kv_cache
key_layer = torch.cat((cache_k, key_layer), dim=0)
value_layer = torch.cat((cache_v, value_layer), dim=0)
# 如果设置了 UseCache,则返回 KV
if use_cache:
kv_cache = (key_layer, value_layer)
else:
kv_cache = None
# MQA 模式下,给 K 和 V 广播到 Q 的形状
# [..., NGroup, ...] => [..., NGroup, 1, ...] =>
# [..., NGroup, NHead // NGroup, ...] =>
# [..., NHead, ...]
if self.multi_query_attention:
# K 变形为 [CacheSeqLen, BatchSize, NGroup, 1, HeadSize]
key_layer = key_layer.unsqueeze(-2)
# K 广播为 [CacheSeqLen, BatchSize, NGroup, NHead // NGroup, HeadSize]
# NHead // NGroup 是每一组的头部数量
# 相当于把最后一维复制了 NHead // NGroup 等份
key_layer = key_layer.expand(
-1, -1, -1, self.num_attention_heads_per_partition // self.num_multi_query_groups_per_partition, -1
)
# K 变形为 [CacheSeqLen, BatchSize, NHead, HeadSize]
key_layer = key_layer.contiguous().view(
key_layer.size()[:2] + (self.num_attention_heads_per_partition, self.hidden_size_per_attention_head)
)
# V 变形为 [CacheSeqLen, BatchSize, NGroup, 1, HeadSize]
value_layer = value_layer.unsqueeze(-2)
# V 广播为 [CacheSeqLen, BatchSize, NGroup, NHead // NGroup, HeadSize]
value_layer = value_layer.expand(
-1, -1, -1, self.num_attention_heads_per_partition // self.num_multi_query_groups_per_partition, -1
)
# V 变形为 [CacheSeqLen, BatchSize, NHead, HeadSize]
value_layer = value_layer.contiguous().view(
value_layer.size()[:2] + (self.num_attention_heads_per_partition, self.hidden_size_per_attention_head)
)
# 将 Q K V 和掩码数组传入核心模块,得到 O
# 尺寸为 [SeqLen, BatchSize, ProjSize]
context_layer = self.core_attention(query_layer, key_layer, value_layer, attention_mask)
# 使用 O 计算输出,尺寸为 [SeqLen, BatchSize, HidSize]
output = self.dense(context_layer)
return output, kv_cache