File size: 7,234 Bytes
c1a46f7 4d62693 c1a46f7 4d62693 c1a46f7 4d62693 c1a46f7 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 c1a46f7 d572bbd c1a46f7 d572bbd c1a46f7 d572bbd 4d62693 c1a46f7 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd 4d62693 d572bbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | """
注意力机制模块 — Person B 负责实现
包含:
1. MultiHeadAttention: 标准多头注意力 (支持 RoPE)
2. FlashMultiHeadAttention: Flash Attention 2 加速版本
技术要点:
- Scaled Dot-Product Attention
- 支持 key_padding_mask 和 attn_mask
- Flash Attention 2 使用 torch.nn.functional.scaled_dot_product_attention
- RoPE 旋转位置编码集成
"""
from __future__ import annotations
import math
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
class MultiHeadAttention(nn.Module):
"""
标准多头注意力机制。
"""
def __init__(
self,
d_model: int = 512,
nhead: int = 8,
dropout: float = 0.1,
use_rotary_embedding: bool = False,
):
super().__init__()
assert d_model % nhead == 0, "d_model 必须能被 nhead 整除"
self.d_model = d_model
self.nhead = nhead
self.d_k = d_model // nhead
self.use_rotary_embedding = use_rotary_embedding
self.q_proj = nn.Linear(d_model, d_model)
self.k_proj = nn.Linear(d_model, d_model)
self.v_proj = nn.Linear(d_model, d_model)
self.out_proj = nn.Linear(d_model, d_model)
self.dropout = nn.Dropout(p=dropout)
self.rope: Optional[nn.Module] = None
def forward(
self,
query: torch.Tensor, # [B, L_q, D]
key: torch.Tensor, # [B, L_k, D]
value: torch.Tensor, # [B, L_v, D]
key_padding_mask: Optional[torch.BoolTensor] = None, # [B, L_k]
attn_mask: Optional[torch.Tensor] = None, # [L_q, L_k]
is_causal: bool = False,
) -> torch.Tensor:
B, L_q, _ = query.size()
L_k = key.size(1)
L_v = value.size(1)
Q = self.q_proj(query)
K = self.k_proj(key)
V = self.v_proj(value)
Q = Q.view(B, L_q, self.nhead, self.d_k).transpose(1, 2)
K = K.view(B, L_k, self.nhead, self.d_k).transpose(1, 2)
V = V.view(B, L_v, self.nhead, self.d_k).transpose(1, 2)
if self.use_rotary_embedding and self.rope is not None:
Q, K = self.rope.apply_rotary_pos_emb(Q, K)
scores = torch.matmul(Q, K.transpose(-2, -1)) / math.sqrt(self.d_k)
if key_padding_mask is not None:
scores = scores.masked_fill(
key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf")
)
if is_causal:
L_q_local, L_k_local = scores.size(-2), scores.size(-1)
causal_mask = torch.triu(
torch.ones(L_q_local, L_k_local, device=scores.device), diagonal=1
).bool()
scores = scores.masked_fill(
causal_mask.unsqueeze(0).unsqueeze(0), float("-inf")
)
if attn_mask is not None:
scores = scores.masked_fill(attn_mask.unsqueeze(0).unsqueeze(0), float("-inf"))
attn_weights = F.softmax(scores, dim=-1)
attn_weights = self.dropout(attn_weights)
attn_output = torch.matmul(attn_weights, V)
attn_output = attn_output.transpose(1, 2).contiguous().view(B, L_q, self.d_model)
output = self.out_proj(attn_output)
return output
try:
from torch.nn.functional import scaled_dot_product_attention
_has_flash_attn = True
except ImportError:
_has_flash_attn = False
class FlashMultiHeadAttention(nn.Module):
"""
Flash Attention 2 加速的多头注意力。
如果 PyTorch < 2.0 或 GPU 不支持,会自动回退到标准注意力。
"""
def __init__(
self,
d_model: int = 512,
nhead: int = 8,
dropout: float = 0.1,
use_rotary_embedding: bool = False,
):
super().__init__()
if not _has_flash_attn:
self._fallback = MultiHeadAttention(
d_model=d_model,
nhead=nhead,
dropout=dropout,
use_rotary_embedding=use_rotary_embedding,
)
self.d_model = d_model
self.nhead = nhead
self.d_k = d_model // nhead
self.use_rotary_embedding = use_rotary_embedding
self.rope: Optional[nn.Module] = None
return
assert d_model % nhead == 0, "d_model 必须能被 nhead 整除"
self.d_model = d_model
self.nhead = nhead
self.d_k = d_model // nhead
self.use_rotary_embedding = use_rotary_embedding
self.dropout_p = dropout
self.q_proj = nn.Linear(d_model, d_model)
self.k_proj = nn.Linear(d_model, d_model)
self.v_proj = nn.Linear(d_model, d_model)
self.out_proj = nn.Linear(d_model, d_model)
self.rope: Optional[nn.Module] = None
def forward(
self,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
key_padding_mask: Optional[torch.BoolTensor] = None,
is_causal: bool = False,
) -> torch.Tensor:
if not _has_flash_attn:
return self._fallback(query, key, value, key_padding_mask, None, is_causal)
B, L_q, _ = query.size()
L_k = key.size(1)
Q = self.q_proj(query)
K = self.k_proj(key)
V = self.v_proj(value)
Q = Q.view(B, L_q, self.nhead, self.d_k).transpose(1, 2)
K = K.view(B, L_k, self.nhead, self.d_k).transpose(1, 2)
V = V.view(B, L_k, self.nhead, self.d_k).transpose(1, 2)
if self.use_rotary_embedding and self.rope is not None:
Q, K = self.rope.apply_rotary_pos_emb(Q, K)
# Build an additive attention bias so we can combine causal mask and
# key_padding_mask without conflicting with the is_causal flag.
# F.scaled_dot_product_attention raises if both is_causal and attn_mask are set.
if is_causal or key_padding_mask is not None:
attn_bias = torch.zeros(B, 1, L_q, L_k, device=Q.device, dtype=Q.dtype)
if is_causal:
causal = torch.triu(
torch.ones(L_q, L_k, device=Q.device, dtype=torch.bool), diagonal=1
)
attn_bias = attn_bias.masked_fill(causal.unsqueeze(0).unsqueeze(0), float("-inf"))
if key_padding_mask is not None:
# key_padding_mask: [B, L_k] bool, True = pad
attn_bias = attn_bias.masked_fill(
key_padding_mask.unsqueeze(1).unsqueeze(2), float("-inf")
)
attn_output = F.scaled_dot_product_attention(
Q, K, V,
attn_mask=attn_bias,
dropout_p=self.dropout_p if self.training else 0.0,
is_causal=False,
scale=1.0 / math.sqrt(self.d_k),
)
else:
attn_output = F.scaled_dot_product_attention(
Q, K, V,
attn_mask=None,
dropout_p=self.dropout_p if self.training else 0.0,
is_causal=False,
scale=1.0 / math.sqrt(self.d_k),
)
attn_output = attn_output.transpose(1, 2).contiguous().view(B, L_q, self.d_model)
output = self.out_proj(attn_output)
return output |