Eunhwan Park commited on
Update modeling_motif.py
Browse files408 unmodified lines40941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944041241341472 unmodified lines4874884895195205215225235244904914928 unmodified lines5015025035395405415425435045055065 unmodified lines512513514555556557515516517518328 unmodified lines847848849892893850894895851852853408 unmodified lines self.num_key_value_heads //= 2 self.n_rep = self.num_heads // self.num_key_value_heads
##mix attn
self.mix_attn = config.mix_attn
if self.mix_attn:
self.cq, self.ck = 6, 11 self.ch = 2
self.key_query_conv = nn.Conv2d( in_channels=self.num_heads*2, out_channels=self.num_heads*2, kernel_size=(self.cq, self.ck), padding="same", groups=self.num_heads*2 )
self.head_conv = nn.Conv1d( in_channels=self.num_heads, out_channels=self.num_heads, kernel_size=1, padding=0, groups=self.num_heads // self.ch )
self.group_norm = nn.GroupNorm(num_groups=self.num_heads, num_channels=self.num_heads)
# re-init projections self.q_proj = nn.Linear(self.hidden_size, self.hidden_size, bias=False) self.k_proj = nn.Linear(self.hidden_size, self.hidden_size // self.n_rep, bias=False)72 unmodified lines attention_mask = torch.triu( torch.full((q_len, kv_seq_len), float("-inf"), dtype=attn_weights.dtype, device=attn_weights.device), 1 + offset) ##attn weights conv2d, softmax and add attention_mask if self.mix_attn: ## condition mask==0, value : 0 attn_weights = attn_weights.masked_fill( attention_mask == 0, 0) attn_weights = self.key_query_conv(attn_weights) attn_weights = attn_weights[:, :, :kv_seq_len, :kv_seq_len]
###add attn attn_weights = attn_weights + attention_mask8 unmodified lines lambda_full = lambda_1 - lambda_2 + self.lambda_init attn_weights = attn_weights.view(bsz, self.num_heads, 2, q_len, -1) attn_weights = attn_weights[:, :, 0] - lambda_full * attn_weights[:, :, 1] ##head_conv if self.mix_attn: attn_weights = attn_weights.view(bsz, self.num_heads, -1).contiguous() attn_weights = self.head_conv(attn_weights) attn_weights = attn_weights.view(bsz, self.num_heads, q_len, -1).contiguous()
##shape : bsz, #heads, seq, head_dim attn_output = torch.matmul(attn_weights, value_states)5 unmodified lines if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim * 2): raise ValueError(f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" f" {attn_output.size()}") if self.mix_attn: attn_output = self.group_norm(attn_output)
attn_output = attn_output.transpose(1, 2).contiguous() attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
328 unmodified lines logger.warning_once( f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; " "unexpected results may be encountered.") if not config.mix_attn: self.self_attn = MOTIF_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx) self.self_attn = MOTIF_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx) else: self.self_attn = MOTIF_ATTENTION_CLASSES["eager"](config, layer_idx) self.mlp = MotifMLP(config) RMSNorm = MorehRMSNorm if MorehRMSNorm is not None else MotifRMSNormmodeling_motif.py+2-47