





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

__lowerCamelCase						: Union[str, Any]		=   random.Random()


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE=1.0  ,      __SCREAMING_SNAKE_CASE=None  ,      __SCREAMING_SNAKE_CASE=None   ):


  """simple docstring"""

  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







@require_torch
@require_torchaudio
class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  def __init__(				self    :					Optional[int]							,	UpperCamelCase__    :					Union[str, Any]							,	UpperCamelCase__    :					List[str]=7							,	UpperCamelCase__    :					List[Any]=400							,	UpperCamelCase__    :					List[str]=2000							,	UpperCamelCase__    :					str=24							,	UpperCamelCase__    :					Optional[Any]=24							,	UpperCamelCase__    :					str=0.0							,	UpperCamelCase__    :					Dict=1_6000							,	UpperCamelCase__    :					Dict=True							,	UpperCamelCase__    :					str=True							,	)     ->						Dict:
    _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       			=num_mel_bins
    _UpperCamelCase       			=padding_value
    _UpperCamelCase       			=sampling_rate
    _UpperCamelCase       			=return_attention_mask
    _UpperCamelCase       			=do_normalize


  def   UpperCamelCase__      (				self    :					int  )     ->						Tuple:
    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   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Any=False							,	UpperCamelCase__    :					Dict=False  )     ->						Any:
    def _flatten(UpperCamelCase__    :					Optional[Any]  ):
      return list(itertools.chain(*UpperCamelCase__  )  )

    if equal_length:
      _UpperCamelCase       			=[floats_list((self.max_seq_length, self.feature_size)  ) for _ in range(self.batch_size  )]
    else:
      # make sure that inputs increase in size
      _UpperCamelCase       			=[
          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 		UpperCAmelCase   (				lowercase_   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				SpeechaTextFeatureExtractor if is_speech_available() else None


  def   UpperCamelCase__      (				self    :					Tuple  )     ->						str:
    _UpperCamelCase       			=SpeechaTextFeatureExtractionTester(self  )


  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Any  )     ->						int:
    self.assertTrue(np.all(np.mean(UpperCamelCase__							,	axis=0  ) < 1E-3  )  )
    self.assertTrue(np.all(np.abs(np.var(UpperCamelCase__							,	axis=0  ) - 1  ) < 1E-3  )  )


  def   UpperCamelCase__      (				self    :					Dict  )     ->						Dict:
    # Tests that all call wrap to encode_plus and batch_encode_plus
    _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 feature size
    _UpperCamelCase       			=feature_extractor(UpperCamelCase__							,	padding=UpperCamelCase__							,	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
    _UpperCamelCase       			=feature_extractor(speech_inputs[0]							,	return_tensors='''np'''  ).input_features
    _UpperCamelCase       			=feature_extractor(np_speech_inputs[0]							,	return_tensors='''np'''  ).input_features
    self.assertTrue(np.allclose(UpperCamelCase__							,	UpperCamelCase__							,	atol=1E-3  )  )

    # Test batched
    _UpperCamelCase       			=feature_extractor(UpperCamelCase__							,	return_tensors='''np'''  ).input_features
    _UpperCamelCase       			=feature_extractor(UpperCamelCase__							,	return_tensors='''np'''  ).input_features
    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       			=feature_extractor(UpperCamelCase__							,	return_tensors='''np'''  ).input_features
    _UpperCamelCase       			=feature_extractor(UpperCamelCase__							,	return_tensors='''np'''  ).input_features
    for enc_seq_a, enc_seq_a in zip(UpperCamelCase__							,	UpperCamelCase__  ):
      self.assertTrue(np.allclose(UpperCamelCase__							,	UpperCamelCase__							,	atol=1E-3  )  )


  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Tuple:
    _UpperCamelCase       			=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
    _UpperCamelCase       			=[floats_list((1, x)  )[0] for x in range(800							,	1400							,	200  )]

    _UpperCamelCase       			=['''longest''', '''max_length''', '''do_not_pad''']
    _UpperCamelCase       			=[None, 16, None]
    for max_length, padding in zip(UpperCamelCase__							,	UpperCamelCase__  ):
      _UpperCamelCase       			=feature_extractor(
          UpperCamelCase__							,	padding=UpperCamelCase__							,	max_length=UpperCamelCase__							,	return_attention_mask=UpperCamelCase__  )
      _UpperCamelCase       			=inputs.input_features
      _UpperCamelCase       			=inputs.attention_mask
      _UpperCamelCase       			=[np.sum(UpperCamelCase__  ) 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   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Union[str, Any]:
    _UpperCamelCase       			=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
    _UpperCamelCase       			=[floats_list((1, x)  )[0] for x in range(800							,	1400							,	200  )]

    _UpperCamelCase       			=['''longest''', '''max_length''', '''do_not_pad''']
    _UpperCamelCase       			=[None, 16, None]
    for max_length, padding in zip(UpperCamelCase__							,	UpperCamelCase__  ):
      _UpperCamelCase       			=feature_extractor(
          UpperCamelCase__							,	max_length=UpperCamelCase__							,	padding=UpperCamelCase__							,	return_tensors='''np'''							,	return_attention_mask=UpperCamelCase__  )
      _UpperCamelCase       			=inputs.input_features
      _UpperCamelCase       			=inputs.attention_mask
      _UpperCamelCase       			=[np.sum(UpperCamelCase__  ) 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   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						str:
    _UpperCamelCase       			=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
    _UpperCamelCase       			=[floats_list((1, x)  )[0] for x in range(800							,	1400							,	200  )]
    _UpperCamelCase       			=feature_extractor(
        UpperCamelCase__							,	padding='''max_length'''							,	max_length=4							,	truncation=UpperCamelCase__							,	return_tensors='''np'''							,	return_attention_mask=UpperCamelCase__							,	)
    _UpperCamelCase       			=inputs.input_features
    _UpperCamelCase       			=inputs.attention_mask
    _UpperCamelCase       			=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   UpperCamelCase__      (				self    :					Dict  )     ->						Dict:
    _UpperCamelCase       			=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
    _UpperCamelCase       			=[floats_list((1, x)  )[0] for x in range(800							,	1400							,	200  )]
    _UpperCamelCase       			=feature_extractor(
        UpperCamelCase__							,	padding='''longest'''							,	max_length=4							,	truncation=UpperCamelCase__							,	return_tensors='''np'''							,	return_attention_mask=UpperCamelCase__							,	)
    _UpperCamelCase       			=inputs.input_features
    _UpperCamelCase       			=inputs.attention_mask
    _UpperCamelCase       			=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)  )

    _UpperCamelCase       			=[floats_list((1, x)  )[0] for x in range(800							,	1400							,	200  )]
    _UpperCamelCase       			=feature_extractor(
        UpperCamelCase__							,	padding='''longest'''							,	max_length=16							,	truncation=UpperCamelCase__							,	return_tensors='''np'''							,	return_attention_mask=UpperCamelCase__							,	)
    _UpperCamelCase       			=inputs.input_features
    _UpperCamelCase       			=inputs.attention_mask
    _UpperCamelCase       			=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   UpperCamelCase__      (				self    :					List[Any]  )     ->						str:
    import torch

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

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


  def   UpperCamelCase__      (				self    :					List[Any]							,	UpperCamelCase__    :					Optional[Any]  )     ->						List[Any]:
    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]




  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Tuple:
    # fmt: off
    _UpperCamelCase       			=np.array([
        -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
        -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
        -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
    ]  )
    # fmt: on

    _UpperCamelCase       			=self._load_datasamples(1  )
    _UpperCamelCase       			=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()  )
    _UpperCamelCase       			=feature_extractor(UpperCamelCase__							,	return_tensors='''pt'''  ).input_features
    self.assertEquals(input_features.shape							,	(1, 584, 24)  )
    self.assertTrue(np.allclose(input_features[0, 0, :30]							,	UpperCamelCase__							,	atol=1E-4  )  )













'''simple docstring'''






from collections.abc import Generator
from math import sin


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if len(__SCREAMING_SNAKE_CASE   ) != 32:
    raise ValueError('''Input must be of length 32'''   )

  _UpperCamelCase       			=b''''''
  for i in [3, 2, 1, 0]:
    little_endian += string_aa[8 * i : 8 * i + 8]
  return little_endian


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if i < 0:
    raise ValueError('''Input must be non-negative'''   )

  _UpperCamelCase       			=format(__SCREAMING_SNAKE_CASE  ,      '''08x'''   )[-8:]
  _UpperCamelCase       			=b''''''
  for i in [3, 2, 1, 0]:
    little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode('''utf-8'''   )
  return little_endian_hex


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=b''''''
  for char in message:
    bit_string += format(__SCREAMING_SNAKE_CASE  ,      '''08b'''   ).encode('''utf-8'''   )
  _UpperCamelCase       			=format(len(__SCREAMING_SNAKE_CASE   )  ,      '''064b'''   ).encode('''utf-8'''   )

  # Pad bit_string to a multiple of 512 chars
  bit_string += b"1"
  while len(__SCREAMING_SNAKE_CASE   ) % 512 != 448:
    bit_string += b"0"
  bit_string += to_little_endian(start_len[32:]   ) + to_little_endian(start_len[:32]   )

  return bit_string


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if len(__SCREAMING_SNAKE_CASE   ) % 512 != 0:
    raise ValueError('''Input must have length that\'s a multiple of 512'''   )

  for pos in range(0  ,      len(__SCREAMING_SNAKE_CASE   )  ,      512   ):
    _UpperCamelCase       			=bit_string[pos : pos + 512]
    _UpperCamelCase       			=[]
    for i in range(0  ,      512  ,      32   ):
      block_words.append(int(to_little_endian(block[i : i + 32]   )  ,      2   )   )
    yield block_words


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if i < 0:
    raise ValueError('''Input must be non-negative'''   )

  _UpperCamelCase       			=format(__SCREAMING_SNAKE_CASE  ,      '''032b'''   )
  _UpperCamelCase       			=''''''
  for c in i_str:
    new_str += "1" if c == "0" else "0"
  return int(__SCREAMING_SNAKE_CASE  ,      2   )


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  return (a + b) % 2**32


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if i < 0:
    raise ValueError('''Input must be non-negative'''   )
  if shift < 0:
    raise ValueError('''Shift must be non-negative'''   )
  return ((i << shift) ^ (i >> (32 - shift))) % 2**32


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=preprocess(__SCREAMING_SNAKE_CASE   )

  _UpperCamelCase       			=[int(2**32 * abs(sin(i + 1   )   )   ) for i in range(64   )]

  # Starting states
  _UpperCamelCase       			=0X6745_2301
  _UpperCamelCase       			=0Xefcd_ab89
  _UpperCamelCase       			=0X98ba_dcfe
  _UpperCamelCase       			=0X1032_5476

  _UpperCamelCase       			=[
      7,
      12,
      17,
      22,
      7,
      12,
      17,
      22,
      7,
      12,
      17,
      22,
      7,
      12,
      17,
      22,
      5,
      9,
      14,
      20,
      5,
      9,
      14,
      20,
      5,
      9,
      14,
      20,
      5,
      9,
      14,
      20,
      4,
      11,
      16,
      23,
      4,
      11,
      16,
      23,
      4,
      11,
      16,
      23,
      4,
      11,
      16,
      23,
      6,
      10,
      15,
      21,
      6,
      10,
      15,
      21,
      6,
      10,
      15,
      21,
      6,
      10,
      15,
      21,
  ]

  # Process bit string in chunks, each with 16 32-char words
  for block_words in get_block_words(__SCREAMING_SNAKE_CASE   ):
    _UpperCamelCase       			=aa
    _UpperCamelCase       			=ba
    _UpperCamelCase       			=ca
    _UpperCamelCase       			=da

    # Hash current chunk
    for i in range(64   ):
      if i <= 15:
        # f = (b & c) | (not_32(b) & d)     # Alternate definition for f
        _UpperCamelCase       			=d ^ (b & (c ^ d))
        _UpperCamelCase       			=i
      elif i <= 31:
        # f = (d & b) | (not_32(d) & c)     # Alternate definition for f
        _UpperCamelCase       			=c ^ (d & (b ^ c))
        _UpperCamelCase       			=(5 * i + 1) % 16
      elif i <= 47:
        _UpperCamelCase       			=b ^ c ^ d
        _UpperCamelCase       			=(3 * i + 5) % 16
      else:
        _UpperCamelCase       			=c ^ (b | not_aa(__SCREAMING_SNAKE_CASE   ))
        _UpperCamelCase       			=(7 * i) % 16
      _UpperCamelCase       			=(f + a + added_consts[i] + block_words[g]) % 2**32
      _UpperCamelCase       			=d
      _UpperCamelCase       			=c
      _UpperCamelCase       			=b
      _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      left_rotate_aa(__SCREAMING_SNAKE_CASE  ,      shift_amounts[i]   )   )

    # Add hashed chunk to running total
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=sum_aa(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )

  _UpperCamelCase       			=reformat_hex(__SCREAMING_SNAKE_CASE   ) + reformat_hex(__SCREAMING_SNAKE_CASE   ) + reformat_hex(__SCREAMING_SNAKE_CASE   ) + reformat_hex(__SCREAMING_SNAKE_CASE   )
  return digest


if __name__ == "__main__":
      import doctest

      doctest.testmod()






