


import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

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


lowerCAmelCase__  :List[Any]             =							{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCAmelCase__  :Any             =							{
    '''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'''
        ),
    },
}

lowerCAmelCase__  :List[Any]             =							{
    '''google/electra-small-generator''': 5_1_2,
    '''google/electra-base-generator''': 5_1_2,
    '''google/electra-large-generator''': 5_1_2,
    '''google/electra-small-discriminator''': 5_1_2,
    '''google/electra-base-discriminator''': 5_1_2,
    '''google/electra-large-discriminator''': 5_1_2,
}

lowerCAmelCase__  :Optional[Any]             =							{
    '''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 		__a				(    UpperCAmelCase      ):
		_a     :  int           =     VOCAB_FILES_NAMES
		_a     :  Union[str, Any]           =     PRETRAINED_VOCAB_FILES_MAP
		_a     :  Any           =     PRETRAINED_INIT_CONFIGURATION
		_a     :  str           =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		_a     :  Optional[int]           =     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      ,   )			->	Union[str, Any]:




									"""simple docstring"""





									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      ,   )

									_UpperCAmelCase					=      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
									):
																_UpperCAmelCase					=      getattr(_SCREAMING_SNAKE_CASE      ,   normalizer_state.pop('type'						)						)
																_UpperCAmelCase					=      do_lower_case
																_UpperCAmelCase					=      strip_accents
																_UpperCAmelCase					=      tokenize_chinese_chars
																_UpperCAmelCase					=      normalizer_class(**_SCREAMING_SNAKE_CASE						)

									_UpperCAmelCase					=      do_lower_case
		def      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE=None						)			->	Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=      [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      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE = None						)			->	List[int]:




									"""simple docstring"""





									_UpperCAmelCase					=      [self.sep_token_id]
									_UpperCAmelCase					=      [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      UpperCAmelCase__    (      self      ,   _SCREAMING_SNAKE_CASE      ,   _SCREAMING_SNAKE_CASE = None						)			->	Tuple[str]:




									"""simple docstring"""





									_UpperCAmelCase					=      self._tokenizer.model.save(_SCREAMING_SNAKE_CASE      ,   name=_SCREAMING_SNAKE_CASE						)
									return tuple(_SCREAMING_SNAKE_CASE						)




import argparse

import torch

from transformers import BertForMaskedLM


if __name__ == "__main__":
		lowerCAmelCase__  :Tuple             =							argparse.ArgumentParser(
		    description=(
		        '''Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned'''
		        ''' Distillation'''
		    )
		)
		parser.add_argument('''--model_type''', default='''bert''', choices=['''bert'''])
		parser.add_argument('''--model_name''', default='''bert-base-uncased''', type=str)
		parser.add_argument('''--dump_checkpoint''', default='''serialization_dir/tf_bert-base-uncased_0247911.pth''', type=str)
		parser.add_argument('''--vocab_transform''', action='''store_true''')
		lowerCAmelCase__  :Optional[int]             =							parser.parse_args()

		if args.model_type == "bert":
				lowerCAmelCase__  :Tuple             =							BertForMaskedLM.from_pretrained(args.model_name)
				lowerCAmelCase__  :Optional[int]             =							'''bert'''
		else:
				raise ValueError('''args.model_type should be "bert".''')

		lowerCAmelCase__  :Any             =							model.state_dict()
		lowerCAmelCase__  :Dict             =							{}

		for w in ["word_embeddings", "position_embeddings"]:
				lowerCAmelCase__  :List[Any]             =							state_dict[f'''{prefix}.embeddings.{w}.weight''']
		for w in ["weight", "bias"]:
				lowerCAmelCase__  :Union[str, Any]             =							state_dict[f'''{prefix}.embeddings.LayerNorm.{w}''']

		lowerCAmelCase__  :str             =							0
		for teacher_idx in [0, 2, 4, 7, 9, 1_1]:
				for w in ["weight", "bias"]:
						lowerCAmelCase__  :Any             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}'''
						]
						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}'''
						]
						lowerCAmelCase__  :List[str]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}'''
						]

						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}'''
						]
						lowerCAmelCase__  :int             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}'''
						]

						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}'''
						]
						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}'''
						]
						lowerCAmelCase__  :List[Any]             =							state_dict[
						    f'''{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}'''
						]
				std_idx += 1

		lowerCAmelCase__  :Optional[int]             =							state_dict['''cls.predictions.decoder.weight''']
		lowerCAmelCase__  :List[str]             =							state_dict['''cls.predictions.bias''']
		if args.vocab_transform:
				for w in ["weight", "bias"]:
						lowerCAmelCase__  :Any             =							state_dict[f'''cls.predictions.transform.dense.{w}''']
						lowerCAmelCase__  :List[str]             =							state_dict[f'''cls.predictions.transform.LayerNorm.{w}''']

		print(f'''N layers selected for distillation: {std_idx}''')
		print(f'''Number of params transferred for distillation: {len(compressed_sd.keys())}''')

		print(f'''Save transferred checkpoint to {args.dump_checkpoint}.''')
		torch.save(compressed_sd, args.dump_checkpoint)
