

import os
import re
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase       :				List[str]         =					logging.get_logger(__name__)
lowerCAmelCase       :				List[Any]         =					{'vocab_file': 'spiece.model'}

lowerCAmelCase       :				Optional[Any]         =					{
    'vocab_file': {
        'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/spiece.model',
        'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/spiece.model',
    },
}

lowerCAmelCase       :				Optional[Any]         =					{
    'google/fnet-base': 5_12,
    'google/fnet-large': 5_12,
}

lowerCAmelCase       :				str         =					'▁'
class 	_A						(					lowerCAmelCase__):
		SCREAMING_SNAKE_CASE      : List[str]    =					VOCAB_FILES_NAMES
		SCREAMING_SNAKE_CASE      : int    =					PRETRAINED_VOCAB_FILES_MAP
		SCREAMING_SNAKE_CASE      : Optional[int]    =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		SCREAMING_SNAKE_CASE      : Dict    =					['''input_ids''', '''token_type_ids''']

		def __init__(			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE=False	,   _SCREAMING_SNAKE_CASE=True	,   _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 = None	,   **_SCREAMING_SNAKE_CASE	,   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Optional[int]          =       (
								    AddedToken(__lowerCAmelCase	,   lstrip=__lowerCAmelCase	,   rstrip=__lowerCAmelCase	,   normalized=__lowerCAmelCase   )
								    if isinstance(__lowerCAmelCase	,   __lowerCAmelCase   )
								    else mask_token
								)

								SCREAMING_SNAKE_CASE_			:					List[Any]          =       {} if sp_model_kwargs is None else sp_model_kwargs

								super().__init__(
								    do_lower_case=__lowerCAmelCase	,   remove_space=__lowerCAmelCase	,   keep_accents=__lowerCAmelCase	,   unk_token=__lowerCAmelCase	,   sep_token=__lowerCAmelCase	,   pad_token=__lowerCAmelCase	,   cls_token=__lowerCAmelCase	,   mask_token=__lowerCAmelCase	,   sp_model_kwargs=self.sp_model_kwargs	,   **__lowerCAmelCase	,   )

								SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       do_lower_case
								SCREAMING_SNAKE_CASE_			:					Any          =       remove_space
								SCREAMING_SNAKE_CASE_			:					str          =       keep_accents
								SCREAMING_SNAKE_CASE_			:					str          =       vocab_file

								SCREAMING_SNAKE_CASE_			:					Tuple          =       spm.SentencePieceProcessor(**self.sp_model_kwargs   )
								self.sp_model.Load(__lowerCAmelCase   )

		@property
		def 	UpperCAmelCase      (			self   ):





								"""simple docstring"""







								return len(self.sp_model   )

		def 	UpperCAmelCase      (			self   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       {self.convert_ids_to_tokens(__lowerCAmelCase   ): i for i in range(self.vocab_size   )}
								vocab.update(self.added_tokens_encoder   )
								return vocab

		def __getstate__(			self   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       self.__dict__.copy()
								SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       None
								return state

		def __setstate__(			self	,   _SCREAMING_SNAKE_CASE   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Any          =       d

								# for backward compatibility
								if not hasattr(self	,   'sp_model_kwargs'   ):
														SCREAMING_SNAKE_CASE_			:					Dict          =       {}

								SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       spm.SentencePieceProcessor(**self.sp_model_kwargs   )
								self.sp_model.Load(self.vocab_file   )

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE   ):





								"""simple docstring"""







								if self.remove_space:
														SCREAMING_SNAKE_CASE_			:					str          =       ' '.join(inputs.strip().split()   )
								else:
														SCREAMING_SNAKE_CASE_			:					List[Any]          =       inputs
								SCREAMING_SNAKE_CASE_			:					Tuple          =       outputs.replace('``'	,   '\"'   ).replace('\'\''	,   '\"'   )

								if not self.keep_accents:
														SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       unicodedata.normalize('NFKD'	,   __lowerCAmelCase   )
														SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       ''.join([c for c in outputs if not unicodedata.combining(__lowerCAmelCase   )]   )
								if self.do_lower_case:
														SCREAMING_SNAKE_CASE_			:					Optional[int]          =       outputs.lower()

								return outputs

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Optional[int]          =       self.preprocess_text(__lowerCAmelCase   )
								SCREAMING_SNAKE_CASE_			:					str          =       self.sp_model.encode(__lowerCAmelCase	,   out_type=__lowerCAmelCase   )
								SCREAMING_SNAKE_CASE_			:					Any          =       []
								for piece in pieces:
														if len(__lowerCAmelCase   ) > 1 and piece[-1] == str(','   ) and piece[-2].isdigit():
																				SCREAMING_SNAKE_CASE_			:					str          =       self.sp_model.EncodeAsPieces(piece[:-1].replace(__lowerCAmelCase	,   ''   )   )
																				if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																										if len(cur_pieces[0]   ) == 1:
																																SCREAMING_SNAKE_CASE_			:					List[Any]          =       cur_pieces[1:]
																										else:
																																SCREAMING_SNAKE_CASE_			:					Dict          =       cur_pieces[0][1:]
																				cur_pieces.append(piece[-1]   )
																				new_pieces.extend(__lowerCAmelCase   )
														else:
																				new_pieces.append(__lowerCAmelCase   )

								return new_pieces

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE   ):





								"""simple docstring"""







								return self.sp_model.PieceToId(__lowerCAmelCase   )

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE   ):





								"""simple docstring"""







								return self.sp_model.IdToPiece(__lowerCAmelCase   )

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       []
								SCREAMING_SNAKE_CASE_			:					str          =       ''
								SCREAMING_SNAKE_CASE_			:					int          =       False
								for token in tokens:
														# make sure that special tokens are not decoded using sentencepiece model
														if token in self.all_special_tokens:
																				if not prev_is_special:
																										out_string += " "
																				out_string += self.sp_model.decode(__lowerCAmelCase   ) + token
																				SCREAMING_SNAKE_CASE_			:					Any          =       True
																				SCREAMING_SNAKE_CASE_			:					Dict          =       []
														else:
																				current_sub_tokens.append(__lowerCAmelCase   )
																				SCREAMING_SNAKE_CASE_			:					List[Any]          =       False
								out_string += self.sp_model.decode(__lowerCAmelCase   )
								return out_string.strip()

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE = False	,   _SCREAMING_SNAKE_CASE = None	,   _SCREAMING_SNAKE_CASE = True	,   **_SCREAMING_SNAKE_CASE	,   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					str          =       kwargs.pop('use_source_tokenizer'	,   __lowerCAmelCase   )

								SCREAMING_SNAKE_CASE_			:					Dict          =       self.convert_ids_to_tokens(__lowerCAmelCase	,   skip_special_tokens=__lowerCAmelCase   )

								# To avoid mixing byte-level and unicode for byte-level BPT
								# we need to build string separately for added tokens and byte-level tokens
								# cf. https://github.com/huggingface/transformers/issues/1133
								SCREAMING_SNAKE_CASE_			:					Dict          =       []
								SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       []
								for token in filtered_tokens:
														if skip_special_tokens and token in self.all_special_ids:
																				continue
														if token in self.added_tokens_encoder:
																				if current_sub_text:
																										sub_texts.append(self.convert_tokens_to_string(__lowerCAmelCase   )   )
																										SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       []
																				sub_texts.append(__lowerCAmelCase   )
														else:
																				current_sub_text.append(__lowerCAmelCase   )
								if current_sub_text:
														sub_texts.append(self.convert_tokens_to_string(__lowerCAmelCase   )   )

								# Mimic the behavior of the Rust tokenizer:
								# No space after <unk>
								if spaces_between_special_tokens:
														SCREAMING_SNAKE_CASE_			:					str          =       re.sub(r'(<unk>) '	,   r'\1'	,   ' '.join(__lowerCAmelCase   )   )
								else:
														SCREAMING_SNAKE_CASE_			:					List[Any]          =       ''.join(__lowerCAmelCase   )

								SCREAMING_SNAKE_CASE_			:					int          =       (
								    clean_up_tokenization_spaces
								    if clean_up_tokenization_spaces is not None
								    else self.clean_up_tokenization_spaces
								)
								if clean_up_tokenization_spaces:
														SCREAMING_SNAKE_CASE_			:					List[Any]          =       self.clean_up_tokenization(__lowerCAmelCase   )
														return clean_text
								else:
														return text

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE = None   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Any          =       [self.sep_token_id]
								SCREAMING_SNAKE_CASE_			:					List[str]          =       [self.cls_token_id]
								if token_ids_a is None:
														return cls + token_ids_a + sep
								return cls + token_ids_a + sep + token_ids_a + sep

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE = None	,   _SCREAMING_SNAKE_CASE = False   ):





								"""simple docstring"""







								if already_has_special_tokens:
														return super().get_special_tokens_mask(
														    token_ids_a=__lowerCAmelCase	,   token_ids_a=__lowerCAmelCase	,   already_has_special_tokens=__lowerCAmelCase   )

								if token_ids_a is not None:
														return [1] + ([0] * len(__lowerCAmelCase   )) + [1] + ([0] * len(__lowerCAmelCase   )) + [1]
								return [1] + ([0] * len(__lowerCAmelCase   )) + [1]

		def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE = None   ):





								"""simple docstring"""







								SCREAMING_SNAKE_CASE_			:					Dict          =       [self.sep_token_id]
								SCREAMING_SNAKE_CASE_			:					Tuple          =       [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   ):





								"""simple docstring"""







								if not os.path.isdir(__lowerCAmelCase   ):
														logger.error(f"Vocabulary path ({save_directory}) should be a directory"   )
														return
								SCREAMING_SNAKE_CASE_			:					List[str]          =       os.path.join(
								    __lowerCAmelCase	,   (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']   )

								if os.path.abspath(self.vocab_file   ) != os.path.abspath(__lowerCAmelCase   ) and os.path.isfile(self.vocab_file   ):
														copyfile(self.vocab_file	,   __lowerCAmelCase   )
								elif not os.path.isfile(self.vocab_file   ):
														with open(__lowerCAmelCase	,   'wb'   ) as fi:
																				SCREAMING_SNAKE_CASE_			:					List[Any]          =       self.sp_model.serialized_model_proto()
																				fi.write(__lowerCAmelCase   )

								return (out_vocab_file,)









import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        ErnieMForInformationExtraction,
        ErnieMForMultipleChoice,
        ErnieMForQuestionAnswering,
        ErnieMForSequenceClassification,
        ErnieMForTokenClassification,
        ErnieMModel,
    )
    from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST
class 	_A						:

  def __init__(			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE=13	,   _SCREAMING_SNAKE_CASE=7	,   _SCREAMING_SNAKE_CASE=True	,   _SCREAMING_SNAKE_CASE=True	,   _SCREAMING_SNAKE_CASE=True	,   _SCREAMING_SNAKE_CASE=99	,   _SCREAMING_SNAKE_CASE=32	,   _SCREAMING_SNAKE_CASE=5	,   _SCREAMING_SNAKE_CASE=4	,   _SCREAMING_SNAKE_CASE=37	,   _SCREAMING_SNAKE_CASE="gelu"	,   _SCREAMING_SNAKE_CASE=0.1	,   _SCREAMING_SNAKE_CASE=0.1	,   _SCREAMING_SNAKE_CASE=512	,   _SCREAMING_SNAKE_CASE=16	,   _SCREAMING_SNAKE_CASE=2	,   _SCREAMING_SNAKE_CASE=0.02	,   _SCREAMING_SNAKE_CASE=3	,   _SCREAMING_SNAKE_CASE=4	,   _SCREAMING_SNAKE_CASE=None	,   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Tuple          =       parent
        SCREAMING_SNAKE_CASE_			:					Tuple          =       batch_size
        SCREAMING_SNAKE_CASE_			:					Dict          =       seq_length
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       is_training
        SCREAMING_SNAKE_CASE_			:					str          =       use_input_mask
        SCREAMING_SNAKE_CASE_			:					str          =       use_labels
        SCREAMING_SNAKE_CASE_			:					int          =       vocab_size
        SCREAMING_SNAKE_CASE_			:					Any          =       hidden_size
        SCREAMING_SNAKE_CASE_			:					int          =       num_hidden_layers
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       num_attention_heads
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       intermediate_size
        SCREAMING_SNAKE_CASE_			:					Dict          =       hidden_act
        SCREAMING_SNAKE_CASE_			:					Tuple          =       hidden_dropout_prob
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       max_position_embeddings
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       type_vocab_size
        SCREAMING_SNAKE_CASE_			:					str          =       type_sequence_label_size
        SCREAMING_SNAKE_CASE_			:					Dict          =       initializer_range
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       num_labels
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       num_choices
        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       scope

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					List[Any]          =       ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size   )

        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       None
        if self.use_input_mask:
              SCREAMING_SNAKE_CASE_			:					Tuple          =       random_attention_mask([self.batch_size, self.seq_length]   )

        SCREAMING_SNAKE_CASE_			:					int          =       None
        SCREAMING_SNAKE_CASE_			:					int          =       None
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       None
        if self.use_labels:
              SCREAMING_SNAKE_CASE_			:					List[str]          =       ids_tensor([self.batch_size]	,   self.type_sequence_label_size   )
              SCREAMING_SNAKE_CASE_			:					str          =       ids_tensor([self.batch_size, self.seq_length]	,   self.num_labels   )
              SCREAMING_SNAKE_CASE_			:					List[str]          =       ids_tensor([self.batch_size]	,   self.num_choices   )

        SCREAMING_SNAKE_CASE_			:					int          =       self.get_config()

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					int          =       ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size   )

        SCREAMING_SNAKE_CASE_			:					int          =       None
        if self.use_input_mask:
              SCREAMING_SNAKE_CASE_			:					str          =       random_attention_mask([self.batch_size, self.seq_length]   )
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       self.get_config()

        return config, input_ids, input_mask

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        return ErnieMConfig(
            vocab_size=self.vocab_size	,   hidden_size=self.hidden_size	,   num_hidden_layers=self.num_hidden_layers	,   num_attention_heads=self.num_attention_heads	,   intermediate_size=self.intermediate_size	,   hidden_act=self.hidden_act	,   hidden_dropout_prob=self.hidden_dropout_prob	,   attention_probs_dropout_prob=self.attention_probs_dropout_prob	,   max_position_embeddings=self.max_position_embeddings	,   type_vocab_size=self.type_vocab_size	,   initializer_range=self.initializer_range	,   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       ErnieMModel(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					Tuple          =       model(_SCREAMING_SNAKE_CASE	,   return_dict=_SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.last_hidden_state.shape	,   (self.batch_size, self.seq_length, self.hidden_size)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       ErnieMForQuestionAnswering(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					Dict          =       model(
            _SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   start_positions=_SCREAMING_SNAKE_CASE	,   end_positions=_SCREAMING_SNAKE_CASE	,   )
        self.parent.assertEqual(result.start_logits.shape	,   (self.batch_size, self.seq_length)   )
        self.parent.assertEqual(result.end_logits.shape	,   (self.batch_size, self.seq_length)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					List[Any]          =       ErnieMForInformationExtraction(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[str]          =       torch.ones_like(_SCREAMING_SNAKE_CASE	,   dtype=torch.floataa   )
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       model(
            _SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   start_positions=_SCREAMING_SNAKE_CASE	,   end_positions=_SCREAMING_SNAKE_CASE	,   )
        self.parent.assertEqual(result.start_logits.shape	,   (self.batch_size, self.seq_length)   )
        self.parent.assertEqual(result.end_logits.shape	,   (self.batch_size, self.seq_length)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					str          =       self.num_labels
        SCREAMING_SNAKE_CASE_			:					Tuple          =       ErnieMForSequenceClassification(_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       model(_SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   labels=_SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_labels)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       self.num_labels
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       ErnieMForTokenClassification(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        input_ids.to(_SCREAMING_SNAKE_CASE   )
        input_mask.to(_SCREAMING_SNAKE_CASE   )
        token_labels.to(_SCREAMING_SNAKE_CASE   )

        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       model(_SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   labels=_SCREAMING_SNAKE_CASE   )

        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.seq_length, self.num_labels)   )

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					str          =       self.num_choices
        SCREAMING_SNAKE_CASE_			:					List[str]          =       ErnieMForMultipleChoice(config=_SCREAMING_SNAKE_CASE   )
        model.to(_SCREAMING_SNAKE_CASE   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       input_ids.unsqueeze(1   ).expand(-1	,   self.num_choices	,   -1   ).contiguous()
        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       input_mask.unsqueeze(1   ).expand(-1	,   self.num_choices	,   -1   ).contiguous()
        SCREAMING_SNAKE_CASE_			:					List[str]          =       model(
            _SCREAMING_SNAKE_CASE	,   attention_mask=_SCREAMING_SNAKE_CASE	,   labels=_SCREAMING_SNAKE_CASE	,   )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_choices)   )



  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Any          =       self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  (
            SCREAMING_SNAKE_CASE_
        )    ,  
        )			:					str          =       config_and_inputs
        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       {'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict

@require_torch
class 	_A						(					__magic_name__				,       __magic_name__				,       unittest.TestCase):
  SCREAMING_SNAKE_CASE      : str    =					(
      (
          ErnieMModel,
          ErnieMForMultipleChoice,
          ErnieMForQuestionAnswering,
          ErnieMForSequenceClassification,
          ErnieMForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE      : Optional[int]    =					()
  SCREAMING_SNAKE_CASE      : Union[str, Any]    =					(
      {
          '''feature-extraction''': ErnieMModel,
          '''question-answering''': ErnieMForQuestionAnswering,
          '''text-classification''': ErnieMForSequenceClassification,
          '''token-classification''': ErnieMForTokenClassification,
          '''zero-shot''': ErnieMForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE      : Union[str, Any]    =					False

  def 	UpperCAmelCase      (			self	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE   ):





        """simple docstring"""







        if pipeline_test_casse_name == "QAPipelineTests":
              return True

        return False

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       ErnieMModelTester(self   )
        SCREAMING_SNAKE_CASE_			:					Dict          =       ConfigTester(self	,   config_class=_SCREAMING_SNAKE_CASE	,   hidden_size=37   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        self.config_tester.run_common_tests()

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Dict          =       self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
              SCREAMING_SNAKE_CASE_			:					Optional[int]          =       type
              self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					List[Any]          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					int          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Union[str, Any]          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Tuple          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_information_extraction(*_SCREAMING_SNAKE_CASE   )

  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					int          =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE   )



  @slow
  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_			:					int          =       ErnieMModel.from_pretrained(_SCREAMING_SNAKE_CASE   )
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )

@require_torch
class 	_A						(					unittest.TestCase):

  @slow
  def 	UpperCAmelCase      (			self   ):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_			:					Tuple          =       ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'   )
        model.eval()
        SCREAMING_SNAKE_CASE_			:					List[str]          =       torch.tensor([[0, 1, 2, 3, 4, 5]]   )
        SCREAMING_SNAKE_CASE_			:					Tuple          =       model(_SCREAMING_SNAKE_CASE   )[0]

        # TODO Replace vocab size
        SCREAMING_SNAKE_CASE_			:					List[Any]          =       768

        SCREAMING_SNAKE_CASE_			:					Optional[int]          =       torch.Size((1, 6, hidden_size)   )
        self.assertEqual(output.shape	,   _SCREAMING_SNAKE_CASE   )

        SCREAMING_SNAKE_CASE_			:					Optional[Any]          =       torch.tensor(
            [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]   )

        self.assertTrue(torch.allclose(output[:, :3, :3]	,   _SCREAMING_SNAKE_CASE	,   atol=1e-3   )   )






