





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

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





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE=1.0							,     _SCREAMING_SNAKE_CASE=None							,     _SCREAMING_SNAKE_CASE=None				)							-> Optional[Any]:




    """simple docstring"""




    if rng is None:
        _A     =							global_rng

    _A     =							[]
    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	):
     '''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				,     ):
         _A     =							parent
         _A     =							batch_size
         _A     =							min_seq_length
         _A     =							max_seq_length
         _A     =							(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
         _A     =							padding_value
         _A     =							sampling_rate
         _A     =							return_attention_mask
         _A     =							do_normalize
         _A     =							feature_size
         _A     =							chunk_length
         _A     =							hop_length




     def        lowerCAmelCase__		(    self							):
         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							):
         def _flatten(snake_case_							):
             return list(itertools.chain(*snake_case_							)							)

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







@require_torch
@require_torchaudio
class      lowerCamelCase(						__snake_case			, unittest.TestCase	):
     '''simple docstring'''


     __magic_name__   =			WhisperFeatureExtractor if is_speech_available() else None




     def        lowerCAmelCase__		(    self							):
         _A     =							WhisperFeatureExtractionTester(self							)




     def        lowerCAmelCase__		(    self							):
         _A     =							self.feature_extraction_class(**self.feat_extract_dict							)

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

         _A     =							feat_extract_first.to_dict()
         _A     =							feat_extract_second.to_dict()
         _A     =							feat_extract_first.mel_filters
         _A     =							feat_extract_second.mel_filters
         self.assertTrue(np.allclose(snake_case_				,     snake_case_							)							)
         self.assertEqual(snake_case_				,     snake_case_							)




     def        lowerCAmelCase__		(    self							):
         _A     =							self.feature_extraction_class(**self.feat_extract_dict							)

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

         _A     =							feat_extract_first.to_dict()
         _A     =							feat_extract_second.to_dict()
         _A     =							feat_extract_first.mel_filters
         _A     =							feat_extract_second.mel_filters
         self.assertTrue(np.allclose(snake_case_				,     snake_case_							)							)
         self.assertEqual(snake_case_				,     snake_case_							)




     def        lowerCAmelCase__		(    self							):
         # Tests that all call wrap to encode_plus and batch_encode_plus
         _A     =							self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
         # create three inputs of length 800, 1000, and 1200
         _A     =							[floats_list((1, x)							)[0] for x in range(800				,     1400				,     200							)]
         _A     =							[np.asarray(snake_case_							) for speech_input in speech_inputs]

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

         # Test batched
         _A     =							feature_extractor(snake_case_				,     return_tensors='np'							).input_features
         _A     =							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.
         _A     =							[floats_list((1, x)							)[0] for x in (800, 800, 800)]
         _A     =							np.asarray(snake_case_							)
         _A     =							feature_extractor(snake_case_				,     return_tensors='np'							).input_features
         _A     =							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
         _A     =							[floats_list((1, x)							)[0] for x in range(200				,     (feature_extractor.n_samples + 500)				,     200							)]
         _A     =							[np.asarray(snake_case_							) for speech_input in speech_inputs]

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

         _A     =							feature_extractor(snake_case_				,     return_tensors='np'							).input_features
         _A     =							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							):
         import torch

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

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




     def        lowerCAmelCase__		(    self				,     snake_case_							):
         _A     =							load_dataset('hf-internal-testing/librispeech_asr_dummy'				,     'clean'				,     split='validation'							)
         # automatic decoding with librispeech
         _A     =							ds.sort('id'							).select(range(snake_case_							)							)[:num_samples]['audio']

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




     def        lowerCAmelCase__		(    self							):
         # fmt: off
         _A     =							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

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












# Copyright 2022 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
import subprocess

from packaging.version import Version, parse

from accelerate.commands.config.config_args import default_config_file, load_config_from_file


__A							:			Optional[int]         =			"Run commands across TPU VMs for initial setup before running `accelerate launch`."





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE=None				)							-> str:




    """simple docstring"""




    if subparsers is not None:
        _A     =							subparsers.add_parser('tpu-config'							,     description=_description				)
    else:
        _A     =							argparse.ArgumentParser('Accelerate tpu-config command'							,     description=_description				)
    # Core arguments
    _A     =							parser.add_argument_group(
        'Config Arguments'							,     'Arguments that can be configured through `accelerate config`.'				)
    config_args.add_argument(
        '--config_file'							,     type=_SCREAMING_SNAKE_CASE							,     default=_SCREAMING_SNAKE_CASE							,     help='Path to the config file to use for accelerate.'							,     )
    config_args.add_argument(
        '--tpu_name'							,     default=_SCREAMING_SNAKE_CASE							,     help='The name of the TPU to use. If not specified, will use the TPU specified in the config file.'							,     )
    config_args.add_argument(
        '--tpu_zone'							,     default=_SCREAMING_SNAKE_CASE							,     help='The zone of the TPU to use. If not specified, will use the zone specified in the config file.'							,     )
    _A     =							parser.add_argument_group('TPU Arguments'							,     'Arguments for options ran inside the TPU.'				)
    pod_args.add_argument(
        '--use_alpha'							,     action='store_true'							,     help='Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.'							,     )
    pod_args.add_argument(
        '--command_file'							,     default=_SCREAMING_SNAKE_CASE							,     help='The path to the file containing the commands to run on the pod on startup.'							,     )
    pod_args.add_argument(
        '--command'							,     action='append'							,     nargs='+'							,     help='A command to run on the pod. Can be passed multiple times.'							,     )
    pod_args.add_argument(
        '--install_accelerate'							,     action='store_true'							,     help='Whether to install accelerate on the pod. Defaults to False.'							,     )
    pod_args.add_argument(
        '--accelerate_version'							,     default='latest'							,     help='The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify \'dev\' to install from GitHub.'							,     )
    pod_args.add_argument(
        '--debug'							,     action='store_true'							,     help='If set, will print the command that would be run instead of running it.'				)

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





def 				__lowerCAmelCase(       _SCREAMING_SNAKE_CASE				)							-> int:




    """simple docstring"""




    _A     =							None

    # Get the default from the config file if it exists.
    if args.config_file is not None or os.path.isfile(_SCREAMING_SNAKE_CASE				):
        _A     =							load_config_from_file(args.config_file				)
        if not args.command_file and defaults.command_file is not None and not args.command:
            _A     =							defaults.command_file
        if not args.command and defaults.commands is not None:
            _A     =							defaults.commands
        if not args.tpu_name:
            _A     =							defaults.tpu_name
        if not args.tpu_zone:
            _A     =							defaults.tpu_zone
    if args.accelerate_version == "dev":
        _A     =							'git+https://github.com/huggingface/accelerate.git'
    elif args.accelerate_version == "latest":
        _A     =							'accelerate -U'
    elif isinstance(parse(args.accelerate_version				)							,     _SCREAMING_SNAKE_CASE				):
        _A     =							F"accelerate=={args.accelerate_version}"

    if not args.command_file and not args.command:
        raise ValueError('You must specify either a command file or a command to run on the pod.'				)

    if args.command_file:
        with open(args.command_file							,     'r'				) as f:
            _A     =							[f.read().splitlines()]

    # To turn list of lists into list of strings
    if isinstance(args.command[0]							,     _SCREAMING_SNAKE_CASE				):
        _A     =							[line for cmd in args.command for line in cmd]
    # Default to the shared folder and install accelerate
    _A     =							['cd /usr/share']
    if args.install_accelerate:
        new_cmd += [F"pip install {args.accelerate_version}"]
    new_cmd += args.command
    _A     =							'; '.join(_SCREAMING_SNAKE_CASE				)

    # Then send it to gcloud
    # Eventually try to use google-api-core to do this instead of subprocess
    _A     =							['gcloud']
    if args.use_alpha:
        cmd += ["alpha"]
    cmd += [
        "compute",
        "tpus",
        "tpu-vm",
        "ssh",
        args.tpu_name,
        "--zone",
        args.tpu_zone,
        "--command",
        args.command,
        "--worker",
        "all",
    ]
    if args.debug:
        print(F"Running {' '.join(_SCREAMING_SNAKE_CASE				)}"				)
        return
    subprocess.run(_SCREAMING_SNAKE_CASE				)
    print('Successfully setup pod.'				)





def 				__lowerCAmelCase(       )							-> Tuple:




    """simple docstring"""




    _A     =							tpu_command_parser()
    _A     =							parser.parse_args()

    tpu_command_launcher(_SCREAMING_SNAKE_CASE				)





