import argparse

import torch
from torch import nn

from transformers import SpeechaTextConfig, SpeechaTextForConditionalGeneration





def 		__lowerCamelCase							(UpperCAmelCase__       :							str					):
					SCREAMING_SNAKE_CASE           =    [
					    "encoder.version",
					    "decoder.version",
					    "model.encoder.version",
					    "model.decoder.version",
					    "decoder.output_projection.weight",
					    "_float_tensor",
					    "encoder.embed_positions._float_tensor",
					    "decoder.embed_positions._float_tensor",
					]
					for k in ignore_keys:
										state_dict.pop(UpperCAmelCase__ ,	UpperCAmelCase__					)





def 		__lowerCamelCase							(UpperCAmelCase__       :							Dict					):
					SCREAMING_SNAKE_CASE           =    list(s_dict.keys()					)
					for key in keys:
										if "transformer_layers" in key:
															SCREAMING_SNAKE_CASE           =    s_dict.pop(UpperCAmelCase__					)
										elif "subsample" in key:
															SCREAMING_SNAKE_CASE           =    s_dict.pop(UpperCAmelCase__					)





def 		__lowerCamelCase							(UpperCAmelCase__       :							str					):
					SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    emb.weight.shape
					SCREAMING_SNAKE_CASE           =    nn.Linear(UpperCAmelCase__ ,	UpperCAmelCase__ ,	bias=UpperCAmelCase__					)
					SCREAMING_SNAKE_CASE           =    emb.weight.data
					return lin_layer





def 		__lowerCamelCase							(UpperCAmelCase__       :							str ,	UpperCAmelCase__       :							Optional[Any]					):
					SCREAMING_SNAKE_CASE           =    torch.load(UpperCAmelCase__ ,	map_location="cpu"					)
					SCREAMING_SNAKE_CASE           =    mam_aaa["args"]
					SCREAMING_SNAKE_CASE           =    mam_aaa["model"]
					SCREAMING_SNAKE_CASE           =    state_dict["decoder.output_projection.weight"]

					remove_ignore_keys_(UpperCAmelCase__					)
					rename_keys(UpperCAmelCase__					)

					SCREAMING_SNAKE_CASE           =    state_dict["decoder.embed_tokens.weight"].shape[0]

					SCREAMING_SNAKE_CASE           =    args.share_decoder_input_output_embed

					SCREAMING_SNAKE_CASE           =    [int(UpperCAmelCase__					) for i in args.conv_kernel_sizes.split(","					)]
					SCREAMING_SNAKE_CASE           =    SpeechaTextConfig(
					    vocab_size=UpperCAmelCase__ ,	max_source_positions=args.max_source_positions ,	max_target_positions=args.max_target_positions ,	encoder_layers=args.encoder_layers ,	decoder_layers=args.decoder_layers ,	encoder_attention_heads=args.encoder_attention_heads ,	decoder_attention_heads=args.decoder_attention_heads ,	encoder_ffn_dim=args.encoder_ffn_embed_dim ,	decoder_ffn_dim=args.decoder_ffn_embed_dim ,	d_model=args.encoder_embed_dim ,	dropout=args.dropout ,	attention_dropout=args.attention_dropout ,	activation_dropout=args.activation_dropout ,	activation_function="relu" ,	num_conv_layers=len(UpperCAmelCase__					) ,	conv_channels=args.conv_channels ,	conv_kernel_sizes=UpperCAmelCase__ ,	input_feat_per_channel=args.input_feat_per_channel ,	input_channels=args.input_channels ,	tie_word_embeddings=UpperCAmelCase__ ,	num_beams=5 ,	max_length=2_0_0 ,	use_cache=UpperCAmelCase__ ,	decoder_start_token_id=2 ,	early_stopping=UpperCAmelCase__ ,	)

					SCREAMING_SNAKE_CASE           =    SpeechaTextForConditionalGeneration(UpperCAmelCase__					)
					SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    model.model.load_state_dict(UpperCAmelCase__ ,	strict=UpperCAmelCase__					)
					if len(UpperCAmelCase__					) > 0 and not set(UpperCAmelCase__					) <= {
					    "encoder.embed_positions.weights",
					    "decoder.embed_positions.weights",
					}:
										raise ValueError(
										    "Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights`  are allowed to be missing,"
										    F" but all the following weights are missing {missing}"					)

					if tie_embeds:
										SCREAMING_SNAKE_CASE           =    make_linear_from_emb(model.model.decoder.embed_tokens					)
					else:
										SCREAMING_SNAKE_CASE           =    lm_head_weights

					model.save_pretrained(UpperCAmelCase__					)


if __name__ == "__main__":
							_lowerCamelCase   :				List[str]     			=						argparse.ArgumentParser()
							# Required parameters
							parser.add_argument('''--fairseq_path''', type=str, help='''Path to the fairseq model (.pt) file.''')
							parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
							_lowerCamelCase   :				Any     			=						parser.parse_args()
							convert_fairseq_sat_checkpoint_to_tfms(args.fairseq_path, args.pytorch_dump_folder_path)





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


_lowerCamelCase   :				Union[str, Any]     			=						logging.get_logger(__name__)

_lowerCamelCase   :				Dict     			=						{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

_lowerCamelCase   :				List[Any]     			=						[
    '''small''',
    '''small-base''',
    '''medium''',
    '''medium-base''',
    '''intermediate''',
    '''intermediate-base''',
    '''large''',
    '''large-base''',
    '''xlarge''',
    '''xlarge-base''',
]

_lowerCamelCase   :				Optional[Any]     			=						{
    '''vocab_file''': {
        '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt''',
        '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt''',
        '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt''',
        '''funnel-transformer/medium-base''': (
            '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt'''
        ),
        '''funnel-transformer/intermediate''': (
            '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt'''
        ),
        '''funnel-transformer/intermediate-base''': (
            '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt'''
        ),
        '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt''',
        '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt''',
        '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt''',
        '''funnel-transformer/xlarge-base''': (
            '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json''',
        '''funnel-transformer/small-base''': (
            '''https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json''',
        '''funnel-transformer/medium-base''': (
            '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/intermediate''': (
            '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/intermediate-base''': (
            '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json''',
        '''funnel-transformer/large-base''': (
            '''https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json''',
        '''funnel-transformer/xlarge-base''': (
            '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json'''
        ),
    },
}
_lowerCamelCase   :				Any     			=						{f"""funnel-transformer/{name}""": 5_12 for name in _model_names}
_lowerCamelCase   :				Optional[Any]     			=						{f"""funnel-transformer/{name}""": {'''do_lower_case''': True} for name in _model_names}


class   lowercase      (       a       ):
							lowercase__  :    Optional[int]									=							VOCAB_FILES_NAMES
							lowercase__  :    Optional[Any]									=							PRETRAINED_VOCAB_FILES_MAP
							lowercase__  :    Optional[Any]									=							PRETRAINED_INIT_CONFIGURATION
							lowercase__  :    Union[str, Any]									=							FunnelTokenizer
							lowercase__  :    List[Any]									=							PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							lowercase__  :    int									=							2




							def __init__(  self				:   str		,							_UpperCamelCase				:   str=None		,							_UpperCamelCase				:   str=None		,							_UpperCamelCase				:   Union[str, Any]=True		,							_UpperCamelCase				:   str="<unk>"		,							_UpperCamelCase				:   Optional[Any]="<sep>"		,							_UpperCamelCase				:   Optional[int]="<pad>"		,							_UpperCamelCase				:   int="<cls>"		,							_UpperCamelCase				:   Dict="<mask>"		,							_UpperCamelCase				:   Union[str, Any]="<s>"		,							_UpperCamelCase				:   Optional[int]="</s>"		,							_UpperCamelCase				:   Dict=True		,							_UpperCamelCase				:   List[Any]=True		,							_UpperCamelCase				:   Any=None		,							_UpperCamelCase				:   Dict="##"		,							**_UpperCamelCase				:   Dict		,							)   ->			Optional[int]:



												'''simple docstring'''

												super().__init__(
												    _UpperCamelCase		,							tokenizer_file=_UpperCamelCase		,							do_lower_case=_UpperCamelCase		,							unk_token=_UpperCamelCase		,							sep_token=_UpperCamelCase		,							pad_token=_UpperCamelCase		,							cls_token=_UpperCamelCase		,							mask_token=_UpperCamelCase		,							bos_token=_UpperCamelCase		,							eos_token=_UpperCamelCase		,							clean_text=_UpperCamelCase		,							tokenize_chinese_chars=_UpperCamelCase		,							strip_accents=_UpperCamelCase		,							wordpieces_prefix=_UpperCamelCase		,							**_UpperCamelCase		,							)

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

												SCREAMING_SNAKE_CASE           =    do_lower_case




							def 		__snake_case(  self				:   Union[str, Any]		,							_UpperCamelCase				:   Union[str, Any]		,							_UpperCamelCase				:   Dict=None )   ->			List[Any]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    [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				:   int		,							_UpperCamelCase				:   List[int]		,							_UpperCamelCase				:   Optional[List[int]] = None )   ->			List[int]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    [self.sep_token_id]
												SCREAMING_SNAKE_CASE           =    [self.cls_token_id]
												if token_ids_a is None:
																	return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0]
												return len(cls ) * [self.cls_token_type_id] + len(token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]





							def 		__snake_case(  self				:   Optional[Any]		,							_UpperCamelCase				:   str		,							_UpperCamelCase				:   Optional[str] = None )   ->			Tuple[str]:



												'''simple docstring'''

												SCREAMING_SNAKE_CASE           =    self._tokenizer.model.save(_UpperCamelCase		,							name=_UpperCamelCase )
												return tuple(_UpperCamelCase )




