



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

lowerCamelCase	 =       random.Random()



def   _A       (   _lowerCAmelCase  ,   _lowerCAmelCase=1.0  ,   _lowerCAmelCase=None  ,   _lowerCAmelCase=None					):



       """simple docstring"""



       if rng is None:
              __lowercase       	=global_rng

       __lowercase       	=[]
       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  :  Tuple  ,       _lowerCAmelCase  :  Union[str, Any]  ,       _lowerCAmelCase  :  List[Any]=7  ,       _lowerCAmelCase  :  int=4_0_0  ,       _lowerCAmelCase  :  Union[str, Any]=2_0_0_0  ,       _lowerCAmelCase  :  Union[str, Any]=1_0  ,       _lowerCAmelCase  :  Optional[Any]=1_6_0  ,       _lowerCAmelCase  :  int=8  ,       _lowerCAmelCase  :  Dict=0.0  ,       _lowerCAmelCase  :  Optional[int]=4_0_0_0  ,       _lowerCAmelCase  :  Optional[int]=False  ,       _lowerCAmelCase  :  Union[str, Any]=True  ,       ):







           '''simple docstring'''

           __lowercase       	=parent
           __lowercase       	=batch_size
           __lowercase       	=min_seq_length
           __lowercase       	=max_seq_length
           __lowercase       	=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           __lowercase       	=padding_value
           __lowercase       	=sampling_rate
           __lowercase       	=return_attention_mask
           __lowercase       	=do_normalize
           __lowercase       	=feature_size
           __lowercase       	=chunk_length
           __lowercase       	=hop_length

    def 				__lowerCamelCase     (							self  :  int):







           '''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  :  List[str]  ,       _lowerCAmelCase  :  Any=False  ,       _lowerCAmelCase  :  Dict=False):







           '''simple docstring'''

           def _flatten(_lowerCAmelCase  :  Any):
                  return list(itertools.chain(*__A))

           if equal_length:
                  __lowercase       	=[floats_list((self.max_seq_length, self.feature_size)) for _ in range(self.batch_size)]
           else:
                  # make sure that inputs increase in size
                  __lowercase       	=[
                      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       	=[np.asarray(__A) for x in speech_inputs]
           return speech_inputs





@require_torch
@require_torchaudio
class _UpperCamelCase		(     UpperCamelCase_      ,       unittest.TestCase		):





    '''simple docstring'''





    lowerCAmelCase__					    =    WhisperFeatureExtractor if is_speech_available() else None

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







           '''simple docstring'''

           __lowercase       	=WhisperFeatureExtractionTester(self)

    def 				__lowerCamelCase     (							self  :  Tuple):







           '''simple docstring'''

           __lowercase       	=self.feature_extraction_class(**self.feat_extract_dict)

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

           __lowercase       	=feat_extract_first.to_dict()
           __lowercase       	=feat_extract_second.to_dict()
           __lowercase       	=feat_extract_first.mel_filters
           __lowercase       	=feat_extract_second.mel_filters
           self.assertTrue(np.allclose(__A  ,       __A))
           self.assertEqual(__A  ,       __A)

    def 				__lowerCamelCase     (							self  :  Optional[Any]):







           '''simple docstring'''

           __lowercase       	=self.feature_extraction_class(**self.feat_extract_dict)

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

           __lowercase       	=feat_extract_first.to_dict()
           __lowercase       	=feat_extract_second.to_dict()
           __lowercase       	=feat_extract_first.mel_filters
           __lowercase       	=feat_extract_second.mel_filters
           self.assertTrue(np.allclose(__A  ,       __A))
           self.assertEqual(__A  ,       __A)

    def 				__lowerCamelCase     (							self  :  Any):







           '''simple docstring'''

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

           # Test feature size
           __lowercase       	=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
           __lowercase       	=feature_extractor(speech_inputs[0]  ,       return_tensors='np').input_features
           __lowercase       	=feature_extractor(np_speech_inputs[0]  ,       return_tensors='np').input_features
           self.assertTrue(np.allclose(__A  ,       __A  ,       atol=1e-3))

           # Test batched
           __lowercase       	=feature_extractor(__A  ,       return_tensors='np').input_features
           __lowercase       	=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.
           __lowercase       	=[floats_list((1, x))[0] for x in (8_0_0, 8_0_0, 8_0_0)]
           __lowercase       	=np.asarray(__A)
           __lowercase       	=feature_extractor(__A  ,       return_tensors='np').input_features
           __lowercase       	=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
           __lowercase       	=[floats_list((1, x))[0] for x in range(2_0_0  ,       (feature_extractor.n_samples + 5_0_0)  ,       2_0_0)]
           __lowercase       	=[np.asarray(__A) for speech_input in speech_inputs]

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

           __lowercase       	=feature_extractor(__A  ,       return_tensors='np').input_features
           __lowercase       	=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  :  Optional[Any]):







           '''simple docstring'''

           import torch

           __lowercase       	=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __lowercase       	=np.random.rand(1_0_0  ,       3_2).astype(np.floataa)
           __lowercase       	=np_speech_inputs.tolist()

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

    def 				__lowerCamelCase     (							self  :  List[str]  ,       _lowerCAmelCase  :  Dict):







           '''simple docstring'''

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

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

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







           '''simple docstring'''

           __lowercase       	=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

           __lowercase       	=self._load_datasamples(1)
           __lowercase       	=WhisperFeatureExtractor()
           __lowercase       	=feature_extractor(__A  ,       return_tensors='pt').input_features
           self.assertEqual(input_features.shape  ,       (1, 8_0, 3_0_0_0))
           self.assertTrue(torch.allclose(input_features[0, 0, :3_0]  ,       __A  ,       atol=1e-4))

    def 				__lowerCamelCase     (							self  :  Tuple):







           '''simple docstring'''

           __lowercase       	=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __lowercase       	=self._load_datasamples(1)[0]
           __lowercase       	=((audio - audio.min()) / (audio.max() - audio.min())) * 6_5_5_3_5  # Rescale to [0, 65535] to show issue
           __lowercase       	=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'''



# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


lowerCamelCase	 =       """Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"""



def   _A       (   ):



							"""simple docstring"""



							__lowercase       	=_ask_options(
							    'In which compute environment are you running?'  ,   ['This machine', 'AWS (Amazon SageMaker)']  ,   _convert_compute_environment  ,   )
							if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
														__lowercase       	=get_sagemaker_input()
							else:
														__lowercase       	=get_cluster_input()
							return config



def   _A       (   _lowerCAmelCase=None					):



							"""simple docstring"""



							if subparsers is not None:
														__lowercase       	=subparsers.add_parser('config'  ,   description=_lowerCAmelCase					)
							else:
														__lowercase       	=argparse.ArgumentParser('Accelerate config command'  ,   description=_lowerCAmelCase					)

							parser.add_argument(
							    '--config_file'  ,   default=_lowerCAmelCase  ,   help=(
							        'The path to use to store the config file. Will default to a file named default_config.yaml in the cache '
							        'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
							        'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
							        'with \'huggingface\'.'
							    )  ,   )

							if subparsers is not None:
														parser.set_defaults(func=_lowerCAmelCase					)
							return parser



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							__lowercase       	=get_user_input()
							if args.config_file is not None:
														__lowercase       	=args.config_file
							else:
														if not os.path.isdir(_lowerCAmelCase					):
																					os.makedirs(_lowerCAmelCase					)
														__lowercase       	=default_yaml_config_file

							if config_file.endswith('.json'					):
														config.to_json_file(_lowerCAmelCase					)
							else:
														config.to_yaml_file(_lowerCAmelCase					)
							print(f"""accelerate configuration saved at {config_file}"""					)



def   _A       (   ):



							"""simple docstring"""



							__lowercase       	=config_command_parser()
							__lowercase       	=parser.parse_args()
							config_command(_lowerCAmelCase					)


if __name__ == "__main__":
		main()



