


"""simple docstring"""







import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a__				:							Optional[int]						=     logging.get_logger(__name__)

a__				:							Dict						=     {
    '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''',
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}




class  UpperCamelCase_	( UpperCamelCase):

	"""simple docstring"""





	snake_case__				:  Tuple    					=			"wav2vec2"




	def __init__( self			:				List[Any]			,			UpperCAmelCase__			:				Optional[Any]=3_2			,			UpperCAmelCase__			:				Dict=7_6_8			,			UpperCAmelCase__			:				Dict=1_2			,			UpperCAmelCase__			:				List[str]=1_2			,			UpperCAmelCase__			:				List[Any]=3_0_7_2			,			UpperCAmelCase__			:				int="gelu"			,			UpperCAmelCase__			:				int=0.1			,			UpperCAmelCase__			:				Optional[Any]=0.1			,			UpperCAmelCase__			:				Dict=0.1			,			UpperCAmelCase__			:				List[Any]=0.0			,			UpperCAmelCase__			:				Any=0.0			,			UpperCAmelCase__			:				Dict=0.1			,			UpperCAmelCase__			:				List[str]=0.1			,			UpperCAmelCase__			:				Optional[int]=0.02			,			UpperCAmelCase__			:				List[Any]=1E-5			,			UpperCAmelCase__			:				Optional[int]="group"			,			UpperCAmelCase__			:				Union[str, Any]="gelu"			,			UpperCAmelCase__			:				int=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)			,			UpperCAmelCase__			:				str=(5, 2, 2, 2, 2, 2, 2)			,			UpperCAmelCase__			:				str=(1_0, 3, 3, 3, 3, 2, 2)			,			UpperCAmelCase__			:				Any=False			,			UpperCAmelCase__			:				List[str]=1_2_8			,			UpperCAmelCase__			:				Optional[int]=1_6			,			UpperCAmelCase__			:				Optional[int]=False			,			UpperCAmelCase__			:				Union[str, Any]=True			,			UpperCAmelCase__			:				Optional[int]=0.05			,			UpperCAmelCase__			:				Tuple=1_0			,			UpperCAmelCase__			:				Union[str, Any]=2			,			UpperCAmelCase__			:				List[str]=0.0			,			UpperCAmelCase__			:				Union[str, Any]=1_0			,			UpperCAmelCase__			:				Tuple=0			,			UpperCAmelCase__			:				str=3_2_0			,			UpperCAmelCase__			:				List[Any]=2			,			UpperCAmelCase__			:				Union[str, Any]=0.1			,			UpperCAmelCase__			:				Dict=1_0_0			,			UpperCAmelCase__			:				str=2_5_6			,			UpperCAmelCase__			:				List[Any]=2_5_6			,			UpperCAmelCase__			:				int=0.1			,			UpperCAmelCase__			:				Dict="sum"			,			UpperCAmelCase__			:				Union[str, Any]=False			,			UpperCAmelCase__			:				Union[str, Any]=False			,			UpperCAmelCase__			:				str=2_5_6			,			UpperCAmelCase__			:				List[Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)			,			UpperCAmelCase__			:				Any=(5, 3, 3, 1, 1)			,			UpperCAmelCase__			:				Union[str, Any]=(1, 2, 3, 1, 1)			,			UpperCAmelCase__			:				Tuple=5_1_2			,			UpperCAmelCase__			:				Optional[int]=0			,			UpperCAmelCase__			:				List[Any]=1			,			UpperCAmelCase__			:				Union[str, Any]=2			,			UpperCAmelCase__			:				Any=False			,			UpperCAmelCase__			:				List[str]=3			,			UpperCAmelCase__			:				Optional[Any]=2			,			UpperCAmelCase__			:				Optional[Any]=3			,			UpperCAmelCase__			:				Union[str, Any]=None			,			UpperCAmelCase__			:				Dict=None			,			**UpperCAmelCase__			:				Dict			,			)       ->	Optional[int]:
						super().__init__(**__a			,			pad_token_id=__a			,			bos_token_id=__a			,			eos_token_id=__a   )
						__SCREAMING_SNAKE_CASE						   =				hidden_size
						__SCREAMING_SNAKE_CASE						   =				feat_extract_norm
						__SCREAMING_SNAKE_CASE						   =				feat_extract_activation
						__SCREAMING_SNAKE_CASE						   =				list(__a   )
						__SCREAMING_SNAKE_CASE						   =				list(__a   )
						__SCREAMING_SNAKE_CASE						   =				list(__a   )
						__SCREAMING_SNAKE_CASE						   =				conv_bias
						__SCREAMING_SNAKE_CASE						   =				num_conv_pos_embeddings
						__SCREAMING_SNAKE_CASE						   =				num_conv_pos_embedding_groups
						__SCREAMING_SNAKE_CASE						   =				len(self.conv_dim   )
						__SCREAMING_SNAKE_CASE						   =				num_hidden_layers
						__SCREAMING_SNAKE_CASE						   =				intermediate_size
						__SCREAMING_SNAKE_CASE						   =				hidden_act
						__SCREAMING_SNAKE_CASE						   =				num_attention_heads
						__SCREAMING_SNAKE_CASE						   =				hidden_dropout
						__SCREAMING_SNAKE_CASE						   =				attention_dropout
						__SCREAMING_SNAKE_CASE						   =				activation_dropout
						__SCREAMING_SNAKE_CASE						   =				feat_proj_dropout
						__SCREAMING_SNAKE_CASE						   =				final_dropout
						__SCREAMING_SNAKE_CASE						   =				layerdrop
						__SCREAMING_SNAKE_CASE						   =				layer_norm_eps
						__SCREAMING_SNAKE_CASE						   =				initializer_range
						__SCREAMING_SNAKE_CASE						   =				vocab_size
						__SCREAMING_SNAKE_CASE						   =				do_stable_layer_norm
						__SCREAMING_SNAKE_CASE						   =				use_weighted_layer_sum

						if (
						    (len(self.conv_stride   ) != self.num_feat_extract_layers)
						    or (len(self.conv_kernel   ) != self.num_feat_extract_layers)
						    or (len(self.conv_dim   ) != self.num_feat_extract_layers)
						):
											raise ValueError(
											    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
											    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
											    F""" {len(self.conv_dim   )}`, `len(config.conv_stride) = {len(self.conv_stride   )}`,"""
											    F""" `len(config.conv_kernel) = {len(self.conv_kernel   )}`."""   )

						# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
						__SCREAMING_SNAKE_CASE						   =				apply_spec_augment
						__SCREAMING_SNAKE_CASE						   =				mask_time_prob
						__SCREAMING_SNAKE_CASE						   =				mask_time_length
						__SCREAMING_SNAKE_CASE						   =				mask_time_min_masks
						__SCREAMING_SNAKE_CASE						   =				mask_feature_prob
						__SCREAMING_SNAKE_CASE						   =				mask_feature_length
						__SCREAMING_SNAKE_CASE						   =				mask_feature_min_masks

						# parameters for pretraining with codevector quantized representations
						__SCREAMING_SNAKE_CASE						   =				num_codevectors_per_group
						__SCREAMING_SNAKE_CASE						   =				num_codevector_groups
						__SCREAMING_SNAKE_CASE						   =				contrastive_logits_temperature
						__SCREAMING_SNAKE_CASE						   =				feat_quantizer_dropout
						__SCREAMING_SNAKE_CASE						   =				num_negatives
						__SCREAMING_SNAKE_CASE						   =				codevector_dim
						__SCREAMING_SNAKE_CASE						   =				proj_codevector_dim
						__SCREAMING_SNAKE_CASE						   =				diversity_loss_weight

						# ctc loss
						__SCREAMING_SNAKE_CASE						   =				ctc_loss_reduction
						__SCREAMING_SNAKE_CASE						   =				ctc_zero_infinity

						# adapter
						__SCREAMING_SNAKE_CASE						   =				add_adapter
						__SCREAMING_SNAKE_CASE						   =				adapter_kernel_size
						__SCREAMING_SNAKE_CASE						   =				adapter_stride
						__SCREAMING_SNAKE_CASE						   =				num_adapter_layers
						__SCREAMING_SNAKE_CASE						   =				output_hidden_size or hidden_size
						__SCREAMING_SNAKE_CASE						   =				adapter_attn_dim

						# SequenceClassification-specific parameter. Feel free to ignore for other classes.
						__SCREAMING_SNAKE_CASE						   =				classifier_proj_size

						# XVector-specific parameters. Feel free to ignore for other classes.
						__SCREAMING_SNAKE_CASE						   =				list(__a   )
						__SCREAMING_SNAKE_CASE						   =				list(__a   )
						__SCREAMING_SNAKE_CASE						   =				list(__a   )
						__SCREAMING_SNAKE_CASE						   =				xvector_output_dim




	@property
	def   UpperCAmelCase_      ( self			:				Optional[Any]   )       ->	Optional[int]:
						return functools.reduce(operator.mul			,			self.conv_stride			,			1   )





from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    get_torch_dist_unique_port,
    require_torch_multi_gpu,
    require_torch_neuroncore,
)
from transformers.training_args import ParallelMode
from transformers.utils import logging


_snake_case		=  logging.get_logger(__name__)


if is_torch_available():
						import torch
						from torch import nn
						from torch.utils.data import Dataset

						from transformers import Trainer



						class   UpperCAmelCase_    (       a):







													def __init__(   self,			__a = 101):

																				'''simple docstring'''

																				_lowerCAmelCase     :							str    =    length







													def __len__(   self):

																				'''simple docstring'''

																				return self.length







													def __getitem__(   self,			__a):

																				'''simple docstring'''

																				return i



						class   UpperCAmelCase_    :







													def __call__(   self,			__a):

																				'''simple docstring'''

																				return {"input_ids": torch.tensor(__a), "labels": torch.tensor(__a)}



						class   UpperCAmelCase_    (       nn.Module):







													def __init__(   self):

																				'''simple docstring'''

																				super().__init__()
																				# Add some (unused) params otherwise DDP will complain.
																				_lowerCAmelCase     :							str    =    nn.Linear(120,			80)







													def    snake_case__      (   self,			__a,			__a=None):

																				'''simple docstring'''

																				if labels is not None:
																											return torch.tensor(0.0,			device=input_ids.device), input_ids
																				else:
																											return input_ids



class   UpperCAmelCase_    (       a):







							@require_torch_neuroncore
							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							int    =    f"--nproc_per_node=2\n            --master_port={get_torch_dist_unique_port()}\n            {self.test_file_dir}/test_trainer_distributed.py\n        ".split()
														_lowerCAmelCase     :							Tuple    =    self.get_auto_remove_tmp_dir()
														_lowerCAmelCase     :							Optional[int]    =    f"--output_dir {output_dir}".split()
														_lowerCAmelCase     :							List[Any]    =    ["torchrun"] + distributed_args + args
														execute_subprocess_async(__a,			env=self.get_env())
														# successful return here == success - any errors would have caused an error in the sub-call



class   UpperCAmelCase_    (       a):







							@require_torch_multi_gpu
							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Dict    =    f"--nproc_per_node={torch.cuda.device_count()}\n            --master_port={get_torch_dist_unique_port()}\n            {self.test_file_dir}/test_trainer_distributed.py\n        ".split()
														_lowerCAmelCase     :							Any    =    self.get_auto_remove_tmp_dir()
														_lowerCAmelCase     :							Optional[int]    =    f"--output_dir {output_dir}".split()
														_lowerCAmelCase     :							Any    =    ["torchrun"] + distributed_args + args
														execute_subprocess_async(__a,			env=self.get_env())
														# successful return here == success - any errors would have caused an error in the sub-call


if __name__ == "__main__":
						# The script below is meant to be run under torch.distributed, on a machine with multiple GPUs:
						#
						# PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py

						_snake_case		=  HfArgumentParser((TrainingArguments,))
						_snake_case		=  parser.parse_args_into_dataclasses()[0]

						logger.warning(
						    f'''Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, '''
						    f'''distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}'''
						)

						# Essentially, what we want to verify in the distributed case is that we get all samples back,
						# in the right order. (this is crucial for prediction for instance)
						for dataset_length in [101, 40, 7]:
												_snake_case		=  DummyDataset(dataset_length)






												def 				A				(					_lowerCamelCase  ):





																			'''simple docstring'''




																			_lowerCAmelCase     :							Dict    =    list(range(len(_lowerCamelCase  )  )  )
																			_lowerCAmelCase     :							Union[str, Any]    =    p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
																			if not success and training_args.local_rank == 0:
																										logger.warning(
																										    "Predictions and/or labels do not match expected results:\n  - predictions: "
																										    F"{p.predictions.tolist()}\n  - labels: {p.label_ids.tolist()}\n  - expected: {sequential}"  )
																			return {"success": success}

												_snake_case		=  Trainer(
												    model=DummyModel(),
												    args=training_args,
												    data_collator=DummyDataCollator(),
												    eval_dataset=dataset,
												    compute_metrics=compute_metrics,
												)
												_snake_case		=  trainer.evaluate()
												logger.info(metrics)
												if metrics["eval_success"] is not True:
																		logger.error(metrics)
																		exit(1)

												_snake_case		=  trainer.predict(dataset)
												logger.info(p.metrics)
												if p.metrics["test_success"] is not True:
																		logger.error(p.metrics)
																		exit(1)

												_snake_case		=  2

												_snake_case		=  trainer.evaluate()
												logger.info(metrics)
												if metrics["eval_success"] is not True:
																		logger.error(metrics)
																		exit(1)

												_snake_case		=  trainer.predict(dataset)
												logger.info(p.metrics)
												if p.metrics["test_success"] is not True:
																		logger.error(p.metrics)
																		exit(1)



												_snake_case		=  None
