





"""simple docstring"""





import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, 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 (
      GPTNeoXForCausalLM,
      GPTNeoXForQuestionAnswering,
      GPTNeoXForSequenceClassification,
      GPTNeoXForTokenClassification,
      GPTNeoXModel,
  )






class 					_lowerCAmelCase   :
       """simple docstring"""




       def __init__(    self    , __UpperCAmelCase    , __UpperCAmelCase=1_3    , __UpperCAmelCase=7    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=True    , __UpperCAmelCase=9_9    , __UpperCAmelCase=6_4    , __UpperCAmelCase=5    , __UpperCAmelCase=4    , __UpperCAmelCase=3_7    , __UpperCAmelCase="gelu"    , __UpperCAmelCase=0.1    , __UpperCAmelCase=0.1    , __UpperCAmelCase=5_1_2    , __UpperCAmelCase=1_6    , __UpperCAmelCase=2    , __UpperCAmelCase=0.02    , __UpperCAmelCase=3    , __UpperCAmelCase=4    , __UpperCAmelCase=None    , ):



         '''simple docstring'''

         lowerCAmelCase__			:Any											=							parent
         lowerCAmelCase__			:Tuple											=							batch_size
         lowerCAmelCase__			:List[Any]											=							seq_length
         lowerCAmelCase__			:str											=							is_training
         lowerCAmelCase__			:Optional[Any]											=							use_input_mask
         lowerCAmelCase__			:Tuple											=							use_token_type_ids
         lowerCAmelCase__			:int											=							use_labels
         lowerCAmelCase__			:Optional[int]											=							vocab_size
         lowerCAmelCase__			:List[str]											=							hidden_size
         lowerCAmelCase__			:Any											=							num_hidden_layers
         lowerCAmelCase__			:str											=							num_attention_heads
         lowerCAmelCase__			:Optional[Any]											=							intermediate_size
         lowerCAmelCase__			:Optional[int]											=							hidden_act
         lowerCAmelCase__			:Union[str, Any]											=							hidden_dropout_prob
         lowerCAmelCase__			:Tuple											=							attention_probs_dropout_prob
         lowerCAmelCase__			:Optional[Any]											=							max_position_embeddings
         lowerCAmelCase__			:List[str]											=							type_vocab_size
         lowerCAmelCase__			:Optional[int]											=							type_sequence_label_size
         lowerCAmelCase__			:Optional[Any]											=							initializer_range
         lowerCAmelCase__			:Optional[Any]											=							num_labels
         lowerCAmelCase__			:Tuple											=							num_choices
         lowerCAmelCase__			:List[Any]											=							scope
         lowerCAmelCase__			:List[str]											=							vocab_size - 1






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							ids_tensor([self.batch_size, self.seq_length]    , self.vocab_size      )

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

         lowerCAmelCase__			:List[str]											=							None
         if self.use_labels:
           lowerCAmelCase__			:Dict											=							ids_tensor([self.batch_size, self.seq_length]    , self.num_labels      )

         lowerCAmelCase__			:Optional[int]											=							self.get_config()

         return config, input_ids, input_mask, token_labels






       def      snake_case						(    self      ):



         '''simple docstring'''

         return GPTNeoXConfig(
             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=__UpperCAmelCase    , initializer_range=self.initializer_range    , pad_token_id=self.pad_token_id    , )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:str											=							self.prepare_config_and_inputs()

         lowerCAmelCase__			:str											=							True

         return config, input_ids, input_mask, token_labels






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:int											=							GPTNeoXModel(config=__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:List[str]											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase      )
         lowerCAmelCase__			:List[Any]											=							model(__UpperCAmelCase      )
         self.parent.assertEqual(result.last_hidden_state.shape    , (self.batch_size, self.seq_length, self.hidden_size)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							True
         lowerCAmelCase__			:Optional[Any]											=							GPTNeoXModel(__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:Any											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase      )
         self.parent.assertEqual(result.last_hidden_state.shape    , (self.batch_size, self.seq_length, self.hidden_size)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							GPTNeoXForCausalLM(config=__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:str											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase    , labels=__UpperCAmelCase      )
         self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.seq_length, self.vocab_size)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							self.num_labels
         lowerCAmelCase__			:Tuple											=							GPTNeoXForQuestionAnswering(__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:List[str]											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase      )
         self.parent.assertEqual(result.start_logits.shape    , (self.batch_size, self.seq_length)      )
         self.parent.assertEqual(result.end_logits.shape    , (self.batch_size, self.seq_length)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							self.num_labels
         lowerCAmelCase__			:int											=							GPTNeoXForSequenceClassification(__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:Dict											=							ids_tensor([self.batch_size]    , self.type_sequence_label_size      )
         lowerCAmelCase__			:int											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase    , labels=__UpperCAmelCase      )
         self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.num_labels)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							self.num_labels
         lowerCAmelCase__			:str											=							GPTNeoXForTokenClassification(__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()
         lowerCAmelCase__			:Union[str, Any]											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase    , labels=__UpperCAmelCase      )
         self.parent.assertEqual(result.logits.shape    , (self.batch_size, self.seq_length, self.num_labels)      )






       def      snake_case						(    self    , __UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__			:Tuple											=							True
         lowerCAmelCase__			:List[str]											=							GPTNeoXForCausalLM(config=__UpperCAmelCase      )
         model.to(__UpperCAmelCase      )
         model.eval()

         # first forward pass
         lowerCAmelCase__			:List[str]											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase    , use_cache=__UpperCAmelCase      )
         lowerCAmelCase__			:Optional[Any]											=							outputs.past_key_values

         # create hypothetical multiple next token and extent to next_input_ids
         lowerCAmelCase__			:List[str]											=							ids_tensor((self.batch_size, 3)    , config.vocab_size      )
         lowerCAmelCase__			:Dict											=							ids_tensor((self.batch_size, 3)    , vocab_size=2      )

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

         lowerCAmelCase__			:Optional[Any]											=							model(__UpperCAmelCase    , attention_mask=__UpperCAmelCase    , output_hidden_states=__UpperCAmelCase      )
         lowerCAmelCase__			:Optional[Any]											=							output_from_no_past['hidden_states'][0]
         lowerCAmelCase__			:str											=							model(
             __UpperCAmelCase    , attention_mask=__UpperCAmelCase    , past_key_values=__UpperCAmelCase    , output_hidden_states=__UpperCAmelCase    , )['hidden_states'][0]

         # select random slice
         lowerCAmelCase__			:Any											=							ids_tensor((1,)    , output_from_past.shape[-1]      ).item()
         lowerCAmelCase__			:Optional[Any]											=							output_from_no_past[:, -3:, random_slice_idx].detach()
         lowerCAmelCase__			: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(__UpperCAmelCase    , __UpperCAmelCase    , atol=1E-3      )      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[int]											=							self.prepare_config_and_inputs()
         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:Dict											=							config_and_inputs
         lowerCAmelCase__			:Tuple											=							{'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict






@require_torch
class 					_lowerCAmelCase   (     a  ,   a  ,   a  ,   unittest.TestCase       ):
       """simple docstring"""




       __magic_name__     :List[Any]		  =					(
           (
               GPTNeoXModel,
               GPTNeoXForCausalLM,
               GPTNeoXForQuestionAnswering,
               GPTNeoXForSequenceClassification,
               GPTNeoXForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       __magic_name__     :Optional[int]		  =					(GPTNeoXForCausalLM,) if is_torch_available() else ()
       __magic_name__     :Optional[Any]		  =					(
           {
               """feature-extraction""": GPTNeoXModel,
               """question-answering""": GPTNeoXForQuestionAnswering,
               """text-classification""": GPTNeoXForSequenceClassification,
               """text-generation""": GPTNeoXForCausalLM,
               """token-classification""": GPTNeoXForTokenClassification,
               """zero-shot""": GPTNeoXForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       __magic_name__     :Union[str, Any]		  =					False
       __magic_name__     :Dict		  =					False
       __magic_name__     :Optional[int]		  =					False
       __magic_name__     :int		  =					False






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:int											=							GPTNeoXModelTester(self      )
         lowerCAmelCase__			:Dict											=							ConfigTester(self    , config_class=__UpperCAmelCase    , hidden_size=6_4    , num_attention_heads=8      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         self.config_tester.run_common_tests()






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:Optional[Any]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:Dict											=							self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:str											=							self.model_tester.prepare_config_and_inputs_for_decoder()

         lowerCAmelCase__			:Optional[Any]											=							None

         self.model_tester.create_and_check_model_as_decoder(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__							,				lowerCAmelCase__			:Dict											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(__UpperCAmelCase    , __UpperCAmelCase    , __UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:List[str]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_causal_lm(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:str											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Optional[Any]											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase      )






       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Any											=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase      )






       @unittest.skip(reason='Feed forward chunking is not implemented'      )
       def      snake_case						(    self      ):



         '''simple docstring'''

         pass






       @parameterized.expand([('linear',), ('dynamic',)]      )
       def      snake_case						(    self    , __UpperCAmelCase      ):



         '''simple docstring'''

         lowerCAmelCase__							,				lowerCAmelCase__			:Optional[Any]											=							self.model_tester.prepare_config_and_inputs_for_common()
         lowerCAmelCase__			:Dict											=							ids_tensor([1, 1_0]    , config.vocab_size      )
         lowerCAmelCase__			:Tuple											=							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
         lowerCAmelCase__			:Union[str, Any]											=							GPTNeoXModel(__UpperCAmelCase      )
         original_model.to(__UpperCAmelCase      )
         original_model.eval()
         lowerCAmelCase__			:int											=							original_model(__UpperCAmelCase      ).last_hidden_state
         lowerCAmelCase__			:List[Any]											=							original_model(__UpperCAmelCase      ).last_hidden_state

         set_seed(4_2      )  # Fixed seed at init time so the two models get the same random weights
         lowerCAmelCase__			:Optional[Any]											=							{'type': scaling_type, 'factor': 10.0}
         lowerCAmelCase__			:str											=							GPTNeoXModel(__UpperCAmelCase      )
         scaled_model.to(__UpperCAmelCase      )
         scaled_model.eval()
         lowerCAmelCase__			:Any											=							scaled_model(__UpperCAmelCase      ).last_hidden_state
         lowerCAmelCase__			:List[str]											=							scaled_model(__UpperCAmelCase      ).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(__UpperCAmelCase    , __UpperCAmelCase    , atol=1E-5      )      )
         else:
           self.assertFalse(torch.allclose(__UpperCAmelCase    , __UpperCAmelCase    , atol=1E-5      )      )

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






@require_torch
class 					_lowerCAmelCase   (     unittest.TestCase       ):
       """simple docstring"""




       @slow
       def      snake_case						(    self      ):



         '''simple docstring'''

         lowerCAmelCase__			:Dict											=							AutoTokenizer.from_pretrained('EleutherAI/pythia-410m-deduped'      )
         for checkpointing in [True, False]:
           lowerCAmelCase__			:str											=							GPTNeoXForCausalLM.from_pretrained('EleutherAI/pythia-410m-deduped'      )

           if checkpointing:
             model.gradient_checkpointing_enable()
           else:
             model.gradient_checkpointing_disable()
           model.to(__UpperCAmelCase      )

           lowerCAmelCase__			:Optional[int]											=							tokenizer('My favorite food is'    , return_tensors='pt'      ).to(__UpperCAmelCase      )
           # The hub repo. is updated on 2023-04-04, resulting in poor outputs.
           # See: https://github.com/huggingface/transformers/pull/24193
           lowerCAmelCase__			:Union[str, Any]											=							'My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI\'m not sure'

           lowerCAmelCase__			:Any											=							model.generate(**__UpperCAmelCase    , do_sample=__UpperCAmelCase    , max_new_tokens=2_0      )
           lowerCAmelCase__			:int											=							tokenizer.batch_decode(__UpperCAmelCase      )[0]

           self.assertEqual(__UpperCAmelCase    , __UpperCAmelCase      )







"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__A											=		{
    """configuration_owlvit""": [
        """OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """OwlViTConfig""",
        """OwlViTOnnxConfig""",
        """OwlViTTextConfig""",
        """OwlViTVisionConfig""",
    ],
    """processing_owlvit""": ["""OwlViTProcessor"""],
}


try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __A											=		["""OwlViTFeatureExtractor"""]
  __A											=		["""OwlViTImageProcessor"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __A											=		[
      """OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """OwlViTModel""",
      """OwlViTPreTrainedModel""",
      """OwlViTTextModel""",
      """OwlViTVisionModel""",
      """OwlViTForObjectDetection""",
  ]

if TYPE_CHECKING:
  from .configuration_owlvit import (
      OWLVIT_PRETRAINED_CONFIG_ARCHIVE_MAP,
      OwlViTConfig,
      OwlViTOnnxConfig,
      OwlViTTextConfig,
      OwlViTVisionConfig,
  )
  from .processing_owlvit import OwlViTProcessor

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_owlvit import OwlViTFeatureExtractor
    from .image_processing_owlvit import OwlViTImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_owlvit import (
        OWLVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
        OwlViTForObjectDetection,
        OwlViTModel,
        OwlViTPreTrainedModel,
        OwlViTTextModel,
        OwlViTVisionModel,
    )

else:
  import sys

  __A											=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
