

"""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"):
			__A						       =      True
			from torch.cuda.amp import autocast


__A						       =      logging.getLogger(__name__)





@dataclass
class    snake_case     :
							SCREAMING_SNAKE_CASE_       :				Optional[Any]       				=  field(
							    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}  )
							SCREAMING_SNAKE_CASE_       :				List[str]       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""},							)
							SCREAMING_SNAKE_CASE_       :				Tuple       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""}  )
							SCREAMING_SNAKE_CASE_       :				List[Any]       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """Whether to log verbose messages or not."""},							)
							SCREAMING_SNAKE_CASE_       :				str       				=  field(
							    default=2.0,							metadata={"""help""": """Maximum temperature for gumbel softmax."""}  )
							SCREAMING_SNAKE_CASE_       :				List[Any]       				=  field(
							    default=0.5,							metadata={"""help""": """Minimum temperature for gumbel softmax."""}  )
							SCREAMING_SNAKE_CASE_       :				str       				=  field(
							    default=0.99_9995,							metadata={"""help""": """Decay of gumbel temperature during training."""}  )






def    a__   (   __SCREAMING_SNAKE_CASE ,			__SCREAMING_SNAKE_CASE			)     ->     Any:
		logging.basicConfig(
		    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s" ,			datefmt="%m/%d/%Y %H:%M:%S" ,			handlers=[logging.StreamHandler(sys.stdout			)] ,			)
		__lowerCAmelCase:	Any   				=      logging.WARNING
		if model_args.verbose_logging:
				__lowerCAmelCase:	Any   				=      logging.DEBUG
		elif trainer_utils.is_main_process(training_args.local_rank			):
				__lowerCAmelCase:	Any   				=      logging.INFO
		logger.setLevel(__SCREAMING_SNAKE_CASE			)





@dataclass
class    snake_case     :
							SCREAMING_SNAKE_CASE_       :				Union[str, Any]       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """The name of the dataset to use (via the datasets library)."""}  )
							SCREAMING_SNAKE_CASE_       :				Optional[Any]       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""}  )
							SCREAMING_SNAKE_CASE_       :				Union[str, Any]       				=  field(
							    default="""train""",							metadata={
							        """help""": """The name of the training data set split to use (via the datasets library). Defaults to 'train'"""
							    },							)
							SCREAMING_SNAKE_CASE_       :				Union[str, Any]       				=  field(
							    default="""validation""",							metadata={
							        """help""": (
							            """The name of the validation data set split to use (via the datasets library). Defaults to 'validation'"""
							        )
							    },							)
							SCREAMING_SNAKE_CASE_       :				Any       				=  field(
							    default="""file""",							metadata={"""help""": """Column in the dataset that contains speech file path. Defaults to 'file'"""},							)
							SCREAMING_SNAKE_CASE_       :				Dict       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""}  )
							SCREAMING_SNAKE_CASE_       :				Tuple       				=  field(
							    default=1,							metadata={
							        """help""": """The percentage of the train set used as validation set in case there's no validation split"""
							    },							)
							SCREAMING_SNAKE_CASE_       :				List[Any]       				=  field(
							    default=lowerCamelCase__,							metadata={"""help""": """The number of processes to use for the preprocessing."""},							)
							SCREAMING_SNAKE_CASE_       :				str       				=  field(
							    default=20.0,							metadata={"""help""": """Filter audio files that are longer than `max_duration_in_seconds` seconds"""}  )





@dataclass
class    snake_case     :
							SCREAMING_SNAKE_CASE_       :				Tuple       				=  42
							SCREAMING_SNAKE_CASE_       :				str       				=  42
							SCREAMING_SNAKE_CASE_       :				int       				=  """longest"""
							SCREAMING_SNAKE_CASE_       :				str       				=  None
							SCREAMING_SNAKE_CASE_       :				int       				=  None

							def __call__(     self							:    int					,  UpperCamelCase__							:    List[Dict[str, Union[List[int], torch.Tensor]]])->       Tuple:



									'''simple docstring'''


									__lowerCAmelCase:	List[Any]   				=      self.feature_extractor.pad(
									    __A					,  max_length=self.max_length					,  padding=self.padding					,  pad_to_multiple_of=self.pad_to_multiple_of					,  return_tensors="pt"					,  )
									__lowerCAmelCase:	int   				=      self.model._get_feat_extract_output_lengths(batch["input_values"].shape[-1])

									__lowerCAmelCase:	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
											__lowerCAmelCase:	Tuple   				=      self.model._get_feat_extract_output_lengths(batch["attention_mask"].sum(-1)).to(
											    torch.long)

											__lowerCAmelCase:	Dict   				=      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
											__lowerCAmelCase:	Tuple   				=      1
											__lowerCAmelCase:	int   				=      attention_mask.flip([-1]).cumsum(-1).flip([-1]).bool()

									# sample randomly masked indices
									__lowerCAmelCase:	Dict   				=      _compute_mask_indices(
									    (batch_size, mask_indices_seq_length)					,  self.model.config.mask_time_prob					,  self.model.config.mask_time_length					,  attention_mask=__A					,  min_masks=2					,  )

									return batch





class    snake_case     (    lowerCamelCase__  ):

							def __init__(     self							:    Union[str, Any]					,  *UpperCamelCase__							:    int					,  UpperCamelCase__							:    Dict=1					,  UpperCamelCase__							:    Any=0					,  UpperCamelCase__							:    Optional[Any]=1.0					,  **UpperCamelCase__							:    Any)->       Optional[int]:



									'''simple docstring'''


									super().__init__(*__A					,  **__A)
									__lowerCAmelCase:	Any   				=      0
									__lowerCAmelCase:	Any   				=      max_gumbel_temp
									__lowerCAmelCase:	Optional[Any]   				=      min_gumbel_temp
									__lowerCAmelCase:	str   				=      gumbel_temp_decay




							def 							lowercase_					(     self							:    Dict					,  UpperCamelCase__							:    nn.Module					,  UpperCamelCase__							:    Dict[str, Union[torch.Tensor, Any]])->       Any:



									'''simple docstring'''


									model.train()
									__lowerCAmelCase:	List[Any]   				=      self._prepare_inputs(__A)

									if self.use_amp:
											with autocast():
													__lowerCAmelCase:	Optional[Any]   				=      self.compute_loss(__A					,  __A)
									else:
											__lowerCAmelCase:	Optional[int]   				=      self.compute_loss(__A					,  __A)

									if self.args.n_gpu > 1 or self.deepspeed:
											if model.module.config.ctc_loss_reduction == "mean":
													__lowerCAmelCase:	Optional[Any]   				=      loss.mean()
											elif model.module.config.ctc_loss_reduction == "sum":
													__lowerCAmelCase:	str   				=      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:
											__lowerCAmelCase:	Any   				=      loss / self.args.gradient_accumulation_steps

									if self.use_amp:
											self.scaler.scale(__A).backward()
									elif self.use_apex:
											with amp.scale_loss(__A					,  self.optimizer) as scaled_loss:
													scaled_loss.backward()
									elif self.deepspeed:
											self.deepspeed.backward(__A)
									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    a__   (   )     ->     Union[str, Any]:
		# 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.

		__lowerCAmelCase:	Optional[Any]   				=      HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)			)

		__lowerCAmelCase:	List[Any]   				=      parser.parse_args_into_dataclasses()
		configure_logger(__SCREAMING_SNAKE_CASE ,			__SCREAMING_SNAKE_CASE			)

		# Downloading and loading a dataset from the hub.
		__lowerCAmelCase:	int   				=      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"
				__lowerCAmelCase:	Union[str, Any]   				=      DatasetDict()
				__lowerCAmelCase:	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 ,			)
				__lowerCAmelCase:	Tuple   				=      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"
				__lowerCAmelCase:	Optional[Any]   				=      DatasetDict()
				__lowerCAmelCase:	List[Any]   				=      load_dataset(
				    data_args.dataset_name ,			data_args.dataset_config_name ,			split="validation" ,			cache_dir=model_args.cache_dir ,			)
				__lowerCAmelCase:	int   				=      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
		__lowerCAmelCase:	Optional[Any]   				=      WavaVecaFeatureExtractor.from_pretrained(
		    model_args.model_name_or_path ,			cache_dir=model_args.cache_dir ,			do_normalize=__SCREAMING_SNAKE_CASE			)

		def prepare_dataset(__SCREAMING_SNAKE_CASE			):
				# check that all files have the correct sampling rate
				__lowerCAmelCase:	Optional[Any]   				=      librosa.load(batch[data_args.speech_file_column] ,			sr=feature_extractor.sampling_rate			)
				return batch

		# load audio files into numpy arrays
		__lowerCAmelCase:	str   				=      datasets.map(
		    __SCREAMING_SNAKE_CASE ,			num_proc=data_args.preprocessing_num_workers ,			remove_columns=datasets["train"].column_names			)

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

		def normalize(__SCREAMING_SNAKE_CASE			):
				return feature_extractor(batch["speech"] ,			sampling_rate=feature_extractor.sampling_rate			)

		# normalize and transform to `BatchFeatures`
		__lowerCAmelCase:	Any   				=      vectorized_datasets.map(
		    __SCREAMING_SNAKE_CASE ,			batched=__SCREAMING_SNAKE_CASE ,			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
		__lowerCAmelCase:	Optional[int]   				=      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\'"			)

		__lowerCAmelCase:	Any   				=      WavaVecaForPreTraining(__SCREAMING_SNAKE_CASE			)

		__lowerCAmelCase:	int   				=      DataCollatorForWavaVecaPretraining(model=__SCREAMING_SNAKE_CASE ,			feature_extractor=__SCREAMING_SNAKE_CASE			)

		__lowerCAmelCase:	Any   				=      WavaVecaPreTrainer(
		    model=__SCREAMING_SNAKE_CASE ,			data_collator=__SCREAMING_SNAKE_CASE ,			args=__SCREAMING_SNAKE_CASE ,			train_dataset=vectorized_datasets["train"] ,			eval_dataset=vectorized_datasets["validation"] ,			tokenizer=__SCREAMING_SNAKE_CASE ,			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()





"""simple docstring"""


import comet  # From: unbabel-comet
import torch

import datasets


__A						       =      datasets.logging.get_logger(__name__)

__A						       =      "\\n@inproceedings{rei-EtAl:2020:WMT,\n   author    = {Rei, Ricardo  and  Stewart, Craig  and  Farinha, Ana C  and  Lavie, Alon},\n   title     = {Unbabel's Participation in the WMT20 Metrics Shared Task},\n   booktitle      = {Proceedings of the Fifth Conference on Machine Translation},\n   month          = {November},\n   year           = {2020},\n   address        = {Online},\n   publisher      = {Association for Computational Linguistics},\n   pages     = {909--918},\n}\n@inproceedings{rei-etal-2020-comet,\n   title = \"{COMET}: A Neural Framework for {MT} Evaluation\",\n   author = \"Rei, Ricardo  and\n      Stewart, Craig  and\n      Farinha, Ana C  and\n      Lavie, Alon\",\n   booktitle = \"Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)\",\n   month = nov,\n   year = \"2020\",\n   address = \"Online\",\n   publisher = \"Association for Computational Linguistics\",\n   url = \"https://www.aclweb.org/anthology/2020.emnlp-main.213\",\n   pages = \"2685--2702\",\n}\n"

__A						       =      "\\nCrosslingual Optimized Metric for Evaluation of Translation (COMET) is an open-source framework used to train Machine Translation metrics that achieve high levels of correlation with different types of human judgments (HTER, DA's or MQM).\nWith the release of the framework the authors also released fully trained models that were used to compete in the WMT20 Metrics Shared Task achieving SOTA in that years competition.\n\nSee the [README.md] file at https://unbabel.github.io/COMET/html/models.html for more information.\n"

__A						       =      "\nCOMET score.\n\nArgs:\n\n`sources` (list of str): Source sentences\n`predictions` (list of str): candidate translations\n`references` (list of str): reference translations\n`cuda` (bool): If set to True, runs COMET using GPU\n`show_progress` (bool): Shows progress\n`model`: COMET model to be used. Will default to `wmt-large-da-estimator-1719` if None.\n\nReturns:\n    `samples`: List of dictionaries with `src`, `mt`, `ref` and `score`.\n    `scores`: List of scores.\n\nExamples:\n\n    >>> comet_metric = datasets.load_metric('comet')\n    >>> # comet_metric = load_metric('comet', 'wmt20-comet-da')  # you can also choose which model to use\n    >>> source = [\"Dem Feuer konnte Einhalt geboten werden\", \"Schulen und Kindergärten wurden eröffnet.\"]\n    >>> hypothesis = [\"The fire could be stopped\", \"Schools and kindergartens were open\"]\n    >>> reference = [\"They were able to control the fire.\", \"Schools and kindergartens opened\"]\n    >>> results = comet_metric.compute(predictions=hypothesis, references=reference, sources=source)\n    >>> print([round(v, 2) for v in results[\"scores\"]])\n    [0.19, 0.92]\n"





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION,							_KWARGS_DESCRIPTION  )
class    snake_case     (    datasets.Metric  ):

				def 							lowercase_					(     self							:    List[Any])->       Dict:



						'''simple docstring'''


						return datasets.MetricInfo(
						    description=_DESCRIPTION					,  citation=_CITATION					,  homepage="https://unbabel.github.io/COMET/html/index.html"					,  inputs_description=_KWARGS_DESCRIPTION					,  features=datasets.Features(
						        {
						            "sources": datasets.Value("string"					,  id="sequence"),
						            "predictions": datasets.Value("string"					,  id="sequence"),
						            "references": datasets.Value("string"					,  id="sequence"),
						        })					,  codebase_urls=["https://github.com/Unbabel/COMET"]					,  reference_urls=[
						        "https://github.com/Unbabel/COMET",
						        "https://www.aclweb.org/anthology/2020.emnlp-main.213/",
						        "http://www.statmt.org/wmt20/pdf/2020.wmt-1.101.pdf6",
						    ]					,  )

				def 							lowercase_					(     self							:    Tuple					,  UpperCamelCase__							:    Any)->       Dict:



						'''simple docstring'''


						if self.config_name == "default":
								__lowerCAmelCase:	Union[str, Any]   				=      comet.load_from_checkpoint(comet.download_model("wmt20-comet-da"))
						else:
								__lowerCAmelCase:	Tuple   				=      comet.load_from_checkpoint(comet.download_model(self.config_name))




				def 							lowercase_					(     self							:    Optional[int]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    Optional[Any]					,  UpperCamelCase__							:    int					,  UpperCamelCase__							:    Optional[int]=None					,  UpperCamelCase__							:    List[str]=False)->       str:



						'''simple docstring'''


						if gpus is None:
								__lowerCAmelCase:	Union[str, Any]   				=      1 if torch.cuda.is_available() else 0
						__lowerCAmelCase:	Dict   				=      {"src": sources, "mt": predictions, "ref": references}
						__lowerCAmelCase:	Union[str, Any]   				=      [dict(zip(UpperCamelCase__					,  UpperCamelCase__)) for t in zip(*data.values())]
						__lowerCAmelCase				,			__lowerCAmelCase:	str   				=      self.scorer.predict(UpperCamelCase__					,  gpus=UpperCamelCase__					,  progress_bar=UpperCamelCase__)
						return {"mean_score": mean_score, "scores": scores}


