



'''simple docstring'''



from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments
from transformers.testing_utils import TestCasePlus, require_torch, slow
from transformers.utils import is_datasets_available


if is_datasets_available():
							import datasets





class 	lowerCamelCase_							(					snake_case_			):







						@slow
						@require_torch
						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			EncoderDecoderModel.from_encoder_decoder_pretrained('''prajjwal1/bert-tiny'''				,    '''prajjwal1/bert-tiny'''				)
													SCREAMING_SNAKE_CASE					:      str     					=			BertTokenizer.from_pretrained('''bert-base-uncased'''				)

													SCREAMING_SNAKE_CASE					:      int     					=			bertabert.config.encoder.vocab_size
													SCREAMING_SNAKE_CASE					:      Dict     					=			tokenizer.sep_token_id
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			tokenizer.cls_token_id
													SCREAMING_SNAKE_CASE					:      Any     					=			1_28

													SCREAMING_SNAKE_CASE					:      List[Any]     					=			datasets.load_dataset('''cnn_dailymail'''				,    '''3.0.0'''				,    split='''train[:1%]'''				)
													SCREAMING_SNAKE_CASE					:      Any     					=			datasets.load_dataset('''cnn_dailymail'''				,    '''3.0.0'''				,    split='''validation[:1%]'''				)

													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			train_dataset.select(range(32				)				)
													SCREAMING_SNAKE_CASE					:      Any     					=			val_dataset.select(range(16				)				)

													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			4

													def _map_to_encoder_decoder_inputs(lowerCAmelCase__   :      str				):
																				# Tokenizer will automatically set [BOS] <text> [EOS]
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			tokenizer(batch['''article''']				,    padding='''max_length'''				,    truncation=lowerCAmelCase__				,    max_length=5_12				)
																				SCREAMING_SNAKE_CASE					:      Any     					=			tokenizer(batch['''highlights''']				,    padding='''max_length'''				,    truncation=lowerCAmelCase__				,    max_length=1_28				)
																				SCREAMING_SNAKE_CASE					:      str     					=			inputs.input_ids
																				SCREAMING_SNAKE_CASE					:      Tuple     					=			inputs.attention_mask

																				SCREAMING_SNAKE_CASE					:      List[str]     					=			outputs.input_ids
																				SCREAMING_SNAKE_CASE					:      int     					=			outputs.input_ids.copy()
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			[
																				    [-1_00 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['''labels''']
																				]
																				SCREAMING_SNAKE_CASE					:      Any     					=			outputs.attention_mask

																				assert all(len(lowerCAmelCase__				) == 5_12 for x in inputs.input_ids				)
																				assert all(len(lowerCAmelCase__				) == 1_28 for x in outputs.input_ids				)

																				return batch

													def _compute_metrics(lowerCAmelCase__   :      Any				):
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			pred.label_ids
																				SCREAMING_SNAKE_CASE					:      str     					=			pred.predictions

																				# all unnecessary tokens are removed
																				SCREAMING_SNAKE_CASE					:      Dict     					=			tokenizer.batch_decode(lowerCAmelCase__				,    skip_special_tokens=lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      int     					=			tokenizer.batch_decode(lowerCAmelCase__				,    skip_special_tokens=lowerCAmelCase__				)

																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			sum([int(pred_str[i] == label_str[i]				) for i in range(len(lowerCAmelCase__				)				)]				) / len(lowerCAmelCase__				)

																				return {"accuracy": accuracy}

													# map train dataset
													SCREAMING_SNAKE_CASE					:      Tuple     					=			train_dataset.map(
													    _map_to_encoder_decoder_inputs				,    batched=lowerCAmelCase__				,    batch_size=lowerCAmelCase__				,    remove_columns=['''article''', '''highlights''']				,    )
													train_dataset.set_format(
													    type='''torch'''				,    columns=['''input_ids''', '''attention_mask''', '''decoder_input_ids''', '''decoder_attention_mask''', '''labels''']				,    )

													# same for validation dataset
													SCREAMING_SNAKE_CASE					:      List[str]     					=			val_dataset.map(
													    _map_to_encoder_decoder_inputs				,    batched=lowerCAmelCase__				,    batch_size=lowerCAmelCase__				,    remove_columns=['''article''', '''highlights''']				,    )
													val_dataset.set_format(
													    type='''torch'''				,    columns=['''input_ids''', '''attention_mask''', '''decoder_input_ids''', '''decoder_attention_mask''', '''labels''']				,    )

													SCREAMING_SNAKE_CASE					:      int     					=			self.get_auto_remove_tmp_dir()

													SCREAMING_SNAKE_CASE					:      List[str]     					=			SeqaSeqTrainingArguments(
													    output_dir=lowerCAmelCase__				,    per_device_train_batch_size=lowerCAmelCase__				,    per_device_eval_batch_size=lowerCAmelCase__				,    predict_with_generate=lowerCAmelCase__				,    evaluation_strategy='''steps'''				,    do_train=lowerCAmelCase__				,    do_eval=lowerCAmelCase__				,    warmup_steps=0				,    eval_steps=2				,    logging_steps=2				,    )

													# instantiate trainer
													SCREAMING_SNAKE_CASE					:      int     					=			SeqaSeqTrainer(
													    model=lowerCAmelCase__				,    args=lowerCAmelCase__				,    compute_metrics=_compute_metrics				,    train_dataset=lowerCAmelCase__				,    eval_dataset=lowerCAmelCase__				,    tokenizer=lowerCAmelCase__				,    )

													# start training
													trainer.train()









'''simple docstring'''



import logging
import sys
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union

import librosa
import torch
from datasets import DatasetDict, load_dataset
from packaging import version
from torch import nn

from transformers import (
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaForPreTraining,
    is_apex_available,
    trainer_utils,
)
from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices


if is_apex_available():
							from apex import amp

if version.parse(version.parse(torch.__version__).base_version) >= version.parse('1.6'):
							lowerCAmelCase_     :       Union[str, Any]								=						True
							from torch.cuda.amp import autocast


lowerCAmelCase_     :       Union[str, Any]								=						logging.getLogger(__name__)
@dataclass
class 	lowerCamelCase_							:
						_lowerCAmelCase     :   str	     =				field(
						    metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}			)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}    ,				)
						_lowerCAmelCase     :   Optional[bool]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Whether to freeze the feature extractor layers of the model.'}			)
						_lowerCAmelCase     :   Optional[bool]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Whether to log verbose messages or not.'}    ,				)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=2.0    ,				metadata={'help': 'Maximum temperature for gumbel softmax.'}			)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=0.5    ,				metadata={'help': 'Minimum temperature for gumbel softmax.'}			)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=0.99_9995    ,				metadata={'help': 'Decay of gumbel temperature during training.'}			)


def 					UpperCAmelCase   (						A  :				ModelArguments					,		A  :				TrainingArguments				):
							logging.basicConfig(
							    format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s'''					,		datefmt='''%m/%d/%Y %H:%M:%S'''					,		handlers=[logging.StreamHandler(sys.stdout				)]					,		)
							SCREAMING_SNAKE_CASE					:      List[Any]     					=			logging.WARNING
							if model_args.verbose_logging:
														SCREAMING_SNAKE_CASE					:      Dict     					=			logging.DEBUG
							elif trainer_utils.is_main_process(training_args.local_rank				):
														SCREAMING_SNAKE_CASE					:      Any     					=			logging.INFO
							logger.setLevel(A				)
@dataclass
class 	lowerCamelCase_							:
						_lowerCAmelCase     :   str	     =				field(
						    default=snake_case_    ,				metadata={'help': 'The name of the dataset to use (via the datasets library).'}			)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'The configuration name of the dataset to use (via the datasets library).'}			)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default='train'    ,				metadata={
						        'help': 'The name of the training data set split to use (via the datasets library). Defaults to \'train\''
						    }    ,				)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default='validation'    ,				metadata={
						        'help': (
						            'The name of the validation data set split to use (via the datasets library). Defaults to \'validation\''
						        )
						    }    ,				)
						_lowerCAmelCase     :   Optional[str]	     =				field(
						    default='file'    ,				metadata={'help': 'Column in the dataset that contains speech file path. Defaults to \'file\''}    ,				)
						_lowerCAmelCase     :   bool	     =				field(
						    default=snake_case_    ,				metadata={'help': 'Overwrite the cached preprocessed datasets or not.'}			)
						_lowerCAmelCase     :   Optional[int]	     =				field(
						    default=1    ,				metadata={
						        'help': 'The percentage of the train set used as validation set in case there\'s no validation split'
						    }    ,				)
						_lowerCAmelCase     :   Optional[int]	     =				field(
						    default=snake_case_    ,				metadata={'help': 'The number of processes to use for the preprocessing.'}    ,				)
						_lowerCAmelCase     :   Optional[float]	     =				field(
						    default=20.0    ,				metadata={'help': 'Filter audio files that are longer than `max_duration_in_seconds` seconds'}			)


@dataclass
class 	lowerCamelCase_							:
						_lowerCAmelCase     :   WavaVecaForPreTraining
						_lowerCAmelCase     :   WavaVecaFeatureExtractor
						_lowerCAmelCase     :   Union[bool, str]	     =				"longest"
						_lowerCAmelCase     :   Optional[int]	     =				None
						_lowerCAmelCase     :   Optional[int]	     =				None







						def __call__(  self   :      Any				,    lowerCAmelCase__   :      List[Dict[str, Union[List[int], torch.Tensor]]]				):

													"""simple docstring"""
													# reformat list to dict and set to pytorch format
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.feature_extractor.pad(
													    lowerCAmelCase__				,    max_length=self.max_length				,    padding=self.padding				,    pad_to_multiple_of=self.pad_to_multiple_of				,    return_tensors='''pt'''				,    )
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			self.model._get_feat_extract_output_lengths(batch['''input_values'''].shape[-1]				)

													SCREAMING_SNAKE_CASE					:      Tuple     					=			batch['''input_values'''].shape[0]

													# make sure that no loss is computed on padded inputs
													if batch["attention_mask"] is not None:
																				# compute real output lengths according to convolution formula
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.model._get_feat_extract_output_lengths(batch['''attention_mask'''].sum(-1				)				).to(
																				    torch.long				)

																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			torch.zeros(
																				    (batch_size, mask_indices_seq_length)				,    dtype=torch.long				,    device=batch['''input_values'''].device				)

																				# these two operations makes sure that all values
																				# before the output lengths indices are attended to
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			1
																				SCREAMING_SNAKE_CASE					:      Dict     					=			attention_mask.flip([-1]				).cumsum(-1				).flip([-1]				).bool()

													# sample randomly masked indices
													SCREAMING_SNAKE_CASE					:      List[str]     					=			_compute_mask_indices(
													    (batch_size, mask_indices_seq_length)				,    self.model.config.mask_time_prob				,    self.model.config.mask_time_length				,    attention_mask=lowerCAmelCase__				,    min_masks=2				,    )

													return batch


class 	lowerCamelCase_							(					snake_case_			):







						def __init__(  self   :      str				,    *lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Any=1				,    lowerCAmelCase__   :      Dict=0				,    lowerCAmelCase__   :      Tuple=1.0				,    **lowerCAmelCase__   :      Optional[Any]				):

													"""simple docstring"""
													super().__init__(*lowerCAmelCase__				,    **lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			0
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			max_gumbel_temp
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			min_gumbel_temp
													SCREAMING_SNAKE_CASE					:      Tuple     					=			gumbel_temp_decay







						def 					__lowercase    (  self   :      str				,    lowerCAmelCase__   :      nn.Module				,    lowerCAmelCase__   :      Dict[str, Union[torch.Tensor, Any]]				):

													"""simple docstring"""
													model.train()
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			self._prepare_inputs(lowerCAmelCase__				)

													if self.use_amp:
																				with autocast():
																											SCREAMING_SNAKE_CASE					:      Any     					=			self.compute_loss(lowerCAmelCase__				,    lowerCAmelCase__				)
													else:
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.compute_loss(lowerCAmelCase__				,    lowerCAmelCase__				)

													if self.args.n_gpu > 1 or self.deepspeed:
																				if model.module.config.ctc_loss_reduction == "mean":
																											SCREAMING_SNAKE_CASE					:      Any     					=			loss.mean()
																				elif model.module.config.ctc_loss_reduction == "sum":
																											SCREAMING_SNAKE_CASE					:      Tuple     					=			loss.sum() / (inputs['''mask_time_indices''']).sum()
																				else:
																											raise ValueError(F"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']"""				)

													if self.args.gradient_accumulation_steps > 1:
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			loss / self.args.gradient_accumulation_steps

													if self.use_amp:
																				self.scaler.scale(lowerCAmelCase__				).backward()
													elif self.use_apex:
																				with amp.scale_loss(lowerCAmelCase__				,    self.optimizer				) as scaled_loss:
																											scaled_loss.backward()
													elif self.deepspeed:
																				self.deepspeed.backward(lowerCAmelCase__				)
													else:
																				loss.backward()

													self.num_update_step += 1
													# make sure gumbel softmax temperature is decayed
													if self.args.n_gpu > 1 or self.deepspeed:
																				model.module.set_gumbel_temperature(
																				    max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step				,    self.min_gumbel_temp				)				)
													else:
																				model.set_gumbel_temperature(
																				    max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step				,    self.min_gumbel_temp				)				)

													return loss.detach()


def 					UpperCAmelCase   (						):
							# See all possible arguments in src/transformers/training_args.py
							# or by passing the --help flag to this script.
							# We now keep distinct sets of args, for a cleaner separation of concerns.

							SCREAMING_SNAKE_CASE					:      Optional[int]     					=			HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)				)

							SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[Any]     					=			parser.parse_args_into_dataclasses()
							configure_logger(A					,		A				)

							# Downloading and loading a dataset from the hub.
							SCREAMING_SNAKE_CASE					:      str     					=			load_dataset(data_args.dataset_name					,		data_args.dataset_config_name					,		cache_dir=model_args.cache_dir				)

							if "validation" not in datasets.keys():
														# make sure only "validation" and "train" keys remain"
														SCREAMING_SNAKE_CASE					:      Any     					=			DatasetDict()
														SCREAMING_SNAKE_CASE					:      Any     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split=F"""{data_args.train_split_name}[:{data_args.validation_split_percentage}%]"""					,		cache_dir=model_args.cache_dir					,		)
														SCREAMING_SNAKE_CASE					:      List[str]     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split=F"""{data_args.train_split_name}[{data_args.validation_split_percentage}%:]"""					,		cache_dir=model_args.cache_dir					,		)
							else:
														# make sure only "validation" and "train" keys remain"
														SCREAMING_SNAKE_CASE					:      str     					=			DatasetDict()
														SCREAMING_SNAKE_CASE					:      Any     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split='''validation'''					,		cache_dir=model_args.cache_dir					,		)
														SCREAMING_SNAKE_CASE					:      Tuple     					=			load_dataset(
														    data_args.dataset_name					,		data_args.dataset_config_name					,		split=F"""{data_args.train_split_name}"""					,		cache_dir=model_args.cache_dir					,		)

							# only normalized-inputs-training is supported
							SCREAMING_SNAKE_CASE					:      int     					=			WavaVecaFeatureExtractor.from_pretrained(
							    model_args.model_name_or_path					,		cache_dir=model_args.cache_dir					,		do_normalize=A				)

							def prepare_dataset(A  :				str				):
														# check that all files have the correct sampling rate
														SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      List[str]     					=			librosa.load(batch[data_args.speech_file_column]					,		sr=feature_extractor.sampling_rate				)
														return batch

							# load audio files into numpy arrays
							SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			datasets.map(
							    A					,		num_proc=data_args.preprocessing_num_workers					,		remove_columns=datasets['''train'''].column_names				)

							# filter audio files that are too long
							SCREAMING_SNAKE_CASE					:      int     					=			vectorized_datasets.filter(
							    lambda A				: len(data['''speech''']				) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate				)				)

							def normalize(A  :				List[Any]				):
														return feature_extractor(batch['''speech''']					,		sampling_rate=feature_extractor.sampling_rate				)

							# normalize and transform to `BatchFeatures`
							SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			vectorized_datasets.map(
							    A					,		batched=A					,		num_proc=data_args.preprocessing_num_workers					,		load_from_cache_file=not data_args.overwrite_cache					,		remove_columns=vectorized_datasets['''train'''].column_names					,		)

							# pretraining is only supported for "newer" stable layer norm architecture
							# apply_spec_augment has to be True, mask_feature_prob has to be 0.0
							SCREAMING_SNAKE_CASE					:      Tuple     					=			WavaVecaConfig.from_pretrained(
							    model_args.model_name_or_path					,		cache_dir=model_args.cache_dir					,		gradient_checkpointing=training_args.gradient_checkpointing					,		)

							if not config.do_stable_layer_norm or config.feat_extract_norm != "layer":
														raise ValueError(
														    '''PreTraining is only supported for ``config.do_stable_layer_norm=True`` and'''
														    ''' ``config.feat_extract_norm=\'layer\''''				)

							SCREAMING_SNAKE_CASE					:      str     					=			WavaVecaForPreTraining(A				)

							SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			DataCollatorForWavaVecaPretraining(model=A					,		feature_extractor=A				)

							SCREAMING_SNAKE_CASE					:      Dict     					=			WavaVecaPreTrainer(
							    model=A					,		data_collator=A					,		args=A					,		train_dataset=vectorized_datasets['''train''']					,		eval_dataset=vectorized_datasets['''validation''']					,		tokenizer=A					,		max_gumbel_temp=model_args.max_gumbel_temperature					,		min_gumbel_temp=model_args.min_gumbel_temperature					,		gumbel_temp_decay=model_args.gumbel_temperature_decay					,		)
							trainer.train()


if __name__ == "__main__":
							main()




