


import glob
import os
from typing import Dict, List, Union

import torch

from diffusers.utils import is_safetensors_available


if is_safetensors_available():
   import safetensors.torch

from huggingface_hub import snapshot_download

from diffusers import DiffusionPipeline, __version__
from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME
from diffusers.utils import CONFIG_NAME, DIFFUSERS_CACHE, ONNX_WEIGHTS_NAME, WEIGHTS_NAME







class lowerCAmelCase_						( lowerCamelCase__       ):

 '''simple docstring'''

 def __init__(	self					):
       self.register_to_config()
       super().__init__()



 def 						UpperCamelCase__     (	self		,      _UpperCAmelCase		,      _UpperCAmelCase					):
       if dicta == dicta:
             return True
       else:
             snake_case_       =      self._remove_meta_keys(UpperCAmelCase_					)
             snake_case_       =      self._remove_meta_keys(UpperCAmelCase_					)
             if configa == configa:
                   print(F'''Warning !: Mismatch in keys {meta_keysa} and {meta_keysa}.'''					)
                   return True
       return False



 def 						UpperCamelCase__     (	self		,      _UpperCAmelCase					):
       snake_case_       =      []
       snake_case_       =      config_dict.copy()
       for key in config_dict.keys():
             if key.startswith('''_'''					):
                   temp_dict.pop(UpperCAmelCase_					)
                   meta_keys.append(UpperCAmelCase_					)
       return (temp_dict, meta_keys)



 @torch.no_grad()
 def 						UpperCamelCase__     (	self		,      _UpperCAmelCase		,      **_UpperCAmelCase					):
       snake_case_       =      kwargs.pop('''cache_dir'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''resume_download'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''force_download'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''proxies'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''local_files_only'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''use_auth_token'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''revision'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''torch_dtype'''		,      UpperCAmelCase_					)
       snake_case_       =      kwargs.pop('''device_map'''		,      UpperCAmelCase_					)

       snake_case_       =      kwargs.pop('''alpha'''		,      0.5					)
       snake_case_       =      kwargs.pop('''interp'''		,      UpperCAmelCase_					)

       print('''Received list'''		,      UpperCAmelCase_					)
       print(F'''Combining with alpha={alpha}, interpolation mode={interp}'''					)

       snake_case_       =      len(UpperCAmelCase_					)
       # Ignore result from model_index_json comparision of the two checkpoints
       snake_case_       =      kwargs.pop('''force'''		,      UpperCAmelCase_					)

       # If less than 2 checkpoints, nothing to merge. If more than 3, not supported for now.
       if checkpoint_count > 3 or checkpoint_count < 2:
             raise ValueError(
                 '''Received incorrect number of checkpoints to merge. Ensure that either 2 or 3 checkpoints are being'''
                 ''' passed.'''					)

       print('''Received the right number of checkpoints'''					)
       # chkpt0, chkpt1 = pretrained_model_name_or_path_list[0:2]
       # chkpt2 = pretrained_model_name_or_path_list[2] if checkpoint_count == 3 else None

       # Validate that the checkpoints can be merged
       # Step 1: Load the model config and compare the checkpoints. We'll compare the model_index.json first while ignoring the keys starting with '_'
       snake_case_       =      []
       for pretrained_model_name_or_path in pretrained_model_name_or_path_list:
             snake_case_       =      DiffusionPipeline.load_config(
                 UpperCAmelCase_		,      cache_dir=UpperCAmelCase_		,      resume_download=UpperCAmelCase_		,      force_download=UpperCAmelCase_		,      proxies=UpperCAmelCase_		,      local_files_only=UpperCAmelCase_		,      use_auth_token=UpperCAmelCase_		,      revision=UpperCAmelCase_		,      )
             config_dicts.append(UpperCAmelCase_					)

       snake_case_       =      True
       for idx in range(1		,      len(UpperCAmelCase_					)					):
             comparison_result &= self._compare_model_configs(config_dicts[idx - 1]		,      config_dicts[idx]					)
             if not force and comparison_result is False:
                   raise ValueError('''Incompatible checkpoints. Please check model_index.json for the models.'''					)
                   print(config_dicts[0]		,      config_dicts[1]					)
       print('''Compatible model_index.json files found'''					)
       # Step 2: Basic Validation has succeeded. Let's download the models and save them into our local files.
       snake_case_       =      []
       for pretrained_model_name_or_path, config_dict in zip(UpperCAmelCase_		,      UpperCAmelCase_					):
             snake_case_       =      [k for k in config_dict.keys() if not k.startswith('''_'''					)]
             snake_case_       =      [os.path.join(UpperCAmelCase_		,      '''*'''					) for k in folder_names]
             allow_patterns += [
                 WEIGHTS_NAME,
                 SCHEDULER_CONFIG_NAME,
                 CONFIG_NAME,
                 ONNX_WEIGHTS_NAME,
                 DiffusionPipeline.config_name,
             ]
             snake_case_       =      config_dict.get('''_class_name'''					)
             snake_case_       =      {"diffusers": __version__, "pipeline_class": requested_pipeline_class}

             snake_case_       =      (
                 pretrained_model_name_or_path
                 if os.path.isdir(UpperCAmelCase_					)
                 else snapshot_download(
                     UpperCAmelCase_		,      cache_dir=UpperCAmelCase_		,      resume_download=UpperCAmelCase_		,      proxies=UpperCAmelCase_		,      local_files_only=UpperCAmelCase_		,      use_auth_token=UpperCAmelCase_		,      revision=UpperCAmelCase_		,      allow_patterns=UpperCAmelCase_		,      user_agent=UpperCAmelCase_		,      )
             )
             print('''Cached Folder'''		,      UpperCAmelCase_					)
             cached_folders.append(UpperCAmelCase_					)

       # Step 3:-
       # Load the first checkpoint as a diffusion pipeline and modify its module state_dict in place
       snake_case_       =      DiffusionPipeline.from_pretrained(
           cached_folders[0]		,      torch_dtype=UpperCAmelCase_		,      device_map=UpperCAmelCase_					)
       final_pipe.to(self.device					)

       snake_case_       =      None
       if len(UpperCAmelCase_					) > 2:
             snake_case_       =      os.path.join(cached_folders[2]					)

       if interp == "sigmoid":
             snake_case_       =      CheckpointMergerPipeline.sigmoid
       elif interp == "inv_sigmoid":
             snake_case_       =      CheckpointMergerPipeline.inv_sigmoid
       elif interp == "add_diff":
             snake_case_       =      CheckpointMergerPipeline.add_difference
       else:
             snake_case_       =      CheckpointMergerPipeline.weighted_sum

       # Find each module's state dict.
       for attr in final_pipe.config.keys():
             if not attr.startswith('''_'''					):
                   snake_case_       =      os.path.join(cached_folders[1]		,      UpperCAmelCase_					)
                   if os.path.exists(UpperCAmelCase_					):
                         snake_case_       =      [
                             *glob.glob(os.path.join(UpperCAmelCase_		,      '''*.safetensors'''					)					),
                             *glob.glob(os.path.join(UpperCAmelCase_		,      '''*.bin'''					)					),
                         ]
                         snake_case_       =      files[0] if len(UpperCAmelCase_					) > 0 else None
                   if len(UpperCAmelCase_					) < 3:
                         snake_case_       =      None
                   else:
                         snake_case_       =      os.path.join(cached_folders[2]		,      UpperCAmelCase_					)
                         if os.path.exists(UpperCAmelCase_					):
                               snake_case_       =      [
                                   *glob.glob(os.path.join(UpperCAmelCase_		,      '''*.safetensors'''					)					),
                                   *glob.glob(os.path.join(UpperCAmelCase_		,      '''*.bin'''					)					),
                               ]
                               snake_case_       =      files[0] if len(UpperCAmelCase_					) > 0 else None
                                # For an attr if both checkpoint_path_1 and 2 are None, ignore.
                                # If atleast one is present, deal with it according to interp method, of course only if the state_dict keys match.
                   if checkpoint_path_a is None and checkpoint_path_a is None:
                         print(F'''Skipping {attr}: not present in 2nd or 3d model'''					)
                         continue
                   try:
                         snake_case_       =      getattr(UpperCAmelCase_		,      UpperCAmelCase_					)
                         if isinstance(UpperCAmelCase_		,      UpperCAmelCase_					):  # ignore requires_safety_checker boolean
                               continue
                         snake_case_       =      getattr(UpperCAmelCase_		,      '''state_dict'''					)
                         snake_case_       =      theta_a()

                         snake_case_       =      getattr(UpperCAmelCase_		,      '''load_state_dict'''					)
                         snake_case_       =      (
                             safetensors.torch.load_file(UpperCAmelCase_					)
                             if (is_safetensors_available() and checkpoint_path_a.endswith('''.safetensors'''					))
                             else torch.load(UpperCAmelCase_		,      map_location='''cpu'''					)
                         )
                         snake_case_       =      None
                         if checkpoint_path_a:
                               snake_case_       =      (
                                   safetensors.torch.load_file(UpperCAmelCase_					)
                                   if (is_safetensors_available() and checkpoint_path_a.endswith('''.safetensors'''					))
                                   else torch.load(UpperCAmelCase_		,      map_location='''cpu'''					)
                               )

                         if not theta_a.keys() == theta_a.keys():
                               print(F'''Skipping {attr}: key mismatch'''					)
                               continue
                         if theta_a and not theta_a.keys() == theta_a.keys():
                               print(F'''Skipping {attr}:y mismatch'''					)
                   except Exception as e:
                         print(F'''Skipping {attr} do to an unexpected error: {str(UpperCAmelCase_					)}'''					)
                         continue
                   print(F'''MERGING {attr}'''					)

                   for key in theta_a.keys():
                         if theta_a:
                               snake_case_       =      theta_func(theta_a[key]		,      theta_a[key]		,      theta_a[key]		,      UpperCAmelCase_					)
                         else:
                               snake_case_       =      theta_func(theta_a[key]		,      theta_a[key]		,      UpperCAmelCase_		,      UpperCAmelCase_					)

                   del theta_a
                   del theta_a
                   update_theta_a(UpperCAmelCase_					)

                   del theta_a
       return final_pipe



 @staticmethod
 def 						UpperCamelCase__     (	_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					):
       return ((1 - alpha) * thetaa) + (alpha * thetaa)



 @staticmethod
 def 						UpperCamelCase__     (	_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					):
       snake_case_       =      alpha * alpha * (3 - (2 * alpha))
       return thetaa + ((thetaa - thetaa) * alpha)



 @staticmethod
 def 						UpperCamelCase__     (	_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					):
       import math

       snake_case_       =      0.5 - math.sin(math.asin(1.0 - 2.0 * alpha					) / 3.0					)
       return thetaa + ((thetaa - thetaa) * alpha)





 @staticmethod
 def 						UpperCamelCase__     (	_UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase					):
       return thetaa + (thetaa - thetaa) * (1.0 - alpha)







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)
snake_case			 =						"""\
"""

snake_case			 =						"""\
    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.
"""

snake_case			 =						"""
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}
"""

snake_case			 =						["""fleurs-asr""", """mls""", """voxpopuli""", """babel""", """covost2""", """fleurs-lang_id""", """minds14"""]
snake_case			 =						""""""

try:
   from jiwer import transforms as tr

   snake_case			 =						True
except ImportError:
   snake_case			 =						False

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


   class        SCREAMING_SNAKE_CASE	(	tr.AbstractTransform    ):
        '''simple docstring'''


        def __init__( self				:		Dict    ,       UpperCAmelCase_				:		str = " "					):
          SCREAMING_SNAKE_CASE				:					List[Any]		  =			sentence_delimiter


        def  _A				( self				:		Optional[int]    ,       UpperCAmelCase_				:		str					):
          return list(UpperCAmelCase_					)






        def  _A				( self				:		str    ,       UpperCAmelCase_				:		List[str]					):
          SCREAMING_SNAKE_CASE				:					Tuple		  =			[]
          for sent_idx, sentence in enumerate(UpperCAmelCase_					):
            chars.extend(self.process_string(UpperCAmelCase_					)					)
            if self.sentence_delimiter is not None and self.sentence_delimiter != "" and sent_idx < len(UpperCAmelCase_					) - 1:
              chars.append(self.sentence_delimiter					)
          return chars





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


def      lowerCamelCase__	(   lowercase  ,   lowercase ):

  """simple docstring"""






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


def      lowerCamelCase__	(   lowercase  ,   lowercase ):

  """simple docstring"""






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


def      lowerCamelCase__	(   lowercase  ,   lowercase  ,   lowercase="exp"  ,   lowercase=None  ,   lowercase=False  ,   lowercase=False  ,   lowercase=None  ,   lowercase=False  ,   ):

  """simple docstring"""






  SCREAMING_SNAKE_CASE				:					str		  =			[[label] for label in labels]
  SCREAMING_SNAKE_CASE				:					Tuple		  =			list(lowercase )
  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\"`." )

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


def      lowerCamelCase__	(   lowercase  ,   lowercase  ,   lowercase  ,   lowercase ):

  """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:
    SCREAMING_SNAKE_CASE				:					Tuple		  =			compute_measures(lowercase  ,   lowercase )["wer"]

    SCREAMING_SNAKE_CASE				:					int		  =			compute_measures(lowercase  ,   lowercase  ,   truth_transform=lowercase  ,   hypothesis_transform=lowercase )["wer"]
    return {"wer": wer, "cer": cer}
  else:

    def compute_score(lowercase  ,   lowercase  ,   lowercase="wer" ):
      SCREAMING_SNAKE_CASE				:					Tuple		  =			0
      SCREAMING_SNAKE_CASE				:					str		  =			0
      for prediction, reference in zip(lowercase  ,   lowercase ):
        if score_type == "wer":
          SCREAMING_SNAKE_CASE				:					int		  =			compute_measures(lowercase  ,   lowercase )
        elif score_type == "cer":
          SCREAMING_SNAKE_CASE				:					int		  =			compute_measures(
              lowercase  ,   lowercase  ,   truth_transform=lowercase  ,   hypothesis_transform=lowercase )
        incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
        total += measures["substitutions"] + measures["deletions"] + measures["hits"]
      return incorrect / total

    return {"wer": compute_score(lowercase  ,   lowercase  ,   "wer" ), "cer": compute_score(lowercase  ,   lowercase  ,   "cer" )}


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION					,						_KWARGS_DESCRIPTION    )
class        SCREAMING_SNAKE_CASE	(	datasets.Metric    ):
     '''simple docstring'''


     def  _A				( self				:		str					):
       if self.config_name not in _CONFIG_NAMES:
         raise KeyError(f'''You should supply a configuration name selected in {_CONFIG_NAMES}'''					)

       SCREAMING_SNAKE_CASE				:					Tuple		  =			"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(UpperCAmelCase_					), "references": datasets.Value(UpperCAmelCase_					)}					)    ,       codebase_urls=[]    ,       reference_urls=[]    ,       format="numpy"    ,       )






     def  _A				( self				:		Optional[Any]    ,       UpperCAmelCase_				:		int    ,       UpperCAmelCase_				:		Union[str, Any]    ,       UpperCAmelCase_				:		str=None    ,       UpperCAmelCase_				:		Dict=None					):
       SCREAMING_SNAKE_CASE				:					str		  =			bleu_kwargs if bleu_kwargs is not None else {}
       SCREAMING_SNAKE_CASE				:					Optional[Any]		  =			wer_kwargs if wer_kwargs is not None else {}

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






