From 295d681b9ccd0811b0cd97a0c9b3ed86431bfe9a Mon Sep 17 00:00:00 2001 From: jingyaogong Date: Sun, 20 Sep 2026 20:39:51 +0800 Subject: [PATCH] [fix] fp16 attention overflow --- model/model_minimind.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/model_minimind.py b/model/model_minimind.py index 0f0c3ef..3f23fdf 100755 --- a/model/model_minimind.py +++ b/model/model_minimind.py @@ -125,10 +125,10 @@ class Attention(nn.Module): if self.flash and (seq_len > 1) and (not self.is_causal or past_key_value is None) and (attention_mask is None or torch.all(attention_mask == 1)): output = F.scaled_dot_product_attention(xq, xk, xv, dropout_p=self.dropout if self.training else 0.0, is_causal=self.is_causal) else: - scores = (xq @ xk.transpose(-2, -1)) / math.sqrt(self.head_dim) + scores = (xq @ xk.transpose(-2, -1)).float() / math.sqrt(self.head_dim) if self.is_causal: scores[:, :, :, -seq_len:] += torch.full((seq_len, seq_len), float("-inf"), device=scores.device).triu(1) if attention_mask is not None: scores += (1.0 - attention_mask.unsqueeze(1).unsqueeze(2)) * -1e9 - output = self.attn_dropout(F.softmax(scores.float(), dim=-1).type_as(xq)) @ xv + output = self.attn_dropout(F.softmax(scores, dim=-1).type_as(xq)) @ xv output = output.transpose(1, 2).reshape(bsz, seq_len, -1) output = self.resid_dropout(self.o_proj(output)) return output, past_kv