





"""simple docstring"""




import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te





def 			a_							(				_lowercase			,		_lowercase=True			,		_lowercase=True			,		_lowercase=True  ):
						if not is_fpa_available():
												raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''  )
						for name, module in model.named_children():
												if isinstance(_lowercase			,		nn.Linear  ) and to_transformer_engine and _convert_linear:
																		# Return early if the linear layer weights are not multiples of 16
																		if any(p % 16 != 0 for p in module.weight.shape  ):
																								return
																		_UpperCamelCase     :   Optional[int]      =       module.bias is not None
																		_UpperCamelCase     :   Optional[Any]      =       te.Linear(
																		    module.in_features			,		module.out_features			,		bias=_lowercase			,		params_dtype=module.weight.dtype  )
																		_UpperCamelCase     :   Optional[int]      =       module.weight.data.clone()
																		if has_bias:
																								_UpperCamelCase     :   Optional[int]      =       module.bias.data.clone()

																		setattr(_lowercase			,		_lowercase			,		_lowercase  )
												elif isinstance(_lowercase			,		nn.LayerNorm  ) and to_transformer_engine and _convert_ln:
																		_UpperCamelCase     :   List[str]      =       te.LayerNorm(module.normalized_shape[0]			,		eps=module.eps			,		params_dtype=module.weight.dtype  )
																		_UpperCamelCase     :   Any      =       module.weight.data.clone()
																		_UpperCamelCase     :   Dict      =       module.bias.data.clone()

																		setattr(_lowercase			,		_lowercase			,		_lowercase  )
												elif isinstance(_lowercase			,		te.Linear  ) and not to_transformer_engine and _convert_linear:
																		_UpperCamelCase     :   List[Any]      =       module.bias is not None
																		_UpperCamelCase     :   Optional[int]      =       nn.Linear(
																		    module.in_features			,		module.out_features			,		bias=_lowercase			,		params_dtype=module.weight.dtype  )
																		_UpperCamelCase     :   int      =       module.weight.data.clone()
																		if has_bias:
																								_UpperCamelCase     :   Any      =       module.bias.data.clone()

																		setattr(_lowercase			,		_lowercase			,		_lowercase  )
												elif isinstance(_lowercase			,		te.LayerNorm  ) and not to_transformer_engine and _convert_ln:
																		_UpperCamelCase     :   List[str]      =       nn.LayerNorm(module.normalized_shape[0]			,		eps=module.eps			,		params_dtype=module.weight.dtype  )
																		_UpperCamelCase     :   Optional[int]      =       module.weight.data.clone()
																		_UpperCamelCase     :   Optional[int]      =       module.bias.data.clone()

																		setattr(_lowercase			,		_lowercase			,		_lowercase  )
												else:
																		convert_model(
																		    _lowercase			,		to_transformer_engine=_lowercase			,		_convert_linear=_convert_linear			,		_convert_ln=_convert_ln			,		)






def 			a_							(				_lowercase  ):
						if not is_fpa_available():
												raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''  )
						for m in model.modules():
												if isinstance(_lowercase			,		(te.LayerNorm, te.Linear)  ):
																		return True
						return False











"""simple docstring"""



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mpnet import MPNetTokenizer


a  :						List[Any]					       =							logging.get_logger(__name__)

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

a  :						Any					       =							{
    '''vocab_file''': {
        '''microsoft/mpnet-base''': '''https://huggingface.co/microsoft/mpnet-base/resolve/main/vocab.txt''',
    },
    '''tokenizer_file''': {
        '''microsoft/mpnet-base''': '''https://huggingface.co/microsoft/mpnet-base/resolve/main/tokenizer.json''',
    },
}

a  :						str					       =							{
    '''microsoft/mpnet-base''': 512,
}

a  :						Union[str, Any]					       =							{
    '''microsoft/mpnet-base''': {'''do_lower_case''': True},
}


class 	_UpperCamelCase ( __UpperCamelCase							):



    '''simple docstring'''


    __lowercase    :   Optional[Any]											=						VOCAB_FILES_NAMES
    __lowercase    :   Optional[int]											=						PRETRAINED_VOCAB_FILES_MAP
    __lowercase    :   Dict											=						PRETRAINED_INIT_CONFIGURATION
    __lowercase    :   Optional[Any]											=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __lowercase    :   Dict											=						MPNetTokenizer
    __lowercase    :   Dict											=						['input_ids', 'attention_mask']


    def __init__(      self   ,			__lowercase=None   ,			__lowercase=None   ,			__lowercase=True   ,			__lowercase="<s>"   ,			__lowercase="</s>"   ,			__lowercase="</s>"   ,			__lowercase="<s>"   ,			__lowercase="[UNK]"   ,			__lowercase="<pad>"   ,			__lowercase="<mask>"   ,			__lowercase=True   ,			__lowercase=None   ,			**__lowercase   ,			):
     super().__init__(
         __lowercase   ,			tokenizer_file=__lowercase   ,			do_lower_case=__lowercase   ,			bos_token=__lowercase   ,			eos_token=__lowercase   ,			sep_token=__lowercase   ,			cls_token=__lowercase   ,			unk_token=__lowercase   ,			pad_token=__lowercase   ,			mask_token=__lowercase   ,			tokenize_chinese_chars=__lowercase   ,			strip_accents=__lowercase   ,			**__lowercase   ,			)

     UpperCAmelCase__ 						=				json.loads(self.backend_tokenizer.normalizer.__getstate__()			)
     if (
         pre_tok_state.get("""lowercase"""   ,			__lowercase			) != do_lower_case
         or pre_tok_state.get("""strip_accents"""   ,			__lowercase			) != strip_accents
     ):
      UpperCAmelCase__ 						=				getattr(__lowercase   ,			pre_tok_state.pop("""type"""			)			)
      UpperCAmelCase__ 						=				do_lower_case
      UpperCAmelCase__ 						=				strip_accents
      UpperCAmelCase__ 						=				pre_tok_class(**__lowercase			)

     UpperCAmelCase__ 						=				do_lower_case


    @property
    def 		A__      (      self			):
     if self._mask_token is None:
      if self.verbose:
       logger.error("""Using mask_token, but it is not set yet."""			)
      return None
     return str(self._mask_token			)


    @mask_token.setter
    def 		A__      (      self   ,			__lowercase			):
     UpperCAmelCase__ 						=				AddedToken(__lowercase   ,			lstrip=__lowercase   ,			rstrip=__lowercase			) if isinstance(__lowercase   ,			__lowercase			) else value
     UpperCAmelCase__ 						=				value


    def 		A__      (      self   ,			__lowercase   ,			__lowercase=None			):
     UpperCAmelCase__ 						=				[self.bos_token_id] + token_ids_a + [self.eos_token_id]
     if token_ids_a is None:
      return output

     return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]


    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None			):
     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 + sep + token_ids_a + sep			) * [0]






    def 		A__      (      self   ,			__lowercase   ,			__lowercase = None			):
     UpperCAmelCase__ 						=				self._tokenizer.model.save(__lowercase   ,			name=__lowercase			)
     return tuple(__lowercase			)



