






'''simple docstring'''
import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


UpperCamelCase__			=      random.Random()




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


   """simple docstring"""




   if rng is None:
      lowercase_						:     Tuple							     =  global_rng

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

   return values


class 							_UpperCAmelCase    (		unittest.TestCase      ):




    def __init__(      self	:							int	,  a	:							int	,  a	:							List[Any]=7	,  a	:							Tuple=4_0_0	,  a	:							int=2_0_0_0	,  a	:							Union[str, Any]=1	,  a	:							List[Any]=0.0	,  a	:							str=1_6_0_0_0	,  a	:							str=True	,  a	:							str=True	,  ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  parent
       lowercase_						:     Dict							     =  batch_size
       lowercase_						:     List[str]							     =  min_seq_length
       lowercase_						:     str							     =  max_seq_length
       lowercase_						:     Dict							     =  (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       lowercase_						:     Tuple							     =  feature_size
       lowercase_						:     Optional[Any]							     =  padding_value
       lowercase_						:     Optional[Any]							     =  sampling_rate
       lowercase_						:     Optional[Any]							     =  return_attention_mask
       lowercase_						:     str							     =  do_normalize




    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''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 					lowerCAmelCase__					(      self	:							Optional[int]	,  a	:							Optional[int]=False	,  a	:							List[Any]=False      ):






       '''simple docstring'''






       def _flatten(a	:							str      ):
          return list(itertools.chain(*a      )      )

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

       return speech_inputs


class 							_UpperCAmelCase    (		snake_case					,       unittest.TestCase      ):
    __lowerCamelCase: Optional[Any]									=     WavaVecaFeatureExtractor




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  WavaVecaFeatureExtractionTester(self      )




    def 					lowerCAmelCase__					(      self	:							List[str]	,  a	:							Dict      ):






       '''simple docstring'''






       self.assertTrue(np.all(np.mean(a	,  axis=0      ) < 1e-3      )      )
       self.assertTrue(np.all(np.abs(np.var(a	,  axis=0      ) - 1      ) < 1e-3      )      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
       # create three inputs of length 800, 1000, and 1200
       lowercase_						:     Optional[Any]							     =  [floats_list((1, x)      )[0] for x in range(8_0_0	,  1_4_0_0	,  2_0_0      )]
       lowercase_						:     List[Any]							     =  [np.asarray(a      ) for speech_input in speech_inputs]

       # Test not batched input
       lowercase_						:     Any							     =  feat_extract(speech_inputs[0]	,  return_tensors="np"      ).input_values
       lowercase_						:     Dict							     =  feat_extract(np_speech_inputs[0]	,  return_tensors="np"      ).input_values
       self.assertTrue(np.allclose(a	,  a	,  atol=1e-3      )      )

       # Test batched
       lowercase_						:     List[str]							     =  feat_extract(a	,  return_tensors="np"      ).input_values
       lowercase_						:     Union[str, Any]							     =  feat_extract(a	,  return_tensors="np"      ).input_values
       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.
       lowercase_						:     List[str]							     =  [floats_list((1, x)      )[0] for x in (8_0_0, 8_0_0, 8_0_0)]
       lowercase_						:     Optional[int]							     =  np.asarray(a      )
       lowercase_						:     Optional[Any]							     =  feat_extract(a	,  return_tensors="np"      ).input_values
       lowercase_						:     Union[str, Any]							     =  feat_extract(a	,  return_tensors="np"      ).input_values
       for enc_seq_a, enc_seq_a in zip(a	,  a      ):
          self.assertTrue(np.allclose(a	,  a	,  atol=1e-3      )      )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Tuple							     =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
       lowercase_						:     int							     =  [floats_list((1, x)      )[0] for x in range(8_0_0	,  1_4_0_0	,  2_0_0      )]

       lowercase_						:     Tuple							     =  ["longest", "max_length", "do_not_pad"]
       lowercase_						:     str							     =  [None, 1_6_0_0, None]
       for max_length, padding in zip(a	,  a      ):
          lowercase_						:     int							     =  feat_extract(a	,  padding=a	,  max_length=a	,  return_tensors="np"      )
          lowercase_						:     Dict							     =  processed.input_values

          self._check_zero_mean_unit_variance(input_values[0][:8_0_0]      )
          self.assertTrue(input_values[0][8_0_0:].sum() < 1e-6      )
          self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]      )
          self.assertTrue(input_values[0][1_0_0_0:].sum() < 1e-6      )
          self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]      )




    def 					lowerCAmelCase__					(      self	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
       lowercase_						:     Dict							     =  range(8_0_0	,  1_4_0_0	,  2_0_0      )
       lowercase_						:     Tuple							     =  [floats_list((1, x)      )[0] for x in lengths]

       lowercase_						:     Union[str, Any]							     =  ["longest", "max_length", "do_not_pad"]
       lowercase_						:     List[str]							     =  [None, 1_6_0_0, None]

       for max_length, padding in zip(a	,  a      ):
          lowercase_						:     Union[str, Any]							     =  feat_extract(a	,  max_length=a	,  padding=a      )
          lowercase_						:     Tuple							     =  processed.input_values

          self._check_zero_mean_unit_variance(input_values[0][:8_0_0]      )
          self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]      )
          self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]      )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
       lowercase_						:     Any							     =  [floats_list((1, x)      )[0] for x in range(8_0_0	,  1_4_0_0	,  2_0_0      )]
       lowercase_						:     Union[str, Any]							     =  feat_extract(
           a	,  truncation=a	,  max_length=1_0_0_0	,  padding="max_length"	,  return_tensors="np"      )
       lowercase_						:     Optional[Any]							     =  processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :8_0_0]      )
       self._check_zero_mean_unit_variance(input_values[1]      )
       self._check_zero_mean_unit_variance(input_values[2]      )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
       lowercase_						:     List[Any]							     =  [floats_list((1, x)      )[0] for x in range(8_0_0	,  1_4_0_0	,  2_0_0      )]
       lowercase_						:     str							     =  feat_extract(
           a	,  truncation=a	,  max_length=1_0_0_0	,  padding="longest"	,  return_tensors="np"      )
       lowercase_						:     Tuple							     =  processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :8_0_0]      )
       self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]      )
       self._check_zero_mean_unit_variance(input_values[2]      )

       # make sure that if max_length < longest -> then pad to max_length
       self.assertTrue(input_values.shape == (3, 1_0_0_0)      )

       lowercase_						:     Union[str, Any]							     =  [floats_list((1, x)      )[0] for x in range(8_0_0	,  1_4_0_0	,  2_0_0      )]
       lowercase_						:     List[Any]							     =  feat_extract(
           a	,  truncation=a	,  max_length=2_0_0_0	,  padding="longest"	,  return_tensors="np"      )
       lowercase_						:     List[Any]							     =  processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :8_0_0]      )
       self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]      )
       self._check_zero_mean_unit_variance(input_values[2]      )

       # make sure that if max_length > longest -> then pad to longest
       self.assertTrue(input_values.shape == (3, 1_2_0_0)      )




    @require_torch
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       import torch

       lowercase_						:     Tuple							     =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
       lowercase_						:     Optional[int]							     =  np.random.rand(1_0_0      ).astype(np.floataa      )
       lowercase_						:     List[Any]							     =  np_speech_inputs.tolist()

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





    @slow
    @require_torch
    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
          lowercase_						:     int							     =  WavaVecaConfig.from_pretrained(a      )
          lowercase_						:     List[str]							     =  WavaVecaFeatureExtractor.from_pretrained(a      )

          # only "layer" feature extraction norm should make use of
          # attention_mask
          self.assertEqual(feat_extract.return_attention_mask	,  config.feat_extract_norm == "layer"      )












'''simple docstring'''
import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command


class 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: List[Any]									=     inspect.getfile(accelerate.test_utils      )
    __lowerCamelCase: List[Any]									=     os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ['scripts', 'test_cli.py']      )

    __lowerCamelCase: Optional[Any]									=     ['accelerate', 'launch']
    __lowerCamelCase: List[str]									=     Path.home() / '.cache/huggingface/accelerate'
    __lowerCamelCase: Dict									=     'default_config.yaml'
    __lowerCamelCase: Union[str, Any]									=     config_folder / config_file
    __lowerCamelCase: Tuple									=     config_folder / '_default_config.yaml'

    __lowerCamelCase: Union[str, Any]									=     Path('tests/test_configs'      )




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							List[Any]      ):






       '''simple docstring'''






       if cls.config_path.is_file():
          cls.config_path.rename(cls.changed_path      )




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							str      ):






       '''simple docstring'''






       if cls.changed_path.is_file():
          cls.changed_path.rename(cls.config_path      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.base_cmd
       if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
          cmd += ["--multi_gpu"]
       execute_subprocess_async(cmd + [self.test_file_path]	,  env=os.environ.copy()      )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       for config in sorted(self.test_config_path.glob("**/*.yaml"      )      ):
          with self.subTest(config_file=a      ):
             execute_subprocess_async(
                 self.base_cmd + ["--config_file", str(a      ), self.test_file_path]	,  env=os.environ.copy()      )





    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       execute_subprocess_async(["accelerate", "test"]	,  env=os.environ.copy()      )


class 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: Tuple									=     'test-tpu'
    __lowerCamelCase: Dict									=     'us-central1-a'
    __lowerCamelCase: List[str]									=     'ls'
    __lowerCamelCase: Optional[int]									=     ['accelerate', 'tpu-config']
    __lowerCamelCase: Optional[Any]									=     'cd /usr/share'
    __lowerCamelCase: Any									=     'tests/test_samples/test_command_file.sh'
    __lowerCamelCase: Tuple									=     'Running gcloud compute tpus tpu-vm ssh'




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     str							     =  run_command(
           self.cmd
           + ["--command", self.command, "--tpu_zone", self.tpu_zone, "--tpu_name", self.tpu_name, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/0_12_0.yaml",
               "--command",
               self.command,
               "--tpu_zone",
               self.tpu_zone,
               "--tpu_name",
               self.tpu_name,
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--debug"]	,  return_stdout=a      )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--command", self.command, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/latest.yaml",
               "--command",
               self.command,
               "--command",
               "echo \"Hello World\"",
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + ["--config_file", "tests/test_configs/latest.yaml", "--command_file", self.command_file, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/0_12_0.yaml",
               "--command_file",
               self.command_file,
               "--tpu_zone",
               self.tpu_zone,
               "--tpu_name",
               self.tpu_name,
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--install_accelerate", "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )





    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/latest.yaml",
               "--install_accelerate",
               "--accelerate_version",
               "12.0.0",
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




