



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

   '''simple docstring'''




   A    :  List[Any]       				=    XGLMConfig
   A    :  Dict       				=    {}
   A    :  Tuple       				=    "gelu"


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




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			Optional[int])	->				str:




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			List[str])	->				Tuple:




         """simple docstring"""

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

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

         lowercase__     =  self.get_config()

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

         return (
             config,
             input_ids,
             input_mask,
             head_mask,
         )


   def 			UpperCAmelCase		(						self  :			List[Any])	->				List[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=lowerCAmelCase			,				bos_token_id=self.bos_token_id			,				eos_token_id=self.eos_token_id			,				pad_token_id=self.pad_token_id			,				return_dict=lowerCAmelCase			,				)



   def 			UpperCAmelCase		(						self  :			str)	->				str:




         """simple docstring"""

         lowercase__     =  self.prepare_config_and_inputs()

         (
             (
             lowercase__
         ),	(
             lowercase__
         ),	(
             lowercase__
         ),	(
             lowercase__
         ),	
         )     =  config_and_inputs

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

         return config, inputs_dict



@require_tf
class       UpperCAmelCase__(							lowerCamelCase     ,							lowerCamelCase     ,							unittest.TestCase	):

   '''simple docstring'''




   A    :  Optional[int]       				=    (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
   A    :  Optional[Any]       				=    (TFXGLMForCausalLM,) if is_tf_available() else ()
   A    :  List[Any]       				=    (
       {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
   )
   A    :  str       				=    False
   A    :  str       				=    False
   A    :  str       				=    False


   def 			UpperCAmelCase		(						self  :			str)	->				List[Any]:




         """simple docstring"""

         lowercase__     =  TFXGLMModelTester(self)
         lowercase__     =  ConfigTester(self			,				config_class=lowerCAmelCase			,				n_embd=37)


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




         """simple docstring"""

         self.config_tester.run_common_tests()


   @slow
   def 			UpperCAmelCase		(						self  :			Optional[int])	->				int:




         """simple docstring"""

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



   @unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.')
   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				str:




         """simple docstring"""

         super().test_resize_token_embeddings()



@require_tf
class       UpperCAmelCase__(							unittest.TestCase	):

   '''simple docstring'''




   @slow
   def 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			List[str]=True)	->				Dict:




         """simple docstring"""

         lowercase__     =  TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')
         lowercase__     =  tf.convert_to_tensor([[2, 2_68, 98_65]]			,				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
         lowercase__     =  [2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81]
         # fmt: on
         lowercase__     =  model.generate(lowerCAmelCase			,				do_sample=lowerCAmelCase			,				num_beams=1)
         if verify_outputs:
               self.assertListEqual(output_ids[0].numpy().tolist()			,				lowerCAmelCase)


   @slow
   def 			UpperCAmelCase		(						self  :			List[Any])	->				str:




         """simple docstring"""

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

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

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



   @slow
   def 			UpperCAmelCase		(						self  :			Any)	->				List[str]:




         """simple docstring"""

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

         lowercase__     =  'left'

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

         lowercase__     =  tokenizer(lowerCAmelCase			,				return_tensors='tf'			,				padding=lowerCAmelCase)
         lowercase__     =  inputs['input_ids']

         lowercase__     =  model.generate(input_ids=lowerCAmelCase			,				attention_mask=inputs['attention_mask']			,				max_new_tokens=12)

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

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

         lowercase__     =  tokenizer.batch_decode(lowerCAmelCase			,				skip_special_tokens=lowerCAmelCase)
         lowercase__     =  tokenizer.decode(output_non_padded[0]			,				skip_special_tokens=lowerCAmelCase)
         lowercase__     =  tokenizer.decode(output_padded[0]			,				skip_special_tokens=lowerCAmelCase)

         lowercase__     =  [
             '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(lowerCAmelCase			,				lowerCAmelCase)
         self.assertListEqual(lowerCAmelCase			,				[non_padded_sentence, padded_sentence])





from ...configuration_utils import PretrainedConfig
from ...utils import logging


a__      :				Dict   	=			logging.get_logger(__name__)

a__      :				List[Any]   	=			{
    "facebook/s2t-small-librispeech-asr": (
        "https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech_to_text
}



class       UpperCAmelCase__(							lowerCamelCase	):

   '''simple docstring'''




   A    :  int       				=    "speech_to_text"
   A    :  Optional[Any]       				=    ["past_key_values"]
   A    :  Optional[int]       				=    {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


   def __init__(						self  :			Optional[int]			,				lowerCAmelCase  :			Tuple=1_00_00			,				lowerCAmelCase  :			int=12			,				lowerCAmelCase  :			int=20_48			,				lowerCAmelCase  :			Union[str, Any]=4			,				lowerCAmelCase  :			str=6			,				lowerCAmelCase  :			Dict=20_48			,				lowerCAmelCase  :			Dict=4			,				lowerCAmelCase  :			Optional[int]=0.0			,				lowerCAmelCase  :			Union[str, Any]=0.0			,				lowerCAmelCase  :			int=True			,				lowerCAmelCase  :			Optional[Any]=True			,				lowerCAmelCase  :			Dict="relu"			,				lowerCAmelCase  :			Tuple=2_56			,				lowerCAmelCase  :			Dict=0.1			,				lowerCAmelCase  :			Optional[Any]=0.0			,				lowerCAmelCase  :			List[Any]=0.0			,				lowerCAmelCase  :			Any=0.02			,				lowerCAmelCase  :			List[Any]=2			,				lowerCAmelCase  :			Tuple=True			,				lowerCAmelCase  :			Tuple=1			,				lowerCAmelCase  :			List[str]=0			,				lowerCAmelCase  :			Union[str, Any]=2			,				lowerCAmelCase  :			Any=60_00			,				lowerCAmelCase  :			Optional[int]=10_24			,				lowerCAmelCase  :			Optional[Any]=2			,				lowerCAmelCase  :			Optional[Any]=(5, 5)			,				lowerCAmelCase  :			Union[str, Any]=10_24			,				lowerCAmelCase  :			List[Any]=80			,				lowerCAmelCase  :			List[str]=1			,				**lowerCAmelCase  :			List[str]			,				)	->				Dict:




         """simple docstring"""

         lowercase__     =  vocab_size
         lowercase__     =  d_model
         lowercase__     =  encoder_ffn_dim
         lowercase__     =  encoder_layers
         lowercase__     =  encoder_attention_heads
         lowercase__     =  decoder_ffn_dim
         lowercase__     =  decoder_layers
         lowercase__     =  decoder_attention_heads
         lowercase__     =  dropout
         lowercase__     =  attention_dropout
         lowercase__     =  activation_dropout
         lowercase__     =  activation_function
         lowercase__     =  init_std
         lowercase__     =  encoder_layerdrop
         lowercase__     =  decoder_layerdrop
         lowercase__     =  use_cache
         lowercase__     =  encoder_layers
         lowercase__     =  scale_embedding  # scale factor will be sqrt(d_model) if True
         lowercase__     =  max_source_positions
         lowercase__     =  max_target_positions
         lowercase__     =  num_conv_layers
         lowercase__     =  list(lowerCAmelCase)
         lowercase__     =  conv_channels
         lowercase__     =  input_feat_per_channel
         lowercase__     =  input_channels

         if len(self.conv_kernel_sizes) != self.num_conv_layers:
               raise ValueError(
                   'Configuration for convolutional module is incorrect. '
                   'It is required that `len(config.conv_kernel_sizes)` == `config.num_conv_layers` '
                   f'''but is `len(config.conv_kernel_sizes) = {len(self.conv_kernel_sizes)}`, '''
                   f'''`config.num_conv_layers = {self.num_conv_layers}`.''')

         super().__init__(
             pad_token_id=lowerCAmelCase			,				bos_token_id=lowerCAmelCase			,				eos_token_id=lowerCAmelCase			,				is_encoder_decoder=lowerCAmelCase			,				decoder_start_token_id=lowerCAmelCase			,				**lowerCAmelCase			,				)
