





from __future__ import annotations

import unittest

from transformers import LEDConfig, is_tf_available
from transformers.testing_utils import 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 TFLEDForConditionalGeneration, TFLEDModel


@require_tf
class 			lowerCamelCase						:
  UpperCAmelCase		:       List[Any]										=     LEDConfig
  UpperCAmelCase		:       List[str]										=     {}
  UpperCAmelCase		:       Optional[int]										=     'gelu'







  def __init__(     self						:   List[Any]  ,					__snake_case						:   Any  ,					__snake_case						:   Dict=13  ,					__snake_case						:   Optional[int]=7  ,					__snake_case						:   Dict=True  ,					__snake_case						:   Dict=False  ,					__snake_case						:   str=99  ,					__snake_case						:   Optional[Any]=32  ,					__snake_case						:   Dict=2  ,					__snake_case						:   str=4  ,					__snake_case						:   Union[str, Any]=37  ,					__snake_case						:   int=0.1  ,					__snake_case						:   Dict=0.1  ,					__snake_case						:   str=20  ,					__snake_case						:   Union[str, Any]=2  ,					__snake_case						:   Optional[Any]=1  ,					__snake_case						:   List[Any]=0  ,					__snake_case						:   str=4  ,					)    ->		Optional[int]:
        _a							:  Optional[int]				= parent
        _a							:  Optional[int]				= batch_size
        _a							:  Optional[Any]				= seq_length
        _a							:  Tuple				= is_training
        _a							:  Optional[Any]				= use_labels
        _a							:  Tuple				= vocab_size
        _a							:  Optional[Any]				= hidden_size
        _a							:  List[Any]				= num_hidden_layers
        _a							:  List[Any]				= num_attention_heads
        _a							:  List[str]				= intermediate_size

        _a							:  Dict				= hidden_dropout_prob
        _a							:  List[Any]				= attention_probs_dropout_prob
        _a							:  Tuple				= max_position_embeddings
        _a							:  Union[str, Any]				= eos_token_id
        _a							:  List[str]				= pad_token_id
        _a							:  Dict				= bos_token_id
        _a							:  List[str]				= attention_window

        # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size
        # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention
        # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1]
        # because its local attention only attends to `self.attention_window` and one before and one after
        _a							:  Any				= self.attention_window + 2

        # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for
        # the `test_attention_outputs` and `test_hidden_states_output` tests
        _a							:  str				= (
            self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
        )







  def      snake_case_							(     self						:   str       )    ->		List[str]:
        _a							:  Tuple				= ids_tensor([self.batch_size, self.seq_length - 1]  ,					self.vocab_size       )
        _a							:  Any				= tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size       )  ,					1       )
        _a							:  List[Any]				= tf.concat([input_ids, eos_tensor]  ,					axis=1       )

        _a							:  Dict				= ids_tensor([self.batch_size, self.seq_length]  ,					self.vocab_size       )

        _a							:  Tuple				= self.config_cls(
            vocab_size=self.vocab_size  ,					d_model=self.hidden_size  ,					encoder_layers=self.num_hidden_layers  ,					decoder_layers=self.num_hidden_layers  ,					encoder_attention_heads=self.num_attention_heads  ,					decoder_attention_heads=self.num_attention_heads  ,					encoder_ffn_dim=self.intermediate_size  ,					decoder_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_ids=[2]  ,					bos_token_id=self.bos_token_id  ,					pad_token_id=self.pad_token_id  ,					decoder_start_token_id=self.pad_token_id  ,					attention_window=self.attention_window  ,					**self.config_updates  ,					)
        _a							:  Any				= prepare_led_inputs_dict(__lowerCamelCase  ,					__lowerCamelCase  ,					__lowerCamelCase       )
        _a							:  Dict				= tf.concat(
            [tf.zeros_like(__lowerCamelCase       )[:, :-1], tf.ones_like(__lowerCamelCase       )[:, -1:]]  ,					axis=-1  ,					)
        _a							:  Tuple				= global_attention_mask
        return config, inputs_dict







  def      snake_case_							(     self						:   Union[str, Any]  ,					__snake_case						:   Optional[Any]  ,					__snake_case						:   Optional[Any]       )    ->		Union[str, Any]:
        _a							:  List[str]				= TFLEDModel(config=__lowerCamelCase       ).get_decoder()
        _a							:  List[Any]				= inputs_dict['''input_ids''']

        _a							:  List[str]				= input_ids[:1, :]
        _a							:  List[str]				= inputs_dict['''attention_mask'''][:1, :]
        _a							:  Optional[Any]				= 1

        # first forward pass
        _a							:  Union[str, Any]				= model(__lowerCamelCase  ,					attention_mask=__lowerCamelCase  ,					use_cache=__lowerCamelCase       )

        _a							:  Optional[Any]				= outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        _a							:  Optional[Any]				= ids_tensor((self.batch_size, 3)  ,					config.vocab_size       )
        _a							:  Dict				= tf.cast(ids_tensor((self.batch_size, 3)  ,					2       )  ,					tf.inta       )

        # append to next input_ids and
        _a							:  Union[str, Any]				= tf.concat([input_ids, next_tokens]  ,					axis=-1       )
        _a							:  Any				= tf.concat([attention_mask, next_attn_mask]  ,					axis=-1       )

        _a							:  List[Any]				= model(__lowerCamelCase  ,					attention_mask=__lowerCamelCase       )[0]
        _a							:  Any				= model(__lowerCamelCase  ,					attention_mask=__lowerCamelCase  ,					past_key_values=__lowerCamelCase       )[0]

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

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

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




def 						lowerCamelCase_	(		UpperCamelCase_			,					UpperCamelCase_			,					UpperCamelCase_			,					UpperCamelCase_=None			,					UpperCamelCase_=None			,					UpperCamelCase_=None			,					UpperCamelCase_=None			,					):







      if attention_mask is None:
            _a							:  Union[str, Any]				= tf.cast(tf.math.not_equal(snake_case_			,					config.pad_token_id       )			,					tf.inta       )
      if decoder_attention_mask is None:
            _a							:  Tuple				= tf.concat(
                [
                    tf.ones(decoder_input_ids[:, :1].shape			,					dtype=tf.inta       ),
                    tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]			,					config.pad_token_id       )			,					tf.inta       ),
                ]			,					axis=-1			,					)
      if head_mask is None:
            _a							:  Union[str, Any]				= tf.ones((config.encoder_layers, config.encoder_attention_heads)       )
      if decoder_head_mask is None:
            _a							:  List[str]				= tf.ones((config.decoder_layers, config.decoder_attention_heads)       )
      return {
          "input_ids": input_ids,
          "attention_mask": attention_mask,
          "decoder_input_ids": decoder_input_ids,
          "decoder_attention_mask": decoder_attention_mask,
          "head_mask": head_mask,
          "decoder_head_mask": decoder_head_mask,
      }


@require_tf
class 			lowerCamelCase						(		SCREAMING_SNAKE_CASE__ ,					SCREAMING_SNAKE_CASE__ ,					unittest.TestCase  ):
  UpperCAmelCase		:       List[str]										=     (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
  UpperCAmelCase		:       int										=     (TFLEDForConditionalGeneration,) if is_tf_available() else ()
  UpperCAmelCase		:       Union[str, Any]										=     (
      {
          'conversational': TFLEDForConditionalGeneration,
          'feature-extraction': TFLEDModel,
          'summarization': TFLEDForConditionalGeneration,
          'text2text-generation': TFLEDForConditionalGeneration,
          'translation': TFLEDForConditionalGeneration,
      }
      if is_tf_available()
      else {}
  )
  UpperCAmelCase		:       List[str]										=     True
  UpperCAmelCase		:       List[str]										=     False
  UpperCAmelCase		:       Any										=     False
  UpperCAmelCase		:       Tuple										=     False







  def      snake_case_							(     self						:   Optional[int]       )    ->		Optional[int]:
        _a							:  Union[str, Any]				= TFLEDModelTester(self       )
        _a							:  Optional[int]				= ConfigTester(self  ,					config_class=__lowerCamelCase       )







  def      snake_case_							(     self						:   Dict       )    ->		str:
        self.config_tester.run_common_tests()







  def      snake_case_							(     self						:   Optional[Any]       )    ->		int:
        _a							:  Dict				= self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*__lowerCamelCase       )







  def      snake_case_							(     self						:   List[str]       )    ->		str:
        _a							:  str				= self.model_tester.prepare_config_and_inputs_for_common()
        _a							:  Optional[int]				= tf.zeros_like(inputs_dict['''attention_mask''']       )
        _a							:  Dict				= 2
        _a							:  Dict				= tf.where(
            tf.range(self.model_tester.seq_length       )[None, :] < num_global_attn_indices  ,					1  ,					inputs_dict['''global_attention_mask''']  ,					)

        _a							:  Any				= True
        _a							:  Any				= self.model_tester.seq_length
        _a							:  Dict				= self.model_tester.encoder_seq_length

        def check_decoder_attentions_output(__snake_case						:   str       ):
              _a							:  int				= outputs.decoder_attentions
              self.assertEqual(len(__lowerCamelCase       )  ,					self.model_tester.num_hidden_layers       )
              self.assertListEqual(
                  list(decoder_attentions[0].shape[-3:]       )  ,					[self.model_tester.num_attention_heads, seq_length, seq_length]  ,					)

        def check_encoder_attentions_output(__snake_case						:   Optional[Any]       ):
              _a							:  Optional[int]				= [t.numpy() for t in outputs.encoder_attentions]
              _a							:  Optional[int]				= [t.numpy() for t in outputs.encoder_global_attentions]
              self.assertEqual(len(__lowerCamelCase       )  ,					self.model_tester.num_hidden_layers       )
              self.assertEqual(len(__lowerCamelCase       )  ,					self.model_tester.num_hidden_layers       )
              self.assertListEqual(
                  list(attentions[0].shape[-3:]       )  ,					[self.model_tester.num_attention_heads, seq_length, seq_length]  ,					)
              self.assertListEqual(
                  list(global_attentions[0].shape[-3:]       )  ,					[self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices]  ,					)

        for model_class in self.all_model_classes:
              _a							:  Tuple				= True
              _a							:  Optional[Any]				= False
              _a							:  Optional[Any]				= False
              _a							:  Optional[int]				= model_class(__lowerCamelCase       )
              _a							:  Optional[int]				= model(self._prepare_for_class(__lowerCamelCase  ,					__lowerCamelCase       )       )
              _a							:  str				= len(__lowerCamelCase       )
              self.assertEqual(config.output_hidden_states  ,					__lowerCamelCase       )
              check_encoder_attentions_output(__lowerCamelCase       )

              if self.is_encoder_decoder:
                    _a							:  int				= model_class(__lowerCamelCase       )
                    _a							:  Any				= model(self._prepare_for_class(__lowerCamelCase  ,					__lowerCamelCase       )       )
                    self.assertEqual(config.output_hidden_states  ,					__lowerCamelCase       )
                    check_decoder_attentions_output(__lowerCamelCase       )

              # Check that output attentions can also be changed via the config
              del inputs_dict["output_attentions"]
              _a							:  Dict				= True
              _a							:  Union[str, Any]				= model_class(__lowerCamelCase       )
              _a							:  Optional[Any]				= model(self._prepare_for_class(__lowerCamelCase  ,					__lowerCamelCase       )       )
              self.assertEqual(config.output_hidden_states  ,					__lowerCamelCase       )
              check_encoder_attentions_output(__lowerCamelCase       )

              # Check attention is always last and order is fine
              _a							:  Tuple				= True
              _a							:  int				= True
              _a							:  Union[str, Any]				= model_class(__lowerCamelCase       )
              _a							:  Dict				= model(self._prepare_for_class(__lowerCamelCase  ,					__lowerCamelCase       )       )

              self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)  ,					len(__lowerCamelCase       )       )
              self.assertEqual(model.config.output_hidden_states  ,					__lowerCamelCase       )
              check_encoder_attentions_output(__lowerCamelCase       )







  @unittest.skip('''LED keeps using potentially symbolic tensors in conditionals and breaks tracing.'''       )
  def      snake_case_							(     self						:   List[str]       )    ->		List[str]:
        pass







  def      snake_case_							(     self						:   Optional[Any]       )    ->		Union[str, Any]:
        pass




def 						lowerCamelCase_	(		UpperCamelCase_       ):
      return tf.constant(snake_case_			,					dtype=tf.intaa       )


__UpperCAmelCase					:     Dict	      =	1E-4





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







  def      snake_case_							(     self						:   Union[str, Any]       )    ->		List[str]:
        _a							:  List[Any]				= TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384'''       ).led

        # change to intended input here
        _a							:  Optional[Any]				= _long_tensor([512 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]       )
        _a							:  Dict				= _long_tensor([128 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]       )
        _a							:  int				= prepare_led_inputs_dict(model.config  ,					__lowerCamelCase  ,					__lowerCamelCase       )
        _a							:  Tuple				= model(**__lowerCamelCase       )[0]
        _a							:  List[str]				= (1, 1024, 768)
        self.assertEqual(output.shape  ,					__lowerCamelCase       )
        # change to expected output here
        _a							:  Optional[int]				= tf.convert_to_tensor(
            [[2.3_050, 2.8_279, 0.6_531], [-1.8_457, -0.1_455, -3.5_661], [-1.0_186, 0.4_586, -2.2_043]]  ,					)
        tf.debugging.assert_near(output[:, :3, :3]  ,					__lowerCamelCase  ,					atol=1E-3       )







  def      snake_case_							(     self						:   Tuple       )    ->		Optional[Any]:
        _a							:  Union[str, Any]				= TFLEDForConditionalGeneration.from_pretrained('''allenai/led-base-16384'''       )

        # change to intended input here
        _a							:  int				= _long_tensor([512 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]       )
        _a							:  Dict				= _long_tensor([128 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]       )
        _a							:  Union[str, Any]				= prepare_led_inputs_dict(model.config  ,					__lowerCamelCase  ,					__lowerCamelCase       )
        _a							:  Dict				= model(**__lowerCamelCase       )[0]
        _a							:  List[str]				= (1, 1024, model.config.vocab_size)
        self.assertEqual(output.shape  ,					__lowerCamelCase       )
        # change to expected output here
        _a							:  str				= tf.convert_to_tensor(
            [[33.6507, 6.4_572, 16.8089], [5.8_739, -2.4_238, 11.2902], [-3.2_139, -4.3_149, 4.2_783]]  ,					)
        tf.debugging.assert_near(output[:, :3, :3]  ,					__lowerCamelCase  ,					atol=1E-3  ,					rtol=1E-3       )












"""simple docstring"""



import functools
import operator

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


a_					=     logging.get_logger(__name__)

a_					=     {
    """microsoft/unispeech-large-1500h-cv""": (
        """https://huggingface.co/microsoft/unispeech-large-1500h-cv/resolve/main/config.json"""
    ),
    # See all UniSpeech models at https://huggingface.co/models?filter=unispeech
}






class 						__snake_case   (						SCREAMING_SNAKE_CASE__	):





 """simple docstring"""


 _lowerCamelCase								=	"""unispeech"""

 def __init__(       self   ,		__lowerCamelCase=32   ,		__lowerCamelCase=768   ,		__lowerCamelCase=12   ,		__lowerCamelCase=12   ,		__lowerCamelCase=3072   ,		__lowerCamelCase="gelu"   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.0   ,		__lowerCamelCase=0.0   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=0.0_2   ,		__lowerCamelCase=1e-5   ,		__lowerCamelCase="group"   ,		__lowerCamelCase="gelu"   ,		__lowerCamelCase=(512, 512, 512, 512, 512, 512, 512)   ,		__lowerCamelCase=(5, 2, 2, 2, 2, 2, 2)   ,		__lowerCamelCase=(10, 3, 3, 3, 3, 2, 2)   ,		__lowerCamelCase=False   ,		__lowerCamelCase=128   ,		__lowerCamelCase=16   ,		__lowerCamelCase=False   ,		__lowerCamelCase=True   ,		__lowerCamelCase=0.0_5   ,		__lowerCamelCase=10   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.0   ,		__lowerCamelCase=10   ,		__lowerCamelCase=0   ,		__lowerCamelCase=320   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase=100   ,		__lowerCamelCase=256   ,		__lowerCamelCase=256   ,		__lowerCamelCase=0.1   ,		__lowerCamelCase="mean"   ,		__lowerCamelCase=False   ,		__lowerCamelCase=False   ,		__lowerCamelCase=256   ,		__lowerCamelCase=80   ,		__lowerCamelCase=0   ,		__lowerCamelCase=1   ,		__lowerCamelCase=2   ,		__lowerCamelCase=0.5   ,		**__lowerCamelCase   ,		):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase   ,		pad_token_id=__lowerCamelCase   ,		bos_token_id=__lowerCamelCase   ,		eos_token_id=__lowerCamelCase    )
     __A       :		Dict							=	hidden_size
     __A       :		Dict							=	feat_extract_norm
     __A       :		int							=	feat_extract_activation
     __A       :		Dict							=	list(__lowerCamelCase    )
     __A       :		str							=	list(__lowerCamelCase    )
     __A       :		Dict							=	list(__lowerCamelCase    )
     __A       :		str							=	conv_bias
     __A       :		Optional[int]							=	num_conv_pos_embeddings
     __A       :		int							=	num_conv_pos_embedding_groups
     __A       :		Union[str, Any]							=	len(self.conv_dim    )
     __A       :		List[str]							=	num_hidden_layers
     __A       :		Dict							=	intermediate_size
     __A       :		List[str]							=	hidden_act
     __A       :		int							=	num_attention_heads
     __A       :		str							=	hidden_dropout
     __A       :		str							=	attention_dropout
     __A       :		Optional[Any]							=	activation_dropout
     __A       :		Optional[int]							=	feat_proj_dropout
     __A       :		Optional[Any]							=	final_dropout
     __A       :		List[Any]							=	layerdrop
     __A       :		Any							=	layer_norm_eps
     __A       :		str							=	initializer_range
     __A       :		List[str]							=	num_ctc_classes
     __A       :		Dict							=	vocab_size
     __A       :		Dict							=	do_stable_layer_norm
     __A       :		Union[str, Any]							=	use_weighted_layer_sum
     __A       :		Tuple							=	classifier_proj_size

     if (
         (len(self.conv_stride    ) != self.num_feat_extract_layers)
         or (len(self.conv_kernel    ) != self.num_feat_extract_layers)
         or (len(self.conv_dim    ) != self.num_feat_extract_layers)
     ):
         raise ValueError(
             '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
             ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
             F""" {len(self.conv_dim    )}`, `len(config.conv_stride) = {len(self.conv_stride    )}`,"""
             F""" `len(config.conv_kernel) = {len(self.conv_kernel    )}`."""    )

     # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
     __A       :		List[Any]							=	apply_spec_augment
     __A       :		str							=	mask_time_prob
     __A       :		Dict							=	mask_time_length
     __A       :		List[str]							=	mask_time_min_masks
     __A       :		Union[str, Any]							=	mask_feature_prob
     __A       :		Tuple							=	mask_feature_length
     __A       :		Any							=	mask_feature_min_masks

     # parameters for pretraining with codevector quantized representations
     __A       :		int							=	num_codevectors_per_group
     __A       :		Any							=	num_codevector_groups
     __A       :		Optional[Any]							=	contrastive_logits_temperature
     __A       :		Dict							=	feat_quantizer_dropout
     __A       :		Tuple							=	num_negatives
     __A       :		List[str]							=	codevector_dim
     __A       :		str							=	proj_codevector_dim
     __A       :		Union[str, Any]							=	diversity_loss_weight

     # ctc loss
     __A       :		Tuple							=	ctc_loss_reduction
     __A       :		Optional[int]							=	ctc_zero_infinity

     # pretraining loss
     __A       :		str							=	replace_prob







 @property
 def    UpperCamelCase__(       self    ):



     '''simple docstring'''




     return functools.reduce(operator.mul   ,		self.conv_stride   ,		1    )




