(
self,
hidden_states: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_value: Optional[Tuple[torch.Tensor]] = None,
output_attentions: bool = False,
use_cache: bool = False,
)
| 14 | |
| 15 | |
| 16 | def forward_2( |
| 17 | self, |
| 18 | hidden_states: torch.Tensor, |
| 19 | attention_mask: Optional[torch.Tensor] = None, |
| 20 | position_ids: Optional[torch.LongTensor] = None, |
| 21 | past_key_value: Optional[Tuple[torch.Tensor]] = None, |
| 22 | output_attentions: bool = False, |
| 23 | use_cache: bool = False, |
| 24 | ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: |
| 25 | bsz, q_len, _ = hidden_states.size() |
| 26 | |
| 27 | query_states = self.q_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) |
| 28 | key_states = self.k_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) |
| 29 | value_states = self.v_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) |
| 30 | |
| 31 | kv_seq_len = key_states.shape[-2] |
| 32 | if past_key_value is not None: |
| 33 | kv_seq_len += past_key_value[0].shape[-2] |
| 34 | cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len) |
| 35 | query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) |
| 36 | |
| 37 | assert not output_attentions, "output_attentions is not supported" |
| 38 | assert not use_cache, "use_cache is not supported" |
| 39 | assert past_key_value is None, "past_key_value is not supported" |
| 40 | |
| 41 | |
| 42 | if past_key_value is not None: |
| 43 | # reuse k, v, self_attention |
| 44 | key_states = torch.cat([past_key_value[0], key_states], dim=2) |
| 45 | value_states = torch.cat([past_key_value[1], value_states], dim=2) |
| 46 | |
| 47 | past_key_value = (key_states, value_states) if use_cache else None |
| 48 | attn_output= F.scaled_dot_product_attention(query_states,key_states,value_states,dropout_p=0.0, is_causal=True) |
| 49 | attn_weights = None |
| 50 | |
| 51 | if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): |
| 52 | raise ValueError( |
| 53 | f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" |
| 54 | f" {attn_output.size()}" |
| 55 | ) |
| 56 | |
| 57 | attn_output = attn_output.transpose(1, 2) |
| 58 | attn_output = attn_output.reshape(bsz, q_len, self.hidden_size) |
| 59 | |
| 60 | attn_output = self.o_proj(attn_output) |
| 61 | |
| 62 | if not output_attentions: |
| 63 | attn_weights = None |
| 64 | |
| 65 | return attn_output, attn_weights, past_key_value |
| 66 | |
| 67 | |
| 68 | def _prepare_decoder_attention_mask(self, attention_mask, input_shape, |
nothing calls this directly
no outgoing calls
no test coverage detected