Upload code/model.py with huggingface_hub
Browse files156 unmodified lines157158159160161162163164165166167168169170156 unmodified linescos = self.rope_cos.to(x.device)sin = self.rope_sin.to(x.device)# Additive Float Attention Mask (0.0 = attend, -inf = mask out)attn_mask = Noneif pad_id is not None and (idx == pad_id).any():causal = torch.tril(torch.ones(T, T, dtype=torch.bool, device=x.device))key_mask = (idx != pad_id).unsqueeze(1).unsqueeze(2) # [B, 1, 1, T]bool_mask = causal.unsqueeze(0).unsqueeze(0) & key_mask # [B, 1, T, T] True = allowedattn_mask = torch.zeros(B, 1, T, T, dtype=x.dtype, device=x.device)attn_mask = attn_mask.masked_fill(~bool_mask, float("-inf"))for block in self.blocks:if self.training and self.cfg.use_checkpoint:156 unmodified lines157158159160161162163164165166167168156 unmodified linescos = self.rope_cos.to(x.device)sin = self.rope_sin.to(x.device)# FIXED: Do NOT mask pad_id (64002 / <|endofturn|>) in Causal LM!# In a right-padded Causal LM with -100 label masking, is_causal=True naturally# prevents real tokens from attending to right-side pads, while allowing tokens# to attend to in-prompt <|endofturn|> delimiters.# Keeping attn_mask=None also enables PyTorch's native FlashAttention-2 kernel.attn_mask = Nonefor block in self.blocks:if self.training and self.cfg.use_checkpoint:
code/model.py+5-7