





import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
     from transformers import SpeechaTextFeatureExtractor

A   : List[Any]			=				random.Random()







def        __lowerCamelCase (							__a       :List[Any]   ,					__a       :int=1.0   ,					__a       :int=None   ,					__a       :List[str]=None   )		->    Optional[int]:






  """simple docstring"""

  if rng is None:
    A__  				=       global_rng

  A__  				=       []
  for batch_idx in range(shape[0]   ):
    values.append([]   )
    for _ in range(shape[1]   ):
      values[-1].append(rng.random() * scale   )

  return values


@require_torch
@require_torchaudio
class   A      (unittest.TestCase   ):
   '''simple docstring'''




   def __init__(      self       :   Union[str, Any]							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   List[str]=7							,     __lowerCAmelCase       :   Optional[Any]=4_00							,     __lowerCAmelCase       :   List[Any]=20_00							,     __lowerCAmelCase       :   Tuple=24							,     __lowerCAmelCase       :   Any=24							,     __lowerCAmelCase       :   Union[str, Any]=0.0							,     __lowerCAmelCase       :   Union[str, Any]=1_60_00							,     __lowerCAmelCase       :   Dict=True							,     __lowerCAmelCase       :   Dict=True							,     )     ->     int:







     """simple docstring"""






     A__  				=       parent
     A__  				=       batch_size
     A__  				=       min_seq_length
     A__  				=       max_seq_length
     A__  				=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
     A__  				=       feature_size
     A__  				=       num_mel_bins
     A__  				=       padding_value
     A__  				=       sampling_rate
     A__  				=       return_attention_mask
     A__  				=       do_normalize






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







     """simple docstring"""






     return {
         "feature_size": self.feature_size,
         "num_mel_bins": self.num_mel_bins,
         "padding_value": self.padding_value,
         "sampling_rate": self.sampling_rate,
         "return_attention_mask": self.return_attention_mask,
         "do_normalize": self.do_normalize,
     }







   def        a_	(      self       :   int							,     __lowerCAmelCase       :   str=False							,     __lowerCAmelCase       :   Optional[Any]=False       )     ->     Tuple:







     """simple docstring"""






     def _flatten(__lowerCAmelCase       :   List[str]       ):
       return list(itertools.chain(*__A       )       )

     if equal_length:
       A__  				=       [floats_list((self.max_seq_length, self.feature_size)       ) for _ in range(self.batch_size       )]
     else:
       # make sure that inputs increase in size
       A__  				=       [
           floats_list((x, self.feature_size)       )
           for x in range(self.min_seq_length							,     self.max_seq_length							,     self.seq_length_diff       )
       ]
     if numpify:
       A__  				=       [np.asarray(__A       ) for x in speech_inputs]
     return speech_inputs


@require_torch
@require_torchaudio
class   A      (SCREAMING_SNAKE_CASE       ,					unittest.TestCase   ):
   '''simple docstring'''




   __lowerCamelCase     :	str          =   SpeechaTextFeatureExtractor if is_speech_available() else None






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







     """simple docstring"""






     A__  				=       SpeechaTextFeatureExtractionTester(self       )






   def        a_	(      self       :   Dict							,     __lowerCAmelCase       :   Tuple       )     ->     List[str]:







     """simple docstring"""






     self.assertTrue(np.all(np.mean(__A							,     axis=0       ) < 1e-3       )       )
     self.assertTrue(np.all(np.abs(np.var(__A							,     axis=0       ) - 1       ) < 1e-3       )       )






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







     """simple docstring"""






     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     # create three inputs of length 800, 1000, and 1200
     A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]
     A__  				=       [np.asarray(__A       ) for speech_input in speech_inputs]

     # Test feature size
     A__  				=       feature_extractor(__A							,     padding=__A							,     return_tensors="""np"""       ).input_features
     self.assertTrue(input_features.ndim == 3       )
     self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size       )

     # Test not batched input
     A__  				=       feature_extractor(speech_inputs[0]							,     return_tensors="""np"""       ).input_features
     A__  				=       feature_extractor(np_speech_inputs[0]							,     return_tensors="""np"""       ).input_features
     self.assertTrue(np.allclose(__A							,     __A							,     atol=1e-3       )       )

     # Test batched
     A__  				=       feature_extractor(__A							,     return_tensors="""np"""       ).input_features
     A__  				=       feature_extractor(__A							,     return_tensors="""np"""       ).input_features
     for enc_seq_a, enc_seq_a in zip(__A							,     __A       ):
       self.assertTrue(np.allclose(__A							,     __A							,     atol=1e-3       )       )

     # Test 2-D numpy arrays are batched.
     A__  				=       [floats_list((1, x)       )[0] for x in (8_00, 8_00, 8_00)]
     A__  				=       np.asarray(__A       )
     A__  				=       feature_extractor(__A							,     return_tensors="""np"""       ).input_features
     A__  				=       feature_extractor(__A							,     return_tensors="""np"""       ).input_features
     for enc_seq_a, enc_seq_a in zip(__A							,     __A       ):
       self.assertTrue(np.allclose(__A							,     __A							,     atol=1e-3       )       )






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







     """simple docstring"""






     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]

     A__  				=       ["""longest""", """max_length""", """do_not_pad"""]
     A__  				=       [None, 16, None]
     for max_length, padding in zip(__A							,     __A       ):
       A__  				=       feature_extractor(
           __A							,     padding=__A							,     max_length=__A							,     return_attention_mask=__A       )
       A__  				=       inputs.input_features
       A__  				=       inputs.attention_mask
       A__  				=       [np.sum(__A       ) for x in attention_mask]

       self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]       )
       self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]       )
       self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]       )






   def        a_	(      self       :   str       )     ->     Dict:







     """simple docstring"""






     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]

     A__  				=       ["""longest""", """max_length""", """do_not_pad"""]
     A__  				=       [None, 16, None]
     for max_length, padding in zip(__A							,     __A       ):
       A__  				=       feature_extractor(
           __A							,     max_length=__A							,     padding=__A							,     return_tensors="""np"""							,     return_attention_mask=__A       )
       A__  				=       inputs.input_features
       A__  				=       inputs.attention_mask
       A__  				=       [np.sum(__A       ) for x in attention_mask]

       self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]       )
       self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1e-6       )
       self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]       )
       self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1e-6       )
       self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]       )






   def        a_	(      self       :   Dict       )     ->     int:







     """simple docstring"""






     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]
     A__  				=       feature_extractor(
         __A							,     padding="""max_length"""							,     max_length=4							,     truncation=__A							,     return_tensors="""np"""							,     return_attention_mask=__A							,     )
     A__  				=       inputs.input_features
     A__  				=       inputs.attention_mask
     A__  				=       np.sum(attention_mask == 1							,     axis=1       )

     self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]       )
     self._check_zero_mean_unit_variance(input_features[1]       )
     self._check_zero_mean_unit_variance(input_features[2]       )






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







     """simple docstring"""






     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]
     A__  				=       feature_extractor(
         __A							,     padding="""longest"""							,     max_length=4							,     truncation=__A							,     return_tensors="""np"""							,     return_attention_mask=__A							,     )
     A__  				=       inputs.input_features
     A__  				=       inputs.attention_mask
     A__  				=       np.sum(attention_mask == 1							,     axis=1       )

     self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]       )
     self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]       )
     self._check_zero_mean_unit_variance(input_features[2]       )

     # make sure that if max_length < longest -> then pad to max_length
     self.assertEqual(input_features.shape							,     (3, 4, 24)       )

     A__  				=       [floats_list((1, x)       )[0] for x in range(8_00							,     14_00							,     2_00       )]
     A__  				=       feature_extractor(
         __A							,     padding="""longest"""							,     max_length=16							,     truncation=__A							,     return_tensors="""np"""							,     return_attention_mask=__A							,     )
     A__  				=       inputs.input_features
     A__  				=       inputs.attention_mask
     A__  				=       np.sum(attention_mask == 1							,     axis=1       )

     self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]       )
     self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]       )
     self._check_zero_mean_unit_variance(input_features[2]       )

     # make sure that if max_length < longest -> then pad to max_length
     self.assertEqual(input_features.shape							,     (3, 6, 24)       )






   def        a_	(      self       :   Dict       )     ->     List[Any]:







     """simple docstring"""






     import torch

     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     A__  				=       np.random.rand(1_00							,     32       ).astype(np.floataa       )
     A__  				=       np_speech_inputs.tolist()

     for inputs in [py_speech_inputs, np_speech_inputs]:
       A__  				=       feature_extractor.pad([{"""input_features""": inputs}]							,     return_tensors="""np"""       )
       self.assertTrue(np_processed.input_features.dtype == np.floataa       )
       A__  				=       feature_extractor.pad([{"""input_features""": inputs}]							,     return_tensors="""pt"""       )
       self.assertTrue(pt_processed.input_features.dtype == torch.floataa       )






   def        a_	(      self       :   int							,     __lowerCAmelCase       :   Dict       )     ->     Union[str, Any]:







     """simple docstring"""






     from datasets import load_dataset

     A__  				=       load_dataset("""hf-internal-testing/librispeech_asr_dummy"""							,     """clean"""							,     split="""validation"""       )
     # automatic decoding with librispeech
     A__  				=       ds.sort("""id"""       ).select(range(__A       )       )[:num_samples]["""audio"""]

     return [x["array"] for x in speech_samples]







   def        a_	(      self       :   Optional[int]       )     ->     Dict:







     """simple docstring"""






     A__  				=       np.array([
         -1.5_7_4_5, -1.7_7_1_3, -1.7_0_2_0, -1.6_0_6_9, -1.2_2_5_0, -1.1_1_0_5, -0.9_0_7_2, -0.8_2_4_1,
         -1.2_3_1_0, -0.8_0_9_8, -0.3_3_2_0, -0.4_1_0_1, -0.7_9_8_5, -0.4_9_9_6, -0.8_2_1_3, -0.9_1_2_8,
         -1.0_4_2_0, -1.1_2_8_6, -1.0_4_4_0, -0.7_9_9_9, -0.8_4_0_5, -1.2_2_7_5, -1.5_4_4_3, -1.4_6_2_5,
     ]       )
     # fmt: on

     A__  				=       self._load_datasamples(1       )
     A__  				=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()       )
     A__  				=       feature_extractor(__A							,     return_tensors="""pt"""       ).input_features
     self.assertEquals(input_features.shape							,     (1, 5_84, 24)       )
     self.assertTrue(np.allclose(input_features[0, 0, :30]							,     __A							,     atol=1e-4       )       )










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


A   : Dict			=				logging.get_logger(__name__)

A   : Dict			=				{
    '''google/pegasus-large''': '''https://huggingface.co/google/pegasus-large/resolve/main/config.json''',
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}


class   A      (SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''




			__lowerCamelCase     :	Tuple          =   '''pegasus'''
			__lowerCamelCase     :	Any          =   ['''past_key_values''']
			__lowerCamelCase     :	List[str]          =   {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






			def __init__(      self       :   List[Any]							,     __lowerCAmelCase       :   str=5_02_65							,     __lowerCAmelCase       :   Dict=10_24							,     __lowerCAmelCase       :   Union[str, Any]=12							,     __lowerCAmelCase       :   Any=40_96							,     __lowerCAmelCase       :   Optional[Any]=16							,     __lowerCAmelCase       :   Dict=12							,     __lowerCAmelCase       :   Optional[int]=40_96							,     __lowerCAmelCase       :   Dict=16							,     __lowerCAmelCase       :   Optional[int]=0.0							,     __lowerCAmelCase       :   Optional[Any]=0.0							,     __lowerCAmelCase       :   Union[str, Any]=True							,     __lowerCAmelCase       :   List[str]=True							,     __lowerCAmelCase       :   str="gelu"							,     __lowerCAmelCase       :   Union[str, Any]=10_24							,     __lowerCAmelCase       :   Union[str, Any]=0.1							,     __lowerCAmelCase       :   Dict=0.0							,     __lowerCAmelCase       :   Union[str, Any]=0.0							,     __lowerCAmelCase       :   Union[str, Any]=0.0_2							,     __lowerCAmelCase       :   List[Any]=0							,     __lowerCAmelCase       :   str=False							,     __lowerCAmelCase       :   int=0							,     __lowerCAmelCase       :   Optional[Any]=1							,     __lowerCAmelCase       :   Optional[int]=1							,     **__lowerCAmelCase       :   Dict							,     )     ->     Dict:







					"""simple docstring"""






					A__  				=       vocab_size
					A__  				=       max_position_embeddings
					A__  				=       d_model
					A__  				=       encoder_ffn_dim
					A__  				=       encoder_layers
					A__  				=       encoder_attention_heads
					A__  				=       decoder_ffn_dim
					A__  				=       decoder_layers
					A__  				=       decoder_attention_heads
					A__  				=       dropout
					A__  				=       attention_dropout
					A__  				=       activation_dropout
					A__  				=       activation_function
					A__  				=       init_std
					A__  				=       encoder_layerdrop
					A__  				=       decoder_layerdrop
					A__  				=       use_cache
					A__  				=       encoder_layers
					A__  				=       scale_embedding  # scale factor will be sqrt(d_model) if True
					super().__init__(
					    pad_token_id=__lowerCAmelCase							,     eos_token_id=__lowerCAmelCase							,     is_encoder_decoder=__lowerCAmelCase							,     decoder_start_token_id=__lowerCAmelCase							,     forced_eos_token_id=__lowerCAmelCase							,     **__lowerCAmelCase							,     )






			@property
			def        a_	(      self       :   Dict       )     ->     int:







					"""simple docstring"""






					return self.encoder_attention_heads







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







					"""simple docstring"""






					return self.d_model



