import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


a  :      str            =    random.Random()

if is_torch_available():
 import torch
def 		snake_case__	(		lowercase							,				lowercase=1.0							,				lowercase=None							,				lowercase=None					):
   if rng is None:
      lowerCAmelCase_:							Dict       =	global_rng

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

   return values

class 					_lowercase    (     unittest.TestCase			):






    '''simple docstring'''




    def __init__(    self       ,						lowerCamelCase__       ,						lowerCamelCase__=7       ,						lowerCamelCase__=400       ,						lowerCamelCase__=2_000       ,						lowerCamelCase__=1       ,						lowerCamelCase__=0.0       ,						lowerCamelCase__=16_000       ,						lowerCamelCase__=True       ,						lowerCamelCase__=True       ,						):
       lowerCAmelCase_:							List[str]       =	parent
       lowerCAmelCase_:							List[str]       =	batch_size
       lowerCAmelCase_:							Optional[int]       =	min_seq_length
       lowerCAmelCase_:							Optional[int]       =	max_seq_length
       lowerCAmelCase_:							Any       =	(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       lowerCAmelCase_:							int       =	feature_size
       lowerCAmelCase_:							List[str]       =	padding_value
       lowerCAmelCase_:							Union[str, Any]       =	sampling_rate
       lowerCAmelCase_:							Optional[Any]       =	return_attention_mask
       lowerCAmelCase_:							Tuple       =	do_normalize




    def      _a    (    self ):
       return {
           "feature_size": self.feature_size,
           "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       ,						lowerCamelCase__=False       ,						lowerCamelCase__=False ):
       def _flatten(lowerCamelCase__ ):
          return list(itertools.chain(*lowerCamelCase__ ) )

       if equal_length:
          lowerCAmelCase_:							int       =	floats_list((self.batch_size, self.max_seq_length) )
       else:
          # make sure that inputs increase in size
          lowerCAmelCase_:							Optional[Any]       =	[
              _flatten(floats_list((x, self.feature_size) ) )
              for x in range(self.min_seq_length       ,						self.max_seq_length       ,						self.seq_length_diff )
          ]

       if numpify:
          lowerCAmelCase_:							Any       =	[np.asarray(lowerCamelCase__ ) for x in speech_inputs]

       return speech_inputs





@require_torch
@require_torchaudio
class 					_lowercase    (     UpperCAmelCase__ ,  unittest.TestCase			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				Optional[Any]          =    ASTFeatureExtractor




    def      _a    (    self ):
       lowerCAmelCase_:							List[str]       =	ASTFeatureExtractionTester(self )




    def      _a    (    self ):
       # Tests that all call wrap to encode_plus and batch_encode_plus
       lowerCAmelCase_:							List[str]       =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
       # create three inputs of length 800, 1000, and 1200
       lowerCAmelCase_:							Optional[int]       =	[floats_list((1, x) )[0] for x in range(800       ,						1_400       ,						200 )]
       lowerCAmelCase_:							Tuple       =	[np.asarray(lowerCamelCase__ ) for speech_input in speech_inputs]

       # Test not batched input
       lowerCAmelCase_:							Dict       =	feat_extract(speech_inputs[0]       ,						return_tensors="np" ).input_values
       lowerCAmelCase_:							List[Any]       =	feat_extract(np_speech_inputs[0]       ,						return_tensors="np" ).input_values
       self.assertTrue(np.allclose(lowerCamelCase__       ,						lowerCamelCase__       ,						atol=1E-3 ) )

       # Test batched
       lowerCAmelCase_:							str       =	feat_extract(lowerCamelCase__       ,						padding=lowerCamelCase__       ,						return_tensors="np" ).input_values
       lowerCAmelCase_:							Dict       =	feat_extract(lowerCamelCase__       ,						padding=lowerCamelCase__       ,						return_tensors="np" ).input_values
       for enc_seq_a, enc_seq_a in zip(lowerCamelCase__       ,						lowerCamelCase__ ):
          self.assertTrue(np.allclose(lowerCamelCase__       ,						lowerCamelCase__       ,						atol=1E-3 ) )

       # Test 2-D numpy arrays are batched.
       lowerCAmelCase_:							Optional[int]       =	[floats_list((1, x) )[0] for x in (800, 800, 800)]
       lowerCAmelCase_:							Tuple       =	np.asarray(lowerCamelCase__ )
       lowerCAmelCase_:							Any       =	feat_extract(lowerCamelCase__       ,						return_tensors="np" ).input_values
       lowerCAmelCase_:							List[str]       =	feat_extract(lowerCamelCase__       ,						return_tensors="np" ).input_values
       for enc_seq_a, enc_seq_a in zip(lowerCamelCase__       ,						lowerCamelCase__ ):
          self.assertTrue(np.allclose(lowerCamelCase__       ,						lowerCamelCase__       ,						atol=1E-3 ) )




    @require_torch
    def      _a    (    self ):
       import torch

       lowerCAmelCase_:							Union[str, Any]       =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
       lowerCAmelCase_:							List[Any]       =	np.random.rand(100 ).astype(np.floataa )
       lowerCAmelCase_:							Tuple       =	np_speech_inputs.tolist()

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




    def      _a    (    self       ,						lowerCamelCase__ ):
       from datasets import load_dataset

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

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




    @require_torch
    def      _a    (    self ):
       # fmt: off
       lowerCAmelCase_:							int       =	torch.tensor(
           [-0.9_8_9_4, -1.2_7_7_6, -0.9_0_6_6, -1.2_7_7_6, -0.9_3_4_9, -1.2_6_0_9, -1.0_3_8_6, -1.2_7_7_6,
            -1.1_5_6_1, -1.2_7_7_6, -1.2_0_5_2, -1.2_7_2_3, -1.2_1_9_0, -1.2_1_3_2, -1.2_7_7_6, -1.1_1_3_3,
            -1.1_9_5_3, -1.1_3_4_3, -1.1_5_8_4, -1.2_2_0_3, -1.1_7_7_0, -1.2_4_7_4, -1.2_3_8_1, -1.1_9_3_6,
            -0.9_2_7_0, -0.8_3_1_7, -0.8_0_4_9, -0.7_7_0_6, -0.7_5_6_5, -0.7_8_6_9] )
       # fmt: on

       lowerCAmelCase_:							int       =	self._load_datasamples(1 )
       lowerCAmelCase_:							Tuple       =	ASTFeatureExtractor()
       lowerCAmelCase_:							str       =	feature_extractor(lowerCamelCase__       ,						return_tensors="pt" ).input_values
       self.assertEquals(input_values.shape       ,						(1, 1_024, 128) )
       self.assertTrue(torch.allclose(input_values[0, 0, :30]       ,						lowerCamelCase__       ,						atol=1E-4 ) )
from ...configuration_utils import PretrainedConfig
from ...utils import logging


a  :      List[Any]            =    logging.get_logger(__name__)

a  :      Any            =    {"""openai-gpt""": """https://huggingface.co/openai-gpt/resolve/main/config.json"""}

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''
    SCREAMING_SNAKE_CASE:				int          =    'openai-gpt'
    SCREAMING_SNAKE_CASE:				List[str]          =    {
        'max_position_embeddings': 'n_positions',
        'hidden_size': 'n_embd',
        'num_attention_heads': 'n_head',
        'num_hidden_layers': 'n_layer',
    }




    def __init__(    self       ,						lowerCamelCase__=40_478       ,						lowerCamelCase__=512       ,						lowerCamelCase__=768       ,						lowerCamelCase__=12       ,						lowerCamelCase__=12       ,						lowerCamelCase__="gelu"       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=0.1       ,						lowerCamelCase__=1E-5       ,						lowerCamelCase__=0.0_2       ,						lowerCamelCase__="cls_index"       ,						lowerCamelCase__=True       ,						lowerCamelCase__=None       ,						lowerCamelCase__=True       ,						lowerCamelCase__=0.1       ,						**lowerCamelCase__       ,						):
       lowerCAmelCase_:							Union[str, Any]       =	vocab_size
       lowerCAmelCase_:							List[Any]       =	n_positions
       lowerCAmelCase_:							Tuple       =	n_embd
       lowerCAmelCase_:							Optional[int]       =	n_layer
       lowerCAmelCase_:							Optional[int]       =	n_head
       lowerCAmelCase_:							int       =	afn
       lowerCAmelCase_:							str       =	resid_pdrop
       lowerCAmelCase_:							Optional[int]       =	embd_pdrop
       lowerCAmelCase_:							Optional[int]       =	attn_pdrop
       lowerCAmelCase_:							Dict       =	layer_norm_epsilon
       lowerCAmelCase_:							List[Any]       =	initializer_range
       lowerCAmelCase_:							Union[str, Any]       =	summary_type
       lowerCAmelCase_:							Any       =	summary_use_proj
       lowerCAmelCase_:							Dict       =	summary_activation
       lowerCAmelCase_:							Dict       =	summary_first_dropout
       lowerCAmelCase_:							List[Any]       =	summary_proj_to_labels
       super().__init__(**lowerCamelCase__ )