

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


SCREAMING_SNAKE_CASE_					:	Optional[Any]							  =						logging.getLogger(__name__)



@dataclass
class 					a						:



    """simple docstring"""



    UpperCAmelCase						 =					field(
        metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}				)
    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},	)
    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "Whether to freeze the feature extractor layers of the model."}				)
    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "Whether to log verbose messages or not."},	)
    UpperCAmelCase						 =					field(
        default=2.0,	metadata={"help": "Maximum temperature for gumbel softmax."}				)
    UpperCAmelCase						 =					field(
        default=0.5,	metadata={"help": "Minimum temperature for gumbel softmax."}				)
    UpperCAmelCase						 =					field(
        default=0.9_9_9_9_9_5,	metadata={"help": "Decay of gumbel temperature during training."}				)





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



@dataclass
class 					a						:



    """simple docstring"""



    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "The name of the dataset to use (via the datasets library)."}				)
    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}				)
    UpperCAmelCase						 =					field(
        default="train",	metadata={
            "help": "The name of the training data set split to use (via the datasets library). Defaults to 'train'"
        },	)
    UpperCAmelCase						 =					field(
        default="validation",	metadata={
            "help": (
                "The name of the validation data set split to use (via the datasets library). Defaults to 'validation'"
            )
        },	)
    UpperCAmelCase						 =					field(
        default="file",	metadata={"help": "Column in the dataset that contains speech file path. Defaults to 'file'"},	)
    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "Overwrite the cached preprocessed datasets or not."}				)
    UpperCAmelCase						 =					field(
        default=1,	metadata={
            "help": "The percentage of the train set used as validation set in case there's no validation split"
        },	)
    UpperCAmelCase						 =					field(
        default=a__,	metadata={"help": "The number of processes to use for the preprocessing."},	)
    UpperCAmelCase						 =					field(
        default=2_0.0,	metadata={"help": "Filter audio files that are longer than `max_duration_in_seconds` seconds"}				)



@dataclass
class 					a						:



    """simple docstring"""



    UpperCAmelCase						 =					42
    UpperCAmelCase						 =					42
    UpperCAmelCase						 =					"longest"
    UpperCAmelCase						 =					None
    UpperCAmelCase						 =					None

    def __call__(     self: Tuple    ,						UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         A__										=  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"""    ,						)
         A__										=  self.model._get_feat_extract_output_lengths(batch["""input_values"""].shape[-1]       )

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

              A__										=  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
              A__										=  1
              A__										=  attention_mask.flip([-1]       ).cumsum(-1       ).flip([-1]       ).bool()

         # sample randomly masked indices
         A__										=  _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 					a						(       a__				):



    """simple docstring"""



    def __init__(     self: Dict    ,						*UpperCamelCase: Union[str, Any]    ,						UpperCamelCase: List[str]=1    ,						UpperCamelCase: Optional[Any]=0    ,						UpperCamelCase: List[Any]=1.0    ,						**UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         super().__init__(*lowerCamelCase_    ,						**lowerCamelCase_       )
         A__										=  0
         A__										=  max_gumbel_temp
         A__										=  min_gumbel_temp
         A__										=  gumbel_temp_decay





    def 				UpperCamelCase	(     self: int    ,						UpperCamelCase: str    ,						UpperCamelCase: Optional[Any]       ):






         """simple docstring"""


         model.train()
         A__										=  self._prepare_inputs(lowerCamelCase_       )

         if self.use_amp:
              with autocast():
                   A__										=  self.compute_loss(lowerCamelCase_    ,						lowerCamelCase_       )
         else:
              A__										=  self.compute_loss(lowerCamelCase_    ,						lowerCamelCase_       )

         if self.args.n_gpu > 1 or self.deepspeed:
              if model.module.config.ctc_loss_reduction == "mean":
                   A__										=  loss.mean()
              elif model.module.config.ctc_loss_reduction == "sum":
                   A__										=  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:
              A__										=  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  _snake_case     (    ):
     # 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.

     A__										=  HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)			)

     A__			,				A__			,				A__										=  parser.parse_args_into_dataclasses()
     configure_logger(UpperCAmelCase_					,   UpperCAmelCase_			)

     # Downloading and loading a dataset from the hub.
     A__										=  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"
          A__										=  DatasetDict()
          A__										=  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					,   )
          A__										=  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"
          A__										=  DatasetDict()
          A__										=  load_dataset(
              data_args.dataset_name					,   data_args.dataset_config_name					,   split="""validation"""					,   cache_dir=model_args.cache_dir					,   )
          A__										=  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
     A__										=  WavaVecaFeatureExtractor.from_pretrained(
         model_args.model_name_or_path					,   cache_dir=model_args.cache_dir					,   do_normalize=UpperCAmelCase_			)

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

     # load audio files into numpy arrays
     A__										=  datasets.map(
         UpperCAmelCase_					,   num_proc=data_args.preprocessing_num_workers					,   remove_columns=datasets["""train"""].column_names			)

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

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

     # normalize and transform to `BatchFeatures`
     A__										=  vectorized_datasets.map(
         UpperCAmelCase_					,   batched=UpperCAmelCase_					,   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
     A__										=  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\'"""			)

     A__										=  WavaVecaForPreTraining(UpperCAmelCase_			)

     A__										=  DataCollatorForWavaVecaPretraining(model=UpperCAmelCase_					,   feature_extractor=UpperCAmelCase_			)

     A__										=  WavaVecaPreTrainer(
         model=UpperCAmelCase_					,   data_collator=UpperCAmelCase_					,   args=UpperCAmelCase_					,   train_dataset=vectorized_datasets["""train"""]					,   eval_dataset=vectorized_datasets["""validation"""]					,   tokenizer=UpperCAmelCase_					,   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"""



from typing import Dict, Optional

import numpy as np

import datasets


SCREAMING_SNAKE_CASE_					:	Tuple							  =						'\nIoU is the area of overlap between the predicted segmentation and the ground truth divided by the area of union\nbetween the predicted segmentation and the ground truth. For binary (two classes) or multi-class segmentation,\nthe mean IoU of the image is calculated by taking the IoU of each class and averaging them.\n'

SCREAMING_SNAKE_CASE_					:	Optional[int]							  =						'\nArgs:\n    predictions (`List[ndarray]`):\n        List of predicted segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n    references (`List[ndarray]`):\n        List of ground truth segmentation maps, each of shape (height, width). Each segmentation map can be of a different size.\n    num_labels (`int`):\n        Number of classes (categories).\n    ignore_index (`int`):\n        Index that will be ignored during evaluation.\n    nan_to_num (`int`, *optional*):\n        If specified, NaN values will be replaced by the number defined by the user.\n    label_map (`dict`, *optional*):\n        If specified, dictionary mapping old label indices to new label indices.\n    reduce_labels (`bool`, *optional*, defaults to `False`):\n        Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 is used for background,\n        and background itself is not included in all classes of a dataset (e.g. ADE20k). The background label will be replaced by 255.\n\nReturns:\n    `Dict[str, float | ndarray]` comprising various elements:\n    - *mean_iou* (`float`):\n        Mean Intersection-over-Union (IoU averaged over all categories).\n    - *mean_accuracy* (`float`):\n        Mean accuracy (averaged over all categories).\n    - *overall_accuracy* (`float`):\n        Overall accuracy on all images.\n    - *per_category_accuracy* (`ndarray` of shape `(num_labels,)`):\n        Per category accuracy.\n    - *per_category_iou* (`ndarray` of shape `(num_labels,)`):\n        Per category IoU.\n\nExamples:\n\n    >>> import numpy as np\n\n    >>> mean_iou = datasets.load_metric("mean_iou")\n\n    >>> # suppose one has 3 different segmentation maps predicted\n    >>> predicted_1 = np.array([[1, 2], [3, 4], [5, 255]])\n    >>> actual_1 = np.array([[0, 3], [5, 4], [6, 255]])\n\n    >>> predicted_2 = np.array([[2, 7], [9, 2], [3, 6]])\n    >>> actual_2 = np.array([[1, 7], [9, 2], [3, 6]])\n\n    >>> predicted_3 = np.array([[2, 2, 3], [8, 2, 4], [3, 255, 2]])\n    >>> actual_3 = np.array([[1, 2, 2], [8, 2, 1], [3, 255, 1]])\n\n    >>> predicted = [predicted_1, predicted_2, predicted_3]\n    >>> ground_truth = [actual_1, actual_2, actual_3]\n\n    >>> results = mean_iou.compute(predictions=predicted, references=ground_truth, num_labels=10, ignore_index=255, reduce_labels=False)\n    >>> print(results) # doctest: +NORMALIZE_WHITESPACE\n    {\'mean_iou\': 0.47750000000000004, \'mean_accuracy\': 0.5916666666666666, \'overall_accuracy\': 0.5263157894736842, \'per_category_iou\': array([0.   , 0.   , 0.375, 0.4  , 0.5  , 0.   , 0.5  , 1.   , 1.   , 1.   ]), \'per_category_accuracy\': array([0.        , 0.        , 0.75      , 0.66666667, 1.        , 0.        , 0.5       , 1.        , 1.        , 1.        ])}\n'

SCREAMING_SNAKE_CASE_					:	int							  =						'\\n@software{MMSegmentation_Contributors_OpenMMLab_Semantic_Segmentation_2020,\nauthor = {{MMSegmentation Contributors}},\nlicense = {Apache-2.0},\nmonth = {7},\ntitle = {{OpenMMLab Semantic Segmentation Toolbox and Benchmark}},\nurl = {https://github.com/open-mmlab/mmsegmentation},\nyear = {2020}\n}'





def  _snake_case     (    UpperCAmelCase_  :					str					,   UpperCAmelCase_  :					int					,   UpperCAmelCase_  :					Optional[Any]					,   UpperCAmelCase_  :					bool					,   UpperCAmelCase_  :					Optional[Dict[int, int]] = None					,   UpperCAmelCase_  :					bool = False					,   ):
     if label_map is not None:
          for old_id, new_id in label_map.items():
               A__										=  new_id

    # turn into Numpy arrays
     A__										=  np.array(UpperCAmelCase_			)
     A__										=  np.array(UpperCAmelCase_			)

     if reduce_labels:
          A__										=  255
          A__										=  label - 1
          A__										=  255

     A__										=  label != ignore_index
     A__										=  np.not_equal(UpperCAmelCase_					,   UpperCAmelCase_			)
     A__										=  pred_label[mask]
     A__										=  np.array(UpperCAmelCase_			)[mask]

     A__										=  pred_label[pred_label == label]

     A__										=  np.histogram(UpperCAmelCase_					,   bins=UpperCAmelCase_					,   range=(0, num_labels - 1)			)[0]
     A__										=  np.histogram(UpperCAmelCase_					,   bins=UpperCAmelCase_					,   range=(0, num_labels - 1)			)[0]
     A__										=  np.histogram(UpperCAmelCase_					,   bins=UpperCAmelCase_					,   range=(0, num_labels - 1)			)[0]

     A__										=  area_pred_label + area_label - area_intersect

     return area_intersect, area_union, area_pred_label, area_label





def  _snake_case     (    UpperCAmelCase_  :					int					,   UpperCAmelCase_  :					List[str]					,   UpperCAmelCase_  :					Any					,   UpperCAmelCase_  :					bool					,   UpperCAmelCase_  :					Optional[Dict[int, int]] = None					,   UpperCAmelCase_  :					bool = False					,   ):
     A__										=  np.zeros((num_labels,)					,   dtype=np.floataa			)
     A__										=  np.zeros((num_labels,)					,   dtype=np.floataa			)
     A__										=  np.zeros((num_labels,)					,   dtype=np.floataa			)
     A__										=  np.zeros((num_labels,)					,   dtype=np.floataa			)
     for result, gt_seg_map in zip(UpperCAmelCase_					,   UpperCAmelCase_			):
          A__			,				A__			,				A__			,				A__										=  intersect_and_union(
              UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_			)
          total_area_intersect += area_intersect
          total_area_union += area_union
          total_area_pred_label += area_pred_label
          total_area_label += area_label
     return total_area_intersect, total_area_union, total_area_pred_label, total_area_label





def  _snake_case     (    UpperCAmelCase_  :					Optional[int]					,   UpperCAmelCase_  :					Any					,   UpperCAmelCase_  :					Tuple					,   UpperCAmelCase_  :					bool					,   UpperCAmelCase_  :					Optional[int] = None					,   UpperCAmelCase_  :					Optional[Dict[int, int]] = None					,   UpperCAmelCase_  :					bool = False					,   ):
     A__			,				A__			,				A__			,				A__										=  total_intersect_and_union(
         UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_					,   UpperCAmelCase_			)

     # compute metrics
     A__										=  {}

     A__										=  total_area_intersect.sum() / total_area_label.sum()
     A__										=  total_area_intersect / total_area_union
     A__										=  total_area_intersect / total_area_label

     A__										=  np.nanmean(UpperCAmelCase_			)
     A__										=  np.nanmean(UpperCAmelCase_			)
     A__										=  all_acc
     A__										=  iou
     A__										=  acc

     if nan_to_num is not None:
          A__										=  {metric: np.nan_to_num(UpperCAmelCase_					,   nan=UpperCAmelCase_			) for metric, metric_value in metrics.items()}

     return metrics



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



    """simple docstring"""



    def 				UpperCamelCase	(     self: str       ):






         """simple docstring"""


         return datasets.MetricInfo(
             description=_DESCRIPTION    ,						citation=_CITATION    ,						inputs_description=_KWARGS_DESCRIPTION    ,						features=datasets.Features(
                 # 1st Seq - height dim, 2nd - width dim
                 {
                     """predictions""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16"""       )       )       ),
                     """references""": datasets.Sequence(datasets.Sequence(datasets.Value("""uint16"""       )       )       ),
                 }       )    ,						reference_urls=[
                 """https://github.com/open-mmlab/mmsegmentation/blob/71c201b1813267d78764f306a297ca717827c4bf/mmseg/core/evaluation/metrics.py"""
             ]    ,						)





    def 				UpperCamelCase	(     self: str    ,						UpperCamelCase: Union[str, Any]    ,						UpperCamelCase: Optional[Any]    ,						UpperCamelCase: int    ,						UpperCamelCase: bool    ,						UpperCamelCase: Optional[int] = None    ,						UpperCamelCase: Optional[Dict[int, int]] = None    ,						UpperCamelCase: bool = False    ,						):






         """simple docstring"""


         A__										=  mean_iou(
             results=UpperCamelCase    ,						gt_seg_maps=UpperCamelCase    ,						num_labels=UpperCamelCase    ,						ignore_index=UpperCamelCase    ,						nan_to_num=UpperCamelCase    ,						label_map=UpperCamelCase    ,						reduce_labels=UpperCamelCase    ,						)
         return iou_result






