


import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowerCAmelCase__      		= random.Random()

if is_torch_available():
    import torch
def 							_UpperCAmelCase				(UpperCamelCase__     :  List[Any]				,							UpperCamelCase__     :  Tuple=1.0				,							UpperCamelCase__     :  Dict=None				,							UpperCamelCase__     :  str=None						):
       if rng is None:
              _A      :       Optional[Any]				    = global_rng

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

       return values







class      lowerCAmelCase__       (				unittest.TestCase):




     '''simple docstring'''


     def __init__(						self ,						__lowerCamelCase ,						__lowerCamelCase=7 ,						__lowerCamelCase=4_0_0 ,						__lowerCamelCase=2_0_0_0 ,						__lowerCamelCase=1 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=1_6_0_0_0 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						)			->				Optional[Any]:
            _A      :       Tuple				    = parent
            _A      :       Dict				    = batch_size
            _A      :       Dict				    = min_seq_length
            _A      :       Optional[int]				    = max_seq_length
            _A      :       Optional[int]				    = (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
            _A      :       List[Any]				    = feature_size
            _A      :       str				    = padding_value
            _A      :       Any				    = sampling_rate
            _A      :       int				    = return_attention_mask
            _A      :       Tuple				    = do_normalize


     def  _lowerCamelCase (						self)			->				Union[str, Any]:
            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 ,						__lowerCamelCase=False ,						__lowerCamelCase=False)			->				Optional[Any]:
            def _flatten(__lowerCamelCase):
                   return list(itertools.chain(*__lowerCamelCase))

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

            return speech_inputs







@require_torch
@require_torchaudio
class      lowerCAmelCase__       (				snake_case_     ,    unittest.TestCase):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE  			= ASTFeatureExtractor


     def  _lowerCamelCase (						self)			->				str:
            _A      :       Optional[int]				    = ASTFeatureExtractionTester(self)


     def  _lowerCamelCase (						self)			->				Any:
            # Tests that all call wrap to encode_plus and batch_encode_plus
            _A      :       List[Any]				    = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            # create three inputs of length 800, 1000, and 1200
            _A      :       List[Any]				    = [floats_list((1, x))[0] for x in range(8_0_0 ,						1_4_0_0 ,						2_0_0)]
            _A      :       Tuple				    = [np.asarray(__lowerCamelCase) for speech_input in speech_inputs]

            # Test not batched input
            _A      :       Tuple				    = feat_extract(speech_inputs[0] ,						return_tensors="np").input_values
            _A      :       int				    = feat_extract(np_speech_inputs[0] ,						return_tensors="np").input_values
            self.assertTrue(np.allclose(__lowerCamelCase ,						__lowerCamelCase ,						atol=1e-3))

            # Test batched
            _A      :       int				    = feat_extract(__lowerCamelCase ,						padding=__lowerCamelCase ,						return_tensors="np").input_values
            _A      :       Optional[Any]				    = feat_extract(__lowerCamelCase ,						padding=__lowerCamelCase ,						return_tensors="np").input_values
            for enc_seq_a, enc_seq_a in zip(__lowerCamelCase ,						__lowerCamelCase):
                   self.assertTrue(np.allclose(__lowerCamelCase ,						__lowerCamelCase ,						atol=1e-3))

            # Test 2-D numpy arrays are batched.
            _A      :       List[Any]				    = [floats_list((1, x))[0] for x in (8_0_0, 8_0_0, 8_0_0)]
            _A      :       List[str]				    = np.asarray(__lowerCamelCase)
            _A      :       Optional[Any]				    = feat_extract(__lowerCamelCase ,						return_tensors="np").input_values
            _A      :       Union[str, Any]				    = feat_extract(__lowerCamelCase ,						return_tensors="np").input_values
            for enc_seq_a, enc_seq_a in zip(__lowerCamelCase ,						__lowerCamelCase):
                   self.assertTrue(np.allclose(__lowerCamelCase ,						__lowerCamelCase ,						atol=1e-3))


     @require_torch
     def  _lowerCamelCase (						self)			->				int:
            import torch

            _A      :       List[str]				    = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
            _A      :       int				    = np.random.rand(1_0_0).astype(np.floataa)
            _A      :       Dict				    = np_speech_inputs.tolist()

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


     def  _lowerCamelCase (						self ,						__lowerCamelCase)			->				int:
            from datasets import load_dataset

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

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


     @require_torch
     def  _lowerCamelCase (						self)			->				int:
            # fmt: off
            _A      :       Union[str, Any]				    = torch.tensor(
                [-0.9_8_9_4, -1.2_7_7_6, -0.9_0_6_6, -1.2_7_7_6, -0.9_3_4_9, -1.2_6_0_9, -1.0_3_8_6, -1.2_7_7_6,
                 -1.1_5_6_1, -1.2_7_7_6, -1.2_0_5_2, -1.2_7_2_3, -1.2_1_9_0, -1.2_1_3_2, -1.2_7_7_6, -1.1_1_3_3,
                 -1.1_9_5_3, -1.1_3_4_3, -1.1_5_8_4, -1.2_2_0_3, -1.1_7_7_0, -1.2_4_7_4, -1.2_3_8_1, -1.1_9_3_6,
                 -0.9_2_7_0, -0.8_3_1_7, -0.8_0_4_9, -0.7_7_0_6, -0.7_5_6_5, -0.7_8_6_9])
            # fmt: on

            _A      :       str				    = self._load_datasamples(1)
            _A      :       List[Any]				    = ASTFeatureExtractor()
            _A      :       List[str]				    = feature_extractor(__lowerCamelCase ,						return_tensors="pt").input_values
            self.assertEquals(input_values.shape ,						(1, 1_0_2_4, 1_2_8))
            self.assertTrue(torch.allclose(input_values[0, 0, :3_0] ,						__lowerCamelCase ,						atol=1e-4))


import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


__A				=logging.get_logger(__name__)





def 			lowerCamelCase_ (				):
				# Get the sagemaker specific mp parameters from smp_options variable.
				lowerCamelCase_						=			os.getenv("SM_HP_MP_PARAMETERS"	, "{}"    )
				try:
								# Parse it and check the field "partitions" is included, it is required for model parallel.
								lowerCamelCase_						=			json.loads(lowerCamelCase__    )
								if "partitions" not in smp_options:
												return False
				except json.JSONDecodeError:
								return False

				# Get the sagemaker specific framework parameters from mpi_options variable.
				lowerCamelCase_						=			os.getenv("SM_FRAMEWORK_PARAMS"	, "{}"    )
				try:
								# Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
								lowerCamelCase_						=			json.loads(lowerCamelCase__    )
								if not mpi_options.get("sagemaker_mpi_enabled"	, lowerCamelCase__    ):
												return False
				except json.JSONDecodeError:
								return False
				# Lastly, check if the `smdistributed` module is present.
				return importlib.util.find_spec("smdistributed"    ) is not None


if is_sagemaker_model_parallel_available():
			import smdistributed.modelparallel.torch as smp

			smp.init()





@dataclass
class     _SCREAMING_SNAKE_CASE     (      snake_case_						):
	lowerCAmelCase__         = field(
	    default=''			,       metadata={'help': 'Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'}			,       )



	def     SCREAMING_SNAKE_CASE_(    self )				->       Tuple:
					super().__post_init__()
					warnings.warn(
					    "`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use "
					    "`TrainingArguments` instead."						,  lowercase						,  )



	@cached_property
	def     SCREAMING_SNAKE_CASE_(    self )				->       "torch.device":
					logger.info("PyTorch: setting up devices" )
					if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
									logger.warning(
									    "torch.distributed process group is initialized, but local_rank == -1. "
									    "In order to use Torch DDP, launch your script with `python -m torch.distributed.launch" )
					if self.no_cuda:
									lowerCamelCase_						=			torch.device("cpu" )
									lowerCamelCase_						=			0
					elif is_sagemaker_model_parallel_available():
									lowerCamelCase_						=			smp.local_rank()
									lowerCamelCase_						=			torch.device("cuda"						,  lowercase )
									lowerCamelCase_						=			1
					elif is_sagemaker_dp_enabled():
									import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

									torch.distributed.init_process_group(backend="smddp"						,  timeout=self.ddp_timeout_delta )
									lowerCamelCase_						=			int(os.getenv("SMDATAPARALLEL_LOCAL_RANK" ) )
									lowerCamelCase_						=			torch.device("cuda"						,  self.local_rank )
									lowerCamelCase_						=			1
					elif self.local_rank == -1:
									# if n_gpu is > 1 we'll use nn.DataParallel.
									# If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
									# Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
									# trigger an error that a device index is missing. Index 0 takes into account the
									# GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
									# will use the first GPU in that env, i.e. GPU#1
									lowerCamelCase_						=			torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
									# Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
									# the default value.
									lowerCamelCase_						=			torch.cuda.device_count()
					else:
									# Here, we'll use torch.distributed.
									# Initializes the distributed backend which will take care of synchronizing nodes/GPUs
									if not torch.distributed.is_initialized():
													torch.distributed.init_process_group(backend="nccl"						,  timeout=self.ddp_timeout_delta )
									lowerCamelCase_						=			torch.device("cuda"						,  self.local_rank )
									lowerCamelCase_						=			1

					if device.type == "cuda":
									torch.cuda.set_device(lowercase )

					return device



	@property
	def     SCREAMING_SNAKE_CASE_(    self )				->       Tuple:
					if is_sagemaker_model_parallel_available():
									return smp.dp_size()

					return super().world_size



	@property
	def     SCREAMING_SNAKE_CASE_(    self )				->       List[str]:
					return not is_sagemaker_model_parallel_available()



	@property
	def     SCREAMING_SNAKE_CASE_(    self )				->       Dict:
					return False


