






"""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							,		_lowerCamelCase							,		_lowerCamelCase=1_3							,		_lowerCamelCase=7							,		_lowerCamelCase=True							,		_lowerCamelCase=True							,		_lowerCamelCase=False							,		_lowerCamelCase=True							,		_lowerCamelCase=9_9							,		_lowerCamelCase=3_2							,		_lowerCamelCase=5							,		_lowerCamelCase=4							,		_lowerCamelCase=3_7							,		_lowerCamelCase="gelu"							,		_lowerCamelCase=0.1							,		_lowerCamelCase=0.1							,		_lowerCamelCase=5_1_2							,		_lowerCamelCase=1_6							,		_lowerCamelCase=2							,		_lowerCamelCase=0.0_2							,		_lowerCamelCase=3							,		_lowerCamelCase=4							,		_lowerCamelCase=None							,		):
      lowercase							    =       parent
      lowercase							    =       batch_size
      lowercase							    =       seq_length
      lowercase							    =       is_training
      lowercase							    =       use_input_mask
      lowercase							    =       use_token_type_ids
      lowercase							    =       use_labels
      lowercase							    =       vocab_size
      lowercase							    =       hidden_size
      lowercase							    =       num_hidden_layers
      lowercase							    =       num_attention_heads
      lowercase							    =       intermediate_size
      lowercase							    =       hidden_act
      lowercase							    =       hidden_dropout_prob
      lowercase							    =       attention_probs_dropout_prob
      lowercase							    =       max_position_embeddings
      lowercase							    =       type_vocab_size
      lowercase							    =       type_sequence_label_size
      lowercase							    =       initializer_range
      lowercase							    =       num_labels
      lowercase							    =       num_choices
      lowercase							    =       scope







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size       )

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

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

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

      lowercase							    =       self.get_config()

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







    def 			UpperCamelCase_      (			self       ):
      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=_lowerCamelCase							,		initializer_range=self.initializer_range							,		use_stable_embedding=_lowerCamelCase							,		)







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       OpenLlamaModel(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase       )
      lowercase							    =       model(_lowerCamelCase       )
      self.parent.assertEqual(result.last_hidden_state.shape							,		(self.batch_size, self.seq_length, self.hidden_size)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		):
      lowercase							    =       True
      lowercase							    =       OpenLlamaModel(_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(
          _lowerCamelCase							,		attention_mask=_lowerCamelCase							,		encoder_hidden_states=_lowerCamelCase							,		encoder_attention_mask=_lowerCamelCase							,		)
      lowercase							    =       model(
          _lowerCamelCase							,		attention_mask=_lowerCamelCase							,		encoder_hidden_states=_lowerCamelCase							,		)
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase       )
      self.parent.assertEqual(result.last_hidden_state.shape							,		(self.batch_size, self.seq_length, self.hidden_size)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		):
      lowercase							    =       OpenLlamaForCausalLM(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		labels=_lowerCamelCase       )
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)       )







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase							,		):
      lowercase							    =       True
      lowercase							    =       True
      lowercase							    =       OpenLlamaForCausalLM(config=_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()

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

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

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

      lowercase							    =       model(
          _lowerCamelCase							,		attention_mask=_lowerCamelCase							,		encoder_hidden_states=_lowerCamelCase							,		encoder_attention_mask=_lowerCamelCase							,		output_hidden_states=_lowerCamelCase							,		)['hidden_states'][0]
      lowercase							    =       model(
          _lowerCamelCase							,		attention_mask=_lowerCamelCase							,		encoder_hidden_states=_lowerCamelCase							,		encoder_attention_mask=_lowerCamelCase							,		past_key_values=_lowerCamelCase							,		output_hidden_states=_lowerCamelCase							,		)['hidden_states'][0]

      # select random slice
      lowercase							    =       ids_tensor((1,)							,		output_from_past.shape[-1]       ).item()
      lowercase							    =       output_from_no_past[:, -3:, random_slice_idx].detach()
      lowercase							    =       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(_lowerCamelCase							,		_lowerCamelCase							,		atol=1e-3       )       )







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


@require_torch
class        a     (   a_,				a_,				a_,				unittest.TestCase	):
    UpperCAmelCase_ :     Optional[Any]	     =(
        (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
    )
    UpperCAmelCase_ :     Optional[int]	     =(OpenLlamaForCausalLM,) if is_torch_available() else ()
    UpperCAmelCase_ :     Optional[int]	     =(
        {
            "feature-extraction": OpenLlamaModel,
            "text-classification": OpenLlamaForSequenceClassification,
            "text-generation": OpenLlamaForCausalLM,
            "zero-shot": OpenLlamaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    UpperCAmelCase_ :     List[str]	     =False
    UpperCAmelCase_ :     Optional[int]	     =False







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       OpenLlamaModelTester(self       )
      lowercase							    =       ConfigTester(self							,		config_class=_lowerCamelCase							,		hidden_size=3_7       )







    def 			UpperCamelCase_      (			self       ):
      self.config_tester.run_common_tests()







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        lowercase							    =       type
        self.model_tester.create_and_check_model(*_lowerCamelCase       )







    def 			UpperCamelCase_      (			self       ):
      lowercase    ,     lowercase							    =       self.model_tester.prepare_config_and_inputs_for_common()
      lowercase							    =       3
      lowercase							    =       input_dict['input_ids']
      lowercase							    =       input_ids.ne(1       ).to(_lowerCamelCase       )
      lowercase							    =       ids_tensor([self.model_tester.batch_size]							,		self.model_tester.type_sequence_label_size       )
      lowercase							    =       OpenLlamaForSequenceClassification(_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		labels=_lowerCamelCase       )
      self.assertEqual(result.logits.shape							,		(self.model_tester.batch_size, self.model_tester.num_labels)       )







    def 			UpperCamelCase_      (			self       ):
      lowercase    ,     lowercase							    =       self.model_tester.prepare_config_and_inputs_for_common()
      lowercase							    =       3
      lowercase							    =       'single_label_classification'
      lowercase							    =       input_dict['input_ids']
      lowercase							    =       input_ids.ne(1       ).to(_lowerCamelCase       )
      lowercase							    =       ids_tensor([self.model_tester.batch_size]							,		self.model_tester.type_sequence_label_size       )
      lowercase							    =       OpenLlamaForSequenceClassification(_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		labels=_lowerCamelCase       )
      self.assertEqual(result.logits.shape							,		(self.model_tester.batch_size, self.model_tester.num_labels)       )







    def 			UpperCamelCase_      (			self       ):
      lowercase    ,     lowercase							    =       self.model_tester.prepare_config_and_inputs_for_common()
      lowercase							    =       3
      lowercase							    =       'multi_label_classification'
      lowercase							    =       input_dict['input_ids']
      lowercase							    =       input_ids.ne(1       ).to(_lowerCamelCase       )
      lowercase							    =       ids_tensor(
          [self.model_tester.batch_size, config.num_labels]							,		self.model_tester.type_sequence_label_size       ).to(torch.float       )
      lowercase							    =       OpenLlamaForSequenceClassification(_lowerCamelCase       )
      model.to(_lowerCamelCase       )
      model.eval()
      lowercase							    =       model(_lowerCamelCase							,		attention_mask=_lowerCamelCase							,		labels=_lowerCamelCase       )
      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 			UpperCamelCase_      (			self       ):
      pass







    @parameterized.expand([('linear',), ('dynamic',)]       )
    def 			UpperCamelCase_      (			self							,		_lowerCamelCase       ):
      lowercase    ,     lowercase							    =       self.model_tester.prepare_config_and_inputs_for_common()
      lowercase							    =       ids_tensor([1, 1_0]							,		config.vocab_size       )
      lowercase							    =       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
      lowercase							    =       OpenLlamaModel(_lowerCamelCase       )
      original_model.to(_lowerCamelCase       )
      original_model.eval()
      lowercase							    =       original_model(_lowerCamelCase       ).last_hidden_state
      lowercase							    =       original_model(_lowerCamelCase       ).last_hidden_state

      set_seed(4_2       )  # Fixed seed at init time so the two models get the same random weights
      lowercase							    =       {'type': scaling_type, 'factor': 1_0.0}
      lowercase							    =       OpenLlamaModel(_lowerCamelCase       )
      scaled_model.to(_lowerCamelCase       )
      scaled_model.eval()
      lowercase							    =       scaled_model(_lowerCamelCase       ).last_hidden_state
      lowercase							    =       scaled_model(_lowerCamelCase       ).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(_lowerCamelCase							,		_lowerCamelCase							,		atol=1e-5       )       )
      else:
        self.assertFalse(torch.allclose(_lowerCamelCase							,		_lowerCamelCase							,		atol=1e-5       )       )

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










"""simple docstring"""



import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class        a     (   a_	):
    UpperCAmelCase_ :     List[Any]	     =["image_processor", "tokenizer"]
    UpperCAmelCase_ :     str	     ="AutoImageProcessor"
    UpperCAmelCase_ :     Any	     ="AutoTokenizer"







    def __init__(			self							,		_lowerCamelCase=None							,		_lowerCamelCase=None							,		**_lowerCamelCase       ):
      lowercase							    =       None
      if "feature_extractor" in kwargs:
        warnings.warn(
            'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
            ' instead.'							,		_lowerCamelCase							,		)
        lowercase							    =       kwargs.pop('feature_extractor'       )

      lowercase							    =       image_processor if image_processor is not None else feature_extractor
      if image_processor is None:
        raise ValueError('You need to specify an `image_processor`.'       )
      if tokenizer is None:
        raise ValueError('You need to specify a `tokenizer`.'       )

      super().__init__(_lowerCamelCase							,		_lowerCamelCase       )
      lowercase							    =       self.image_processor
      lowercase							    =       False







    def __call__(			self							,		*_lowerCamelCase							,		**_lowerCamelCase       ):
      # For backward compatibility
      if self._in_target_context_manager:
        return self.current_processor(*_lowerCamelCase							,		**_lowerCamelCase       )

      lowercase							    =       kwargs.pop('images'							,		_lowerCamelCase       )
      lowercase							    =       kwargs.pop('text'							,		_lowerCamelCase       )
      if len(_lowerCamelCase       ) > 0:
        lowercase							    =       args[0]
        lowercase							    =       args[1:]

      if images is None and text is None:
        raise ValueError('You need to specify either an `images` or `text` input to process.'       )

      if images is not None:
        lowercase							    =       self.image_processor(_lowerCamelCase							,		*_lowerCamelCase							,		**_lowerCamelCase       )
      if text is not None:
        lowercase							    =       self.tokenizer(_lowerCamelCase							,		**_lowerCamelCase       )

      if text is None:
        return inputs
      elif images is None:
        return encodings
      else:
        lowercase							    =       encodings['input_ids']
        return inputs







    def 			UpperCamelCase_      (			self							,		*_lowerCamelCase							,		**_lowerCamelCase       ):
      return self.tokenizer.batch_decode(*_lowerCamelCase							,		**_lowerCamelCase       )







    def 			UpperCamelCase_      (			self							,		*_lowerCamelCase							,		**_lowerCamelCase       ):
      return self.tokenizer.decode(*_lowerCamelCase							,		**_lowerCamelCase       )







    @contextmanager
    def 			UpperCamelCase_      (			self       ):
      warnings.warn(
          '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
          'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
          'your images inputs, or in a separate call.'       )
      lowercase							    =       True
      lowercase							    =       self.tokenizer
      yield
      lowercase							    =       self.image_processor
      lowercase							    =       False







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase=False							,		_lowerCamelCase=None       ):
      if added_vocab is None:
        lowercase							    =       self.tokenizer.get_added_vocab()

      lowercase							    =       {}

      while tokens:
        lowercase							    =       re.search(R'<s_(.*?)>'							,		_lowerCamelCase							,		re.IGNORECASE       )
        if start_token is None:
          break
        lowercase							    =       start_token.group(1       )
        lowercase							    =       re.search(RF'</s_{key}>'							,		_lowerCamelCase							,		re.IGNORECASE       )
        lowercase							    =       start_token.group()
        if end_token is None:
          lowercase							    =       tokens.replace(_lowerCamelCase							,		''       )
        else:
          lowercase							    =       end_token.group()
          lowercase							    =       re.escape(_lowerCamelCase       )
          lowercase							    =       re.escape(_lowerCamelCase       )
          lowercase							    =       re.search(F'{start_token_escaped}(.*?){end_token_escaped}'							,		_lowerCamelCase							,		re.IGNORECASE       )
          if content is not None:
            lowercase							    =       content.group(1       ).strip()
            if r"<s_" in content and r"</s_" in content:  # non-leaf node
              lowercase							    =       self.tokenajson(_lowerCamelCase							,		is_inner_value=_lowerCamelCase							,		added_vocab=_lowerCamelCase       )
              if value:
                if len(_lowerCamelCase       ) == 1:
                  lowercase							    =       value[0]
                lowercase							    =       value
            else:  # leaf nodes
              lowercase							    =       []
              for leaf in content.split(R'<sep/>'       ):
                lowercase							    =       leaf.strip()
                if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                  lowercase							    =       leaf[1:-2]  # for categorical special tokens
                output[key].append(_lowerCamelCase       )
              if len(output[key]       ) == 1:
                lowercase							    =       output[key][0]

          lowercase							    =       tokens[tokens.find(_lowerCamelCase       ) + len(_lowerCamelCase       ) :].strip()
          if tokens[:6] == r"<sep/>":  # non-leaf nodes
            return [output] + self.tokenajson(tokens[6:]							,		is_inner_value=_lowerCamelCase							,		added_vocab=_lowerCamelCase       )

      if len(_lowerCamelCase       ):
        return [output] if is_inner_value else output
      else:
        return [] if is_inner_value else {"text_sequence": tokens}







    @property
    def 			UpperCamelCase_      (			self       ):
      warnings.warn(
          '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'							,		_lowerCamelCase							,		)
      return self.image_processor_class







    @property
    def 			UpperCamelCase_      (			self       ):
      warnings.warn(
          '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'							,		_lowerCamelCase							,		)
      return self.image_processor


