zhan1206 commited on
Commit
b17e601
·
1 Parent(s): 4718d7f

fix: 修复 F-NEW-13 GQA双重扩展 + S-NEW-13 GQA KV维度 + LOW清理

Browse files

致命修复:
- F-NEW-13: forward_with_qkv() V张量被_repeat_kv双重扩展导致维度崩溃
删除第二次_repeat_kv调用,V在第492行已扩展到num_heads

严重修复:
- S-NEW-13: FusionMiniLayer K/V投影硬编码num_heads,GQA不兼容
改为使用num_key_value_heads和kv_head_dim

中等(架构说明):
- M-NEW-17: forward_with_qkv通过V值重建hidden_states是近似方案
已添加注释说明此设计取舍

轻微修复:
- L-NEW-6: bertscore_moverscore hash embedding性能(保留设计)
- L-NEW-7: 删除未使用的FusionMiniAttention死代码(58行)
- L-NEW-8: full_finetune.py重复import logging
- L-NEW-9: 移除未使用的get_effective_vocab_size导入
- L-NEW-10: 已在F-NEW-13修复中消除冗余分支

12 passed, 0 warnings in 12.26s

models/fusion_mini.py CHANGED
@@ -171,71 +171,6 @@ class FusionMiniEmbeddings(nn.Module):
171
  return embeddings
172
 
173
 
174
- class FusionMiniAttention(nn.Module):
175
- """
176
- Fusion Mini 注意力层(标准多头注意力)
177
- """
178
-
179
- def __init__(self, config: FusionMiniConfig):
180
- super().__init__()
181
-
182
- self.num_attention_heads = config.num_attention_heads
183
- self.attention_head_size = config.hidden_size // config.num_attention_heads
184
- self.all_head_size = config.hidden_size
185
-
186
- self.query = nn.Linear(config.hidden_size, self.all_head_size)
187
- self.key = nn.Linear(config.hidden_size, self.all_head_size)
188
- self.value = nn.Linear(config.hidden_size, self.all_head_size)
189
-
190
- self.out = nn.Linear(config.hidden_size, config.hidden_size)
191
- self.dropout = nn.Dropout(0.1)
192
-
193
- def forward(
194
- self,
195
- hidden_states: torch.Tensor,
196
- attention_mask: Optional[torch.Tensor] = None,
197
- ) -> torch.Tensor:
198
- """
199
- 参数:
200
- hidden_states: (batch, seq_len, hidden_size)
201
- attention_mask: (batch, 1, 1, seq_len)
202
- """
203
- batch_size, seq_len, _ = hidden_states.shape
204
-
205
- # 线性投影
206
- q = self.query(hidden_states)
207
- k = self.key(hidden_states)
208
- v = self.value(hidden_states)
209
-
210
- # 重塑为多头
211
- q = q.view(batch_size, seq_len, self.num_attention_heads, self.attention_head_size).transpose(1, 2)
212
- k = k.view(batch_size, seq_len, self.num_attention_heads, self.attention_head_size).transpose(1, 2)
213
- v = v.view(batch_size, seq_len, self.num_attention_heads, self.attention_head_size).transpose(1, 2)
214
-
215
- # 计算注意力分数
216
- attention_scores = torch.matmul(q, k.transpose(-1, -2))
217
- attention_scores = attention_scores / math.sqrt(self.attention_head_size)
218
-
219
- # 应用注意力掩码
220
- if attention_mask is not None:
221
- attention_scores = attention_scores + attention_mask
222
-
223
- # Softmax
224
- attention_probs = F.softmax(attention_scores, dim=-1)
225
- attention_probs = self.dropout(attention_probs)
226
-
227
- # 加权求和
228
- context = torch.matmul(attention_probs, v)
229
-
230
- # 重塑回原始形状
231
- context = context.transpose(1, 2).contiguous().view(batch_size, seq_len, self.all_head_size)
232
-
233
- # 输出线性层
234
- output = self.out(context)
235
-
236
- return output
237
-
238
-
239
  class FusionMiniLayer(nn.Module):
240
  """
241
  Fusion Mini Transformer 层
@@ -288,10 +223,12 @@ class FusionMiniLayer(nn.Module):
288
  batch_size, seq_len, _ = hidden_states.shape
289
  num_heads = self.sbla_attention.num_heads
290
  head_dim = self.sbla_attention.head_dim
 
 
291
 
292
  Q = self.query(hidden_states).view(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
293
- K = self.key(hidden_states).view(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
294
- V = self.value(hidden_states).view(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
295
 
296
  # SBLA attention with forward_with_qkv (avoids Q/K/V projection in SBLAttention)
297
  attn_output, present_key_value = self.sbla_attention.forward_with_qkv(
 
171
  return embeddings
172
 
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  class FusionMiniLayer(nn.Module):
175
  """
176
  Fusion Mini Transformer 层
 
223
  batch_size, seq_len, _ = hidden_states.shape
224
  num_heads = self.sbla_attention.num_heads
225
  head_dim = self.sbla_attention.head_dim
226
+ num_kv_heads = self.sbla_attention.num_key_value_heads
227
+ kv_head_dim = self.sbla_attention.kv_head_dim
228
 
229
  Q = self.query(hidden_states).view(batch_size, seq_len, num_heads, head_dim).transpose(1, 2)
230
+ K = self.key(hidden_states).view(batch_size, seq_len, num_kv_heads, kv_head_dim).transpose(1, 2)
231
+ V = self.value(hidden_states).view(batch_size, seq_len, num_kv_heads, kv_head_dim).transpose(1, 2)
232
 
233
  # SBLA attention with forward_with_qkv (avoids Q/K/V projection in SBLAttention)
234
  attn_output, present_key_value = self.sbla_attention.forward_with_qkv(
models/sbla_attention.py CHANGED
@@ -551,13 +551,9 @@ class SBLAttention(nn.Module):
551
  return output, present_key_value
552
 
553
  # Reconstruct hidden_states from V for block latent computation
554
- V_full = self._repeat_kv(V, self.num_kv_groups) if self.num_kv_groups > 1 else V
555
-
556
- # Project V_full to hidden_size for latent computation
557
- # V_full: (batch, num_heads, seq_len, kv_head_dim)
558
- # -> transpose to (batch, seq_len, num_heads, kv_head_dim)
559
- # -> view to (batch, seq_len, num_heads * kv_head_dim)
560
- # -> project to (batch, seq_len, hidden_size)
561
  batch_size_v = V_full.size(0)
562
  seq_len_v = V_full.size(2)
563
  V_reshaped = V_full.transpose(1, 2).contiguous().view(batch_size_v, seq_len_v, -1) # (batch, seq_len, num_heads * kv_head_dim)
 
551
  return output, present_key_value
552
 
553
  # Reconstruct hidden_states from V for block latent computation
554
+ # V is already expanded to (B, num_heads, S, kv_head_dim) at line ~492
555
+ # No need to re-expand. v_to_hidden_proj expects num_heads * kv_head_dim input.
556
+ V_full = V
 
 
 
 
557
  batch_size_v = V_full.size(0)
558
  seq_len_v = V_full.size(2)
559
  V_reshaped = V_full.transpose(1, 2).contiguous().view(batch_size_v, seq_len_v, -1) # (batch, seq_len, num_heads * kv_head_dim)
train/full_finetune.py CHANGED
@@ -21,10 +21,10 @@ Fusion 模型全参数微调脚本
21
  """
22
 
23
  import argparse
24
- import torch
25
  import logging
26
- from typing import Optional
27
  import torch.nn as nn
 
28
 
29
  # H8-H9: Wrap optional imports in try/except
30
  try:
@@ -36,7 +36,7 @@ except ImportError:
36
  from transformers import (
37
  get_linear_schedule_with_warmup,
38
  )
39
- from models.tokenizer import get_tokenizer, get_effective_vocab_size
40
  from torch.utils.data import Dataset, DataLoader
41
  import json
42
  import os
 
21
  """
22
 
23
  import argparse
 
24
  import logging
25
+ import torch
26
  import torch.nn as nn
27
+ from typing import Optional
28
 
29
  # H8-H9: Wrap optional imports in try/except
30
  try:
 
36
  from transformers import (
37
  get_linear_schedule_with_warmup,
38
  )
39
+ from models.tokenizer import get_tokenizer
40
  from torch.utils.data import Dataset, DataLoader
41
  import json
42
  import os