




from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
	import tensorflow as tf

	from transformers.models.xglm.modeling_tf_xglm import (
	    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
	    TFXGLMForCausalLM,
	    TFXGLMModel,
	)







@require_tf
class   A:



	'''simple docstring'''


	UpperCamelCase						       =						XGLMConfig
	UpperCamelCase						       =						{}
	UpperCamelCase						       =						'''gelu'''

	def __init__(    self :  Union[str, Any]						,							A_ :  int						,							A_ :  Any=14						,							A_ :  Optional[Any]=7						,							A_ :  int=True						,							A_ :  List[Any]=True						,							A_ :  Optional[int]=True						,							A_ :  Dict=99						,							A_ :  int=32						,							A_ :  Tuple=2						,							A_ :  List[Any]=4						,							A_ :  str=37						,							A_ :  Dict="gelu"						,							A_ :  Optional[int]=0.1						,							A_ :  int=0.1						,							A_ :  str=512						,							A_ :  Dict=0.02						,							)       ->     Optional[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   parent
							lowerCamelCase_							=   batch_size
							lowerCamelCase_							=   seq_length
							lowerCamelCase_							=   is_training
							lowerCamelCase_							=   use_input_mask
							lowerCamelCase_							=   use_labels
							lowerCamelCase_							=   vocab_size
							lowerCamelCase_							=   d_model
							lowerCamelCase_							=   num_hidden_layers
							lowerCamelCase_							=   num_attention_heads
							lowerCamelCase_							=   ffn_dim
							lowerCamelCase_							=   activation_function
							lowerCamelCase_							=   activation_dropout
							lowerCamelCase_							=   attention_dropout
							lowerCamelCase_							=   max_position_embeddings
							lowerCamelCase_							=   initializer_range
							lowerCamelCase_							=   None
							lowerCamelCase_							=   0
							lowerCamelCase_							=   2
							lowerCamelCase_							=   1

	def     a__			(    self :  List[Any]  )       ->     int:

							"""simple docstring"""






							return XGLMConfig.from_pretrained('facebook/xglm-564M'  )

	def     a__			(    self :  Dict  )       ->     List[str]:

							"""simple docstring"""






							lowerCamelCase_							=   tf.clip_by_value(
							    ids_tensor([self.batch_size, self.seq_length]						,							self.vocab_size  )						,							clip_value_min=0						,							clip_value_max=3  )

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

							lowerCamelCase_							=   self.get_config()

							lowerCamelCase_							=   floats_tensor([self.num_hidden_layers, self.num_attention_heads]						,							2  )

							return (
							    config,
							    input_ids,
							    input_mask,
							    head_mask,
							)

	def     a__			(    self :  int  )       ->     int:

							"""simple docstring"""






							return XGLMConfig(
							    vocab_size=self.vocab_size						,							d_model=self.hidden_size						,							num_layers=self.num_hidden_layers						,							attention_heads=self.num_attention_heads						,							ffn_dim=self.ffn_dim						,							activation_function=self.activation_function						,							activation_dropout=self.activation_dropout						,							attention_dropout=self.attention_dropout						,							max_position_embeddings=self.max_position_embeddings						,							initializer_range=self.initializer_range						,							use_cache=A_						,							bos_token_id=self.bos_token_id						,							eos_token_id=self.eos_token_id						,							pad_token_id=self.pad_token_id						,							return_dict=A_						,							)





	def     a__			(    self :  List[str]  )       ->     Dict:

							"""simple docstring"""






							lowerCamelCase_							=   self.prepare_config_and_inputs()

							(
							    (
							    lowerCamelCase_    
							)					,    (
							    lowerCamelCase_    
							)					,    (
							    lowerCamelCase_    
							)					,    (
							    lowerCamelCase_    
							)					,        
							)							=   config_and_inputs

							lowerCamelCase_							=   {
							    'input_ids': input_ids,
							    'head_mask': head_mask,
							}

							return config, inputs_dict







@require_tf
class   A(						UpperCamelCase							,				UpperCamelCase							,				unittest.TestCase       ):



	'''simple docstring'''


	UpperCamelCase						       =						(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
	UpperCamelCase						       =						(TFXGLMForCausalLM,) if is_tf_available() else ()
	UpperCamelCase						       =						(
	    {'''feature-extraction''': TFXGLMModel, '''text-generation''': TFXGLMForCausalLM} if is_tf_available() else {}
	)
	UpperCamelCase						       =						False
	UpperCamelCase						       =						False
	UpperCamelCase						       =						False

	def     a__			(    self :  Optional[int]  )       ->     Optional[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   TFXGLMModelTester(self  )
							lowerCamelCase_							=   ConfigTester(self						,							config_class=A_						,							n_embd=37  )

	def     a__			(    self :  Optional[Any]  )       ->     Dict:

							"""simple docstring"""






							self.config_tester.run_common_tests()

	@slow
	def     a__			(    self :  Optional[Any]  )       ->     Union[str, Any]:

							"""simple docstring"""






							for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowerCamelCase_							=   TFXGLMModel.from_pretrained(A_  )
													self.assertIsNotNone(A_  )





	@unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.'  )
	def     a__			(    self :  int  )       ->     Any:

							"""simple docstring"""






							super().test_resize_token_embeddings()







@require_tf
class   A(						unittest.TestCase       ):



	'''simple docstring'''


	@slow
	def     a__			(    self :  int						,							A_ :  Dict=True  )       ->     Any:

							"""simple docstring"""






							lowerCamelCase_							=   TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'  )
							lowerCamelCase_							=   tf.convert_to_tensor([[2, 268, 9865]]						,							dtype=tf.intaa  )  # The dog
							# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
							# fmt: off
							lowerCamelCase_							=   [2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581]
							# fmt: on
							lowerCamelCase_							=   model.generate(A_						,							do_sample=A_						,							num_beams=1  )
							if verify_outputs:
													self.assertListEqual(output_ids[0].numpy().tolist()						,							A_  )

	@slow
	def     a__			(    self :  Optional[Any]  )       ->     Optional[Any]:

							"""simple docstring"""






							lowerCamelCase_							=   XGLMTokenizer.from_pretrained('facebook/xglm-564M'  )
							lowerCamelCase_							=   TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'  )

							tf.random.set_seed(0  )
							lowerCamelCase_							=   tokenizer('Today is a nice day and'						,							return_tensors='tf'  )
							lowerCamelCase_							=   tokenized.input_ids
							# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
							with tf.device(':/CPU:0'  ):
													lowerCamelCase_							=   model.generate(A_						,							do_sample=A_						,							seed=[7, 0]  )
							lowerCamelCase_							=   tokenizer.decode(output_ids[0]						,							skip_special_tokens=A_  )

							lowerCamelCase_							=   (
							    'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'
							)
							self.assertEqual(A_						,							A_  )





	@slow
	def     a__			(    self :  Dict  )       ->     str:

							"""simple docstring"""






							lowerCamelCase_							=   TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'  )
							lowerCamelCase_							=   XGLMTokenizer.from_pretrained('facebook/xglm-564M'  )

							lowerCamelCase_							=   'left'

							# use different length sentences to test batching
							lowerCamelCase_							=   [
							    'This is an extremelly long sentence that only exists to test the ability of the model to cope with '
							    'left-padding, such as in batched generation. The output for the sequence below should be the same '
							    'regardless of whether left padding is applied or not. When',
							    'Hello, my dog is a little',
							]

							lowerCamelCase_							=   tokenizer(A_						,							return_tensors='tf'						,							padding=A_  )
							lowerCamelCase_							=   inputs['input_ids']

							lowerCamelCase_							=   model.generate(input_ids=A_						,							attention_mask=inputs['attention_mask']						,							max_new_tokens=12  )

							lowerCamelCase_							=   tokenizer(sentences[0]						,							return_tensors='tf'  ).input_ids
							lowerCamelCase_							=   model.generate(input_ids=A_						,							max_new_tokens=12  )

							lowerCamelCase_							=   tokenizer(sentences[1]						,							return_tensors='tf'  ).input_ids
							lowerCamelCase_							=   model.generate(input_ids=A_						,							max_new_tokens=12  )

							lowerCamelCase_							=   tokenizer.batch_decode(A_						,							skip_special_tokens=A_  )
							lowerCamelCase_							=   tokenizer.decode(output_non_padded[0]						,							skip_special_tokens=A_  )
							lowerCamelCase_							=   tokenizer.decode(output_padded[0]						,							skip_special_tokens=A_  )

							lowerCamelCase_							=   [
							    'This is an extremelly long sentence that only exists to test the ability of the model to cope with '
							    'left-padding, such as in batched generation. The output for the sequence below should be the same '
							    'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '
							    'a single',
							    'Hello, my dog is a little bit of a shy one, but he is very friendly',
							]
							self.assertListEqual(A_						,							A_  )
							self.assertListEqual(A_						,							[non_padded_sentence, padded_sentence]  )









import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class   A(						UpperCamelCase       ):



 '''simple docstring'''


 UpperCamelCase						       =						['''image_processor''', '''tokenizer''']
 UpperCamelCase						       =						'''OwlViTImageProcessor'''
 UpperCamelCase						       =						('''CLIPTokenizer''', '''CLIPTokenizerFast''')

 def __init__(    self :  Optional[Any]						,							A_ :  Tuple=None						,							A_ :  Tuple=None						,							**A_ :  List[str]  )       ->     Any:

       """simple docstring"""






       lowerCamelCase_							=   None
       if "feature_extractor" in kwargs:
             warnings.warn(
                 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                 ' instead.'						,							A_						,							)
             lowerCamelCase_							=   kwargs.pop('feature_extractor'  )

       lowerCamelCase_							=   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__(A_						,							A_  )

 def __call__(    self :  List[str]						,							A_ :  List[str]=None						,							A_ :  List[Any]=None						,							A_ :  Dict=None						,							A_ :  Tuple="max_length"						,							A_ :  int="np"						,							**A_ :  Union[str, Any]  )       ->     Union[str, Any]:

       """simple docstring"""






       if text is None and query_images is None and images is None:
             raise ValueError(
                 'You have to specify at least one text or query image or image. All three cannot be none.'  )

       if text is not None:
             if isinstance(A_						,							A_  ) or (isinstance(A_						,							A_  ) and not isinstance(text[0]						,							A_  )):
                   lowerCamelCase_							=   [self.tokenizer(A_						,							padding=A_						,							return_tensors=A_						,							**A_  )]

             elif isinstance(A_						,							A_  ) and isinstance(text[0]						,							A_  ):
                   lowerCamelCase_							=   []

                   # Maximum number of queries across batch
                   lowerCamelCase_							=   max([len(A_  ) for t in text]  )

                   # Pad all batch samples to max number of text queries
                   for t in text:
                         if len(A_  ) != max_num_queries:
                               lowerCamelCase_							=   t + [' '] * (max_num_queries - len(A_  ))

                         lowerCamelCase_							=   self.tokenizer(A_						,							padding=A_						,							return_tensors=A_						,							**A_  )
                         encodings.append(A_  )
             else:
                   raise TypeError('Input text should be a string, a list of strings or a nested list of strings'  )

             if return_tensors == "np":
                   lowerCamelCase_							=   np.concatenate([encoding['input_ids'] for encoding in encodings]						,							axis=0  )
                   lowerCamelCase_							=   np.concatenate([encoding['attention_mask'] for encoding in encodings]						,							axis=0  )

             elif return_tensors == "jax" and is_flax_available():
                   import jax.numpy as jnp

                   lowerCamelCase_							=   jnp.concatenate([encoding['input_ids'] for encoding in encodings]						,							axis=0  )
                   lowerCamelCase_							=   jnp.concatenate([encoding['attention_mask'] for encoding in encodings]						,							axis=0  )

             elif return_tensors == "pt" and is_torch_available():
                   import torch

                   lowerCamelCase_							=   torch.cat([encoding['input_ids'] for encoding in encodings]						,							dim=0  )
                   lowerCamelCase_							=   torch.cat([encoding['attention_mask'] for encoding in encodings]						,							dim=0  )

             elif return_tensors == "tf" and is_tf_available():
                   import tensorflow as tf

                   lowerCamelCase_							=   tf.stack([encoding['input_ids'] for encoding in encodings]						,							axis=0  )
                   lowerCamelCase_							=   tf.stack([encoding['attention_mask'] for encoding in encodings]						,							axis=0  )

             else:
                   raise ValueError('Target return tensor type could not be returned'  )

             lowerCamelCase_							=   BatchEncoding()
             lowerCamelCase_							=   input_ids
             lowerCamelCase_							=   attention_mask

       if query_images is not None:
             lowerCamelCase_							=   BatchEncoding()
             lowerCamelCase_							=   self.image_processor(
                 A_						,							return_tensors=A_						,							**A_  ).pixel_values
             lowerCamelCase_							=   query_pixel_values

       if images is not None:
             lowerCamelCase_							=   self.image_processor(A_						,							return_tensors=A_						,							**A_  )

       if text is not None and images is not None:
             lowerCamelCase_							=   image_features.pixel_values
             return encoding
       elif query_images is not None and images is not None:
             lowerCamelCase_							=   image_features.pixel_values
             return encoding
       elif text is not None or query_images is not None:
             return encoding
       else:
             return BatchEncoding(data=dict(**A_  )						,							tensor_type=A_  )

 def     a__			(    self :  Tuple						,							*A_ :  Dict						,							**A_ :  Dict  )       ->     Any:

       """simple docstring"""






       return self.image_processor.post_process(*A_						,							**A_  )

 def     a__			(    self :  List[str]						,							*A_ :  Any						,							**A_ :  List[Any]  )       ->     Optional[int]:

       """simple docstring"""






       return self.image_processor.post_process_object_detection(*A_						,							**A_  )

 def     a__			(    self :  Any						,							*A_ :  str						,							**A_ :  List[Any]  )       ->     Any:

       """simple docstring"""






       return self.image_processor.post_process_image_guided_detection(*A_						,							**A_  )

 def     a__			(    self :  Union[str, Any]						,							*A_ :  Any						,							**A_ :  Union[str, Any]  )       ->     str:

       """simple docstring"""






       return self.tokenizer.batch_decode(*A_						,							**A_  )

 def     a__			(    self :  Optional[int]						,							*A_ :  List[Any]						,							**A_ :  int  )       ->     int:

       """simple docstring"""






       return self.tokenizer.decode(*A_						,							**A_  )

 @property
 def     a__			(    self :  List[Any]  )       ->     List[Any]:

       """simple docstring"""






       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'						,							A_						,							)
       return self.image_processor_class





 @property
 def     a__			(    self :  str  )       ->     List[str]:

       """simple docstring"""






       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'						,							A_						,							)
       return self.image_processor



