






'''simple docstring'''
from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel



def     A__						(	A_    ,      A_    ,      A_=None    ,      A_=None		)     ->				List[Any]:
					if attention_mask is None:
										_lowercase			= tf.cast(tf.math.not_equal(__snake_case    ,      config.pad_token_id		)    ,      tf.inta		)
					return {"input_ids": input_ids, "attention_mask": attention_mask}







@require_tf
class 	UpperCamelCase__					:

		"""simple docstring"""
		UpperCAmelCase__     			=						OPTConfig
		UpperCAmelCase__     			=						{}
		UpperCAmelCase__     			=						'gelu'




		def __init__( self						:						Optional[Any]							,    __A						:						Any							,    __A						:						Union[str, Any]=1_3							,    __A						:						str=7							,    __A						:						Optional[Any]=True							,    __A						:						Optional[int]=False							,    __A						:						Tuple=9_9							,    __A						:						Optional[Any]=1_6							,    __A						:						Tuple=2							,    __A						:						Optional[int]=4							,    __A						:						List[Any]=4							,    __A						:						Optional[int]="gelu"							,    __A						:						Union[str, Any]=0.1							,    __A						:						str=0.1							,    __A						:						str=2_0							,    __A						:						List[str]=2							,    __A						:						Dict=1							,    __A						:						int=0							,    __A						:						Tuple=1_6							,    __A						:						List[Any]=1_6							,    ):







							"""simple docstring"""
							_lowercase			= parent
							_lowercase			= batch_size
							_lowercase			= seq_length
							_lowercase			= is_training
							_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			= eos_token_id
							_lowercase			= pad_token_id
							_lowercase			= bos_token_id
							_lowercase			= embed_dim
							_lowercase			= word_embed_proj_dim
							_lowercase			= False




		def       snake_case     ( self						:						Optional[Any]    ):







							"""simple docstring"""
							_lowercase			= ids_tensor([self.batch_size, self.seq_length - 1]							,    self.vocab_size    )
							_lowercase			= tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size    )							,    1    )
							_lowercase			= tf.concat([input_ids, eos_tensor]							,    axis=1    )

							_lowercase			= self.config_cls(
							    vocab_size=self.vocab_size							,    hidden_size=self.hidden_size							,    num_hidden_layers=self.num_hidden_layers							,    num_attention_heads=self.num_attention_heads							,    ffn_dim=self.intermediate_size							,    dropout=self.hidden_dropout_prob							,    attention_dropout=self.attention_probs_dropout_prob							,    max_position_embeddings=self.max_position_embeddings							,    eos_token_id=self.eos_token_id							,    bos_token_id=self.bos_token_id							,    pad_token_id=self.pad_token_id							,    embed_dim=self.embed_dim							,    word_embed_proj_dim=self.word_embed_proj_dim							,    is_encoder_decoder=__UpperCamelCase							,    **self.config_updates							,    )
							_lowercase			= prepare_opt_inputs_dict(__UpperCamelCase							,    __UpperCamelCase    )
							return config, inputs_dict




		def       snake_case     ( self						:						Optional[int]							,    __A						:						Tuple							,    __A						:						Union[str, Any]    ):







							"""simple docstring"""
							_lowercase			= TFOPTModel(config=__UpperCamelCase    )
							_lowercase			= inputs_dict["input_ids"]

							_lowercase			= input_ids[:1, :]
							_lowercase			= inputs_dict["attention_mask"][:1, :]
							_lowercase			= 1

							# first forward pass
							_lowercase			= model(__UpperCamelCase							,    attention_mask=__UpperCamelCase							,    use_cache=__UpperCamelCase    )

							_lowercase	,			_lowercase			= outputs.to_tuple()

							# create hypothetical next token and extent to next_input_ids
							_lowercase			= ids_tensor((self.batch_size, 3)							,    config.vocab_size    )
							_lowercase			= tf.cast(ids_tensor((self.batch_size, 3)							,    2    )							,    tf.inta    )

							# append to next input_ids and
							_lowercase			= tf.concat([input_ids, next_tokens]							,    axis=-1    )
							_lowercase			= tf.concat([attention_mask, next_attn_mask]							,    axis=-1    )

							_lowercase			= model(__UpperCamelCase							,    attention_mask=__UpperCamelCase    )[0]
							_lowercase			= model(__UpperCamelCase							,    attention_mask=__UpperCamelCase							,    past_key_values=__UpperCamelCase    )[0]

							self.parent.assertEqual(next_tokens.shape[1]							,    output_from_past.shape[1]    )

							# select random slice
							_lowercase			= int(ids_tensor((1,)							,    output_from_past.shape[-1]    )    )
							_lowercase			= output_from_no_past[:, -3:, random_slice_idx]
							_lowercase			= output_from_past[:, :, random_slice_idx]

							# test that outputs are equal for slice
							tf.debugging.assert_near(__UpperCamelCase							,    __UpperCamelCase							,    rtol=1e-3    )







@require_tf
class 	UpperCamelCase__					(     a_      ,     a_      ,     unittest.TestCase			):

		"""simple docstring"""
		UpperCAmelCase__     			=						(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
		UpperCAmelCase__     			=						(TFOPTForCausalLM,) if is_tf_available() else ()
		UpperCAmelCase__     			=						(
		    {'feature-extraction': TFOPTModel, 'text-generation': TFOPTForCausalLM} if is_tf_available() else {}
		)
		UpperCAmelCase__     			=						False
		UpperCAmelCase__     			=						False
		UpperCAmelCase__     			=						False
		UpperCAmelCase__     			=						10




		def       snake_case     ( self						:						Dict    ):







							"""simple docstring"""
							_lowercase			= TFOPTModelTester(self    )
							_lowercase			= ConfigTester(self							,    config_class=__UpperCamelCase    )




		def       snake_case     ( self						:						Tuple    ):







							"""simple docstring"""
							self.config_tester.run_common_tests()




		def       snake_case     ( self						:						str    ):







							"""simple docstring"""
							_lowercase			= self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.check_decoder_model_past_large_inputs(*__UpperCamelCase    )




		def       snake_case     ( self						:						Optional[Any]    ):







							"""simple docstring"""
							_lowercase	,			_lowercase			= self.model_tester.prepare_config_and_inputs_for_common()

							def _get_word_embedding_weight(__A						:						Any							,    __A						:						Any    ):
												if hasattr(__UpperCamelCase							,    "weight"    ):
																	return embedding_layer.weight
												else:
																	# Here we build the word embeddings weights if not exists.
																	# And then we retry to get the attribute once built.
																	model.build()
																	if hasattr(__UpperCamelCase							,    "weight"    ):
																						return embedding_layer.weight
																	else:
																						return None

							for model_class in self.all_model_classes:
												for size in [config.vocab_size - 1_0, config.vocab_size + 1_0]:
																	# build the embeddings
																	_lowercase			= model_class(config=__UpperCamelCase    )
																	_lowercase			= _get_word_embedding_weight(__UpperCamelCase							,    model.get_input_embeddings()    )
																	_lowercase			= _get_word_embedding_weight(__UpperCamelCase							,    model.get_output_embeddings()    )

																	# reshape the embeddings
																	model.resize_token_embeddings(__UpperCamelCase    )
																	_lowercase			= _get_word_embedding_weight(__UpperCamelCase							,    model.get_input_embeddings()    )
																	_lowercase			= _get_word_embedding_weight(__UpperCamelCase							,    model.get_output_embeddings()    )

																	# check that the resized embeddings size matches the desired size.
																	_lowercase			= size if size is not None else config.vocab_size

																	self.assertEqual(new_input_embeddings.shape[0]							,    __UpperCamelCase    )

																	# check that weights remain the same after resizing
																	_lowercase			= True
																	for pa, pa in zip(old_input_embeddings.value()							,    new_input_embeddings.value()    ):
																						if tf.math.reduce_sum(tf.math.abs(pa - pa    )    ) > 0:
																											_lowercase			= False
																	self.assertTrue(__UpperCamelCase    )

																	if old_output_embeddings is not None and new_output_embeddings is not None:
																						self.assertEqual(new_output_embeddings.shape[0]							,    __UpperCamelCase    )

																						_lowercase			= True
																						for pa, pa in zip(old_output_embeddings.value()							,    new_output_embeddings.value()    ):
																											if tf.math.reduce_sum(tf.math.abs(pa - pa    )    ) > 0:
																																_lowercase			= False
																						self.assertTrue(__UpperCamelCase    )





def     A__						(	A_		)     ->				str:
					return tf.constant(__snake_case    ,      dtype=tf.intaa		)







@require_tf
class 	UpperCamelCase__					(     unittest.TestCase			):

		"""simple docstring"""
		UpperCAmelCase__     			=						99




		def       snake_case     ( self						:						Tuple    ):







							"""simple docstring"""
							_lowercase			= tf.ones((4, 1)							,    dtype=tf.intaa    ) * 2
							_lowercase			= tf.concat([ids_tensor((4, 6)							,    self.vocab_size - 3    ) + 3, eos_column_vector]							,    axis=1    )
							_lowercase			= input_ids.shape[0]
							_lowercase			= OPTConfig(
							    vocab_size=self.vocab_size							,    hidden_size=2_4							,    num_hidden_layers=2							,    num_attention_heads=2							,    ffn_dim=3_2							,    max_position_embeddings=4_8							,    eos_token_id=2							,    pad_token_id=1							,    bos_token_id=0							,    )
							return config, input_ids, batch_size







@require_sentencepiece
@require_tf
class 	UpperCamelCase__					(     unittest.TestCase			):

		"""simple docstring"""




		@slow
		def       snake_case     ( self						:						Optional[int]    ):







							"""simple docstring"""
							_lowercase			= TFOPTModel.from_pretrained("facebook/opt-350m"    )
							_lowercase			= _long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]    )
							_lowercase			= tf.not_equal(__UpperCamelCase							,    model.config.pad_token_id    )
							with tf.GradientTape():
												_lowercase			= model(input_ids=__UpperCamelCase							,    attention_mask=__UpperCamelCase    ).last_hidden_state
							_lowercase			= (1, 1_1, 5_1_2)
							self.assertEqual(output.shape							,    __UpperCamelCase    )
							_lowercase			= tf.constant(
							    [[-0.2_8_7_3, -1.9_2_1_8, -0.3_0_3_3], [-1.2_7_1_0, -0.1_3_3_8, -0.1_9_0_2], [0.4_0_9_5, 0.1_2_1_4, -1.3_1_2_1]]    )
							self.assertTrue(np.allclose(output[:, :3, :3]							,    __UpperCamelCase							,    atol=4e-3    )    )

							_lowercase			= tf.function(__UpperCamelCase							,    jit_compile=__UpperCamelCase    )
							_lowercase			= xla_generate(__UpperCamelCase							,    __UpperCamelCase    )[0]
							self.assertTrue(np.allclose(output[:, :3, :3]							,    __UpperCamelCase							,    atol=4e-2    )    )







@require_tf
@slow
class 	UpperCamelCase__					(     unittest.TestCase			):

		"""simple docstring"""




		def       snake_case     ( self						:						List[Any]    ):







							"""simple docstring"""
							super().setUp()
							_lowercase			= "facebook/opt-350m"




		def       snake_case     ( self						:						Union[str, Any]    ):







							"""simple docstring"""
							_lowercase			= TFOPTForCausalLM.from_pretrained(self.path_model    )
							_lowercase			= GPTaTokenizer.from_pretrained(self.path_model    )

							_lowercase			= [
							    "Today is a beautiful day and I want to",
							    "In the city of",
							    "Paris is the capital of France and",
							    "Computers and mobile phones have taken",
							]
							# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
							_lowercase			= tokenizer(__UpperCamelCase							,    return_tensors="tf"							,    padding=__UpperCamelCase							,    add_special_tokens=__UpperCamelCase    )
							_lowercase			= tf.math.reduce_mean(model(inputs.input_ids							,    attention_mask=inputs.attention_mask    )[0]							,    axis=-1    )
							_lowercase			= tf.constant(
							    [
							        [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
							        [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
							        [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
							        [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
							    ]    )
							self.assertTrue(np.allclose(__UpperCamelCase							,    __UpperCamelCase							,    atol=1e-4    )    )

							_lowercase			= tf.function(__UpperCamelCase							,    jit_compile=__UpperCamelCase    )
							_lowercase			= tf.math.reduce_mean(xla_generate(inputs.input_ids							,    attention_mask=inputs.attention_mask    )[0]							,    axis=-1    )
							self.assertTrue(np.allclose(__UpperCamelCase							,    __UpperCamelCase							,    atol=1e-4    )    )







@require_tf
@slow
class 	UpperCamelCase__					(     unittest.TestCase			):

		"""simple docstring"""




		@property
		def       snake_case     ( self						:						Optional[Any]    ):







							"""simple docstring"""
							return [
							    "Today is a beautiful day and I want",
							    "In the city of",
							    "Paris is the capital of France and",
							    "Computers and mobile phones have taken",
							]




		def       snake_case     ( self						:						int    ):







							"""simple docstring"""
							_lowercase			= "facebook/opt-125m"

							_lowercase			= [
							    "Today is a beautiful day and I want to",
							    "In the city of New York, the city",
							    "Paris is the capital of France and the capital",
							    "Computers and mobile phones have taken over the",
							]

							_lowercase			= []
							_lowercase			= GPTaTokenizer.from_pretrained(__UpperCamelCase    )
							_lowercase			= TFOPTForCausalLM.from_pretrained(__UpperCamelCase    )

							for prompt in self.prompts:
												_lowercase			= tokenizer(__UpperCamelCase							,    return_tensors="tf"    ).input_ids

												_lowercase			= model.generate(__UpperCamelCase							,    max_length=1_0    )

												_lowercase			= tokenizer.batch_decode(__UpperCamelCase							,    skip_special_tokens=__UpperCamelCase    )
												predicted_outputs += generated_string

							self.assertListEqual(__UpperCamelCase							,    __UpperCamelCase    )




		def       snake_case     ( self						:						Optional[Any]    ):







							"""simple docstring"""
							_lowercase			= "facebook/opt-350m"

							_lowercase			= GPTaTokenizer.from_pretrained(__UpperCamelCase    )
							_lowercase			= TFOPTForCausalLM.from_pretrained(__UpperCamelCase    )

							_lowercase			= "left"

							# use different length sentences to test batching
							_lowercase			= [
							    "Hello, my dog is a little",
							    "Today, I",
							]

							_lowercase			= tokenizer(__UpperCamelCase							,    return_tensors="tf"							,    padding=__UpperCamelCase    )
							_lowercase			= inputs["input_ids"]

							_lowercase			= model.generate(input_ids=__UpperCamelCase							,    attention_mask=inputs["attention_mask"]    )

							_lowercase			= tokenizer(sentences[0]							,    return_tensors="tf"    ).input_ids
							_lowercase			= model.generate(input_ids=__UpperCamelCase    )

							_lowercase			= inputs_non_padded.shape[-1] - tf.math.reduce_sum(
							    tf.cast(inputs["attention_mask"][-1]							,    tf.intaa    )    )
							_lowercase			= tokenizer(sentences[1]							,    return_tensors="tf"    ).input_ids
							_lowercase			= model.generate(input_ids=__UpperCamelCase							,    max_length=model.config.max_length - num_paddings    )

							_lowercase			= tokenizer.batch_decode(__UpperCamelCase							,    skip_special_tokens=__UpperCamelCase    )
							_lowercase			= tokenizer.decode(output_non_padded[0]							,    skip_special_tokens=__UpperCamelCase    )
							_lowercase			= tokenizer.decode(output_padded[0]							,    skip_special_tokens=__UpperCamelCase    )

							_lowercase			= [
							    "Hello, my dog is a little bit of a dork.\nI'm a little bit",
							    "Today, I was in the middle of a conversation with a friend about the",
							]
							self.assertListEqual(__UpperCamelCase							,    __UpperCamelCase    )
							self.assertListEqual(__UpperCamelCase							,    [non_padded_sentence, padded_sentence]    )




		def       snake_case     ( self						:						Optional[Any]    ):







							"""simple docstring"""
							_lowercase			= "facebook/opt-350m"

							_lowercase			= [
							    "Today is a beautiful day and I want to",
							    "In the city of San Francisco, the city",
							    "Paris is the capital of France and the capital",
							    "Computers and mobile phones have taken over the",
							]

							_lowercase			= []
							_lowercase			= GPTaTokenizer.from_pretrained(__UpperCamelCase    )
							_lowercase			= TFOPTForCausalLM.from_pretrained(__UpperCamelCase    )

							for prompt in self.prompts:
												_lowercase			= tokenizer(__UpperCamelCase							,    return_tensors="tf"    ).input_ids

												_lowercase			= model.generate(__UpperCamelCase							,    max_length=1_0    )

												_lowercase			= tokenizer.batch_decode(__UpperCamelCase							,    skip_special_tokens=__UpperCamelCase    )
												predicted_outputs += generated_string

							self.assertListEqual(__UpperCamelCase							,    __UpperCamelCase    )









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


lowerCamelCase      :       List[Any]    =						{
    '''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:
 lowerCamelCase      :       List[Any]    =						['''OwlViTFeatureExtractor''']
 lowerCamelCase      :       str    =						['''OwlViTImageProcessor''']

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

 lowerCamelCase      :       List[str]    =						_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






