



'''simple docstring'''






import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel

class 				a__:







    def __init__( self					:    Any		,     __snake_case					:    List[Any]		,     __snake_case					:    Union[str, Any]=13		,     __snake_case					:    Optional[Any]=7		,     __snake_case					:    Tuple=True		,     __snake_case					:    List[str]=True		,     __snake_case					:    Union[str, Any]=False		,     __snake_case					:    Optional[int]=True		,     __snake_case					:    List[Any]=99		,     __snake_case					:    Optional[Any]=32		,     __snake_case					:    Union[str, Any]=5		,     __snake_case					:    Dict=4		,     __snake_case					:    List[str]=37		,     __snake_case					:    Any="gelu"		,     __snake_case					:    Union[str, Any]=0.1		,     __snake_case					:    int=0.1		,     __snake_case					:    List[Any]=5_12		,     __snake_case					:    int=16		,     __snake_case					:    Optional[Any]=2		,     __snake_case					:    Any=0.02		,     __snake_case					:    Optional[int]=3		,     __snake_case					:    int=4		,     __snake_case					:    Any=None		,     ):
      a		:   Tuple	  =	parent
      a		:   Union[str, Any]	  =	batch_size
      a		:   str	  =	seq_length
      a		:   List[str]	  =	is_training
      a		:   Optional[int]	  =	use_input_mask
      a		:   Optional[int]	  =	use_token_type_ids
      a		:   Optional[int]	  =	use_labels
      a		:   str	  =	vocab_size
      a		:   Tuple	  =	hidden_size
      a		:   List[str]	  =	num_hidden_layers
      a		:   List[str]	  =	num_attention_heads
      a		:   List[Any]	  =	intermediate_size
      a		:   Union[str, Any]	  =	hidden_act
      a		:   int	  =	hidden_dropout_prob
      a		:   str	  =	attention_probs_dropout_prob
      a		:   Union[str, Any]	  =	max_position_embeddings
      a		:   Any	  =	type_vocab_size
      a		:   str	  =	type_sequence_label_size
      a		:   Dict	  =	initializer_range
      a		:   Optional[Any]	  =	num_labels
      a		:   Optional[int]	  =	num_choices
      a		:   Optional[Any]	  =	scope







    def      lowercase_			( self					:    Tuple      ):
      a		:   str	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )

      a		:   Tuple	  =	None
      if self.use_input_mask:
        a		:   int	  =	random_attention_mask([self.batch_size, self.seq_length]      )

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

      a		:   List[Any]	  =	None
      a		:   Optional[int]	  =	None
      a		:   Optional[Any]	  =	None
      if self.use_labels:
        a		:   str	  =	ids_tensor([self.batch_size]		,     self.type_sequence_label_size      )
        a		:   Any	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels      )
        a		:   str	  =	ids_tensor([self.batch_size]		,     self.num_choices      )

      a		:   str	  =	self.get_config()

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







    def      lowercase_			( self					:    List[str]      ):
      return OpenLlamaConfig(
          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=__snake_case		,     initializer_range=self.initializer_range		,     use_stable_embedding=__snake_case		,     )







    def      lowercase_			( self					:    str		,     __snake_case					:    Any		,     __snake_case					:    Tuple		,     __snake_case					:    Any		,     __snake_case					:    Any		,     __snake_case					:    str		,     __snake_case					:    List[str]		,     __snake_case					:    Optional[Any]      ):
      a		:   Any	  =	OpenLlamaModel(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Union[str, Any]	  =	model(__snake_case		,     attention_mask=__snake_case      )
      a		:   Union[str, Any]	  =	model(__snake_case      )
      self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )







    def      lowercase_			( self					:    Optional[int]		,     __snake_case					:    str		,     __snake_case					:    int		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Optional[Any]		,     __snake_case					:    List[str]		,     __snake_case					:    Optional[int]		,     __snake_case					:    Tuple		,     __snake_case					:    List[str]		,     __snake_case					:    Tuple		,     ):
      a		:   Any	  =	True
      a		:   Tuple	  =	OpenLlamaModel(__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Dict	  =	model(
          __snake_case		,     attention_mask=__snake_case		,     encoder_hidden_states=__snake_case		,     encoder_attention_mask=__snake_case		,     )
      a		:   Dict	  =	model(
          __snake_case		,     attention_mask=__snake_case		,     encoder_hidden_states=__snake_case		,     )
      a		:   List[Any]	  =	model(__snake_case		,     attention_mask=__snake_case      )
      self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )







    def      lowercase_			( self					:    Tuple		,     __snake_case					:    Dict		,     __snake_case					:    Dict		,     __snake_case					:    List[str]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    Any		,     __snake_case					:    Tuple		,     __snake_case					:    str		,     __snake_case					:    List[str]		,     __snake_case					:    Any		,     ):
      a		:   List[str]	  =	OpenLlamaForCausalLM(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Optional[int]	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)      )







    def      lowercase_			( self					:    Optional[Any]		,     __snake_case					:    Dict		,     __snake_case					:    Optional[int]		,     __snake_case					:    Tuple		,     __snake_case					:    Optional[Any]		,     __snake_case					:    str		,     __snake_case					:    str		,     __snake_case					:    Optional[int]		,     __snake_case					:    Optional[Any]		,     __snake_case					:    List[Any]		,     ):
      a		:   List[Any]	  =	True
      a		:   Dict	  =	True
      a		:   Tuple	  =	OpenLlamaForCausalLM(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()

      # first forward pass
      a		:   Any	  =	model(
          __snake_case		,     attention_mask=__snake_case		,     encoder_hidden_states=__snake_case		,     encoder_attention_mask=__snake_case		,     use_cache=__snake_case		,     )
      a		:   List[str]	  =	outputs.past_key_values

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

      # append to next input_ids and
      a		:   str	  =	torch.cat([input_ids, next_tokens]		,     dim=-1      )
      a		:   Optional[int]	  =	torch.cat([input_mask, next_mask]		,     dim=-1      )

      a		:   int	  =	model(
          __snake_case		,     attention_mask=__snake_case		,     encoder_hidden_states=__snake_case		,     encoder_attention_mask=__snake_case		,     output_hidden_states=__snake_case		,     )['hidden_states'][0]
      a		:   List[Any]	  =	model(
          __snake_case		,     attention_mask=__snake_case		,     encoder_hidden_states=__snake_case		,     encoder_attention_mask=__snake_case		,     past_key_values=__snake_case		,     output_hidden_states=__snake_case		,     )['hidden_states'][0]

      # select random slice
      a		:   List[str]	  =	ids_tensor((1,)		,     output_from_past.shape[-1]      ).item()
      a		:   List[str]	  =	output_from_no_past[:, -3:, random_slice_idx].detach()
      a		:   Optional[int]	  =	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(__snake_case		,     __snake_case		,     atol=1e-3      )      )







    def      lowercase_			( self					:    str      ):
      a		:   Any	  =	self.prepare_config_and_inputs()
      (
          (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      (
          a
      )  ,      
      )		:   int	  =	config_and_inputs
      a		:   int	  =	{'input_ids': input_ids, 'attention_mask': input_mask}
      return config, inputs_dict

@require_torch
class 				a__(   lowerCamelCase__						,					lowerCamelCase__						,					lowerCamelCase__						,					unittest.TestCase      ):
    lowercase__					=       (
        (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
    )
    lowercase__					=       (OpenLlamaForCausalLM,) if is_torch_available() else ()
    lowercase__					=       (
        {
            """feature-extraction""": OpenLlamaModel,
            """text-classification""": OpenLlamaForSequenceClassification,
            """text-generation""": OpenLlamaForCausalLM,
            """zero-shot""": OpenLlamaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowercase__					=       False
    lowercase__					=       False







    def      lowercase_			( self					:    List[Any]      ):
      a		:   List[str]	  =	OpenLlamaModelTester(self      )
      a		:   List[Any]	  =	ConfigTester(self		,     config_class=__snake_case		,     hidden_size=37      )







    def      lowercase_			( self					:    Tuple      ):
      self.config_tester.run_common_tests()







    def      lowercase_			( self					:    Optional[Any]      ):
      a		:   List[str]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__snake_case      )







    def      lowercase_			( self					:    Tuple      ):
      a		:   str	  =	self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        a		:   Union[str, Any]	  =	type
        self.model_tester.create_and_check_model(*__snake_case      )







    def      lowercase_			( self					:    Any      ):
      a  ,      a		:   Union[str, Any]	  =	self.model_tester.prepare_config_and_inputs_for_common()
      a		:   List[str]	  =	3
      a		:   List[Any]	  =	input_dict['input_ids']
      a		:   Optional[Any]	  =	input_ids.ne(1      ).to(__snake_case      )
      a		:   Dict	  =	ids_tensor([self.model_tester.batch_size]		,     self.model_tester.type_sequence_label_size      )
      a		:   int	  =	OpenLlamaForSequenceClassification(__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   int	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.assertEqual(result.logits.shape		,     (self.model_tester.batch_size, self.model_tester.num_labels)      )







    def      lowercase_			( self					:    Optional[Any]      ):
      a  ,      a		:   str	  =	self.model_tester.prepare_config_and_inputs_for_common()
      a		:   str	  =	3
      a		:   Any	  =	'single_label_classification'
      a		:   Optional[int]	  =	input_dict['input_ids']
      a		:   Tuple	  =	input_ids.ne(1      ).to(__snake_case      )
      a		:   Optional[int]	  =	ids_tensor([self.model_tester.batch_size]		,     self.model_tester.type_sequence_label_size      )
      a		:   Dict	  =	OpenLlamaForSequenceClassification(__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Any	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.assertEqual(result.logits.shape		,     (self.model_tester.batch_size, self.model_tester.num_labels)      )







    def      lowercase_			( self					:    Any      ):
      a  ,      a		:   str	  =	self.model_tester.prepare_config_and_inputs_for_common()
      a		:   List[str]	  =	3
      a		:   Any	  =	'multi_label_classification'
      a		:   List[Any]	  =	input_dict['input_ids']
      a		:   Any	  =	input_ids.ne(1      ).to(__snake_case      )
      a		:   Any	  =	ids_tensor(
          [self.model_tester.batch_size, config.num_labels]		,     self.model_tester.type_sequence_label_size      ).to(torch.float      )
      a		:   Union[str, Any]	  =	OpenLlamaForSequenceClassification(__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   int	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.assertEqual(result.logits.shape		,     (self.model_tester.batch_size, self.model_tester.num_labels)      )







    @unittest.skip('Open-Llama buffers include complex numbers, which breaks this test'      )
    def      lowercase_			( self					:    Tuple      ):
      pass







    @parameterized.expand([('linear',), ('dynamic',)]      )
    def      lowercase_			( self					:    Optional[Any]		,     __snake_case					:    Dict      ):
      a  ,      a		:   Optional[Any]	  =	self.model_tester.prepare_config_and_inputs_for_common()
      a		:   int	  =	ids_tensor([1, 10]		,     config.vocab_size      )
      a		:   List[Any]	  =	ids_tensor([1, int(config.max_position_embeddings * 1.5      )]		,     config.vocab_size      )

      set_seed(42      )  # Fixed seed at init time so the two models get the same random weights
      a		:   Any	  =	OpenLlamaModel(__snake_case      )
      original_model.to(__snake_case      )
      original_model.eval()
      a		:   List[str]	  =	original_model(__snake_case      ).last_hidden_state
      a		:   str	  =	original_model(__snake_case      ).last_hidden_state

      set_seed(42      )  # Fixed seed at init time so the two models get the same random weights
      a		:   List[str]	  =	{'type': scaling_type, 'factor': 10.0}
      a		:   int	  =	OpenLlamaModel(__snake_case      )
      scaled_model.to(__snake_case      )
      scaled_model.eval()
      a		:   List[str]	  =	scaled_model(__snake_case      ).last_hidden_state
      a		:   Optional[int]	  =	scaled_model(__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(__snake_case		,     __snake_case		,     atol=1e-5      )      )
      else:
        self.assertFalse(torch.allclose(__snake_case		,     __snake_case		,     atol=1e-5      )      )

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




'''simple docstring'''






from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType

class 				a__(   lowerCamelCase__      ):
    lowercase__					=       ["""image_processor""", """tokenizer"""]
    lowercase__					=       """BridgeTowerImageProcessor"""
    lowercase__					=       ("""RobertaTokenizer""", """RobertaTokenizerFast""")







    def __init__( self					:    int		,     __snake_case					:    str		,     __snake_case					:    List[str]      ):
      super().__init__(__snake_case		,     __snake_case      )







    def __call__( self					:    int		,     __snake_case					:    Optional[Any]		,     __snake_case					:    Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None		,     __snake_case					:    bool = True		,     __snake_case					:    Union[bool, str, PaddingStrategy] = False		,     __snake_case					:    Union[bool, str, TruncationStrategy] = None		,     __snake_case					:    Optional[int] = None		,     __snake_case					:    int = 0		,     __snake_case					:    Optional[int] = None		,     __snake_case					:    Optional[bool] = None		,     __snake_case					:    Optional[bool] = None		,     __snake_case					:    bool = False		,     __snake_case					:    bool = False		,     __snake_case					:    bool = False		,     __snake_case					:    bool = False		,     __snake_case					:    bool = True		,     __snake_case					:    Optional[Union[str, TensorType]] = None		,     **__snake_case					:    List[Any]		,     ):
      a		:   Optional[int]	  =	self.tokenizer(
          text=__snake_case		,     add_special_tokens=__snake_case		,     padding=__snake_case		,     truncation=__snake_case		,     max_length=__snake_case		,     stride=__snake_case		,     pad_to_multiple_of=__snake_case		,     return_token_type_ids=__snake_case		,     return_attention_mask=__snake_case		,     return_overflowing_tokens=__snake_case		,     return_special_tokens_mask=__snake_case		,     return_offsets_mapping=__snake_case		,     return_length=__snake_case		,     verbose=__snake_case		,     return_tensors=__snake_case		,     **__snake_case		,     )
      # add pixel_values + pixel_mask
      a		:   List[str]	  =	self.image_processor(
          __snake_case		,     return_tensors=__snake_case		,     do_normalize=__snake_case		,     do_center_crop=__snake_case		,     **__snake_case      )
      encoding.update(__snake_case      )

      return encoding







    def      lowercase_			( self					:    int		,     *__snake_case					:    List[str]		,     **__snake_case					:    List[str]      ):
      return self.tokenizer.batch_decode(*__snake_case		,     **__snake_case      )







    def      lowercase_			( self					:    List[str]		,     *__snake_case					:    Tuple		,     **__snake_case					:    Union[str, Any]      ):
      return self.tokenizer.decode(*__snake_case		,     **__snake_case      )







    @property
    def      lowercase_			( self					:    Optional[Any]      ):
      a		:   Optional[Any]	  =	self.tokenizer.model_input_names
      a		:   Union[str, Any]	  =	self.image_processor.model_input_names
      return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names      )      )