
"""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      ,       _a      ,       _a=13      ,       _a=7      ,       _a=True      ,       _a=True      ,       _a=False      ,       _a=True      ,       _a=99      ,       _a=32      ,       _a=5      ,       _a=4      ,       _a=37      ,       _a="gelu"      ,       _a=0.1      ,       _a=0.1      ,       _a=512      ,       _a=16      ,       _a=2      ,       _a=0.0_2      ,       _a=3      ,       _a=4      ,       _a=None      ,       ):
          lowercase       :							Union[str, Any]									=				parent
          lowercase       :							List[Any]									=				batch_size
          lowercase       :							str									=				seq_length
          lowercase       :							Tuple									=				is_training
          lowercase       :							Optional[Any]									=				use_input_mask
          lowercase       :							List[str]									=				use_token_type_ids
          lowercase       :							int									=				use_labels
          lowercase       :							Union[str, Any]									=				vocab_size
          lowercase       :							int									=				hidden_size
          lowercase       :							List[str]									=				num_hidden_layers
          lowercase       :							Union[str, Any]									=				num_attention_heads
          lowercase       :							List[Any]									=				intermediate_size
          lowercase       :							Union[str, Any]									=				hidden_act
          lowercase       :							List[str]									=				hidden_dropout_prob
          lowercase       :							List[str]									=				attention_probs_dropout_prob
          lowercase       :							Union[str, Any]									=				max_position_embeddings
          lowercase       :							int									=				type_vocab_size
          lowercase       :							str									=				type_sequence_label_size
          lowercase       :							str									=				initializer_range
          lowercase       :							Dict									=				num_labels
          lowercase       :							Optional[Any]									=				num_choices
          lowercase       :							Optional[int]									=				scope
   def      __magic_name__     (     self							):
          lowercase       :							int									=				ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size							)

          lowercase       :							Union[str, Any]									=				None
          if self.use_input_mask:
                 lowercase       :							Optional[int]									=				random_attention_mask([self.batch_size, self.seq_length]							)

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

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

          lowercase       :							str									=				self.get_config()

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def      __magic_name__     (     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=_a      ,       initializer_range=self.initializer_range      ,       use_stable_embedding=_a      ,       )
   def      __magic_name__     (     self      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a							):
          lowercase       :							int									=				OpenLlamaModel(config=_a							)
          model.to(_a							)
          model.eval()
          lowercase       :							int									=				model(_a      ,       attention_mask=_a							)
          lowercase       :							List[Any]									=				model(_a							)
          self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)							)
   def      __magic_name__     (     self      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       ):
          lowercase       :							Optional[int]									=				True
          lowercase       :							int									=				OpenLlamaModel(_a							)
          model.to(_a							)
          model.eval()
          lowercase       :							Optional[int]									=				model(
              _a      ,       attention_mask=_a      ,       encoder_hidden_states=_a      ,       encoder_attention_mask=_a      ,       )
          lowercase       :							Union[str, Any]									=				model(
              _a      ,       attention_mask=_a      ,       encoder_hidden_states=_a      ,       )
          lowercase       :							Tuple									=				model(_a      ,       attention_mask=_a							)
          self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)							)
   def      __magic_name__     (     self      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       ):
          lowercase       :							Union[str, Any]									=				OpenLlamaForCausalLM(config=_a							)
          model.to(_a							)
          model.eval()
          lowercase       :							int									=				model(_a      ,       attention_mask=_a      ,       labels=_a							)
          self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)							)
   def      __magic_name__     (     self      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       _a      ,       ):
          lowercase       :							Union[str, Any]									=				True
          lowercase       :							int									=				True
          lowercase       :							List[Any]									=				OpenLlamaForCausalLM(config=_a							)
          model.to(_a							)
          model.eval()

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

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

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

          lowercase       :							List[Any]									=				model(
              _a      ,       attention_mask=_a      ,       encoder_hidden_states=_a      ,       encoder_attention_mask=_a      ,       output_hidden_states=_a      ,       )["hidden_states"][0]
          lowercase       :							int									=				model(
              _a      ,       attention_mask=_a      ,       encoder_hidden_states=_a      ,       encoder_attention_mask=_a      ,       past_key_values=_a      ,       output_hidden_states=_a      ,       )["hidden_states"][0]

          # select random slice
          lowercase       :							Union[str, Any]									=				ids_tensor((1,)      ,       output_from_past.shape[-1]							).item()
          lowercase       :							int									=				output_from_no_past[:, -3:, random_slice_idx].detach()
          lowercase       :							List[Any]									=				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(_a      ,       _a      ,       atol=1E-3							)							)

   def      __magic_name__     (     self							):
          lowercase       :							int									=				self.prepare_config_and_inputs()
          (
              (
              lowercase
          )						,       (
              lowercase
          )						,       (
              lowercase
          )						,       (
              lowercase
          )						,       (
              lowercase
          )						,       (
              lowercase
          )						,       (
              lowercase
          )						,       
          )       :							Optional[int]									=				config_and_inputs
          lowercase       :							Any									=				{"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict


@require_torch
class 						a__		( a_,     a_,     a_,     unittest.TestCase		):
   __lowerCAmelCase				=							(
       (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
   )
   __lowerCAmelCase				=							(OpenLlamaForCausalLM,) if is_torch_available() else ()
   __lowerCAmelCase				=							(
       {
           """feature-extraction""": OpenLlamaModel,
           """text-classification""": OpenLlamaForSequenceClassification,
           """text-generation""": OpenLlamaForCausalLM,
           """zero-shot""": OpenLlamaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __lowerCAmelCase				=							False
   __lowerCAmelCase				=							False
   def      __magic_name__     (     self							):
          lowercase       :							List[Any]									=				OpenLlamaModelTester(self							)
          lowercase       :							Union[str, Any]									=				ConfigTester(self      ,       config_class=_a      ,       hidden_size=37							)
   def      __magic_name__     (     self							):
          self.config_tester.run_common_tests()
   def      __magic_name__     (     self							):
          lowercase       :							List[Any]									=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_a							)
   def      __magic_name__     (     self							):
          lowercase       :							Dict									=				self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
                 lowercase       :							Optional[int]									=				type
                 self.model_tester.create_and_check_model(*_a							)
   def      __magic_name__     (     self							):
          lowercase						,       lowercase       :							List[Any]									=				self.model_tester.prepare_config_and_inputs_for_common()
          lowercase       :							List[Any]									=				3
          lowercase       :							Optional[int]									=				input_dict["input_ids"]
          lowercase       :							str									=				input_ids.ne(1							).to(_a							)
          lowercase       :							Dict									=				ids_tensor([self.model_tester.batch_size]      ,       self.model_tester.type_sequence_label_size							)
          lowercase       :							Union[str, Any]									=				OpenLlamaForSequenceClassification(_a							)
          model.to(_a							)
          model.eval()
          lowercase       :							Dict									=				model(_a      ,       attention_mask=_a      ,       labels=_a							)
          self.assertEqual(result.logits.shape      ,       (self.model_tester.batch_size, self.model_tester.num_labels)							)
   def      __magic_name__     (     self							):
          lowercase						,       lowercase       :							str									=				self.model_tester.prepare_config_and_inputs_for_common()
          lowercase       :							List[Any]									=				3
          lowercase       :							Optional[int]									=				"single_label_classification"
          lowercase       :							List[str]									=				input_dict["input_ids"]
          lowercase       :							str									=				input_ids.ne(1							).to(_a							)
          lowercase       :							Optional[Any]									=				ids_tensor([self.model_tester.batch_size]      ,       self.model_tester.type_sequence_label_size							)
          lowercase       :							int									=				OpenLlamaForSequenceClassification(_a							)
          model.to(_a							)
          model.eval()
          lowercase       :							Dict									=				model(_a      ,       attention_mask=_a      ,       labels=_a							)
          self.assertEqual(result.logits.shape      ,       (self.model_tester.batch_size, self.model_tester.num_labels)							)
   def      __magic_name__     (     self							):
          lowercase						,       lowercase       :							Dict									=				self.model_tester.prepare_config_and_inputs_for_common()
          lowercase       :							str									=				3
          lowercase       :							int									=				"multi_label_classification"
          lowercase       :							str									=				input_dict["input_ids"]
          lowercase       :							str									=				input_ids.ne(1							).to(_a							)
          lowercase       :							str									=				ids_tensor(
              [self.model_tester.batch_size, config.num_labels]      ,       self.model_tester.type_sequence_label_size							).to(torch.float							)
          lowercase       :							Optional[Any]									=				OpenLlamaForSequenceClassification(_a							)
          model.to(_a							)
          model.eval()
          lowercase       :							Optional[int]									=				model(_a      ,       attention_mask=_a      ,       labels=_a							)
          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      __magic_name__     (     self							):
          pass

   @parameterized.expand([("linear",), ("dynamic",)]							)
   def      __magic_name__     (     self      ,       _a							):
          lowercase						,       lowercase       :							Union[str, Any]									=				self.model_tester.prepare_config_and_inputs_for_common()
          lowercase       :							Dict									=				ids_tensor([1, 10]      ,       config.vocab_size							)
          lowercase       :							Dict									=				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
          lowercase       :							Dict									=				OpenLlamaModel(_a							)
          original_model.to(_a							)
          original_model.eval()
          lowercase       :							List[str]									=				original_model(_a							).last_hidden_state
          lowercase       :							List[str]									=				original_model(_a							).last_hidden_state

          set_seed(42							)  # Fixed seed at init time so the two models get the same random weights
          lowercase       :							Dict									=				{"type": scaling_type, "factor": 1_0.0}
          lowercase       :							Tuple									=				OpenLlamaModel(_a							)
          scaled_model.to(_a							)
          scaled_model.eval()
          lowercase       :							Optional[int]									=				scaled_model(_a							).last_hidden_state
          lowercase       :							Any									=				scaled_model(_a							).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(_a      ,       _a      ,       atol=1E-5							)							)
          else:
                 self.assertFalse(torch.allclose(_a      ,       _a      ,       atol=1E-5							)							)

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




"""simple docstring"""




import warnings

from ...utils import logging
from .image_processing_owlvit import OwlViTImageProcessor


_A     :			Optional[int]  						=			logging.get_logger(__name__)

class 						a__		( a_		):
   def __init__(     self      ,       *_a      ,       **_a							):
          warnings.warn(
              "The class OwlViTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
              " use OwlViTImageProcessor instead."      ,       _a      ,       )
          super().__init__(*_a      ,       **_a							)


