


"""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

__snake_case             =   random.Random()







def   __lowerCAmelCase						(						lowercase  :  str						,							lowercase  :  int=1.0						,							lowercase  :  List[Any]=None						,							lowercase  :  List[Any]=None			)     ->						Tuple:


    """simple docstring"""





    if rng is None:
        snake_case   :				Any				 =   global_rng

    snake_case   :				int				 =   []
    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 _lowerCAmelCase    (				unittest.TestCase     ):







   def __init__(     self						,     UpperCamelCase__						,     UpperCamelCase__=7						,     UpperCamelCase__=400						,     UpperCamelCase__=2000						,     UpperCamelCase__=10						,     UpperCamelCase__=160						,     UpperCamelCase__=8						,     UpperCamelCase__=0.0						,     UpperCamelCase__=4000						,     UpperCamelCase__=False						,     UpperCamelCase__=True						,     )     ->		Dict:

       '''simple docstring'''




       snake_case   :				Any				 =   parent
       snake_case   :				Optional[Any]				 =   batch_size
       snake_case   :				Dict				 =   min_seq_length
       snake_case   :				Any				 =   max_seq_length
       snake_case   :				Optional[int]				 =   (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       snake_case   :				Union[str, Any]				 =   padding_value
       snake_case   :				Union[str, Any]				 =   sampling_rate
       snake_case   :				List[str]				 =   return_attention_mask
       snake_case   :				List[str]				 =   do_normalize
       snake_case   :				Tuple				 =   feature_size
       snake_case   :				int				 =   chunk_length
       snake_case   :				int				 =   hop_length







   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       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						,     UpperCamelCase__=False						,     UpperCamelCase__=False						)     ->		Tuple:

       '''simple docstring'''




       def _flatten(UpperCamelCase__						):
           return list(itertools.chain(*__a						)						)

       if equal_length:
           snake_case   :				Dict				 =   [floats_list((self.max_seq_length, self.feature_size)						) for _ in range(self.batch_size						)]
       else:
           # make sure that inputs increase in size
           snake_case   :				Union[str, Any]				 =   [
               floats_list((x, self.feature_size)						)
               for x in range(self.min_seq_length						,     self.max_seq_length						,     self.seq_length_diff						)
           ]
       if numpify:
           snake_case   :				Dict				 =   [np.asarray(__a						) for x in speech_inputs]
       return speech_inputs







@require_torch
@require_torchaudio
class _lowerCAmelCase    (				UpperCamelCase__   ,							unittest.TestCase     ):
   __UpperCAmelCase			:	Dict							  =  WhisperFeatureExtractor if is_speech_available() else None







   def 			lowerCamelCase		(     self						)     ->		Dict:

       '''simple docstring'''




       snake_case   :				List[str]				 =   WhisperFeatureExtractionTester(self						)







   def 			lowerCamelCase		(     self						)     ->		List[str]:

       '''simple docstring'''




       snake_case   :				Optional[int]				 =   self.feature_extraction_class(**self.feat_extract_dict						)

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

       snake_case   :				List[Any]				 =   feat_extract_first.to_dict()
       snake_case   :				Union[str, Any]				 =   feat_extract_second.to_dict()
       snake_case   :				Optional[Any]				 =   feat_extract_first.mel_filters
       snake_case   :				List[Any]				 =   feat_extract_second.mel_filters
       self.assertTrue(np.allclose(__a						,     __a						)						)
       self.assertEqual(__a						,     __a						)







   def 			lowerCamelCase		(     self						)     ->		Tuple:

       '''simple docstring'''




       snake_case   :				int				 =   self.feature_extraction_class(**self.feat_extract_dict						)

       with tempfile.TemporaryDirectory() as tmpdirname:
           snake_case   :				int				 =   os.path.join(__a						,     "feat_extract.json"						)
           feat_extract_first.to_json_file(__a						)
           snake_case   :				Tuple				 =   self.feature_extraction_class.from_json_file(__a						)

       snake_case   :				Any				 =   feat_extract_first.to_dict()
       snake_case   :				Optional[Any]				 =   feat_extract_second.to_dict()
       snake_case   :				Dict				 =   feat_extract_first.mel_filters
       snake_case   :				Tuple				 =   feat_extract_second.mel_filters
       self.assertTrue(np.allclose(__a						,     __a						)						)
       self.assertEqual(__a						,     __a						)







   def 			lowerCamelCase		(     self						)     ->		Optional[int]:

       '''simple docstring'''




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

       # Test feature size
       snake_case   :				str				 =   feature_extractor(__a						,     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
       snake_case   :				List[Any]				 =   feature_extractor(speech_inputs[0]						,     return_tensors="np"						).input_features
       snake_case   :				Any				 =   feature_extractor(np_speech_inputs[0]						,     return_tensors="np"						).input_features
       self.assertTrue(np.allclose(__a						,     __a						,     atol=1e-3						)						)

       # Test batched
       snake_case   :				str				 =   feature_extractor(__a						,     return_tensors="np"						).input_features
       snake_case   :				Union[str, Any]				 =   feature_extractor(__a						,     return_tensors="np"						).input_features
       for enc_seq_a, enc_seq_a in zip(__a						,     __a						):
           self.assertTrue(np.allclose(__a						,     __a						,     atol=1e-3						)						)

       # Test 2-D numpy arrays are batched.
       snake_case   :				Any				 =   [floats_list((1, x)						)[0] for x in (800, 800, 800)]
       snake_case   :				Any				 =   np.asarray(__a						)
       snake_case   :				List[Any]				 =   feature_extractor(__a						,     return_tensors="np"						).input_features
       snake_case   :				Optional[Any]				 =   feature_extractor(__a						,     return_tensors="np"						).input_features
       for enc_seq_a, enc_seq_a in zip(__a						,     __a						):
           self.assertTrue(np.allclose(__a						,     __a						,     atol=1e-3						)						)

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

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

       snake_case   :				Optional[int]				 =   feature_extractor(__a						,     return_tensors="np"						).input_features
       snake_case   :				List[Any]				 =   feature_extractor(__a						,     return_tensors="np"						).input_features
       for enc_seq_a, enc_seq_a in zip(__a						,     __a						):
           self.assertTrue(np.allclose(__a						,     __a						,     atol=1e-3						)						)







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       import torch

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

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







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						)     ->		List[Any]:

       '''simple docstring'''




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

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







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				int				 =   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

       snake_case   :				int				 =   self._load_datasamples(1						)
       snake_case   :				Optional[Any]				 =   WhisperFeatureExtractor()
       snake_case   :				Optional[Any]				 =   feature_extractor(__a						,     return_tensors="pt"						).input_features
       self.assertEqual(input_features.shape						,     (1, 80, 3000)						)
       self.assertTrue(torch.allclose(input_features[0, 0, :30]						,     __a						,     atol=1e-4						)						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Union[str, Any]				 =   self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
       snake_case   :				str				 =   self._load_datasamples(1						)[0]
       snake_case   :				Any				 =   ((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
       snake_case   :				List[Any]				 =   feat_extract.zero_mean_unit_var_norm([audio]						,     attention_mask=__a						)[0]

       self.assertTrue(np.all(np.mean(__a						) < 1e-3						)						)
       self.assertTrue(np.all(np.abs(np.var(__a						) - 1						) < 1e-3						)						)






"""simple docstring"""



import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
  import tensorflow as tf
  from tensorflow.python.eager import context
  from tensorflow.python.framework import ops

  from transformers import GradientAccumulator, create_optimizer







@require_tf
class _lowerCAmelCase    (				unittest.TestCase     ):







   def 			lowerCamelCase		(     self						,     UpperCamelCase__						,     UpperCamelCase__						,     UpperCamelCase__						)     ->		Optional[Any]:

       '''simple docstring'''




       self.assertEqual(len(UpperCamelCase__						)						,     len(UpperCamelCase__						)						)
       for a, b in zip(UpperCamelCase__						,     UpperCamelCase__						):
           self.assertAlmostEqual(UpperCamelCase__						,     UpperCamelCase__						,     delta=UpperCamelCase__						)







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				List[Any]				 =   GradientAccumulator()
       accumulator([tf.constant([1.0, 2.0]						)]						)
       accumulator([tf.constant([-2.0, 1.0]						)]						)
       accumulator([tf.constant([-1.0, 2.0]						)]						)
       with self.assertRaises(UpperCamelCase__						):
           accumulator([tf.constant([1.0, 1.0]						), tf.constant([2.0, 2.0]						)]						)
       self.assertEqual(accumulator.step						,     3						)
       self.assertEqual(len(accumulator.gradients						)						,     1						)
       self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()						,     [-2.0, 5.0]						,     tol=1e-2						)
       accumulator.reset()
       self.assertEqual(accumulator.step						,     0						)
       self.assertListAlmostEqual(accumulator.gradients[0].numpy().tolist()						,     [0.0, 0.0]						,     tol=1e-2						)







   def 			lowerCamelCase		(     self						)     ->		int:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   None
       ops.enable_eager_execution_internal()
       snake_case   :				str				 =   tf.config.list_physical_devices("CPU"						)
       if len(UpperCamelCase__						) == 1:
           tf.config.set_logical_device_configuration(
               physical_devices[0]						,     [tf.config.LogicalDeviceConfiguration(), tf.config.LogicalDeviceConfiguration()]						)
       snake_case   :				Optional[int]				 =   tf.config.list_logical_devices(device_type="CPU"						)
       snake_case   :				List[str]				 =   tf.distribute.MirroredStrategy(devices=devices[:2]						)

       with strategy.scope():
           snake_case   :				int				 =   GradientAccumulator()
           snake_case   :				int				 =   tf.Variable([4.0, 3.0]						)
           snake_case ,snake_case   :				Any				 =   create_optimizer(5e-5						,     10						,     5						)
           snake_case   :				Tuple				 =   tf.Variable([0.0, 0.0]						,     trainable=UpperCamelCase__						)

       def accumulate_on_replica(UpperCamelCase__						):
           accumulator([gradient]						)

       def apply_on_replica():
           optimizer.apply_gradients(list(zip(accumulator.gradients						,     [variable]						)						)						)

       @tf.function
       def accumulate(UpperCamelCase__						,     UpperCamelCase__						):
           with strategy.scope():
               snake_case   :				Union[str, Any]				 =   strategy.experimental_local_results(UpperCamelCase__						)
               local_variables[0].assign(UpperCamelCase__						)
               local_variables[1].assign(UpperCamelCase__						)
               strategy.run(UpperCamelCase__						,     args=(gradient_placeholder,)						)

       @tf.function
       def apply_grad():
           with strategy.scope():
               strategy.run(UpperCamelCase__						)

       def _check_local_values(UpperCamelCase__						,     UpperCamelCase__						):
           snake_case   :				List[Any]				 =   strategy.experimental_local_results(accumulator._gradients[0]						)
           self.assertListAlmostEqual(values[0].value()						,     UpperCamelCase__						,     tol=1e-2						)
           self.assertListAlmostEqual(values[1].value()						,     UpperCamelCase__						,     tol=1e-2						)

       accumulate([1.0, 2.0]						,     [-1.0, 1.0]						)
       accumulate([3.0, -1.0]						,     [-1.0, -1.0]						)
       accumulate([-2.0, 2.0]						,     [3.0, -2.0]						)
       self.assertEqual(accumulator.step						,     3						)
       _check_local_values([2.0, 3.0]						,     [1.0, -2.0]						)
       apply_grad()
       self.assertListAlmostEqual(variable.value()						,     [4.0, 3.0]						,     tol=1e-2						)
       accumulator.reset()
       self.assertEqual(accumulator.step						,     0						)
       _check_local_values([0.0, 0.0]						,     [0.0, 0.0]						)





