


'''simple docstring'''






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    __magic_name__				:
       UpperCAmelCase		 =XGLMConfig
       UpperCAmelCase		 ={}
       UpperCAmelCase		 ="gelu"






       def __init__(     self    ,				snake_case    ,				snake_case=1_4    ,				snake_case=7    ,				snake_case=True    ,				snake_case=True    ,				snake_case=True    ,				snake_case=9_9    ,				snake_case=3_2    ,				snake_case=2    ,				snake_case=4    ,				snake_case=3_7    ,				snake_case="gelu"    ,				snake_case=0.1    ,				snake_case=0.1    ,				snake_case=5_1_2    ,				snake_case=0.02    ,				)		->    Any:




          '''simple docstring'''
          _UpperCAmelCase  :  str				       =parent
          _UpperCAmelCase  :  int				       =batch_size
          _UpperCAmelCase  :  Tuple				       =seq_length
          _UpperCAmelCase  :  Any				       =is_training
          _UpperCAmelCase  :  Tuple				       =use_input_mask
          _UpperCAmelCase  :  str				       =use_labels
          _UpperCAmelCase  :  Dict				       =vocab_size
          _UpperCAmelCase  :  Optional[Any]				       =d_model
          _UpperCAmelCase  :  Any				       =num_hidden_layers
          _UpperCAmelCase  :  Any				       =num_attention_heads
          _UpperCAmelCase  :  str				       =ffn_dim
          _UpperCAmelCase  :  Union[str, Any]				       =activation_function
          _UpperCAmelCase  :  Tuple				       =activation_dropout
          _UpperCAmelCase  :  int				       =attention_dropout
          _UpperCAmelCase  :  List[Any]				       =max_position_embeddings
          _UpperCAmelCase  :  Any				       =initializer_range
          _UpperCAmelCase  :  str				       =None
          _UpperCAmelCase  :  Tuple				       =0
          _UpperCAmelCase  :  int				       =2
          _UpperCAmelCase  :  Any				       =1






       def      lowerCAmelCase						(     self)		->    List[str]:




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






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[Any]				       =tf.clip_by_value(
              ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size)    ,				clip_value_min=0    ,				clip_value_max=3)

          _UpperCAmelCase  :  Dict				       =None
          if self.use_input_mask:
             _UpperCAmelCase  :  List[Any]				       =random_attention_mask([self.batch_size, self.seq_length])

          _UpperCAmelCase  :  List[str]				       =self.get_config()

          _UpperCAmelCase  :  Union[str, Any]				       =floats_tensor([self.num_hidden_layers, self.num_attention_heads]    ,				2)

          return (
              config,
              input_ids,
              input_mask,
              head_mask,
          )






       def      lowerCAmelCase						(     self)		->    Union[str, Any]:




          '''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=snake_case    ,				bos_token_id=self.bos_token_id    ,				eos_token_id=self.eos_token_id    ,				pad_token_id=self.pad_token_id    ,				return_dict=snake_case    ,				)






       def      lowerCAmelCase						(     self)		->    List[str]:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =self.prepare_config_and_inputs()

          (
              (
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			(
              _UpperCAmelCase
          )       ,			
          )  :  Optional[int]				       =config_and_inputs

          _UpperCAmelCase  :  Tuple				       ={
              'input_ids': input_ids,
              'head_mask': head_mask,
          }

          return config, inputs_dict






@require_tf
class    __magic_name__				(						lowerCAmelCase   ,lowerCAmelCase   ,unittest.TestCase							):
       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      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =TFXGLMModelTester(self)
          _UpperCAmelCase  :  int				       =ConfigTester(self    ,				config_class=snake_case    ,				n_embd=3_7)






       def      lowerCAmelCase						(     self)		->    int:




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






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




          '''simple docstring'''
          for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCAmelCase  :  Optional[Any]				       =TFXGLMModel.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)






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




          '''simple docstring'''
          super().test_resize_token_embeddings()






@require_tf
class    __magic_name__				(						unittest.TestCase							):






       @slow
       def      lowerCAmelCase						(     self    ,				snake_case=True)		->    Optional[int]:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')
          _UpperCAmelCase  :  Dict				       =tf.convert_to_tensor([[2, 2_6_8, 9_8_6_5]]    ,				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
          _UpperCAmelCase  :  Optional[int]				       =[2, 2_6_8, 9_8_6_5, 6_7, 1_1, 1_9_8_8, 5_7_2_5_2, 9_8_6_5, 5, 9_8_4, 6_7, 1_9_8_8, 2_1_3_8_3_8, 1_6_5_8, 5_3, 7_0_4_4_6, 3_3, 6_6_5_7, 2_7_8, 1_5_8_1]
          # fmt: on
          _UpperCAmelCase  :  Any				       =model.generate(snake_case    ,				do_sample=snake_case    ,				num_beams=1)
          if verify_outputs:
             self.assertListEqual(output_ids[0].numpy().tolist()    ,				snake_case)






       @slow
       def      lowerCAmelCase						(     self)		->    str:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =XGLMTokenizer.from_pretrained('facebook/xglm-564M')
          _UpperCAmelCase  :  Optional[Any]				       =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')

          tf.random.set_seed(0)
          _UpperCAmelCase  :  Optional[Any]				       =tokenizer('Today is a nice day and'    ,				return_tensors='tf')
          _UpperCAmelCase  :  Union[str, Any]				       =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'):
             _UpperCAmelCase  :  Dict				       =model.generate(snake_case    ,				do_sample=snake_case    ,				seed=[7, 0])
          _UpperCAmelCase  :  Dict				       =tokenizer.decode(output_ids[0]    ,				skip_special_tokens=snake_case)

          _UpperCAmelCase  :  List[str]				       =(
              'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'
          )
          self.assertEqual(snake_case    ,				snake_case)






       @slow
       def      lowerCAmelCase						(     self)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  List[str]				       =TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')
          _UpperCAmelCase  :  str				       =XGLMTokenizer.from_pretrained('facebook/xglm-564M')

          _UpperCAmelCase  :  List[Any]				       ='left'

          # use different length sentences to test batching
          _UpperCAmelCase  :  List[Any]				       =[
              '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',
          ]

          _UpperCAmelCase  :  Optional[int]				       =tokenizer(snake_case    ,				return_tensors='tf'    ,				padding=snake_case)
          _UpperCAmelCase  :  Any				       =inputs['input_ids']

          _UpperCAmelCase  :  Dict				       =model.generate(input_ids=snake_case    ,				attention_mask=inputs['attention_mask']    ,				max_new_tokens=1_2)

          _UpperCAmelCase  :  List[Any]				       =tokenizer(sentences[0]    ,				return_tensors='tf').input_ids
          _UpperCAmelCase  :  Union[str, Any]				       =model.generate(input_ids=snake_case    ,				max_new_tokens=1_2)

          _UpperCAmelCase  :  Optional[Any]				       =tokenizer(sentences[1]    ,				return_tensors='tf').input_ids
          _UpperCAmelCase  :  Optional[int]				       =model.generate(input_ids=snake_case    ,				max_new_tokens=1_2)

          _UpperCAmelCase  :  Tuple				       =tokenizer.batch_decode(snake_case    ,				skip_special_tokens=snake_case)
          _UpperCAmelCase  :  Tuple				       =tokenizer.decode(output_non_padded[0]    ,				skip_special_tokens=snake_case)
          _UpperCAmelCase  :  int				       =tokenizer.decode(output_padded[0]    ,				skip_special_tokens=snake_case)

          _UpperCAmelCase  :  Optional[int]				       =[
              '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(snake_case    ,				snake_case)
          self.assertListEqual(snake_case    ,				[non_padded_sentence, padded_sentence])





'''simple docstring'''






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class    __magic_name__				(						lowerCAmelCase							):
       UpperCAmelCase		 =["image_processor", "tokenizer"]
       UpperCAmelCase		 ="CLIPImageProcessor"
       UpperCAmelCase		 =("CLIPTokenizer", "CLIPTokenizerFast")






       def __init__(     self    ,				snake_case=None    ,				snake_case=None    ,				**snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Union[str, Any]				       =None
          if "feature_extractor" in kwargs:
             warnings.warn(
                 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                 ' instead.'    ,				snake_case    ,				)
             _UpperCAmelCase  :  str				       =kwargs.pop('feature_extractor')

          _UpperCAmelCase  :  List[str]				       =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__(snake_case    ,				snake_case)






       def __call__(     self    ,				snake_case=None    ,				snake_case=None    ,				snake_case=None    ,				**snake_case)		->    Optional[Any]:




          '''simple docstring'''

          if text is None and images is None:
             raise ValueError('You have to specify either text or images. Both cannot be none.')

          if text is not None:
             _UpperCAmelCase  :  List[Any]				       =self.tokenizer(snake_case    ,				return_tensors=snake_case    ,				**snake_case)

          if images is not None:
             _UpperCAmelCase  :  List[str]				       =self.image_processor(snake_case    ,				return_tensors=snake_case    ,				**snake_case)

          if text is not None and images is not None:
             _UpperCAmelCase  :  Any				       =image_features.pixel_values
             return encoding
          elif text is not None:
             return encoding
          else:
             return BatchEncoding(data=dict(**snake_case)    ,				tensor_type=snake_case)






       def      lowerCAmelCase						(     self    ,				*snake_case    ,				**snake_case)		->    int:




          '''simple docstring'''
          return self.tokenizer.batch_decode(*snake_case    ,				**snake_case)






       def      lowerCAmelCase						(     self    ,				*snake_case    ,				**snake_case)		->    Optional[int]:




          '''simple docstring'''
          return self.tokenizer.decode(*snake_case    ,				**snake_case)






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




          '''simple docstring'''
          _UpperCAmelCase  :  List[Any]				       =self.tokenizer.model_input_names
          _UpperCAmelCase  :  Any				       =self.image_processor.model_input_names
          return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))






       @property
       def      lowerCAmelCase						(     self)		->    Any:




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






       @property
       def      lowerCAmelCase						(     self)		->    Optional[int]:




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

