

'''simple docstring'''







from typing import List

import jiwer
import jiwer.transforms as tr
from packaging import version

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


__lowerCamelCase		       =    ''''''


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


      class 					A__      (      tr.AbstractTransform			):



          def __init__(					self		,				UpperCamelCase__ = " "       )						->      int:





                 '''simple docstring'''
                 A_						=      sentence_delimiter



          def 					snake_case_			(					self		,				UpperCamelCase__       )						->      str:





                 '''simple docstring'''
                 return list(UpperCamelCase__       )







          def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Dict:





                 '''simple docstring'''
                 A_						=      []
                 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

      __lowerCamelCase		       =    tr.Compose(
          [tr.RemoveMultipleSpaces(), tr.Strip(), SentencesToListOfCharacters(SENTENCE_DELIMITER)]
      )
else:
      __lowerCamelCase		       =    tr.Compose(
          [
              tr.RemoveMultipleSpaces(),
              tr.Strip(),
              tr.ReduceToSingleSentence(SENTENCE_DELIMITER),
              tr.ReduceToListOfListOfChars(),
          ]
      )


__lowerCamelCase		       =    '''\
@inproceedings{inproceedings,
    author = {Morris, Andrew and Maier, Viktoria and Green, Phil},
    year = {2004},
    month = {01},
    pages = {},
    title = {From WER and RIL to MER and WIL: improved evaluation measures for connected speech recognition.}
}
'''

__lowerCamelCase		       =    '''\
Character error rate (CER) is a common metric of the performance of an automatic speech recognition system.

CER is similar to Word Error Rate (WER), but operates on character instead of word. Please refer to docs of WER for further information.

Character error rate can be computed as:

CER = (S + D + I) / N = (S + D + I) / (S + D + C)

where

S is the number of substitutions,
D is the number of deletions,
I is the number of insertions,
C is the number of correct characters,
N is the number of characters in the reference (N=S+D+C).

CER\'s output is not always a number between 0 and 1, in particular when there is a high number of insertions. This value is often associated to the percentage of characters that were incorrectly predicted. The lower the value, the better the
performance of the ASR system with a CER of 0 being a perfect score.
'''

__lowerCamelCase		       =    '''
Computes CER score of transcribed segments against references.
Args:
    references: list of references for each speech input.
    predictions: list of transcribtions to score.
    concatenate_texts: Whether or not to concatenate sentences before evaluation, set to True for more accurate result.
Returns:
    (float): the character error rate

Examples:

    >>> predictions = ["this is the prediction", "there is an other sample"]
    >>> references = ["this is the reference", "there is another one"]
    >>> cer = datasets.load_metric("cer")
    >>> cer_score = cer.compute(predictions=predictions, references=references)
    >>> print(cer_score)
    0.34146341463414637
'''


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



    def 					snake_case_			(					self       )						->      Optional[Any]:





           '''simple docstring'''
           return datasets.MetricInfo(
               description=_DESCRIPTION		,				citation=_CITATION		,				inputs_description=_KWARGS_DESCRIPTION		,				features=datasets.Features(
                   {
                       """predictions""": datasets.Value("""string"""		,				id="""sequence"""       ),
                       """references""": datasets.Value("""string"""		,				id="""sequence"""       ),
                   }       )		,				codebase_urls=["""https://github.com/jitsi/jiwer/"""]		,				reference_urls=[
                   """https://en.wikipedia.org/wiki/Word_error_rate""",
                   """https://sites.google.com/site/textdigitisation/qualitymeasures/computingerrorrates""",
               ]		,				)







    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__=False       )						->      Tuple:





           '''simple docstring'''
           if concatenate_texts:
                  return jiwer.compute_measures(
                      UpperCamelCase__		,				UpperCamelCase__		,				truth_transform=UpperCamelCase__		,				hypothesis_transform=UpperCamelCase__		,				)["wer"]

           A_						=      0
           A_						=      0
           for prediction, reference in zip(UpperCamelCase__		,				UpperCamelCase__       ):
                  A_						=      jiwer.compute_measures(
                      UpperCamelCase__		,				UpperCamelCase__		,				truth_transform=UpperCamelCase__		,				hypothesis_transform=UpperCamelCase__		,				)
                  incorrect += measures["substitutions"] + measures["deletions"] + measures["insertions"]
                  total += measures["substitutions"] + measures["deletions"] + measures["hits"]

           return incorrect / total



'''simple docstring'''







import argparse
import re

from flax.traverse_util import flatten_dict, unflatten_dict
from tax import checkpoints

from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration
from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model
from transformers.utils import logging


logging.set_verbosity_info()


# should not include what is already done by the `from_pt` argument
__lowerCamelCase		       =    {
    '''/attention/''': '''/0/SelfAttention/''',
    '''/self_attention/''': '''/0/SelfAttention/''',
    '''/encoder_decoder_attention/''': '''/1/EncDecAttention/''',
    '''value''': '''v''',
    '''query''': '''q''',
    '''key''': '''k''',
    '''out''': '''o''',
    '''pre_self_attention_layer_norm''': '''0/layer_norm''',
    '''pre_cross_attention_layer_norm''': '''1/layer_norm''',
    '''pre_attention_layer_norm''': '''0/layer_norm''',  # previously 1, but seems wrong
    '''token_embedder''': '''shared''',
    '''encoder_norm''': '''final_layer_norm''',
    '''decoder_norm''': '''final_layer_norm''',
    '''relpos_bias/rel_embedding''': '''block/0/layer/0/SelfAttention/relative_attention_bias/weight''',
    '''router/router_weights/w/''': '''router/classifier/''',
    '''roer/roer_weights/w/''': '''router/classifier/''',
    '''logits_dense''': '''lm_head''',
}
def       UpperCAmelCase__    (      UpperCAmelCase__				)			->			Tuple:
       # 1. in HF T5, we have block.{x}.layer.{y}. which corresponds to layer.{x} in
       # the original model
       A_						=      list(s_dict.keys()				)
       for key in keys:
              A_						=      r""".*/layers_(\d+)"""
              A_						=      key
              if re.match(UpperCAmelCase__,       UpperCAmelCase__				):
                     A_						=      re.sub(r"""layers_(\d+)""",       r"""block/\1/layer""",       UpperCAmelCase__				)

              A_						=      r"""(encoder|decoder)\/"""

              if re.match(UpperCAmelCase__,       UpperCAmelCase__				):
                     A_						=      re.match(UpperCAmelCase__,       UpperCAmelCase__				).groups()
                     if groups[0] == "encoder":
                            A_						=      re.sub(r"""/mlp/""",       r"""/1/mlp/""",       UpperCAmelCase__				)
                            A_						=      re.sub(r"""/pre_mlp_layer_norm/""",       r"""/1/layer_norm/""",       UpperCAmelCase__				)

                     elif groups[0] == "decoder":
                            A_						=      re.sub(r"""/mlp/""",       r"""/2/mlp/""",       UpperCAmelCase__				)
                            A_						=      re.sub(r"""/pre_mlp_layer_norm/""",       r"""/2/layer_norm/""",       UpperCAmelCase__				)

        # 2. Convert other classic mappings
              for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items():
                     if old_key in new_key:
                            A_						=      new_key.replace(UpperCAmelCase__,       UpperCAmelCase__				)

              print(F'''{key} -> {new_key}'''				)
              A_						=      s_dict.pop(UpperCAmelCase__				)

       if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict:
              A_						=      s_dict[
                  """encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight"""
              ].T
       if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict:
              A_						=      s_dict[
                  """decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight"""
              ].T

       # 3. Take extra care of the EXPERTS layer
       for key in list(s_dict.keys()				):
              if "expert" in key:
                     A_						=      s_dict[key].shape[0]
                     A_						=      s_dict[key]
                     for idx in range(UpperCAmelCase__				):
                            A_						=      expert_weihts[idx]
                            print(F'''{key} -> {key.replace("expert/",       "nested fstring"				)}'''				)

                     s_dict.pop(UpperCAmelCase__				)

       return s_dict


__lowerCamelCase		       =    {
    '''NUM_ENCODER_LAYERS''': '''num_layers''',
    '''NUM_DECODER_LAYERS''': '''num_decoder_layers''',
    '''NUM_HEADS''': '''num_heads''',
    '''HEAD_DIM''': '''d_kv''',
    '''EMBED_DIM''': '''d_model''',
    '''MLP_DIM''': '''d_ff''',
    '''NUM_SELECTED_EXPERTS''': '''num_selected_experts''',
    '''NUM_ENCODER_SPARSE_LAYERS''': '''num_sparse_encoder_layers''',
    '''NUM_DECODER_SPARSE_LAYERS''': '''num_sparse_decoder_layers''',
    '''dense.MlpBlock.activations''': '''feed_forward_proj''',
}
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__				)			->			List[Any]:
       # Convert a google style config to the hugging face fromat
       import regex as re

       with open(UpperCAmelCase__,       """r"""				) as f:
              A_						=      f.read()

       A_						=      re.findall(r"""(.*) = ([0-9.]*)""",       UpperCAmelCase__				)
       A_						=      {}
       for param, value in regex_match:
              if param in GIN_TO_CONFIG_MAPPING and value != "":
                     A_						=      float(UpperCAmelCase__				) if """.""" in value else int(UpperCAmelCase__				)

       A_						=      re.findall(r"""(.*activations) = \(\'(.*)\',\)""",       UpperCAmelCase__				)[0]
       A_						=      str(activation[1]				)

       A_						=      num_experts
       A_						=      SwitchTransformersConfig(**UpperCAmelCase__				)
       return config
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__,       UpperCAmelCase__=None,       UpperCAmelCase__="./",       UpperCAmelCase__=8				)			->			List[str]:
       # Initialise PyTorch model

       print(F'''Loading flax weights from : {flax_checkpoint_path}'''				)
       A_						=      checkpoints.load_tax_checkpoint(UpperCAmelCase__				)

       if gin_file is not None:
              A_						=      convert_gin_to_config(UpperCAmelCase__,       UpperCAmelCase__				)
       else:
              A_						=      SwitchTransformersConfig.from_pretrained(UpperCAmelCase__				)

       A_						=      SwitchTransformersForConditionalGeneration(UpperCAmelCase__				)

       A_						=      flax_params["""target"""]
       A_						=      flatten_dict(UpperCAmelCase__,       sep="""/"""				)
       A_						=      rename_keys(UpperCAmelCase__				)
       A_						=      unflatten_dict(UpperCAmelCase__,       sep="""/"""				)

       # Load the flax params in the PT model
       load_flax_weights_in_pytorch_model(UpperCAmelCase__,       UpperCAmelCase__				)

       print(F'''Save PyTorch model to {pytorch_dump_path}'''				)
       pt_model.save_pretrained(UpperCAmelCase__				)


if __name__ == "__main__":
      __lowerCamelCase		       =    argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          '''--switch_t5x_checkpoint_path''',
          default=None,
          type=str,
          required=True,
          help=(
              '''The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the'''
              ''' model architecture. If not provided, a `gin_file` has to be provided.'''
          ),
      )
      parser.add_argument(
          '''--gin_file''',
          default=None,
          type=str,
          required=False,
          help='''Path to the gin config file. If not provided, a `config_file` has to be passed   ''',
      )
      parser.add_argument(
          '''--config_name''', default=None, type=str, required=False, help='''Config name of SwitchTransformers model.'''
      )
      parser.add_argument(
          '''--pytorch_dump_folder_path''', default=None, type=str, required=True, help='''Path to the output pytorch model.'''
      )
      parser.add_argument('''--num_experts''', default=8, type=int, required=False, help='''Number of experts''')
      __lowerCamelCase		       =    parser.parse_args()
      convert_flax_checkpoint_to_pytorch(
          args.switch_tax_checkpoint_path,
          args.config_name,
          args.gin_file,
          args.pytorch_dump_folder_path,
          args.num_experts,
      )
