
import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
		from transformers import SpeechaTextFeatureExtractor

lowercase						:      List[str]									= random.Random()




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    List[Any]		,						_lowerCamelCase				:    str=1.0		,						_lowerCamelCase				:    str=None		,						_lowerCamelCase				:    Any=None)      ->					Optional[int]:






							'''simple docstring'''





							if rng is None:
														__UpperCamelCase			:     Optional[int]         =      global_rng

							__UpperCamelCase			:     Tuple         =      []
							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	:int  ,     a	:Any  ,     a	:Any=7  ,     a	:Dict=4_0_0  ,     a	:int=2_0_0_0  ,     a	:Dict=2_4  ,     a	:int=2_4  ,     a	:Dict=0.0  ,     a	:Optional[int]=1_6_0_0_0  ,     a	:List[Any]=True  ,     a	:int=True  ,     )	->				Optional[Any]:
								__UpperCamelCase			:     int         =      parent
								__UpperCamelCase			:     List[Any]         =      batch_size
								__UpperCamelCase			:     Tuple         =      min_seq_length
								__UpperCamelCase			:     Optional[int]         =      max_seq_length
								__UpperCamelCase			:     int         =      (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
								__UpperCamelCase			:     int         =      feature_size
								__UpperCamelCase			:     Any         =      num_mel_bins
								__UpperCamelCase			:     Dict         =      padding_value
								__UpperCamelCase			:     Dict         =      sampling_rate
								__UpperCamelCase			:     Any         =      return_attention_mask
								__UpperCamelCase			:     List[Any]         =      do_normalize







	def      _lowerCamelCase       (      self	:Dict		)	->				List[str]:
								return {
								    "feature_size": self.feature_size,
								    "num_mel_bins": self.num_mel_bins,
								    "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	:Any  ,     a	:Tuple=False  ,     a	:List[Any]=False		)	->				List[Any]:
								def _flatten(a	:Tuple		):
															return list(itertools.chain(*a		)		)

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




@require_torch
@require_torchaudio
class 				lowerCamelCase__							(       lowercase_  ,		unittest.TestCase):




	'''simple docstring'''




	_A			=  SpeechaTextFeatureExtractor if is_speech_available() else None







	def      _lowerCamelCase       (      self	:Tuple		)	->				Optional[Any]:
								__UpperCamelCase			:     Any         =      SpeechaTextFeatureExtractionTester(self		)







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

								# Test feature size
								__UpperCamelCase			:     Any         =      feature_extractor(a  ,     padding=a  ,     return_tensors="np"		).input_features
								self.assertTrue(input_features.ndim == 3		)
								self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size		)

								# Test not batched input
								__UpperCamelCase			:     Optional[Any]         =      feature_extractor(speech_inputs[0]  ,     return_tensors="np"		).input_features
								__UpperCamelCase			:     int         =      feature_extractor(np_speech_inputs[0]  ,     return_tensors="np"		).input_features
								self.assertTrue(np.allclose(a  ,     a  ,     atol=1E-3		)		)

								# Test batched
								__UpperCamelCase			:     Dict         =      feature_extractor(a  ,     return_tensors="np"		).input_features
								__UpperCamelCase			:     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		)		)

								# Test 2-D numpy arrays are batched.
								__UpperCamelCase			:     Tuple         =      [floats_list((1, x)		)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
								__UpperCamelCase			:     Optional[Any]         =      np.asarray(a		)
								__UpperCamelCase			:     List[Any]         =      feature_extractor(a  ,     return_tensors="np"		).input_features
								__UpperCamelCase			:     Optional[int]         =      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	:Union[str, Any]		)	->				str:
								__UpperCamelCase			:     Optional[int]         =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
								__UpperCamelCase			:     Dict         =      [floats_list((1, x)		)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0		)]

								__UpperCamelCase			:     str         =      ["longest", "max_length", "do_not_pad"]
								__UpperCamelCase			:     Tuple         =      [None, 1_6, None]
								for max_length, padding in zip(a  ,     a		):
															__UpperCamelCase			:     Tuple         =      feature_extractor(
															    a  ,     padding=a  ,     max_length=a  ,     return_attention_mask=a		)
															__UpperCamelCase			:     List[Any]         =      inputs.input_features
															__UpperCamelCase			:     List[Any]         =      inputs.attention_mask
															__UpperCamelCase			:     Dict         =      [np.sum(a		) for x in attention_mask]

															self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]		)
															self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]		)
															self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]		)







	def      _lowerCamelCase       (      self	:Optional[Any]		)	->				List[Any]:
								__UpperCamelCase			:     List[str]         =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
								__UpperCamelCase			:     Optional[int]         =      [floats_list((1, x)		)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0		)]

								__UpperCamelCase			:     List[str]         =      ["longest", "max_length", "do_not_pad"]
								__UpperCamelCase			:     List[Any]         =      [None, 1_6, None]
								for max_length, padding in zip(a  ,     a		):
															__UpperCamelCase			:     Union[str, Any]         =      feature_extractor(
															    a  ,     max_length=a  ,     padding=a  ,     return_tensors="np"  ,     return_attention_mask=a		)
															__UpperCamelCase			:     List[Any]         =      inputs.input_features
															__UpperCamelCase			:     Union[str, Any]         =      inputs.attention_mask
															__UpperCamelCase			:     int         =      [np.sum(a		) for x in attention_mask]

															self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]]		)
															self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6		)
															self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]]		)
															self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6		)
															self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]]		)







	def      _lowerCamelCase       (      self	:Any		)	->				List[str]:
								__UpperCamelCase			:     Tuple         =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
								__UpperCamelCase			:     Tuple         =      [floats_list((1, x)		)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0		)]
								__UpperCamelCase			:     Tuple         =      feature_extractor(
								    a  ,     padding="max_length"  ,     max_length=4  ,     truncation=a  ,     return_tensors="np"  ,     return_attention_mask=a  ,     )
								__UpperCamelCase			:     Optional[Any]         =      inputs.input_features
								__UpperCamelCase			:     List[Any]         =      inputs.attention_mask
								__UpperCamelCase			:     List[str]         =      np.sum(attention_mask == 1  ,     axis=1		)

								self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
								self._check_zero_mean_unit_variance(input_features[1]		)
								self._check_zero_mean_unit_variance(input_features[2]		)







	def      _lowerCamelCase       (      self	:str		)	->				List[Any]:
								__UpperCamelCase			:     Optional[Any]         =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
								__UpperCamelCase			:     int         =      [floats_list((1, x)		)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0		)]
								__UpperCamelCase			:     Dict         =      feature_extractor(
								    a  ,     padding="longest"  ,     max_length=4  ,     truncation=a  ,     return_tensors="np"  ,     return_attention_mask=a  ,     )
								__UpperCamelCase			:     int         =      inputs.input_features
								__UpperCamelCase			:     int         =      inputs.attention_mask
								__UpperCamelCase			:     int         =      np.sum(attention_mask == 1  ,     axis=1		)

								self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
								self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]		)
								self._check_zero_mean_unit_variance(input_features[2]		)

								# make sure that if max_length < longest -> then pad to max_length
								self.assertEqual(input_features.shape  ,     (3, 4, 2_4)		)

								__UpperCamelCase			:     str         =      [floats_list((1, x)		)[0] for x in range(8_0_0  ,     1_4_0_0  ,     2_0_0		)]
								__UpperCamelCase			:     List[Any]         =      feature_extractor(
								    a  ,     padding="longest"  ,     max_length=1_6  ,     truncation=a  ,     return_tensors="np"  ,     return_attention_mask=a  ,     )
								__UpperCamelCase			:     Optional[Any]         =      inputs.input_features
								__UpperCamelCase			:     Dict         =      inputs.attention_mask
								__UpperCamelCase			:     Tuple         =      np.sum(attention_mask == 1  ,     axis=1		)

								self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]]		)
								self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]]		)
								self._check_zero_mean_unit_variance(input_features[2]		)

								# make sure that if max_length < longest -> then pad to max_length
								self.assertEqual(input_features.shape  ,     (3, 6, 2_4)		)







	def      _lowerCamelCase       (      self	:Optional[Any]		)	->				Union[str, Any]:
								import torch

								__UpperCamelCase			:     Any         =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
								__UpperCamelCase			:     str         =      np.random.rand(1_0_0  ,     3_2		).astype(np.floataa		)
								__UpperCamelCase			:     Optional[int]         =      np_speech_inputs.tolist()

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







	def      _lowerCamelCase       (      self	:List[str]  ,     a	:Any		)	->				Optional[Any]:
								from datasets import load_dataset

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

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







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				List[str]:
								# fmt: off
								__UpperCamelCase			:     Dict         =      np.array([
								    -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
								    -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
								    -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
								]		)
								# fmt: on

								__UpperCamelCase			:     str         =      self._load_datasamples(1		)
								__UpperCamelCase			:     str         =      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
								__UpperCamelCase			:     Dict         =      feature_extractor(a  ,     return_tensors="pt"		).input_features
								self.assertEquals(input_features.shape  ,     (1, 5_8_4, 2_4)		)
								self.assertTrue(np.allclose(input_features[0, 0, :3_0]  ,     a  ,     atol=1E-4		)		)

from .constants import (
    MODEL_NAME,
    OPTIMIZER_NAME,
    RNG_STATE_NAME,
    SAFE_WEIGHTS_INDEX_NAME,
    SAFE_WEIGHTS_NAME,
    SCALER_NAME,
    SCHEDULER_NAME,
    TORCH_LAUNCH_PARAMS,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
)
from .dataclasses import (
    BnbQuantizationConfig,
    ComputeEnvironment,
    CustomDtype,
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    DynamoBackend,
    FPaRecipeKwargs,
    FullyShardedDataParallelPlugin,
    GradientAccumulationPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    KwargsHandler,
    LoggerType,
    MegatronLMPlugin,
    PrecisionType,
    ProjectConfiguration,
    RNGType,
    SageMakerDistributedType,
    TensorInformation,
    TorchDynamoPlugin,
)
from .environment import get_int_from_env, parse_choice_from_env, parse_flag_from_env
from .imports import (
    get_ccl_version,
    is_abit_bnb_available,
    is_abit_bnb_available,
    is_aim_available,
    is_bfaa_available,
    is_bnb_available,
    is_botoa_available,
    is_ccl_available,
    is_comet_ml_available,
    is_datasets_available,
    is_deepspeed_available,
    is_fpa_available,
    is_ipex_available,
    is_megatron_lm_available,
    is_mlflow_available,
    is_mps_available,
    is_npu_available,
    is_rich_available,
    is_safetensors_available,
    is_sagemaker_available,
    is_tensorboard_available,
    is_tpu_available,
    is_transformers_available,
    is_wandb_available,
    is_xpu_available,
)
from .modeling import (
    check_device_map,
    check_tied_parameters_in_config,
    check_tied_parameters_on_same_device,
    compute_module_sizes,
    convert_file_size_to_int,
    dtype_byte_size,
    find_tied_parameters,
    get_balanced_memory,
    get_max_layer_size,
    get_max_memory,
    get_mixed_precision_context_manager,
    id_tensor_storage,
    infer_auto_device_map,
    load_checkpoint_in_model,
    load_offloaded_weights,
    load_state_dict,
    named_module_tensors,
    retie_parameters,
    set_module_tensor_to_device,
    shard_checkpoint,
)
from .offload import (
    OffloadedWeightsLoader,
    PrefixedDataset,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
    save_offload_index,
)
from .operations import (
    broadcast,
    broadcast_object_list,
    concatenate,
    convert_outputs_to_fpaa,
    convert_to_fpaa,
    find_batch_size,
    find_device,
    gather,
    gather_object,
    get_data_structure,
    honor_type,
    initialize_tensors,
    is_namedtuple,
    is_tensor_information,
    is_torch_tensor,
    listify,
    pad_across_processes,
    recursively_apply,
    reduce,
    send_to_device,
    slice_tensors,
)
from .versions import compare_versions, is_torch_version


if is_deepspeed_available():
		from .deepspeed import (
		    DeepSpeedEngineWrapper,
		    DeepSpeedOptimizerWrapper,
		    DeepSpeedSchedulerWrapper,
		    DummyOptim,
		    DummyScheduler,
		    HfDeepSpeedConfig,
		)

from .bnb import has_abit_bnb_layers, load_and_quantize_model
from .fsdp_utils import load_fsdp_model, load_fsdp_optimizer, save_fsdp_model, save_fsdp_optimizer
from .launch import (
    PrepareForLaunch,
    _filter_args,
    prepare_deepspeed_cmd_env,
    prepare_multi_gpu_env,
    prepare_sagemager_args_inputs,
    prepare_simple_launcher_cmd_env,
    prepare_tpu,
)
from .megatron_lm import (
    AbstractTrainStep,
    BertTrainStep,
    GPTTrainStep,
    MegatronEngine,
    MegatronLMDummyDataLoader,
    MegatronLMDummyScheduler,
    MegatronLMOptimizerWrapper,
    MegatronLMSchedulerWrapper,
    TaTrainStep,
    avg_losses_across_data_parallel_group,
    gather_across_data_parallel_groups,
)
from .megatron_lm import initialize as megatron_lm_initialize
from .megatron_lm import prepare_data_loader as megatron_lm_prepare_data_loader
from .megatron_lm import prepare_model as megatron_lm_prepare_model
from .megatron_lm import prepare_optimizer as megatron_lm_prepare_optimizer
from .megatron_lm import prepare_scheduler as megatron_lm_prepare_scheduler
from .memory import find_executable_batch_size, release_memory
from .other import (
    extract_model_from_parallel,
    get_pretty_name,
    is_port_in_use,
    merge_dicts,
    patch_environment,
    save,
    wait_for_everyone,
    write_basic_config,
)
from .random import set_seed, synchronize_rng_state, synchronize_rng_states
from .torch_xla import install_xla
from .tqdm import tqdm
from .transformer_engine import convert_model, has_transformer_engine_layers