



"""simple docstring"""





import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
     from transformers import WhisperFeatureExtractor

if is_torch_available():
     import torch

lowercase  							=					random.Random()




def 	UpperCAmelCase     (		A	:						int     ,				A	:						Tuple=1.0     ,				A	:						Optional[Any]=None     ,				A	:						List[Any]=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 			lowercase__			(      unittest.TestCase    ):
       '''simple docstring'''







       def __init__(  self	,       snake_case	,       snake_case=7	,       snake_case=400	,       snake_case=2000	,       snake_case=10	,       snake_case=160	,       snake_case=8	,       snake_case=0.0	,       snake_case=4000	,       snake_case=False	,       snake_case=True	,       )			->							Tuple:
         _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							 =		padding_value
         _UpperCAmelCase							 =		sampling_rate
         _UpperCAmelCase							 =		return_attention_mask
         _UpperCAmelCase							 =		do_normalize
         _UpperCAmelCase							 =		feature_size
         _UpperCAmelCase							 =		chunk_length
         _UpperCAmelCase							 =		hop_length






       def        lowerCamelCase_   (  self						)			->							str:
         return {
             "feature_size": self.feature_size,
             "hop_length": self.hop_length,
             "chunk_length": self.chunk_length,
             "padding_value": self.padding_value,
             "sampling_rate": self.sampling_rate,
             "return_attention_mask": self.return_attention_mask,
             "do_normalize": self.do_normalize,
         }






       def        lowerCamelCase_   (  self	,       snake_case=False	,       snake_case=False						)			->							Optional[Any]:
         def _flatten(snake_case						):
           return list(itertools.chain(*snake_case						)						)

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







@require_torch
@require_torchaudio
class 			lowercase__			(      UpperCamelCase_,					unittest.TestCase    ):
       '''simple docstring'''







       _UpperCAmelCase      				=					WhisperFeatureExtractor if is_speech_available() else None






       def        lowerCamelCase_   (  self						)			->							Dict:
         _UpperCAmelCase							 =		WhisperFeatureExtractionTester(self						)






       def        lowerCamelCase_   (  self						)			->							int:
         _UpperCAmelCase							 =		self.feature_extraction_class(**self.feat_extract_dict						)

         with tempfile.TemporaryDirectory() as tmpdirname:
           _UpperCAmelCase							 =		feat_extract_first.save_pretrained(snake_case						)[0]
           check_json_file_has_correct_format(snake_case						)
           _UpperCAmelCase							 =		self.feature_extraction_class.from_pretrained(snake_case						)

         _UpperCAmelCase							 =		feat_extract_first.to_dict()
         _UpperCAmelCase							 =		feat_extract_second.to_dict()
         _UpperCAmelCase							 =		feat_extract_first.mel_filters
         _UpperCAmelCase							 =		feat_extract_second.mel_filters
         self.assertTrue(np.allclose(snake_case	,       snake_case						)						)
         self.assertEqual(snake_case	,       snake_case						)






       def        lowerCamelCase_   (  self						)			->							Tuple:
         _UpperCAmelCase							 =		self.feature_extraction_class(**self.feat_extract_dict						)

         with tempfile.TemporaryDirectory() as tmpdirname:
           _UpperCAmelCase							 =		os.path.join(snake_case	,       'feat_extract.json'						)
           feat_extract_first.to_json_file(snake_case						)
           _UpperCAmelCase							 =		self.feature_extraction_class.from_json_file(snake_case						)

         _UpperCAmelCase							 =		feat_extract_first.to_dict()
         _UpperCAmelCase							 =		feat_extract_second.to_dict()
         _UpperCAmelCase							 =		feat_extract_first.mel_filters
         _UpperCAmelCase							 =		feat_extract_second.mel_filters
         self.assertTrue(np.allclose(snake_case	,       snake_case						)						)
         self.assertEqual(snake_case	,       snake_case						)






       def        lowerCamelCase_   (  self						)			->							Tuple:
         # 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(snake_case						) for speech_input in speech_inputs]

         # Test feature size
         _UpperCAmelCase							 =		feature_extractor(snake_case	,       padding='max_length'	,       return_tensors='np'						).input_features
         self.assertTrue(input_features.ndim == 3						)
         self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames						)
         self.assertTrue(input_features.shape[-2] == 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(snake_case	,       snake_case	,       atol=1E-3						)						)

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

         # Test truncation required
         _UpperCAmelCase							 =		[floats_list((1, x)						)[0] for x in range(200	,       (feature_extractor.n_samples + 500)	,       200						)]
         _UpperCAmelCase							 =		[np.asarray(snake_case						) for speech_input in speech_inputs]

         _UpperCAmelCase							 =		[x[: feature_extractor.n_samples] for x in speech_inputs]
         _UpperCAmelCase							 =		[np.asarray(snake_case						) for speech_input in speech_inputs_truncated]

         _UpperCAmelCase							 =		feature_extractor(snake_case	,       return_tensors='np'						).input_features
         _UpperCAmelCase							 =		feature_extractor(snake_case	,       return_tensors='np'						).input_features
         for enc_seq_a, enc_seq_a in zip(snake_case	,       snake_case						):
           self.assertTrue(np.allclose(snake_case	,       snake_case	,       atol=1E-3						)						)






       def        lowerCamelCase_   (  self						)			->							Dict:
         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        lowerCamelCase_   (  self	,       snake_case						)			->							str:
         _UpperCAmelCase							 =		load_dataset('hf-internal-testing/librispeech_asr_dummy'	,       'clean'	,       split='validation'						)
         # automatic decoding with librispeech
         _UpperCAmelCase							 =		ds.sort('id'						).select(range(snake_case						)						)[:num_samples]['''audio''']

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






       def        lowerCamelCase_   (  self						)			->							int:
         # fmt: off
         _UpperCAmelCase							 =		torch.tensor(
             [
                 0.1193, -0.0946, -0.1098, -0.0196, 0.0225, -0.0690, -0.1736, 0.0951,
                 0.0971, -0.0817, -0.0702, 0.0162, 0.0260, 0.0017, -0.0192, -0.1678,
                 0.0709, -0.1867, -0.0655, -0.0274, -0.0234, -0.1884, -0.0516, -0.0554,
                 -0.0274, -0.1425, -0.1423, 0.0837, 0.0377, -0.0854
             ]						)
         # fmt: on

         _UpperCAmelCase							 =		self._load_datasamples(1						)
         _UpperCAmelCase							 =		WhisperFeatureExtractor()
         _UpperCAmelCase							 =		feature_extractor(snake_case	,       return_tensors='pt'						).input_features
         self.assertEqual(input_features.shape	,       (1, 80, 3000)						)
         self.assertTrue(torch.allclose(input_features[0, 0, :30]	,       snake_case	,       atol=1E-4						)						)






       def        lowerCamelCase_   (  self						)			->							Optional[Any]:
         _UpperCAmelCase							 =		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
         _UpperCAmelCase							 =		self._load_datasamples(1						)[0]
         _UpperCAmelCase							 =		((audio - audio.min()) / (audio.max() - audio.min())) * 65535  # Rescale to [0, 65535] to show issue
         _UpperCAmelCase							 =		feat_extract.zero_mean_unit_var_norm([audio]	,       attention_mask=snake_case						)[0]

         self.assertTrue(np.all(np.mean(snake_case						) < 1E-3						)						)
         self.assertTrue(np.all(np.abs(np.var(snake_case						) - 1						) < 1E-3						)						)







"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
					import jax.numpy as jnp

if is_tf_available():
					import tensorflow as tf

if is_torch_available():
					import torch




class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		{
									    'task_specific_params': {
									        'summarization': {'length_penalty': 1.0, 'max_length': 128, 'min_length': 12, 'num_beams': 4},
									        'summarization_cnn': {'length_penalty': 2.0, 'max_length': 142, 'min_length': 56, 'num_beams': 4},
									        'summarization_xsum': {'length_penalty': 1.0, 'max_length': 62, 'min_length': 11, 'num_beams': 6},
									    }
									}
									_UpperCAmelCase							 =		{
									    'task_specific_params.summarization.length_penalty': 1.0,
									    'task_specific_params.summarization.max_length': 128,
									    'task_specific_params.summarization.min_length': 12,
									    'task_specific_params.summarization.num_beams': 4,
									    'task_specific_params.summarization_cnn.length_penalty': 2.0,
									    'task_specific_params.summarization_cnn.max_length': 142,
									    'task_specific_params.summarization_cnn.min_length': 56,
									    'task_specific_params.summarization_cnn.num_beams': 4,
									    'task_specific_params.summarization_xsum.length_penalty': 1.0,
									    'task_specific_params.summarization_xsum.max_length': 62,
									    'task_specific_params.summarization_xsum.min_length': 11,
									    'task_specific_params.summarization_xsum.num_beams': 6,
									}

									self.assertEqual(flatten_dict(snake_case						)	,       snake_case						)






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       x.transpose()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       x.transpose((1, 2, 0)						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       transpose(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       transpose(snake_case	,       axes=(1, 2, 0)						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       transpose(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       transpose(snake_case	,       axes=(1, 2, 0)						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case						)	,       np.asarray(transpose(snake_case						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(transpose(snake_case	,       axes=(1, 2, 0)						)	,       np.asarray(transpose(snake_case	,       axes=(1, 2, 0)						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       np.reshape(snake_case	,       (4, 3)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       np.reshape(snake_case	,       (12, 5)						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Optional[Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       reshape(snake_case	,       (4, 3)						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       reshape(snake_case	,       (12, 5)						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       reshape(snake_case	,       (4, 3)						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       reshape(snake_case	,       (12, 5)						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (4, 3)						)	,       np.asarray(reshape(snake_case	,       (4, 3)						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(3	,       4	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(reshape(snake_case	,       (12, 5)						)	,       np.asarray(reshape(snake_case	,       (12, 5)						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       np.squeeze(snake_case						)						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       np.squeeze(snake_case	,       axis=2						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       squeeze(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       squeeze(snake_case	,       axis=2						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       squeeze(snake_case						).numpy()						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       squeeze(snake_case	,       axis=2						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(1	,       3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case						)	,       np.asarray(squeeze(snake_case						)						)						)						)

									_UpperCAmelCase							 =		np.random.randn(1	,       4	,       1	,       5						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(squeeze(snake_case	,       axis=2						)	,       np.asarray(squeeze(snake_case	,       axis=2						)						)						)						)






							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       np.expand_dims(snake_case	,       axis=1						)						)						)






							@require_torch
							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		torch.tensor(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       expand_dims(snake_case	,       axis=1						).numpy()						)						)






							@require_tf
							def        lowerCamelCase_   (  self						)			->							int:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		tf.constant(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       expand_dims(snake_case	,       axis=1						).numpy()						)						)






							@require_flax
							def        lowerCamelCase_   (  self						)			->							str:
									_UpperCAmelCase							 =		np.random.randn(3	,       4						)
									_UpperCAmelCase							 =		jnp.array(snake_case						)
									self.assertTrue(np.allclose(expand_dims(snake_case	,       axis=1						)	,       np.asarray(expand_dims(snake_case	,       axis=1						)						)						)						)


