



'''simple docstring'''




# This test is meant to be run in on an instance with TPUs like this:
#
#   python examples/pytorch/xla_spawn.py --num_cores=8 tests/test_trainer_tpu.py
#
# Replace 8 with the number of TPU cores you have.
#

import sys
from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.utils import logging


a  :     int       =       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_					(				__magic_name__     ):





								def __init__(    self						,						A = 101       )     ->     List[Any]:
															UpperCAmelCase	:						Optional[int]					=						length





								def __len__(    self       )     ->     Optional[Any]:
															return self.length






								def __getitem__(    self						,						A       )     ->     int:
															return i




							class      UpperCamelCase_					:





								def __call__(    self						,						A       )     ->     Any:
															return {"input_ids": torch.tensor(A       ), "labels": torch.tensor(A       )}




							class      UpperCamelCase_					(				nn.Module     ):





								def __init__(    self       )     ->     Optional[Any]:
															super().__init__()
															# Add some (unused) params otherwise DDP will complain.
															UpperCAmelCase	:						Dict					=						nn.Linear(120						,						80       )






								def 					_lowercase(    self						,						A						,						A=None       )     ->     Union[str, Any]:
															if labels is not None:
																						return torch.tensor(0.0						,						device=input_ids.device       ), input_ids
															else:
																						return input_ids



def  __lowerCamelCase    (   ) ->     List[Any]:
							UpperCAmelCase	:						List[Any]					=						HfArgumentParser((TrainingArguments,)						)
							sys.argv += ["--output_dir", "./examples"]
							UpperCAmelCase	:						List[str]					=						parser.parse_args_into_dataclasses()[0]

							logger.warning(
							    F'''Process rank: {training_args.local_rank}, device: {training_args.device}, '''
							    F'''tpu_num_cores: {training_args.tpu_num_cores}'''      ,							)

							# 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 [1_0_0_1, 2_5_6, 1_5]:
														UpperCAmelCase	:						Any					=						DummyDataset(_lowercase						)

														def compute_metrics(_lowercase						) -> Dict:
																					UpperCAmelCase	:						List[Any]					=						list(range(len(_lowercase						)						)						)
																					UpperCAmelCase	:						List[Any]					=						p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
																					return {"success": success}

														UpperCAmelCase	:						Optional[Any]					=						Trainer(
														    model=DummyModel()      ,							args=_lowercase      ,							data_collator=DummyDataCollator()      ,							eval_dataset=_lowercase      ,							compute_metrics=_lowercase      ,							)
														UpperCAmelCase	:						Dict					=						trainer.evaluate()
														logger.info(_lowercase						)
														if metrics["eval_success"] is not True:
																					logger.error(_lowercase						)
																					exit(1						)

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

														UpperCAmelCase	:						Tuple					=						2

														UpperCAmelCase	:						Union[str, Any]					=						trainer.evaluate()
														logger.info(_lowercase						)
														if metrics["eval_success"] is not True:
																					logger.error(_lowercase						)
																					exit(1						)

														UpperCAmelCase	:						Optional[Any]					=						trainer.predict(_lowercase						)
														logger.info(p.metrics						)
														if p.metrics["test_success"] is not True:
																					logger.error(p.metrics						)
																					exit(1						)

														UpperCAmelCase	:						Tuple					=						None

							logger.info("""🔥 All distributed tests successful"""						)



def  __lowerCamelCase    (   _lowercase						) ->     Dict:
							# For xla_spawn (TPUs)
							main()


if __name__ == "__main__":
							main()






'''simple docstring'''




import functools
import operator

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


a  :     Union[str, Any]       =       logging.get_logger(__name__)

a  :     int       =       {
    """facebook/hubert-base-ls960""": """https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json""",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}

class      UpperCamelCase_					(				__magic_name__     ):
	lowercase		=					'hubert'





	def __init__(    self						,						A=32						,						A=768						,						A=12						,						A=12						,						A=3072						,						A="gelu"						,						A=0.1						,						A=0.1						,						A=0.1						,						A=True						,						A=0.0						,						A=0.1						,						A=0.1						,						A=0.0_2						,						A=1e-5						,						A="group"						,						A="gelu"						,						A=(512, 512, 512, 512, 512, 512, 512)						,						A=(5, 2, 2, 2, 2, 2, 2)						,						A=(10, 3, 3, 3, 3, 2, 2)						,						A=False						,						A=128						,						A=16						,						A=False						,						A=True						,						A=0.0_5						,						A=10						,						A=2						,						A=0.0						,						A=10						,						A=0						,						A="sum"						,						A=False						,						A=False						,						A=256						,						A=0						,						A=1						,						A=2						,						**A						,						)     ->     List[str]:
								super().__init__(**A						,						pad_token_id=A						,						bos_token_id=A						,						eos_token_id=A       )
								UpperCAmelCase	:						List[Any]					=						hidden_size
								UpperCAmelCase	:						List[Any]					=						feat_extract_norm
								UpperCAmelCase	:						Optional[Any]					=						feat_extract_activation
								UpperCAmelCase	:						Optional[int]					=						list(A       )
								UpperCAmelCase	:						int					=						list(A       )
								UpperCAmelCase	:						Any					=						list(A       )
								UpperCAmelCase	:						Any					=						conv_bias
								UpperCAmelCase	:						Dict					=						num_conv_pos_embeddings
								UpperCAmelCase	:						Tuple					=						num_conv_pos_embedding_groups
								UpperCAmelCase	:						str					=						len(self.conv_dim       )
								UpperCAmelCase	:						Optional[Any]					=						num_hidden_layers
								UpperCAmelCase	:						int					=						intermediate_size
								UpperCAmelCase	:						Any					=						hidden_act
								UpperCAmelCase	:						Tuple					=						num_attention_heads
								UpperCAmelCase	:						int					=						hidden_dropout
								UpperCAmelCase	:						Any					=						attention_dropout
								UpperCAmelCase	:						Any					=						activation_dropout
								UpperCAmelCase	:						str					=						feat_proj_layer_norm
								UpperCAmelCase	:						int					=						feat_proj_dropout
								UpperCAmelCase	:						List[Any]					=						final_dropout
								UpperCAmelCase	:						int					=						layerdrop
								UpperCAmelCase	:						Dict					=						layer_norm_eps
								UpperCAmelCase	:						Union[str, Any]					=						initializer_range
								UpperCAmelCase	:						Any					=						vocab_size
								UpperCAmelCase	:						Optional[int]					=						do_stable_layer_norm
								UpperCAmelCase	:						Optional[int]					=						use_weighted_layer_sum
								UpperCAmelCase	:						Dict					=						classifier_proj_size

								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
								UpperCAmelCase	:						Union[str, Any]					=						apply_spec_augment
								UpperCAmelCase	:						Tuple					=						mask_time_prob
								UpperCAmelCase	:						Optional[Any]					=						mask_time_length
								UpperCAmelCase	:						List[Any]					=						mask_time_min_masks
								UpperCAmelCase	:						Any					=						mask_feature_prob
								UpperCAmelCase	:						Any					=						mask_feature_length
								UpperCAmelCase	:						Tuple					=						mask_feature_min_masks

								# ctc loss
								UpperCAmelCase	:						int					=						ctc_loss_reduction
								UpperCAmelCase	:						Any					=						ctc_zero_infinity






	@property
	def 					_lowercase(    self       )     ->     Union[str, Any]:
								return functools.reduce(operator.mul						,						self.conv_stride						,						1       )

