


'''simple docstring'''




import argparse
import json
import os
from collections import OrderedDict

import numpy as np
import tensorflow as tf
import torch





def   _a		(lowercase__			:   List[str]						)			->			Any:






     """simple docstring"""
     __snake_case           =						os.path.join(args.tf_model_dir							,   'parameters.json'						)
     __snake_case           =						json.loads(open(lowercase__						).read()						)
     if not params:
          raise ValueError(
              f'It seems that the json file at {parameter_file} is empty. Make sure you have a correct json file.'						)
     if not args.output.endswith('.pt'						):
          __snake_case           =						args.output + '.pt'
     __snake_case           =						OrderedDict()
     with tf.device('/CPU:0'						):
          __snake_case           =						tf.train.load_checkpoint(args.tf_model_dir						)
          __snake_case           =						reader.get_variable_to_shape_map()
          for key_name in shapes.keys():
               __snake_case           =						reader.get_tensor(lowercase__						).astype(np.floataa						)
               if key_name.endswith('/adam_m'						) or key_name.endswith('/adam_v'						):
                    continue
               if key_name.startswith('pasts/'						):
                    if key_name.startswith('pasts/mlp'						):
                         __snake_case           =						int(key_name[9]						)
                    elif key_name.startswith('pasts/out'						):
                         __snake_case           =						8
                    __snake_case           =						'model.sqout.%d.weight' % (player * 2)  # enter to nn.Sequencial with Tanh, so 2 at a time
                    __snake_case           =						vnp.transpose([1, 0]						).copy()  # Mesh-Tensorflow is a diagonal matrix
                    __snake_case           =						torch.tensor(lowercase__						)
               elif key_name.startswith('model/moe'						):
                    __snake_case           =						int(key_name[9:].split('/'						)[0]						)
                    if key_name.endswith('/switch_gating/kernel'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.mlp.router.classifier.weight' % player
                         __snake_case           =						vnp.transpose([1, 0]						).copy()  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/softmlp/kernel'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.soft_bypass_mlp.weight' % player
                         __snake_case           =						vnp.transpose([1, 0]						).copy()  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/wo/kernel'						) or key_name.endswith('/wi/kernel'						):
                         __snake_case           =						key_name[-9:-7]
                         for i in range(1_6						):
                              __snake_case           =						'model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight' % (player, i, nlayer)
                              __snake_case           =						(
                                  vnp[i].transpose([1, 0]						).copy()
                              )  # In Mesh-Tensorflow, it is one array, so it is divided
                              __snake_case           =						torch.tensor(lowercase__						)
               elif key_name.startswith('model/mlp'						):
                    __snake_case           =						int(key_name[9:].split('/'						)[0]						)
                    if key_name.endswith('/p1/kernel'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.mlp.wi.weight' % player
                         __snake_case           =						vnp.transpose([1, 0]						).copy()  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/p1/bias'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.mlp.wi.bias' % player
                         __snake_case           =						vnp.copy()  # same because it is one dimensional
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/p2/kernel'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.mlp.wo.weight' % player
                         __snake_case           =						vnp.transpose([1, 0]						).copy()  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/p2/bias'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.mlp.wo.bias' % player
                         __snake_case           =						vnp.copy()  # same because it is one dimensional
                         __snake_case           =						torch.tensor(lowercase__						)
               elif key_name.startswith('model/ln'						):
                    __snake_case           =						int(key_name[8:].split('/'						)[0]						)
                    if key_name.endswith('/b'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.norm.bias' % player
                         __snake_case           =						vnp.copy()  # same because it is one dimensional
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/g'						):
                         __snake_case           =						'model.blocks.%d.feed_forward.norm.weight' % player
                         __snake_case           =						vnp.copy()  # same because it is one dimensional
                         __snake_case           =						torch.tensor(lowercase__						)
               elif key_name.startswith('model/att'						):
                    __snake_case           =						int(key_name[9:].split('/'						)[0]						)
                    if key_name.endswith('/qkv/kernel'						):
                         __snake_case           =						vnp.copy()  # Compute same dimension as Mesh-tensorflow using einsum
                         __snake_case           =						state[:, 0, :, :]
                         __snake_case           =						state[:, 1, :, :]
                         __snake_case           =						state[:, 2, :, :]
                         __snake_case           =						(
                             state_q.reshape([state_q.shape[0], state_q.shape[1] * state_q.shape[2]]						)
                             .transpose([1, 0]						)
                             .copy()
                         )  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						(
                             state_k.reshape([state_k.shape[0], state_k.shape[1] * state_k.shape[2]]						)
                             .transpose([1, 0]						)
                             .copy()
                         )  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						(
                             state_v.reshape([state_v.shape[0], state_v.shape[1] * state_v.shape[2]]						)
                             .transpose([1, 0]						)
                             .copy()
                         )  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						'model.blocks.%d.self_attn.self_attn.q_proj.weight' % player
                         __snake_case           =						torch.tensor(lowercase__						)
                         __snake_case           =						'model.blocks.%d.self_attn.self_attn.k_proj.weight' % player
                         __snake_case           =						torch.tensor(lowercase__						)
                         __snake_case           =						'model.blocks.%d.self_attn.self_attn.v_proj.weight' % player
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/o/kernel'						):
                         __snake_case           =						'model.blocks.%d.self_attn.self_attn.out_proj.weight' % player
                         __snake_case           =						(
                             vnp.reshape([vnp.shape[0] * vnp.shape[1], vnp.shape[2]]						).transpose([1, 0]						).copy()
                         )  # Mesh-Tensorflow is a diagonal matrix
                         __snake_case           =						torch.tensor(lowercase__						)
               elif key_name.startswith('model/an'						):
                    __snake_case           =						int(key_name[8:].split('/'						)[0]						)
                    if key_name.endswith('/b'						):
                         __snake_case           =						'model.blocks.%d.self_attn.norm.bias' % player
                         __snake_case           =						vnp.copy()  # same because it is one dimensional
                         __snake_case           =						torch.tensor(lowercase__						)
                    elif key_name.endswith('/g'						):
                         __snake_case           =						'model.blocks.%d.self_attn.norm.weight' % player
                         __snake_case           =						vnp.copy()  # same because it is one dimensional
                         __snake_case           =						torch.tensor(lowercase__						)
               elif (
                   key_name.startswith('model/wte'						)
                   or key_name.startswith('model/wpe'						)
                   or key_name.startswith('model/ete'						)
               ):
                    __snake_case           =						{'wte': 'embed_tokens', 'wpe': 'position_embeddings', 'ete': 'extra_position_embeddings'}[
                        key_name[-3:]
                    ]
                    __snake_case           =						'model.%s.weight' % nlayer
                    __snake_case           =						vnp.copy()  # same in embedded
                    __snake_case           =						torch.tensor(lowercase__						)
                    if key_name.startswith('model/wte'						):
                         __snake_case           =						'lm_head.weight'
                         __snake_case           =						vnp.copy()  # same in embedded
                         __snake_case           =						torch.tensor(lowercase__						)
               elif key_name.startswith('model/wob'						):
                    __snake_case           =						'final_logits_bias'
                    __snake_case           =						vnp.copy()  # same in embedded
                    __snake_case           =						state.reshape((1, -1)						)
                    __snake_case           =						torch.tensor(lowercase__						)
               elif key_name == "model/dense/kernel":
                    __snake_case           =						'model.last_project.weight'
                    __snake_case           =						vnp.transpose([1, 0]						).copy()  # Mesh-Tensorflow is a diagonal matrix
                    __snake_case           =						torch.tensor(lowercase__						)
               elif key_name == "model/dense_1/bias":
                    __snake_case           =						'model.last_project.bias'
                    __snake_case           =						vnp.copy()  # same because it is one dimensional
                    __snake_case           =						torch.tensor(lowercase__						)
     torch.save(lowercase__							,   args.output						)


if __name__ == "__main__":
       _a   :   Optional[Any]												=   argparse.ArgumentParser(
           description="model converter.", formatter_class=argparse.ArgumentDefaultsHelpFormatter
       )
       parser.add_argument("--tf_model_dir", metavar="PATH", type=str, required=True, help="import model")
       parser.add_argument("--output", metavar="PATH", type=str, required=True, help="output model")
       _a   :   List[str]												=   parser.parse_args()
       convert_tf_gptsan_to_pt(args)









"""simple docstring"""







import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


UpperCamelCase     =							{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

UpperCamelCase     =							{
    """vocab_file""": {
        """google/electra-small-generator""": (
            """https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt"""
        ),
        """google/electra-base-generator""": """https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt""",
        """google/electra-large-generator""": (
            """https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt"""
        ),
        """google/electra-small-discriminator""": (
            """https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt"""
        ),
        """google/electra-base-discriminator""": (
            """https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt"""
        ),
        """google/electra-large-discriminator""": (
            """https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """google/electra-small-generator""": (
            """https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json"""
        ),
        """google/electra-base-generator""": (
            """https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json"""
        ),
        """google/electra-large-generator""": (
            """https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json"""
        ),
        """google/electra-small-discriminator""": (
            """https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json"""
        ),
        """google/electra-base-discriminator""": (
            """https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json"""
        ),
        """google/electra-large-discriminator""": (
            """https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json"""
        ),
    },
}

UpperCamelCase     =							{
    """google/electra-small-generator""": 512,
    """google/electra-base-generator""": 512,
    """google/electra-large-generator""": 512,
    """google/electra-small-discriminator""": 512,
    """google/electra-base-discriminator""": 512,
    """google/electra-large-discriminator""": 512,
}

UpperCamelCase     =							{
    """google/electra-small-generator""": {"""do_lower_case""": True},
    """google/electra-base-generator""": {"""do_lower_case""": True},
    """google/electra-large-generator""": {"""do_lower_case""": True},
    """google/electra-small-discriminator""": {"""do_lower_case""": True},
    """google/electra-base-discriminator""": {"""do_lower_case""": True},
    """google/electra-large-discriminator""": {"""do_lower_case""": True},
}






class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			A__	:					Dict							  =     VOCAB_FILES_NAMES
			A__	:					str							  =     PRETRAINED_VOCAB_FILES_MAP
			A__	:					List[Any]							  =     PRETRAINED_INIT_CONFIGURATION
			A__	:					Any							  =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			A__	:					List[str]							  =     ElectraTokenizer


			def __init__(      self						,	SCREAMING_SNAKE_CASE__=None						,	SCREAMING_SNAKE_CASE__=None						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__="[UNK]"						,	SCREAMING_SNAKE_CASE__="[SEP]"						,	SCREAMING_SNAKE_CASE__="[PAD]"						,	SCREAMING_SNAKE_CASE__="[CLS]"						,	SCREAMING_SNAKE_CASE__="[MASK]"						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=None						,	**SCREAMING_SNAKE_CASE__						,	)      ->	str:
				super().__init__(
				    SCREAMING_SNAKE_CASE__						,	tokenizer_file=SCREAMING_SNAKE_CASE__						,	do_lower_case=SCREAMING_SNAKE_CASE__						,	unk_token=SCREAMING_SNAKE_CASE__						,	sep_token=SCREAMING_SNAKE_CASE__						,	pad_token=SCREAMING_SNAKE_CASE__						,	cls_token=SCREAMING_SNAKE_CASE__						,	mask_token=SCREAMING_SNAKE_CASE__						,	tokenize_chinese_chars=SCREAMING_SNAKE_CASE__						,	strip_accents=SCREAMING_SNAKE_CASE__						,	**SCREAMING_SNAKE_CASE__						,	)

				A__											=					json.loads(self.backend_tokenizer.normalizer.__getstate__()		)
				if (
				    normalizer_state.get("lowercase"						,	SCREAMING_SNAKE_CASE__		) != do_lower_case
				    or normalizer_state.get("strip_accents"						,	SCREAMING_SNAKE_CASE__		) != strip_accents
				    or normalizer_state.get("handle_chinese_chars"						,	SCREAMING_SNAKE_CASE__		) != tokenize_chinese_chars
				):
					A__											=					getattr(SCREAMING_SNAKE_CASE__						,	normalizer_state.pop("type"		)		)
					A__											=					do_lower_case
					A__											=					strip_accents
					A__											=					tokenize_chinese_chars
					A__											=					normalizer_class(**SCREAMING_SNAKE_CASE__		)

				A__											=					do_lower_case


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=None		)      ->	List[str]:
				A__											=					[self.cls_token_id] + token_ids_a + [self.sep_token_id]

				if token_ids_a:
					output += token_ids_a + [self.sep_token_id]

				return output


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	List[int]:
				A__											=					[self.sep_token_id]
				A__											=					[self.cls_token_id]
				if token_ids_a is None:
					return len(cls + token_ids_a + sep		) * [0]
				return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	Tuple[str]:
				A__											=					self._tokenizer.model.save(SCREAMING_SNAKE_CASE__						,	name=SCREAMING_SNAKE_CASE__		)
				return tuple(SCREAMING_SNAKE_CASE__		)




