
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  __magic_name__						:





	def __init__(		self	: List[str]    ,__SCREAMING_SNAKE_CASE	: int    ,__SCREAMING_SNAKE_CASE	: Union[str, Any]=1_3    ,__SCREAMING_SNAKE_CASE	: List[Any]=7    ,__SCREAMING_SNAKE_CASE	: List[str]=True    ,__SCREAMING_SNAKE_CASE	: List[Any]=True    ,__SCREAMING_SNAKE_CASE	: str=False    ,__SCREAMING_SNAKE_CASE	: List[Any]=True    ,__SCREAMING_SNAKE_CASE	: List[Any]=9_9    ,__SCREAMING_SNAKE_CASE	: Optional[int]=3_2    ,__SCREAMING_SNAKE_CASE	: str=5    ,__SCREAMING_SNAKE_CASE	: List[Any]=4    ,__SCREAMING_SNAKE_CASE	: Tuple=3_7    ,__SCREAMING_SNAKE_CASE	: Any="gelu"    ,__SCREAMING_SNAKE_CASE	: Any=0.1    ,__SCREAMING_SNAKE_CASE	: Any=0.1    ,__SCREAMING_SNAKE_CASE	: List[str]=5_1_2    ,__SCREAMING_SNAKE_CASE	: Dict=1_6    ,__SCREAMING_SNAKE_CASE	: Dict=2    ,__SCREAMING_SNAKE_CASE	: str=0.02    ,__SCREAMING_SNAKE_CASE	: List[str]=3    ,__SCREAMING_SNAKE_CASE	: Tuple=4    ,__SCREAMING_SNAKE_CASE	: Optional[Any]=None    ,):
						UpperCAmelCase				   =  parent
						UpperCAmelCase				   =  batch_size
						UpperCAmelCase				   =  seq_length
						UpperCAmelCase				   =  is_training
						UpperCAmelCase				   =  use_input_mask
						UpperCAmelCase				   =  use_token_type_ids
						UpperCAmelCase				   =  use_labels
						UpperCAmelCase				   =  vocab_size
						UpperCAmelCase				   =  hidden_size
						UpperCAmelCase				   =  num_hidden_layers
						UpperCAmelCase				   =  num_attention_heads
						UpperCAmelCase				   =  intermediate_size
						UpperCAmelCase				   =  hidden_act
						UpperCAmelCase				   =  hidden_dropout_prob
						UpperCAmelCase				   =  attention_probs_dropout_prob
						UpperCAmelCase				   =  max_position_embeddings
						UpperCAmelCase				   =  type_vocab_size
						UpperCAmelCase				   =  type_sequence_label_size
						UpperCAmelCase				   =  initializer_range
						UpperCAmelCase				   =  num_labels
						UpperCAmelCase				   =  num_choices
						UpperCAmelCase				   =  scope





	def 	_UpperCAmelCase						(		self	: Dict       ):
						UpperCAmelCase				   =  ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size       )

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

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

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

						UpperCAmelCase				   =  self.get_config()

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





	def 	_UpperCAmelCase						(		self	: Optional[int]       ):
						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 	_UpperCAmelCase						(		self	: Union[str, Any]    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: Optional[int]    ,__SCREAMING_SNAKE_CASE	: str    ,__SCREAMING_SNAKE_CASE	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: Any    ,__SCREAMING_SNAKE_CASE	: List[Any]       ):
						UpperCAmelCase				   =  OpenLlamaModel(config=A_       )
						model.to(A_       )
						model.eval()
						UpperCAmelCase				   =  model(A_    ,attention_mask=A_       )
						UpperCAmelCase				   =  model(A_       )
						self.parent.assertEqual(result.last_hidden_state.shape    ,(self.batch_size, self.seq_length, self.hidden_size)       )





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





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





	def 	_UpperCAmelCase						(		self	: Any    ,__SCREAMING_SNAKE_CASE	: str    ,__SCREAMING_SNAKE_CASE	: int    ,__SCREAMING_SNAKE_CASE	: Optional[Any]    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,__SCREAMING_SNAKE_CASE	: int    ,__SCREAMING_SNAKE_CASE	: int    ,__SCREAMING_SNAKE_CASE	: Dict    ,__SCREAMING_SNAKE_CASE	: Any    ,__SCREAMING_SNAKE_CASE	: List[Any]    ,):
						UpperCAmelCase				   =  True
						UpperCAmelCase				   =  True
						UpperCAmelCase				   =  OpenLlamaForCausalLM(config=A_       )
						model.to(A_       )
						model.eval()

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

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

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

						UpperCAmelCase				   =  model(
						    A_    ,attention_mask=A_    ,encoder_hidden_states=A_    ,encoder_attention_mask=A_    ,output_hidden_states=A_    ,)["hidden_states"][0]
						UpperCAmelCase				   =  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
						UpperCAmelCase				   =  ids_tensor((1,)    ,output_from_past.shape[-1]       ).item()
						UpperCAmelCase				   =  output_from_no_past[:, -3:, random_slice_idx].detach()
						UpperCAmelCase				   =  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 	_UpperCAmelCase						(		self	: Optional[int]       ):
						UpperCAmelCase				   =  self.prepare_config_and_inputs()
						(
						    (
						    UpperCAmelCase							
						)   ,     (
						    UpperCAmelCase							
						)   ,     (
						    UpperCAmelCase							
						)   ,     (
						    UpperCAmelCase							
						)   ,     (
						    UpperCAmelCase							
						)   ,     (
						    UpperCAmelCase							
						)   ,     (
						    UpperCAmelCase							
						)   ,     							
						)				   =  config_and_inputs
						UpperCAmelCase				   =  {"input_ids": input_ids, "attention_mask": input_mask}
						return config, inputs_dict







@require_torch
class  __magic_name__						(     _a  ,							_a  ,							_a  ,							unittest.TestCase):
	_UpperCAmelCase	:			Tuple		   =     (
	    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
	)
	_UpperCAmelCase	:			str		   =     (OpenLlamaForCausalLM,) if is_torch_available() else ()
	_UpperCAmelCase	:			Tuple		   =     (
	    {
	        'feature-extraction': OpenLlamaModel,
	        'text-classification': OpenLlamaForSequenceClassification,
	        'text-generation': OpenLlamaForCausalLM,
	        'zero-shot': OpenLlamaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_UpperCAmelCase	:			List[str]		   =     False
	_UpperCAmelCase	:			Optional[Any]		   =     False





	def 	_UpperCAmelCase						(		self	: Optional[int]       ):
						UpperCAmelCase				   =  OpenLlamaModelTester(self       )
						UpperCAmelCase				   =  ConfigTester(self    ,config_class=A_    ,hidden_size=3_7       )





	def 	_UpperCAmelCase						(		self	: List[Any]       ):
						self.config_tester.run_common_tests()





	def 	_UpperCAmelCase						(		self	: Optional[Any]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A_       )





	def 	_UpperCAmelCase						(		self	: List[str]       ):
						UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
											UpperCAmelCase				   =  type
											self.model_tester.create_and_check_model(*A_       )





	def 	_UpperCAmelCase						(		self	: List[str]       ):
						UpperCAmelCase   ,     UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()
						UpperCAmelCase				   =  3
						UpperCAmelCase				   =  input_dict["input_ids"]
						UpperCAmelCase				   =  input_ids.ne(1       ).to(A_       )
						UpperCAmelCase				   =  ids_tensor([self.model_tester.batch_size]    ,self.model_tester.type_sequence_label_size       )
						UpperCAmelCase				   =  OpenLlamaForSequenceClassification(A_       )
						model.to(A_       )
						model.eval()
						UpperCAmelCase				   =  model(A_    ,attention_mask=A_    ,labels=A_       )
						self.assertEqual(result.logits.shape    ,(self.model_tester.batch_size, self.model_tester.num_labels)       )





	def 	_UpperCAmelCase						(		self	: Tuple       ):
						UpperCAmelCase   ,     UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()
						UpperCAmelCase				   =  3
						UpperCAmelCase				   =  "single_label_classification"
						UpperCAmelCase				   =  input_dict["input_ids"]
						UpperCAmelCase				   =  input_ids.ne(1       ).to(A_       )
						UpperCAmelCase				   =  ids_tensor([self.model_tester.batch_size]    ,self.model_tester.type_sequence_label_size       )
						UpperCAmelCase				   =  OpenLlamaForSequenceClassification(A_       )
						model.to(A_       )
						model.eval()
						UpperCAmelCase				   =  model(A_    ,attention_mask=A_    ,labels=A_       )
						self.assertEqual(result.logits.shape    ,(self.model_tester.batch_size, self.model_tester.num_labels)       )





	def 	_UpperCAmelCase						(		self	: Tuple       ):
						UpperCAmelCase   ,     UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()
						UpperCAmelCase				   =  3
						UpperCAmelCase				   =  "multi_label_classification"
						UpperCAmelCase				   =  input_dict["input_ids"]
						UpperCAmelCase				   =  input_ids.ne(1       ).to(A_       )
						UpperCAmelCase				   =  ids_tensor(
						    [self.model_tester.batch_size, config.num_labels]    ,self.model_tester.type_sequence_label_size       ).to(torch.float       )
						UpperCAmelCase				   =  OpenLlamaForSequenceClassification(A_       )
						model.to(A_       )
						model.eval()
						UpperCAmelCase				   =  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 	_UpperCAmelCase						(		self	: List[Any]       ):
						pass





	@parameterized.expand([("linear",), ("dynamic",)]       )
	def 	_UpperCAmelCase						(		self	: str    ,__SCREAMING_SNAKE_CASE	: str       ):
						UpperCAmelCase   ,     UpperCAmelCase				   =  self.model_tester.prepare_config_and_inputs_for_common()
						UpperCAmelCase				   =  ids_tensor([1, 1_0]    ,config.vocab_size       )
						UpperCAmelCase				   =  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
						UpperCAmelCase				   =  OpenLlamaModel(A_       )
						original_model.to(A_       )
						original_model.eval()
						UpperCAmelCase				   =  original_model(A_       ).last_hidden_state
						UpperCAmelCase				   =  original_model(A_       ).last_hidden_state

						set_seed(4_2       )  # Fixed seed at init time so the two models get the same random weights
						UpperCAmelCase				   =  {"type": scaling_type, "factor": 10.0}
						UpperCAmelCase				   =  OpenLlamaModel(A_       )
						scaled_model.to(A_       )
						scaled_model.eval()
						UpperCAmelCase				   =  scaled_model(A_       ).last_hidden_state
						UpperCAmelCase				   =  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       )       )









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,
)


UpperCamelCase_												=	{
    '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:
    UpperCamelCase_												=	['OwlViTFeatureExtractor']
    UpperCamelCase_												=	['OwlViTImageProcessor']

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    UpperCamelCase_												=	[
        '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

    UpperCamelCase_												=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



