
"""simple docstring"""

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			:       Dict									=				random.Random()

if is_torch_available():
      import torch






def 							A		(       snake_case   :Dict       ,      snake_case   :Dict=1.0       ,      snake_case   :Union[str, Any]=None       ,      snake_case   :int=None	)   ->			Optional[int]:
 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(*__UpperCAmelCase   )   )

      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(__UpperCAmelCase   ) for x in speech_inputs]

      return speech_inputs


@require_torch
@require_torchaudio
class __lowerCAmelCase	(   __SCREAMING_SNAKE_CASE    ,						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(__UpperCAmelCase   ) 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(__UpperCAmelCase			,  __UpperCAmelCase			,  atol=1E-3   )   )

      # Test batched
      __UpperCamelCase       						= feat_extract(__UpperCAmelCase			,  padding=__UpperCAmelCase			,  return_tensors='np'   ).input_values
      __UpperCamelCase       						= feat_extract(__UpperCAmelCase			,  padding=__UpperCAmelCase			,  return_tensors='np'   ).input_values
      for enc_seq_a, enc_seq_a in zip(__UpperCAmelCase			,  __UpperCAmelCase   ):
       self.assertTrue(np.allclose(__UpperCAmelCase			,  __UpperCAmelCase			,  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(__UpperCAmelCase   )
      __UpperCamelCase       						= feat_extract(__UpperCAmelCase			,  return_tensors='np'   ).input_values
      __UpperCamelCase       						= feat_extract(__UpperCAmelCase			,  return_tensors='np'   ).input_values
      for enc_seq_a, enc_seq_a in zip(__UpperCAmelCase			,  __UpperCAmelCase   ):
       self.assertTrue(np.allclose(__UpperCAmelCase			,  __UpperCAmelCase			,  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(__UpperCAmelCase   )   )[: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(__UpperCAmelCase			,  return_tensors='pt'   ).input_values
      self.assertEquals(input_values.shape			,  (1, 1024, 128)   )
      self.assertTrue(torch.allclose(input_values[0, 0, :30]			,  __UpperCAmelCase			,  atol=1E-4   )   )





"""simple docstring"""

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCamelCase			:       int									=				logging.get_logger(__name__)

UpperCamelCase			:       int									=				{
    "google/bit-50": "https://huggingface.co/google/bit-50/resolve/main/config.json",
}

class __lowerCAmelCase	(   __SCREAMING_SNAKE_CASE    ,						__SCREAMING_SNAKE_CASE    ):
     lowercase		     =		"bit"
     lowercase		     =		["preactivation", "bottleneck"]
     lowercase		     =		["SAME", "VALID"]





     def __init__(			self			,  __UpperCAmelCase=3			,  __UpperCAmelCase=64			,  __UpperCAmelCase=[256, 512, 1024, 2048]			,  __UpperCAmelCase=[3, 4, 6, 3]			,  __UpperCAmelCase="preactivation"			,  __UpperCAmelCase="relu"			,  __UpperCAmelCase=None			,  __UpperCAmelCase=32			,  __UpperCAmelCase=0.0			,  __UpperCAmelCase=False			,  __UpperCAmelCase=32			,  __UpperCAmelCase=1			,  __UpperCAmelCase=None			,  __UpperCAmelCase=None			,  **__UpperCAmelCase			,  ):

      '''simple docstring'''




      super().__init__(**__UpperCAmelCase   )
      if layer_type not in self.layer_types:
       raise ValueError(F'layer_type={layer_type} is not one of {",".join(self.layer_types   )}'   )
      if global_padding is not None:
       if global_padding.upper() in self.supported_padding:
        __UpperCamelCase       						= global_padding.upper()
       else:
        raise ValueError(F'Padding strategy {global_padding} not supported'   )
      __UpperCamelCase       						= num_channels
      __UpperCamelCase       						= embedding_size
      __UpperCamelCase       						= hidden_sizes
      __UpperCamelCase       						= depths
      __UpperCamelCase       						= layer_type
      __UpperCamelCase       						= hidden_act
      __UpperCamelCase       						= global_padding
      __UpperCamelCase       						= num_groups
      __UpperCamelCase       						= drop_path_rate
      __UpperCamelCase       						= embedding_dynamic_padding
      __UpperCamelCase       						= output_stride
      __UpperCamelCase       						= width_factor

      __UpperCamelCase       						= ['stem'] + [F'stage{idx}' for idx in range(1			,  len(__UpperCAmelCase   ) + 1   )]
      __UpperCamelCase						,			__UpperCamelCase       						= get_aligned_output_features_output_indices(
          out_features=__UpperCAmelCase			,  out_indices=__UpperCAmelCase			,  stage_names=self.stage_names   )



