

from typing import List

from packaging import version
from sklearn.metrics import fa_score

import datasets
from datasets.config import PY_VERSION


if PY_VERSION < version.parse('''3.8'''):
       import importlib_metadata
else:
       import importlib.metadata as importlib_metadata


# TODO(Patrick/Anton)
lowerCamelCase									= '''\
'''

lowerCamelCase									= '''\
    XTREME-S is a benchmark to evaluate universal cross-lingual speech representations in many languages.
    XTREME-S covers four task families: speech recognition, classification, speech-to-text translation and retrieval.
'''

lowerCamelCase									= '''
Compute XTREME-S evaluation metric associated to each XTREME-S dataset.
Args:
    predictions: list of predictions to score.
        Each translation should be tokenized into a list of tokens.
    references: list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
    bleu_kwargs: optional dict of keywords to be passed when computing \'bleu\'.
        Keywords include Dict can be one of \'smooth_method\', \'smooth_value\', \'force\', \'lowercase\',
        \'tokenize\', \'use_effective_order\'.
    wer_kwargs: optional dict of keywords to be passed when computing \'wer\' and \'cer\'.
        Keywords include \'concatenate_texts\'.
Returns: depending on the XTREME-S task, one or several of:
    "accuracy": Accuracy - for \'fleurs-lang_id\', \'minds14\'
    "f1": F1 score - for \'minds14\'
    "wer": Word error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'
    "cer": Character error rate - for \'mls\', \'fleurs-asr\', \'voxpopuli\', \'babel\'
    "bleu": BLEU score according to the `sacrebleu` metric - for \'covost2\'
Examples:

    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'mls\')  # \'mls\', \'voxpopuli\', \'fleurs-asr\' or \'babel\'
    >>> references = ["it is sunny here", "paper and pen are essentials"]
    >>> predictions = ["it\'s sunny", "paper pen are essential"]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {\'wer\': 0.56, \'cer\': 0.27}

    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'covost2\')
    >>> references = ["bonjour paris", "il est necessaire de faire du sport de temps en temp"]
    >>> predictions = ["bonjour paris", "il est important de faire du sport souvent"]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {\'bleu\': 31.65}

    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'fleurs-lang_id\')
    >>> references = [0, 1, 0, 0, 1]
    >>> predictions = [0, 1, 1, 0, 0]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {\'accuracy\': 0.6}

    >>> xtreme_s_metric = datasets.load_metric(\'xtreme_s\', \'minds14\')
    >>> references = [0, 1, 0, 0, 1]
    >>> predictions = [0, 1, 1, 0, 0]
    >>> results = xtreme_s_metric.compute(predictions=predictions, references=references)
    >>> print({k: round(v, 2) for k, v in results.items()})
    {\'f1\': 0.58, \'accuracy\': 0.6}
'''

lowerCamelCase									= ['''fleurs-asr''', '''mls''', '''voxpopuli''', '''babel''', '''covost2''', '''fleurs-lang_id''', '''minds14''']
lowerCamelCase									= ''''''

try:
       from jiwer import transforms as tr

       lowerCamelCase									= True
except ImportError:
       lowerCamelCase									= False

if _jiwer_available and version.parse(importlib_metadata.version('''jiwer''')) < version.parse('''2.3.0'''):







       class 						__magic_name__      (							tr.AbstractTransform							):

          '''simple docstring'''




          def __init__(    self,   lowercase_ = " "	)							->   Tuple:






            """simple docstring"""






            a__  =sentence_delimiter



          def 			_UpperCAmelCase			(    self,   lowercase_	)							->   Any:






            """simple docstring"""






            return list(lowercase_	)



          def 			_UpperCAmelCase			(    self,   lowercase_	)							->   Tuple:






            """simple docstring"""






            a__  =[]
            for sent_idx, sentence in enumerate(lowercase_	):
              chars.extend(self.process_string(lowercase_	)	)
              if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(lowercase_	) - 1:
                chars.append(self.sentence_delimiter	)
            return chars





       lowerCamelCase									= tr.Compose(
           [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
       )
elif _jiwer_available:
       lowerCamelCase									= tr.Compose(
           [
               tr.RemoveMultipleSpaces(),
               tr.Strip(),
               tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
               tr.ReduceToListOfListOfChars(),
           ]
       )
else:
       lowerCamelCase									= None




def       UpperCAmelCase__    (      _A				:			str     ,				_A				:			Optional[Any]							):





  '''simple docstring'''







  return float((preds == labels).mean()							)




def       UpperCAmelCase__    (      _A				:			List[str]     ,				_A				:			Tuple							):





  '''simple docstring'''







  return {
      "f1": float(fa_score(y_true=_A     ,				y_pred=_A     ,				average='''macro'''							)							),
      "accuracy": simple_accuracy(_A     ,				_A							),
  }




def       UpperCAmelCase__    (      _A				:			str     ,				_A				:			Any     ,				_A				:			Any="exp"     ,				_A				:			str=None     ,				_A				:			List[str]=False     ,				_A				:			Dict=False     ,				_A				:			Dict=None     ,				_A				:			Dict=False     ,				):





  '''simple docstring'''







  a__  =[[label] for label in labels]
  a__  =list(_A							)
  try:
    import sacrebleu as scb
  except ImportError:
    raise ValueError(
        '''sacrebleu has to be installed in order to apply the bleu metric for covost2.'''
        '''You can install it via `pip install sacrebleu`.'''							)

  if version.parse(scb.__version__							) < version.parse('''1.4.12'''							):
    raise ImportWarning(
        '''To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn\'t match this condition.\n'''
        '''You can install it with `pip install "sacrebleu>=1.4.12"`.'''							)

  a__  =len(labels[0]							)
  if any(len(_A							) != references_per_prediction for refs in labels							):
    raise ValueError('''Sacrebleu requires the same number of references for each prediction'''							)
  a__  =[[refs[i] for refs in labels] for i in range(_A							)]
  a__  =scb.corpus_bleu(
      _A     ,				_A     ,				smooth_method=_A     ,				smooth_value=_A     ,				force=_A     ,				lowercase=_A     ,				use_effective_order=_A     ,				**({'''tokenize''': tokenize} if tokenize else {})     ,				)
  return {"bleu": output.score}




def       UpperCAmelCase__    (      _A				:			int     ,				_A				:			Optional[int]     ,				_A				:			Any     ,				_A				:			Optional[Any]							):





  '''simple docstring'''







  try:
    from jiwer import compute_measures
  except ImportError:
    raise ValueError(
        F"""jiwer has to be installed in order to apply the wer metric for {config_name}."""
        '''You can install it via `pip install jiwer`.'''							)

  if concatenate_texts:
    a__  =compute_measures(_A     ,				_A							)['''wer''']

    a__  =compute_measures(_A     ,				_A     ,				truth_transform=_A     ,				hypothesis_transform=_A							)['''wer''']
    return {"wer": wer, "cer": cer}
  else:

    def compute_score(_A				:			Any     ,				_A				:			Dict     ,				_A				:			Optional[Any]="wer"							):
      a__  =0
      a__  =0
      for prediction, reference in zip(_A     ,				_A							):
        if score_type == "wer":
          a__  =compute_measures(_A     ,				_A							)
        elif score_type == "cer":
          a__  =compute_measures(
              _A     ,				_A     ,				truth_transform=_A     ,				hypothesis_transform=_A							)
        incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
        total += measures["substitutions"] + measures["deletions"] + measures["hits"]
      return incorrect / total

    return {"wer": compute_score(_A     ,				_A     ,				'''wer'''							), "cer": compute_score(_A     ,				_A     ,				'''cer'''							)}







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

   '''simple docstring'''




   def 			_UpperCAmelCase			(    self	)							->   str:






     """simple docstring"""






     if self.config_name not in _CONFIG_NAMES:
       raise KeyError(F"""You should supply a configuration name selected in {_CONFIG_NAMES}"""	)

     a__  ='''int64''' if self.config_name in ['''fleurs-lang_id''', '''minds14'''] else '''string'''

     return datasets.MetricInfo(
         description=_DESCRIPTION,   citation=_CITATION,   inputs_description=_KWARGS_DESCRIPTION,   features=datasets.Features(
             {'''predictions''': datasets.Value(lowercase_	), '''references''': datasets.Value(lowercase_	)}	),   codebase_urls=[],   reference_urls=[],   format='''numpy''',   )



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_,   lowercase_=None,   lowercase_=None	)							->   List[str]:






     """simple docstring"""






     a__  =bleu_kwargs if bleu_kwargs is not None else {}
     a__  =wer_kwargs if wer_kwargs is not None else {}

     if self.config_name == "fleurs-lang_id":
       return {"accuracy": simple_accuracy(lowercase_,   lowercase_	)}
     elif self.config_name == "minds14":
       return fa_and_simple_accuracy(lowercase_,   lowercase_	)
     elif self.config_name == "covost2":
       a__  =bleu_kwargs.pop('''smooth_method''',   '''exp'''	)
       a__  =bleu_kwargs.pop('''smooth_value''',   lowercase_	)
       a__  =bleu_kwargs.pop('''force''',   lowercase_	)
       a__  =bleu_kwargs.pop('''lowercase''',   lowercase_	)
       a__  =bleu_kwargs.pop('''tokenize''',   lowercase_	)
       a__  =bleu_kwargs.pop('''use_effective_order''',   lowercase_	)
       return bleu(
           preds=lowercase_,   labels=lowercase_,   smooth_method=lowercase_,   smooth_value=lowercase_,   force=lowercase_,   lowercase=lowercase_,   tokenize=lowercase_,   use_effective_order=lowercase_,   )
     elif self.config_name in ["fleurs-asr", "mls", "voxpopuli", "babel"]:
       a__  =wer_kwargs.pop('''concatenate_texts''',   lowercase_	)
       return wer_and_cer(lowercase_,   lowercase_,   lowercase_,   self.config_name	)
     else:
       raise KeyError(F"""You should supply a configuration name selected in {_CONFIG_NAMES}"""	)



from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


lowerCamelCase									= logging.get_logger(__name__)

lowerCamelCase									= {
    '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json''',
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}







class 						__magic_name__      (							lowerCamelCase__							):

   '''simple docstring'''




   lowerCamelCase__			:     List[Any]    =       'blenderbot'
   lowerCamelCase__			:     int    =       ['past_key_values']
   lowerCamelCase__			:     Optional[Any]    =       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}



   def __init__(    self,   lowercase_=8008,   lowercase_=128,   lowercase_=2,   lowercase_=10240,   lowercase_=32,   lowercase_=24,   lowercase_=10240,   lowercase_=32,   lowercase_=0.0,   lowercase_=0.0,   lowercase_=True,   lowercase_=True,   lowercase_="gelu",   lowercase_=2560,   lowercase_=0.1,   lowercase_=0.0,   lowercase_=0.0,   lowercase_=0.02,   lowercase_=1,   lowercase_=False,   lowercase_=0,   lowercase_=1,   lowercase_=2,   lowercase_=3,   lowercase_=2,   **lowercase_,   )							->   Any:






     """simple docstring"""






     a__  =vocab_size
     a__  =max_position_embeddings
     a__  =d_model
     a__  =encoder_ffn_dim
     a__  =encoder_layers
     a__  =encoder_attention_heads
     a__  =decoder_ffn_dim
     a__  =decoder_layers
     a__  =decoder_attention_heads
     a__  =dropout
     a__  =attention_dropout
     a__  =activation_dropout
     a__  =activation_function
     a__  =init_std
     a__  =encoder_layerdrop
     a__  =decoder_layerdrop
     a__  =use_cache
     a__  =encoder_layers
     a__  =scale_embedding  # scale factor will be sqrt(d_model) if True

     super().__init__(
         pad_token_id=lowercase_,   bos_token_id=lowercase_,   eos_token_id=lowercase_,   is_encoder_decoder=lowercase_,   decoder_start_token_id=lowercase_,   encoder_no_repeat_ngram_size=lowercase_,   forced_eos_token_id=lowercase_,   **lowercase_,   )







class 						__magic_name__      (							lowerCamelCase__							):

   '''simple docstring'''




   @property
   def 			_UpperCAmelCase			(    self	)							->   Mapping[str, Mapping[int, str]]:






     """simple docstring"""






     if self.task in ["default", "seq2seq-lm"]:
       a__  =OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
           ]	)
       if self.use_past:
         a__  ={0: '''batch'''}
         a__  ={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
       else:
         a__  ={0: '''batch''', 1: '''decoder_sequence'''}
         a__  ={0: '''batch''', 1: '''decoder_sequence'''}
       if self.use_past:
         self.fill_with_past_key_values_(lowercase_,   direction='''inputs'''	)
     elif self.task == "causal-lm":
       a__  =OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
           ]	)
       if self.use_past:
         a__,				a__  =self.num_layers
         for i in range(lowercase_	):
           a__  ={0: '''batch''', 2: '''past_sequence + sequence'''}
           a__  ={0: '''batch''', 2: '''past_sequence + sequence'''}
     else:
       a__  =OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
               ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
           ]	)

     return common_inputs



   @property
   # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
   def 			_UpperCAmelCase			(    self	)							->   Mapping[str, Mapping[int, str]]:






     """simple docstring"""






     if self.task in ["default", "seq2seq-lm"]:
       a__  =super().outputs
     else:
       a__  =super(lowercase_,   self	).outputs
       if self.use_past:
         a__,				a__  =self.num_layers
         for i in range(lowercase_	):
           a__  ={0: '''batch''', 2: '''past_sequence + sequence'''}
           a__  ={0: '''batch''', 2: '''past_sequence + sequence'''}
     return common_outputs



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_ = -1,   lowercase_ = -1,   lowercase_ = False,   lowercase_ = None,   )							->   Mapping[str, Any]:






     """simple docstring"""






     a__  =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         lowercase_,   lowercase_,   lowercase_,   lowercase_,   lowercase_	)
     # Generate decoder inputs
     a__  =seq_length if not self.use_past else 1
     a__  =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         lowercase_,   lowercase_,   lowercase_,   lowercase_,   lowercase_	)
     a__  ={F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
     a__  =dict(**lowercase_,   **lowercase_	)

     if self.use_past:
       if not is_torch_available():
         raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
       else:
         import torch
       a__,				a__  =common_inputs['''input_ids'''].shape
       a__  =common_inputs['''decoder_input_ids'''].shape[1]
       a__,				a__  =self.num_attention_heads
       a__  =(
           batch,
           num_encoder_attention_heads,
           encoder_seq_length,
           self._config.hidden_size // num_encoder_attention_heads,
       )
       a__  =decoder_seq_length
       a__  =(
           batch,
           num_decoder_attention_heads,
           decoder_past_length,
           self._config.hidden_size // num_decoder_attention_heads,
       )
       a__  =torch.cat(
           [common_inputs['''decoder_attention_mask'''], torch.ones(lowercase_,   lowercase_	)],   dim=1	)
       a__  =[]
       a__,				a__  =self.num_layers

       for _ in range(lowercase_	):
         common_inputs["past_key_values"].append(
             (
                 torch.zeros(lowercase_	),
                 torch.zeros(lowercase_	),
                 torch.zeros(lowercase_	),
                 torch.zeros(lowercase_	),
             )	)
     return common_inputs



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_ = -1,   lowercase_ = -1,   lowercase_ = False,   lowercase_ = None,   )							->   Mapping[str, Any]:






     """simple docstring"""






     a__  =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         lowercase_,   lowercase_,   lowercase_,   lowercase_,   lowercase_	)

     if self.use_past:
       if not is_torch_available():
         raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
       else:
         import torch
       a__,				a__  =common_inputs['''input_ids'''].shape
       a__  =seqlen
       a__,				a__  =self.num_layers
       a__,				a__  =self.num_attention_heads
       a__  =(
           batch,
           num_encoder_attention_heads,
           past_key_values_length,
           self._config.hidden_size // num_encoder_attention_heads,
       )
       a__  =common_inputs['''attention_mask'''].dtype
       a__  =torch.cat(
           [common_inputs['''attention_mask'''], torch.ones(lowercase_,   lowercase_,   dtype=lowercase_	)],   dim=1	)
       a__  =[
           (torch.zeros(lowercase_	), torch.zeros(lowercase_	)) for _ in range(lowercase_	)
       ]
     return common_inputs



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_ = -1,   lowercase_ = -1,   lowercase_ = False,   lowercase_ = None,   )							->   Mapping[str, Any]:






     """simple docstring"""






     a__  =compute_effective_axis_dimension(
         lowercase_,   fixed_dimension=OnnxConfig.default_fixed_batch,   num_token_to_add=0	)

     # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
     a__  =tokenizer.num_special_tokens_to_add(lowercase_	)
     a__  =compute_effective_axis_dimension(
         lowercase_,   fixed_dimension=OnnxConfig.default_fixed_sequence,   num_token_to_add=lowercase_	)

     # Generate dummy inputs according to compute batch and sequence
     a__  =[''' '''.join([tokenizer.unk_token]	) * seq_length] * batch_size
     a__  =dict(tokenizer(lowercase_,   return_tensors=lowercase_	)	)
     return common_inputs



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_ = -1,   lowercase_ = -1,   lowercase_ = False,   lowercase_ = None,   )							->   Mapping[str, Any]:






     """simple docstring"""






     if self.task in ["default", "seq2seq-lm"]:
       a__  =self._generate_dummy_inputs_for_default_and_seqaseq_lm(
           lowercase_,   batch_size=lowercase_,   seq_length=lowercase_,   is_pair=lowercase_,   framework=lowercase_	)

     elif self.task == "causal-lm":
       a__  =self._generate_dummy_inputs_for_causal_lm(
           lowercase_,   batch_size=lowercase_,   seq_length=lowercase_,   is_pair=lowercase_,   framework=lowercase_	)
     else:
       a__  =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           lowercase_,   batch_size=lowercase_,   seq_length=lowercase_,   is_pair=lowercase_,   framework=lowercase_	)

     return common_inputs



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_,   lowercase_,   lowercase_	)							->   Optional[int]:






     """simple docstring"""






     if self.task in ["default", "seq2seq-lm"]:
       a__  =super()._flatten_past_key_values_(lowercase_,   lowercase_,   lowercase_,   lowercase_	)
     else:
       a__  =super(lowercase_,   self	)._flatten_past_key_values_(
           lowercase_,   lowercase_,   lowercase_,   lowercase_	)



   def 			_UpperCAmelCase			(    self,   lowercase_,   lowercase_	)							->   Tuple:






     """simple docstring"""






     if direction not in ["inputs", "outputs"]:
       raise ValueError(F"""direction must either be \"inputs\" or \"outputs\", but {direction} was given"""	)

     a__  ='''past_key_values''' if direction == '''inputs''' else '''present'''
     a__,				a__  =self.num_layers

     a__  ='''past_encoder_sequence'''
     a__  ='''past_decoder_sequence''' if direction == '''inputs''' else '''past_decoder_sequence + sequence'''

     for i in range(lowercase_	):
       a__  ={0: '''batch''', 2: decoder_sequence}
       a__  ={0: '''batch''', 2: decoder_sequence}
       a__  ={0: '''batch''', 2: encoder_sequence}
       a__  ={0: '''batch''', 2: encoder_sequence}
