| from typing import Optional, Tuple, Union |
| from functools import partial |
|
|
| import torch |
| from transformers.cache_utils import Cache, DynamicCache |
| from transformers.modeling_flash_attention_utils import FlashAttentionKwargs |
| from transformers.modeling_outputs import BaseModelOutputWithPast |
| from transformers.processing_utils import Unpack |
| from transformers.utils import logging |
| from transformers import AutoModel |
| from transformers.models.mistral.configuration_mistral import MistralConfig |
| from transformers.models.mistral.modeling_mistral import MistralModel |
| from transformers.modeling_attn_mask_utils import _prepare_4d_attention_mask, _prepare_4d_attention_mask_for_sdpa |
|
|
| from .configuration_mistral_dual import MistralDualConfig |
|
|
| logger = logging.get_logger(__name__) |
|
|
| class MistralDualModel(MistralModel): |
| config_class = MistralDualConfig |
|
|
| def __init__(self, config: MistralDualConfig): |
| super().__init__(config) |
| for layer in self.layers: |
| layer.self_attn.is_causal = False |
|
|
| def forward( |
| self, |
| input_ids: torch.LongTensor = None, |
| attention_mask: Optional[torch.Tensor] = None, |
| position_ids: Optional[torch.LongTensor] = None, |
| past_key_values: Optional[Cache] = None, |
| inputs_embeds: Optional[torch.FloatTensor] = None, |
| use_cache: Optional[bool] = None, |
| output_attentions: Optional[bool] = None, |
| output_hidden_states: Optional[bool] = None, |
| return_dict: Optional[bool] = None, |
| cache_position: Optional[torch.LongTensor] = None, |
| is_causal = False, |
| **flash_attn_kwargs: Unpack[FlashAttentionKwargs], |
| ) -> Union[Tuple, BaseModelOutputWithPast]: |
| output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions |
| output_hidden_states = ( |
| output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states |
| ) |
| use_cache = use_cache if use_cache is not None else self.config.use_cache |
| return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
|
|
| if (input_ids is None) ^ (inputs_embeds is not None): |
| raise ValueError("You must specify exactly one of input_ids or inputs_embeds") |
|
|
| if self.gradient_checkpointing and self.training and use_cache: |
| logger.warning_once( |
| "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`." |
| ) |
| use_cache = False |
|
|
| if inputs_embeds is None: |
| inputs_embeds = self.embed_tokens(input_ids) |
|
|
| if use_cache and past_key_values is None: |
| past_key_values = DynamicCache() |
|
|
| if cache_position is None: |
| past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0 |
| cache_position = torch.arange( |
| past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device |
| ) |
|
|
| if position_ids is None: |
| position_ids = cache_position.unsqueeze(0) |
|
|
| causal_mask = self._update_causal_mask( |
| attention_mask, inputs_embeds, cache_position, past_key_values, output_attentions |
| ) |
|
|
| |
|
|
| hidden_states = inputs_embeds |
|
|
| |
| position_embeddings = self.rotary_emb(hidden_states, position_ids) |
|
|
| |
| all_hidden_states = () if output_hidden_states else None |
| all_self_attns = () if output_attentions else None |
|
|
| for decoder_layer in self.layers[: self.config.num_hidden_layers]: |
| if output_hidden_states: |
| all_hidden_states += (hidden_states,) |
|
|
| if self.gradient_checkpointing and self.training: |
| layer_outputs = self._gradient_checkpointing_func( |
| partial(decoder_layer.__call__, is_causal=is_causal), |
| hidden_states, |
| causal_mask, |
| position_ids, |
| past_key_values, |
| output_attentions, |
| use_cache, |
| cache_position, |
| position_embeddings, |
| ) |
| else: |
| layer_outputs = decoder_layer( |
| hidden_states, |
| attention_mask=causal_mask, |
| position_ids=position_ids, |
| past_key_value=past_key_values, |
| output_attentions=output_attentions, |
| use_cache=use_cache, |
| cache_position=cache_position, |
| position_embeddings=position_embeddings, |
| is_causal=is_causal, |
| **flash_attn_kwargs, |
| ) |
|
|
| hidden_states = layer_outputs[0] |
|
|
| if output_attentions: |
| all_self_attns += (layer_outputs[1],) |
|
|
| hidden_states = self.norm(hidden_states) |
|
|
| |
| if output_hidden_states: |
| all_hidden_states += (hidden_states,) |
|
|
| output = BaseModelOutputWithPast( |
| last_hidden_state=hidden_states, |
| past_key_values=past_key_values if use_cache else None, |
| hidden_states=all_hidden_states, |
| attentions=all_self_attns, |
| ) |
| return output if return_dict else output.to_tuple() |
|
|
| @staticmethod |
| def _prepare_4d_causal_attention_mask_with_cache_position( |
| attention_mask: torch.Tensor, |
| sequence_length: int, |
| target_length: int, |
| dtype: torch.dtype, |
| device: torch.device, |
| cache_position: torch.Tensor, |
| batch_size: int, |
| config: MistralConfig, |
| past_key_values: Cache, |
| ): |
| """ |
| Creates a bidirectional 4D attention mask of shape `(batch_size, 1, query_length, key_value_length)`, |
| where all tokens can attend to all others. |
| """ |
| if attention_mask is not None and attention_mask.dim() == 4: |
| return attention_mask |
|
|
| min_dtype = torch.finfo(dtype).min |
| |
| bidirectional_mask = torch.zeros((sequence_length, target_length), dtype=dtype, device=device) |
| bidirectional_mask = bidirectional_mask[None, None, :, :].expand(batch_size, 1, -1, -1) |
|
|
| if attention_mask is not None: |
| bidirectional_mask = bidirectional_mask.clone() |
| if attention_mask.shape[-1] > target_length: |
| attention_mask = attention_mask[:, :target_length] |
| mask_length = attention_mask.shape[-1] |
| padding_mask = bidirectional_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :] |
| padding_mask = padding_mask == 0 |
| bidirectional_mask[:, :, :, :mask_length] = bidirectional_mask[:, :, :, :mask_length].masked_fill( |
| padding_mask, min_dtype |
| ) |
| |
| return bidirectional_mask |
|
|
|
|
| AutoModel.register(MistralDualConfig, MistralDualModel) |
| MistralDualModel.register_for_auto_class() |