Eunhwan Park commited on
Update modeling_motif.py
Browse files408 unmodified lines40941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244372 unmodified lines5165175185195205215225235245255265278 unmodified lines5365375385395405415425435445455465 unmodified lines552553554555556557558559560328 unmodified lines889890891892893894895896897898408 unmodified linesself.num_key_value_heads //= 2self.n_rep = self.num_heads // self.num_key_value_heads##mix attnself.mix_attn = config.mix_attnif self.mix_attn:self.cq, self.ck = 6, 11self.ch = 2self.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 projectionsself.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 linesattention_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_maskif self.mix_attn:## condition mask==0, value : 0attn_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 attnattn_weights = attn_weights + attention_mask8 unmodified lineslambda_full = lambda_1 - lambda_2 + self.lambda_initattn_weights = attn_weights.view(bsz, self.num_heads, 2, q_len, -1)attn_weights = attn_weights[:, :, 0] - lambda_full * attn_weights[:, :, 1]##head_convif 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_dimattn_output = torch.matmul(attn_weights, value_states)5 unmodified linesif 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 lineslogger.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)else:self.self_attn = MOTIF_ATTENTION_CLASSES["eager"](config, layer_idx)self.mlp = MotifMLP(config)RMSNorm = MorehRMSNorm if MorehRMSNorm is not None else MotifRMSNorm408 unmodified lines40941041141241341472 unmodified lines4874884894904914928 unmodified lines5015025035045055065 unmodified lines512513514515516517518328 unmodified lines847848849850851852853408 unmodified linesself.num_key_value_heads //= 2self.n_rep = self.num_heads // self.num_key_value_heads# re-init projectionsself.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 linesattention_mask = torch.triu(torch.full((q_len, kv_seq_len), float("-inf"), dtype=attn_weights.dtype, device=attn_weights.device),1 + offset)###add attnattn_weights = attn_weights + attention_mask8 unmodified lineslambda_full = lambda_1 - lambda_2 + self.lambda_initattn_weights = attn_weights.view(bsz, self.num_heads, 2, q_len, -1)attn_weights = attn_weights[:, :, 0] - lambda_full * attn_weights[:, :, 1]##shape : bsz, #heads, seq, head_dimattn_output = torch.matmul(attn_weights, value_states)5 unmodified linesif 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()}")attn_output = attn_output.transpose(1, 2).contiguous()attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)328 unmodified lineslogger.warning_once(f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; ""unexpected results may be encountered.")self.self_attn = MOTIF_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)self.mlp = MotifMLP(config)RMSNorm = MorehRMSNorm if MorehRMSNorm is not None else MotifRMSNorm
modeling_motif.py+2-47