
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


UpperCamelCase     :    Optional[Any]       =			random.Random()

if is_torch_available():
      import torch






def 							A						(    snake_case				:Dict    , snake_case				:List[str]=1.0    , snake_case				:List[str]=None    , snake_case				:str=None		)				->     Any:



 if rng is None:
  __UpperCamelCase      =	global_rng

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

 return values

class __lowerCAmelCase		(				unittest.TestCase      ):





     def __init__(     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=7     ,					__UpperCAmelCase=400     ,					__UpperCAmelCase=2000     ,					__UpperCAmelCase=1     ,					__UpperCAmelCase=0.0     ,					__UpperCAmelCase=1_6000     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=True     ,					):

      '''simple docstring'''




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





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      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 						UpperCAmelCase (     self     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=False				):

      '''simple docstring'''




      def _flatten(__UpperCAmelCase				):
       return list(itertools.chain(*_lowerCamelCase				)				)

      if equal_length:
       __UpperCamelCase      =	floats_list((self.batch_size, self.max_seq_length)				)
      else:
       # make sure that inputs increase in size
       __UpperCamelCase      =	[
           _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:
       __UpperCamelCase      =	[np.asarray(_lowerCamelCase				) for x in speech_inputs]

      return speech_inputs


@require_torch
@require_torchaudio
class __lowerCAmelCase		(				a__      ,  unittest.TestCase      ):
     lowercase						       =						ASTFeatureExtractor





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	ASTFeatureExtractionTester(self				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
      # create three inputs of length 800, 1000, and 1200
      __UpperCamelCase      =	[floats_list((1, x)				)[0] for x in range(800     ,					1400     ,					200				)]
      __UpperCamelCase      =	[np.asarray(_lowerCamelCase				) for speech_input in speech_inputs]

      # Test not batched input
      __UpperCamelCase      =	feat_extract(speech_inputs[0]     ,					return_tensors='np'				).input_values
      __UpperCamelCase      =	feat_extract(np_speech_inputs[0]     ,					return_tensors='np'				).input_values
      self.assertTrue(np.allclose(_lowerCamelCase     ,					_lowerCamelCase     ,					atol=1E-3				)				)

      # Test batched
      __UpperCamelCase      =	feat_extract(_lowerCamelCase     ,					padding=_lowerCamelCase     ,					return_tensors='np'				).input_values
      __UpperCamelCase      =	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.
      __UpperCamelCase      =	[floats_list((1, x)				)[0] for x in (800, 800, 800)]
      __UpperCamelCase      =	np.asarray(_lowerCamelCase				)
      __UpperCamelCase      =	feat_extract(_lowerCamelCase     ,					return_tensors='np'				).input_values
      __UpperCamelCase      =	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 						UpperCAmelCase (     self				):

      '''simple docstring'''




      import torch

      __UpperCamelCase      =	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
      __UpperCamelCase      =	np.random.rand(100				).astype(np.floataa				)
      __UpperCamelCase      =	np_speech_inputs.tolist()

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





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      from datasets import load_dataset

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

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







     @require_torch
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	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

      __UpperCamelCase      =	self._load_datasamples(1				)
      __UpperCamelCase      =	ASTFeatureExtractor()
      __UpperCamelCase      =	feature_extractor(_lowerCamelCase     ,					return_tensors='pt'				).input_values
      self.assertEquals(input_values.shape     ,					(1, 1024, 128)				)
      self.assertTrue(torch.allclose(input_values[0, 0, :30]     ,					_lowerCamelCase     ,					atol=1E-4				)				)





"""simple docstring"""

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase     :    Union[str, Any]       =			logging.get_logger(__name__)

UpperCamelCase     :    int       =			{
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}

class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ):
     lowercase						       =						"distilbert"
     lowercase						       =						{
         "hidden_size": "dim",
         "num_attention_heads": "n_heads",
         "num_hidden_layers": "n_layers",
     }





     def __init__(     self     ,					__UpperCAmelCase=3_0522     ,					__UpperCAmelCase=512     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=6     ,					__UpperCAmelCase=12     ,					__UpperCAmelCase=768     ,					__UpperCAmelCase=4 * 768     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase="gelu"     ,					__UpperCAmelCase=0.0_2     ,					__UpperCAmelCase=0.1     ,					__UpperCAmelCase=0.2     ,					__UpperCAmelCase=0     ,					**__UpperCAmelCase     ,					):

      '''simple docstring'''




      __UpperCamelCase      =	vocab_size
      __UpperCamelCase      =	max_position_embeddings
      __UpperCamelCase      =	sinusoidal_pos_embds
      __UpperCamelCase      =	n_layers
      __UpperCamelCase      =	n_heads
      __UpperCamelCase      =	dim
      __UpperCamelCase      =	hidden_dim
      __UpperCamelCase      =	dropout
      __UpperCamelCase      =	attention_dropout
      __UpperCamelCase      =	activation
      __UpperCamelCase      =	initializer_range
      __UpperCamelCase      =	qa_dropout
      __UpperCamelCase      =	seq_classif_dropout
      super().__init__(**__UpperCAmelCase     ,					pad_token_id=__UpperCAmelCase				)


class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ):





     @property
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      if self.task == "multiple-choice":
       __UpperCamelCase      =	{0: 'batch', 1: 'choice', 2: 'sequence'}
      else:
       __UpperCamelCase      =	{0: 'batch', 1: 'sequence'}
      return OrderedDict(
          [
              ('input_ids', dynamic_axis),
              ('attention_mask', dynamic_axis),
          ]				)



