





import collections
from typing import List, Optional, Union

from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging
from ..bert.tokenization_bert_fast import BertTokenizerFast
from .tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer, DPRReaderTokenizer


_snake_case    :       List[str]		       =    logging.get_logger(__name__)

_snake_case    :       Dict		       =    {'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

_snake_case    :       Optional[Any]		       =    {
    'vocab_file': {
        'facebook/dpr-ctx_encoder-single-nq-base': (
            'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/vocab.txt'
        ),
        'facebook/dpr-ctx_encoder-multiset-base': (
            'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'facebook/dpr-ctx_encoder-single-nq-base': (
            'https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/tokenizer.json'
        ),
        'facebook/dpr-ctx_encoder-multiset-base': (
            'https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/tokenizer.json'
        ),
    },
}
_snake_case    :       Union[str, Any]		       =    {
    'vocab_file': {
        'facebook/dpr-question_encoder-single-nq-base': (
            'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/vocab.txt'
        ),
        'facebook/dpr-question_encoder-multiset-base': (
            'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'facebook/dpr-question_encoder-single-nq-base': (
            'https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/tokenizer.json'
        ),
        'facebook/dpr-question_encoder-multiset-base': (
            'https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/tokenizer.json'
        ),
    },
}
_snake_case    :       Dict		       =    {
    'vocab_file': {
        'facebook/dpr-reader-single-nq-base': (
            'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/vocab.txt'
        ),
        'facebook/dpr-reader-multiset-base': (
            'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'facebook/dpr-reader-single-nq-base': (
            'https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/tokenizer.json'
        ),
        'facebook/dpr-reader-multiset-base': (
            'https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/tokenizer.json'
        ),
    },
}

_snake_case    :       Tuple		       =    {
    'facebook/dpr-ctx_encoder-single-nq-base': 512,
    'facebook/dpr-ctx_encoder-multiset-base': 512,
}
_snake_case    :       Tuple		       =    {
    'facebook/dpr-question_encoder-single-nq-base': 512,
    'facebook/dpr-question_encoder-multiset-base': 512,
}
_snake_case    :       Dict		       =    {
    'facebook/dpr-reader-single-nq-base': 512,
    'facebook/dpr-reader-multiset-base': 512,
}


_snake_case    :       Optional[int]		       =    {
    'facebook/dpr-ctx_encoder-single-nq-base': {'do_lower_case': True},
    'facebook/dpr-ctx_encoder-multiset-base': {'do_lower_case': True},
}
_snake_case    :       int		       =    {
    'facebook/dpr-question_encoder-single-nq-base': {'do_lower_case': True},
    'facebook/dpr-question_encoder-multiset-base': {'do_lower_case': True},
}
_snake_case    :       Tuple		       =    {
    'facebook/dpr-reader-single-nq-base': {'do_lower_case': True},
    'facebook/dpr-reader-multiset-base': {'do_lower_case': True},
}



class 							_UpperCAmelCase       (       _UpperCamelCase       ):

  """simple docstring"""


  a_              =					VOCAB_FILES_NAMES
  a_              =					CONTEXT_ENCODER_PRETRAINED_VOCAB_FILES_MAP
  a_              =					CONTEXT_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a_              =					CONTEXT_ENCODER_PRETRAINED_INIT_CONFIGURATION
  a_              =					DPRContextEncoderTokenizer





class 							_UpperCAmelCase       (       _UpperCamelCase       ):

  """simple docstring"""


  a_              =					VOCAB_FILES_NAMES
  a_              =					QUESTION_ENCODER_PRETRAINED_VOCAB_FILES_MAP
  a_              =					QUESTION_ENCODER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a_              =					QUESTION_ENCODER_PRETRAINED_INIT_CONFIGURATION
  a_              =					DPRQuestionEncoderTokenizer





_snake_case    :       Tuple		       =    collections.namedtuple(
    'DPRSpanPrediction', ['span_score', 'relevance_score', 'doc_id', 'start_index', 'end_index', 'text']
)

_snake_case    :       Optional[Any]		       =    collections.namedtuple('DPRReaderOutput', ['start_logits', 'end_logits', 'relevance_logits'])


_snake_case    :       Any		       =    R'\n    Return a dictionary with the token ids of the input strings and other information to give to `.decode_best_spans`.\n    It converts the strings of a question and different passages (title and text) in a sequence of IDs (integers),\n    using the tokenizer and vocabulary. The resulting `input_ids` is a matrix of size `(n_passages, sequence_length)`\n    with the format:\n\n    [CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>\n\n    Args:\n        questions (`str` or `List[str]`):\n            The questions to be encoded. You can specify one question for many passages. In this case, the question\n            will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in\n            `titles` or `texts`.\n        titles (`str` or `List[str]`):\n            The passages titles to be encoded. This can be a string or a list of strings if there are several passages.\n        texts (`str` or `List[str]`):\n            The passages texts to be encoded. This can be a string or a list of strings if there are several passages.\n        padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):\n            Activates and controls padding. Accepts the following values:\n\n            - `True` or `\'longest\'`: Pad to the longest sequence in the batch (or no padding if only a single sequence\n              if provided).\n            - `\'max_length\'`: Pad to a maximum length specified with the argument `max_length` or to the maximum\n              acceptable input length for the model if that argument is not provided.\n            - `False` or `\'do_not_pad\'` (default): No padding (i.e., can output a batch with sequences of different\n              lengths).\n        truncation (`bool`, `str` or [`~tokenization_utils_base.TruncationStrategy`], *optional*, defaults to `False`):\n            Activates and controls truncation. Accepts the following values:\n\n            - `True` or `\'longest_first\'`: Truncate to a maximum length specified with the argument `max_length` or to\n              the maximum acceptable input length for the model if that argument is not provided. This will truncate\n              token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch\n              of pairs) is provided.\n            - `\'only_first\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n              acceptable input length for the model if that argument is not provided. This will only truncate the first\n              sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n            - `\'only_second\'`: Truncate to a maximum length specified with the argument `max_length` or to the maximum\n              acceptable input length for the model if that argument is not provided. This will only truncate the\n              second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.\n            - `False` or `\'do_not_truncate\'` (default): No truncation (i.e., can output batch with sequence lengths\n              greater than the model maximum admissible input size).\n        max_length (`int`, *optional*):\n                Controls the maximum length to use by one of the truncation/padding parameters.\n\n                If left unset or set to `None`, this will use the predefined model maximum length if a maximum length\n                is required by one of the truncation/padding parameters. If the model has no specific maximum input\n                length (like XLNet) truncation/padding to a maximum length will be deactivated.\n        return_tensors (`str` or [`~utils.TensorType`], *optional*):\n                If set, will return tensors instead of list of python integers. Acceptable values are:\n\n                - `\'tf\'`: Return TensorFlow `tf.constant` objects.\n                - `\'pt\'`: Return PyTorch `torch.Tensor` objects.\n                - `\'np\'`: Return Numpy `np.ndarray` objects.\n        return_attention_mask (`bool`, *optional*):\n            Whether or not to return the attention mask. If not set, will return the attention mask according to the\n            specific tokenizer\'s default, defined by the `return_outputs` attribute.\n\n            [What are attention masks?](../glossary#attention-mask)\n\n    Return:\n        `Dict[str, List[List[int]]]`: A dictionary with the following keys:\n\n        - `input_ids`: List of token ids to be fed to a model.\n        - `attention_mask`: List of indices specifying which tokens should be attended to by the model.\n    '



@add_start_docstrings(_UpperCamelCase       )
class 							_UpperCAmelCase       :

  """simple docstring"""


  def __call__(							self					:						Any   ,    lowerCAmelCase_					:						Any   ,    lowerCAmelCase_					:						Optional[str] = None   ,    lowerCAmelCase_					:						Optional[str] = None   ,    lowerCAmelCase_					:						Union[bool, str] = False   ,    lowerCAmelCase_					:						Union[bool, str] = False   ,    lowerCAmelCase_					:						Optional[int] = None   ,    lowerCAmelCase_					:						Optional[Union[str, TensorType]] = None   ,    lowerCAmelCase_					:						Optional[bool] = None   ,    **lowerCAmelCase_					:						Optional[Any]   ,    )				->		BatchEncoding:
     if titles is None and texts is None:
        return super().__call__(
            lowerCAmelCase_   ,    padding=lowerCAmelCase_   ,    truncation=lowerCAmelCase_   ,    max_length=lowerCAmelCase_   ,    return_tensors=lowerCAmelCase_   ,    return_attention_mask=lowerCAmelCase_   ,    **lowerCAmelCase_   ,    )
     elif titles is None or texts is None:
        __lowerCAmelCase				      =      titles if texts is None else texts
        return super().__call__(
            lowerCAmelCase_   ,    lowerCAmelCase_   ,    padding=lowerCAmelCase_   ,    truncation=lowerCAmelCase_   ,    max_length=lowerCAmelCase_   ,    return_tensors=lowerCAmelCase_   ,    return_attention_mask=lowerCAmelCase_   ,    **lowerCAmelCase_   ,    )
     __lowerCAmelCase				      =      titles if not isinstance(lowerCAmelCase_   ,    lowerCAmelCase_	) else [titles]
     __lowerCAmelCase				      =      texts if not isinstance(lowerCAmelCase_   ,    lowerCAmelCase_	) else [texts]
     __lowerCAmelCase				      =      len(lowerCAmelCase_	)
     __lowerCAmelCase				      =      questions if not isinstance(lowerCAmelCase_   ,    lowerCAmelCase_	) else [questions] * n_passages
     assert len(lowerCAmelCase_	) == len(
         lowerCAmelCase_	), f"""There should be as many titles than texts but got {len(lowerCAmelCase_	)} titles and {len(lowerCAmelCase_	)} texts."""
     __lowerCAmelCase				      =      super().__call__(lowerCAmelCase_   ,    lowerCAmelCase_   ,    padding=lowerCAmelCase_   ,    truncation=lowerCAmelCase_	)['input_ids']
     __lowerCAmelCase				      =      super().__call__(lowerCAmelCase_   ,    add_special_tokens=lowerCAmelCase_   ,    padding=lowerCAmelCase_   ,    truncation=lowerCAmelCase_	)['input_ids']
     __lowerCAmelCase				      =      {
         'input_ids': [
             (encoded_question_and_title + encoded_text)[:max_length]
             if max_length is not None and truncation
             else encoded_question_and_title + encoded_text
             for encoded_question_and_title, encoded_text in zip(lowerCAmelCase_   ,    lowerCAmelCase_	)
         ]
     }
     if return_attention_mask is not False:
        __lowerCAmelCase				      =      []
        for input_ids in encoded_inputs["input_ids"]:
           attention_mask.append([int(input_id != self.pad_token_id	) for input_id in input_ids]	)
        __lowerCAmelCase				      =      attention_mask
     return self.pad(lowerCAmelCase_   ,    padding=lowerCAmelCase_   ,    max_length=lowerCAmelCase_   ,    return_tensors=lowerCAmelCase_	)






  def 							lowercase						(							self					:						int   ,    lowerCAmelCase_					:						BatchEncoding   ,    lowerCAmelCase_					:						DPRReaderOutput   ,    lowerCAmelCase_					:						int = 1_6   ,    lowerCAmelCase_					:						int = 6_4   ,    lowerCAmelCase_					:						int = 4   ,    )				->		List[DPRSpanPrediction]:
     __lowerCAmelCase				      =      reader_input['input_ids']
     __lowerCAmelCase      ,      __lowerCAmelCase      ,      __lowerCAmelCase				      =      reader_output[:3]
     __lowerCAmelCase				      =      len(lowerCAmelCase_	)
     __lowerCAmelCase				      =      sorted(range(lowerCAmelCase_	)   ,    reverse=lowerCAmelCase_   ,    key=relevance_logits.__getitem__	)
     __lowerCAmelCase				      =      []
     for doc_id in sorted_docs:
        __lowerCAmelCase				      =      list(input_ids[doc_id]	)
        # assuming question & title information is at the beginning of the sequence
        __lowerCAmelCase				      =      sequence_ids.index(self.sep_token_id   ,    2	) + 1  # second sep id
        if sequence_ids[-1] == self.pad_token_id:
           __lowerCAmelCase				      =      sequence_ids.index(self.pad_token_id	)
        else:
           __lowerCAmelCase				      =      len(lowerCAmelCase_	)

        __lowerCAmelCase				      =      self._get_best_spans(
            start_logits=start_logits[doc_id][passage_offset:sequence_len]   ,    end_logits=end_logits[doc_id][passage_offset:sequence_len]   ,    max_answer_length=lowerCAmelCase_   ,    top_spans=lowerCAmelCase_   ,    )
        for start_index, end_index in best_spans:
           start_index += passage_offset
           end_index += passage_offset
           nbest_spans_predictions.append(
               DPRSpanPrediction(
                   span_score=start_logits[doc_id][start_index] + end_logits[doc_id][end_index]   ,    relevance_score=relevance_logits[doc_id]   ,    doc_id=lowerCAmelCase_   ,    start_index=lowerCAmelCase_   ,    end_index=lowerCAmelCase_   ,    text=self.decode(sequence_ids[start_index : end_index + 1]	)   ,    )	)
        if len(lowerCAmelCase_	) >= num_spans:
           break
     return nbest_spans_predictions[:num_spans]






  def 							lowercase						(							self					:						Any   ,    lowerCAmelCase_					:						List[int]   ,    lowerCAmelCase_					:						List[int]   ,    lowerCAmelCase_					:						int   ,    lowerCAmelCase_					:						int   ,    )				->		List[DPRSpanPrediction]:
     __lowerCAmelCase				      =      []
     for start_index, start_score in enumerate(lowerCAmelCase_	):
        for answer_length, end_score in enumerate(end_logits[start_index : start_index + max_answer_length]	):
           scores.append(((start_index, start_index + answer_length), start_score + end_score)	)
     __lowerCAmelCase				      =      sorted(lowerCAmelCase_   ,    key=lambda lowerCAmelCase_	: x[1]   ,    reverse=lowerCAmelCase_	)
     __lowerCAmelCase				      =      []
     for (start_index, end_index), score in scores:
        assert start_index <= end_index, f"""Wrong span indices: [{start_index}:{end_index}]"""
        __lowerCAmelCase				      =      end_index - start_index + 1
        assert length <= max_answer_length, f"""Span is too long: {length} > {max_answer_length}"""
        if any(
            start_index <= prev_start_index <= prev_end_index <= end_index
            or prev_start_index <= start_index <= end_index <= prev_end_index
            for (prev_start_index, prev_end_index) in chosen_span_intervals	):
           continue
        chosen_span_intervals.append((start_index, end_index)	)

        if len(lowerCAmelCase_	) == top_spans:
           break
     return chosen_span_intervals





@add_end_docstrings(_UpperCamelCase       )
class 							_UpperCAmelCase       (       _UpperCamelCase						,       _UpperCamelCase       ):

  """simple docstring"""


  a_              =					VOCAB_FILES_NAMES
  a_              =					READER_PRETRAINED_VOCAB_FILES_MAP
  a_              =					READER_PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a_              =					READER_PRETRAINED_INIT_CONFIGURATION
  a_              =					["""input_ids""", """attention_mask"""]
  a_              =					DPRReaderTokenizer



"""simple docstring"""




import unittest

from parameterized import parameterized

from transformers import LlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer







class     lowercase:



     def __init__(							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=1_3     ,				__SCREAMING_SNAKE_CASE=7     ,				__SCREAMING_SNAKE_CASE=True     ,				__SCREAMING_SNAKE_CASE=True     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=True     ,				__SCREAMING_SNAKE_CASE=9_9     ,				__SCREAMING_SNAKE_CASE=3_2     ,				__SCREAMING_SNAKE_CASE=5     ,				__SCREAMING_SNAKE_CASE=4     ,				__SCREAMING_SNAKE_CASE=3_7     ,				__SCREAMING_SNAKE_CASE="gelu"     ,				__SCREAMING_SNAKE_CASE=0.1     ,				__SCREAMING_SNAKE_CASE=0.1     ,				__SCREAMING_SNAKE_CASE=5_1_2     ,				__SCREAMING_SNAKE_CASE=1_6     ,				__SCREAMING_SNAKE_CASE=2     ,				__SCREAMING_SNAKE_CASE=0.02     ,				__SCREAMING_SNAKE_CASE=3     ,				__SCREAMING_SNAKE_CASE=4     ,				__SCREAMING_SNAKE_CASE=None     ,				)		->     int:







         """simple docstring"""



         a__		=			parent
         a__		=			batch_size
         a__		=			seq_length
         a__		=			is_training
         a__		=			use_input_mask
         a__		=			use_token_type_ids
         a__		=			use_labels
         a__		=			vocab_size
         a__		=			hidden_size
         a__		=			num_hidden_layers
         a__		=			num_attention_heads
         a__		=			intermediate_size
         a__		=			hidden_act
         a__		=			hidden_dropout_prob
         a__		=			attention_probs_dropout_prob
         a__		=			max_position_embeddings
         a__		=			type_vocab_size
         a__		=			type_sequence_label_size
         a__		=			initializer_range
         a__		=			num_labels
         a__		=			num_choices
         a__		=			scope



     def 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         a__		=			ids_tensor([self.batch_size, self.seq_length]     ,				self.vocab_size   )

         a__		=			None
         if self.use_input_mask:
             a__		=			random_attention_mask([self.batch_size, self.seq_length]   )

         a__		=			None
         if self.use_token_type_ids:
             a__		=			ids_tensor([self.batch_size, self.seq_length]     ,				self.type_vocab_size   )

         a__		=			None
         a__		=			None
         a__		=			None
         if self.use_labels:
             a__		=			ids_tensor([self.batch_size]     ,				self.type_sequence_label_size   )
             a__		=			ids_tensor([self.batch_size, self.seq_length]     ,				self.num_labels   )
             a__		=			ids_tensor([self.batch_size]     ,				self.num_choices   )

         a__		=			self.get_config()

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         return LlamaConfig(
             vocab_size=self.vocab_size     ,				hidden_size=self.hidden_size     ,				num_hidden_layers=self.num_hidden_layers     ,				num_attention_heads=self.num_attention_heads     ,				intermediate_size=self.intermediate_size     ,				hidden_act=self.hidden_act     ,				hidden_dropout_prob=self.hidden_dropout_prob     ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,				max_position_embeddings=self.max_position_embeddings     ,				type_vocab_size=self.type_vocab_size     ,				is_decoder=__SCREAMING_SNAKE_CASE     ,				initializer_range=self.initializer_range     ,				)



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )		->     Dict:







         """simple docstring"""



         a__		=			LlamaModel(config=__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE   )
         a__		=			model(__SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				)		->     Any:







         """simple docstring"""



         a__		=			True
         a__		=			LlamaModel(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				)
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				)
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				)		->     List[str]:







         """simple docstring"""



         a__		=			LlamaForCausalLM(config=__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.seq_length, self.vocab_size)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				)		->     Optional[int]:







         """simple docstring"""



         a__		=			True
         a__		=			True
         a__		=			LlamaForCausalLM(config=__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()

         # first forward pass
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				use_cache=__SCREAMING_SNAKE_CASE     ,				)
         a__		=			outputs.past_key_values

         # create hypothetical multiple next token and extent to next_input_ids
         a__		=			ids_tensor((self.batch_size, 3)     ,				config.vocab_size   )
         a__		=			ids_tensor((self.batch_size, 3)     ,				vocab_size=2   )

         # append to next input_ids and
         a__		=			torch.cat([input_ids, next_tokens]     ,				dim=-1   )
         a__		=			torch.cat([input_mask, next_mask]     ,				dim=-1   )

         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				output_hidden_states=__SCREAMING_SNAKE_CASE     ,				)['hidden_states'][0]
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				past_key_values=__SCREAMING_SNAKE_CASE     ,				output_hidden_states=__SCREAMING_SNAKE_CASE     ,				)['hidden_states'][0]

         # select random slice
         a__		=			ids_tensor((1,)     ,				output_from_past.shape[-1]   ).item()
         a__		=			output_from_no_past[:, -3:, random_slice_idx].detach()
         a__		=			output_from_past[:, :, random_slice_idx].detach()

         self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

         # test that outputs are equal for slice
         self.parent.assertTrue(torch.allclose(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-3   )   )




     def 			lowercase__  (							self   )		->     Dict:







         """simple docstring"""



         a__		=			self.prepare_config_and_inputs()
         (
             (
             a__
         )	,     (
             a__
         )	,     (
             a__
         )	,     (
             a__
         )	,     (
             a__
         )	,     (
             a__
         )	,     (
             a__
         )	,     
         )		=			config_and_inputs
         a__		=			{'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict







@require_torch
class     lowercase(_lowercase    ,						_lowercase    ,						_lowercase    ,						unittest.TestCase       ):
     __snake_case:	Optional[int]						=     (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else ()
     __snake_case:	Optional[int]						=     (LlamaForCausalLM,) if is_torch_available() else ()
     __snake_case:	List[Any]						=     (
         {
             'feature-extraction': LlamaModel,
             'text-classification': LlamaForSequenceClassification,
             'text-generation': LlamaForCausalLM,
             'zero-shot': LlamaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __snake_case:	List[Any]						=     False
     __snake_case:	List[str]						=     False



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         a__		=			LlamaModelTester(self   )
         a__		=			ConfigTester(self     ,				config_class=__SCREAMING_SNAKE_CASE     ,				hidden_size=3_7   )



     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         self.config_tester.run_common_tests()



     def 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         a__		=			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         a__		=			self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
             a__		=			type
             self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__	,     a__		=			self.model_tester.prepare_config_and_inputs_for_common()
         a__		=			3
         a__		=			input_dict['input_ids']
         a__		=			input_ids.ne(1   ).to(__SCREAMING_SNAKE_CASE   )
         a__		=			ids_tensor([self.model_tester.batch_size]     ,				self.model_tester.type_sequence_label_size   )
         a__		=			LlamaForSequenceClassification(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(result.logits.shape     ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )



     def 			lowercase__  (							self   )		->     Union[str, Any]:







         """simple docstring"""



         a__	,     a__		=			self.model_tester.prepare_config_and_inputs_for_common()
         a__		=			3
         a__		=			'single_label_classification'
         a__		=			input_dict['input_ids']
         a__		=			input_ids.ne(1   ).to(__SCREAMING_SNAKE_CASE   )
         a__		=			ids_tensor([self.model_tester.batch_size]     ,				self.model_tester.type_sequence_label_size   )
         a__		=			LlamaForSequenceClassification(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(result.logits.shape     ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__	,     a__		=			self.model_tester.prepare_config_and_inputs_for_common()
         a__		=			3
         a__		=			'multi_label_classification'
         a__		=			input_dict['input_ids']
         a__		=			input_ids.ne(1   ).to(__SCREAMING_SNAKE_CASE   )
         a__		=			ids_tensor(
             [self.model_tester.batch_size, config.num_labels]     ,				self.model_tester.type_sequence_label_size   ).to(torch.float   )
         a__		=			LlamaForSequenceClassification(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(result.logits.shape     ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )



     @unittest.skip('LLaMA buffers include complex numbers, which breaks this test'   )
     def 			lowercase__  (							self   )		->     List[str]:







         """simple docstring"""



         pass




     @parameterized.expand([('linear',), ('dynamic',)]   )
     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Optional[int]:







         """simple docstring"""



         a__	,     a__		=			self.model_tester.prepare_config_and_inputs_for_common()
         a__		=			ids_tensor([1, 1_0]     ,				config.vocab_size   )
         a__		=			ids_tensor([1, int(config.max_position_embeddings * 1.5   )]     ,				config.vocab_size   )

         set_seed(4_2   )  # Fixed seed at init time so the two models get the same random weights
         a__		=			LlamaModel(__SCREAMING_SNAKE_CASE   )
         original_model.to(__SCREAMING_SNAKE_CASE   )
         original_model.eval()
         a__		=			original_model(__SCREAMING_SNAKE_CASE   ).last_hidden_state
         a__		=			original_model(__SCREAMING_SNAKE_CASE   ).last_hidden_state

         set_seed(4_2   )  # Fixed seed at init time so the two models get the same random weights
         a__		=			{'type': scaling_type, 'factor': 10.0}
         a__		=			LlamaModel(__SCREAMING_SNAKE_CASE   )
         scaled_model.to(__SCREAMING_SNAKE_CASE   )
         scaled_model.eval()
         a__		=			scaled_model(__SCREAMING_SNAKE_CASE   ).last_hidden_state
         a__		=			scaled_model(__SCREAMING_SNAKE_CASE   ).last_hidden_state

         # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
         # maximum sequence length, so the outputs for the short input should match.
         if scaling_type == "dynamic":
             self.assertTrue(torch.allclose(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5   )   )
         else:
             self.assertFalse(torch.allclose(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5   )   )

         # The output should be different for long inputs
         self.assertFalse(torch.allclose(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5   )   )







@require_torch
class     lowercase(unittest.TestCase       ):



     @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!'   )
     @slow
     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         a__		=			[1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor([input_ids]   )   )
         # Expected mean on dim = -1
         a__		=			torch.tensor([[-6.65_50, -4.12_27, -4.98_59, -3.24_06, 0.82_62, -3.00_33, 1.29_64, -3.36_99]]   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # slicing logits[0, 0, 0:30]
         # fmt: off
         a__		=			torch.tensor([-12.82_81, -7.44_53, -0.46_39, -8.06_25, -7.25_00, -8.00_00, -6.48_83, -7.76_95, -7.84_38, -7.03_12, -6.21_88, -7.13_28, -1.84_96, 1.99_61, -8.62_50, -6.72_27, -12.82_81, -6.94_92, -7.07_42, -7.78_52, -7.58_20, -7.90_62, -6.93_75, -7.98_05, -8.34_38, -8.15_62, -8.04_69, -7.62_50, -7.74_22, -7.33_98,]   )
         # fmt: on
         torch.testing.assert_close(out[0, 0, :3_0]     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5     ,				rtol=1e-5   )



     @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!'   )
     @slow
     def 			lowercase__  (							self   )		->     Optional[int]:







         """simple docstring"""



         a__		=			[1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor(__SCREAMING_SNAKE_CASE   )   )
         # Expected mean on dim = -1
         a__		=			torch.tensor([[-2.06_22, -1.27_94, -1.16_38, -0.97_88, -1.46_03, -1.02_38, -1.78_93, -1.44_11]]   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # slicing logits[0, 0, 0:30]
         # fmt: off
         a__		=			torch.tensor([-8.14_06, -8.05_47, 2.74_61, -1.23_44, -0.14_48, -1.82_62, -1.00_20, -1.81_54, -1.68_95, -1.85_16, -2.35_74, -0.92_77, 3.75_98, 6.57_42, -1.29_98, -0.11_77, -8.14_06, -2.96_88, -2.91_99, -3.16_99, -3.52_54, -2.35_55, -2.79_88, -3.41_41, -2.82_62, -4.51_95, -3.33_79, -3.31_64, -2.78_32, -3.02_73]   )
         # fmt: on
         torch.testing.assert_close(out[0, 0, :3_0]     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5     ,				rtol=1e-5   )



     @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!'   )
     @slow
     def 			lowercase__  (							self   )		->     str:







         """simple docstring"""



         a__		=			[1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor(__SCREAMING_SNAKE_CASE   )   )
         # Expected mean on dim = -1
         a__		=			torch.tensor([[-0.85_62, -1.85_20, -0.75_51, -0.41_62, -1.51_61, -1.20_38, -2.48_23, -2.32_54]]   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # slicing logits[0, 0, 0:30]
         # fmt: off
         a__		=			torch.tensor([-2.22_27, 4.88_28, 0.90_23, -0.45_78, -0.78_71, -0.10_33, -0.62_21, -0.57_86, -0.78_03, -1.06_74, -1.29_20, -0.15_70, 0.80_08, 2.07_23, -0.94_97, 0.27_71, -2.22_27, -0.76_12, -1.43_46, -1.20_61, -1.64_26, -0.30_00, -0.71_39, -1.19_34, -1.86_91, -1.69_73, -1.59_47, -1.27_05, -0.35_23, -0.55_13]   )
         # fmt: on
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )



     @unittest.skip(
         'Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test'   )
     @slow
     def 			lowercase__  (							self   )		->     Tuple:







         """simple docstring"""



         a__		=			[1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor(__SCREAMING_SNAKE_CASE   )   )

         a__		=			torch.tensor(
             [[-4.23_27, -3.33_60, -4.66_65, -4.76_31, -1.81_80, -3.41_70, -1.42_11, -3.18_10]]     ,				dtype=torch.floataa   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # fmt: off
         a__		=			torch.tensor([-9.49_22, -3.95_51, 1.79_98, -5.67_58, -5.10_55, -5.89_84, -4.83_20, -6.80_86, -6.53_91, -5.61_72, -5.58_20, -5.53_52, 1.78_81, 3.62_89, -6.51_17, -3.47_85, -9.50_00, -6.03_52, -6.81_25, -6.01_95, -6.68_36, -5.47_27, -6.28_12, -6.03_91, -7.33_98, -7.42_97, -7.48_44, -6.58_20, -5.87_89, -5.53_12]   )
         # fmt: on
         torch.testing.assert_close(out[0, 0, :3_0]     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5     ,				rtol=1e-5   )




     @unittest.skip('Model is curently gated'   )
     @slow
     def 			lowercase__  (							self   )		->     List[str]:







         """simple docstring"""



         a__		=			'Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the "princi'
         a__		=			'Simply put, the theory of relativity states that '
         a__		=			LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf'   )
         a__		=			tokenizer.encode(__SCREAMING_SNAKE_CASE     ,				return_tensors='pt'   )
         a__		=			LlamaForCausalLM.from_pretrained(
             'meta-llama/Llama-2-13b-chat-hf'     ,				device_map='sequential'     ,				use_safetensors=__SCREAMING_SNAKE_CASE   )

         # greedy generation outputs
         a__		=			model.generate(__SCREAMING_SNAKE_CASE     ,				max_new_tokens=6_4     ,				top_p=__SCREAMING_SNAKE_CASE     ,				temperature=1     ,				do_sample=__SCREAMING_SNAKE_CASE   )
         a__		=			tokenizer.decode(generated_ids[0]     ,				skip_special_tokens=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )


