





'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import RoFormerConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    TFRoFormerForCausalLM,
					    TFRoFormerForMaskedLM,
					    TFRoFormerForMultipleChoice,
					    TFRoFormerForQuestionAnswering,
					    TFRoFormerForSequenceClassification,
					    TFRoFormerForTokenClassification,
					    TFRoFormerModel,
					)
					from transformers.models.roformer.modeling_tf_roformer import (
					    TFRoFormerSelfAttention,
					    TFRoFormerSinusoidalPositionalEmbedding,
					)


class 					__a      :

							def __init__(							self							:     Optional[Any]       ,     __magic_name__							:     str       ,     __magic_name__							:     Any=13       ,     __magic_name__							:     List[str]=7       ,     __magic_name__							:     Union[str, Any]=True       ,     __magic_name__							:     Tuple=True       ,     __magic_name__							:     Optional[Any]=True       ,     __magic_name__							:     str=True       ,     __magic_name__							:     Dict=99       ,     __magic_name__							:     Any=32       ,     __magic_name__							:     Tuple=2       ,     __magic_name__							:     List[Any]=4       ,     __magic_name__							:     Any=37       ,     __magic_name__							:     List[Any]="gelu"       ,     __magic_name__							:     Any=0.1       ,     __magic_name__							:     List[str]=0.1       ,     __magic_name__							:     List[str]=5_12       ,     __magic_name__							:     Optional[Any]=16       ,     __magic_name__							:     Dict=2       ,     __magic_name__							:     int=0.0_2       ,     __magic_name__							:     List[str]=3       ,     __magic_name__							:     List[Any]=4       ,     __magic_name__							:     str=None       ,     )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  parent
													UpperCAmelCase_			:      Optional[Any]     						=  13
													UpperCAmelCase_			:      List[str]     						=  7
													UpperCAmelCase_			:      List[str]     						=  True
													UpperCAmelCase_			:      Union[str, Any]     						=  True
													UpperCAmelCase_			:      List[str]     						=  True
													UpperCAmelCase_			:      int     						=  True
													UpperCAmelCase_			:      Dict     						=  99
													UpperCAmelCase_			:      Dict     						=  32
													UpperCAmelCase_			:      Dict     						=  2
													UpperCAmelCase_			:      Optional[Any]     						=  4
													UpperCAmelCase_			:      int     						=  37
													UpperCAmelCase_			:      Union[str, Any]     						=  '''gelu'''
													UpperCAmelCase_			:      str     						=  0.1
													UpperCAmelCase_			:      Optional[int]     						=  0.1
													UpperCAmelCase_			:      Union[str, Any]     						=  5_12
													UpperCAmelCase_			:      str     						=  16
													UpperCAmelCase_			:      Dict     						=  2
													UpperCAmelCase_			:      List[Any]     						=  0.0_2
													UpperCAmelCase_			:      Optional[Any]     						=  3
													UpperCAmelCase_			:      List[Any]     						=  4
													UpperCAmelCase_			:      Any     						=  None

							def     UpperCAmelCase__	(							self							:     Any   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size   )

													UpperCAmelCase_			:      List[str]     						=  None
													if self.use_input_mask:
																			UpperCAmelCase_			:      Union[str, Any]     						=  random_attention_mask([self.batch_size, self.seq_length]   )

													UpperCAmelCase_			:      str     						=  None
													if self.use_token_type_ids:
																			UpperCAmelCase_			:      Any     						=  ids_tensor([self.batch_size, self.seq_length]       ,     self.type_vocab_size   )

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

													UpperCAmelCase_			:      Dict     						=  RoFormerConfig(
													    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       ,     return_dict=__magic_name__       ,     )

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

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Dict       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     str       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Union[str, Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  TFRoFormerModel(config=__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

													UpperCAmelCase_			:      Optional[Any]     						=  [input_ids, input_mask]
													UpperCAmelCase_			:      Any     						=  model(__magic_name__   )

													UpperCAmelCase_			:      str     						=  model(__magic_name__   )

													self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size)   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     int       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Any       ,     __magic_name__							:     Dict       ,     __magic_name__							:     Dict   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  True
													UpperCAmelCase_			:      Any     						=  TFRoFormerForCausalLM(config=__magic_name__   )
													UpperCAmelCase_			:      Dict     						=  {
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}
													UpperCAmelCase_			:      str     						=  model(__magic_name__   )['''logits''']
													self.parent.assertListEqual(
													    list(prediction_scores.numpy().shape   )       ,     [self.batch_size, self.seq_length, self.vocab_size]   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Optional[int]   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  TFRoFormerForMaskedLM(config=__magic_name__   )
													UpperCAmelCase_			:      Union[str, Any]     						=  {
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}
													UpperCAmelCase_			:      List[str]     						=  model(__magic_name__   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.vocab_size)   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     str   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  self.num_labels
													UpperCAmelCase_			:      Tuple     						=  TFRoFormerForSequenceClassification(config=__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  {
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}

													UpperCAmelCase_			:      Any     						=  model(__magic_name__   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_labels)   )

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     int       ,     __magic_name__							:     Dict       ,     __magic_name__							:     int       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     str   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.num_choices
													UpperCAmelCase_			:      List[Any]     						=  TFRoFormerForMultipleChoice(config=__magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  tf.tile(tf.expand_dims(__magic_name__       ,     1   )       ,     (1, self.num_choices, 1)   )
													UpperCAmelCase_			:      List[str]     						=  tf.tile(tf.expand_dims(__magic_name__       ,     1   )       ,     (1, self.num_choices, 1)   )
													UpperCAmelCase_			:      Optional[int]     						=  tf.tile(tf.expand_dims(__magic_name__       ,     1   )       ,     (1, self.num_choices, 1)   )
													UpperCAmelCase_			:      Optional[Any]     						=  {
													    '''input_ids''': multiple_choice_inputs_ids,
													    '''attention_mask''': multiple_choice_input_mask,
													    '''token_type_ids''': multiple_choice_token_type_ids,
													}
													UpperCAmelCase_			:      int     						=  model(__magic_name__   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_choices)   )

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     int       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Any       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     List[Any]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  self.num_labels
													UpperCAmelCase_			:      Optional[int]     						=  TFRoFormerForTokenClassification(config=__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  {
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}
													UpperCAmelCase_			:      Dict     						=  model(__magic_name__   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.num_labels)   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     str       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Union[str, Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  TFRoFormerForQuestionAnswering(config=__magic_name__   )
													UpperCAmelCase_			:      Union[str, Any]     						=  {
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}

													UpperCAmelCase_			:      Any     						=  model(__magic_name__   )
													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							:     Any   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  self.prepare_config_and_inputs()
													(
													    (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       (
													    UpperCAmelCase_
													)						,       
													)			:      int     						=  config_and_inputs
													UpperCAmelCase_			:      Any     						=  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
													return config, inputs_dict





@require_tf
class 					__a      (lowerCamelCase       ,		lowerCamelCase       ,		unittest.TestCase     ):
							__a					:   Tuple									=       (
							    (
							        TFRoFormerModel,
							        TFRoFormerForCausalLM,
							        TFRoFormerForMaskedLM,
							        TFRoFormerForQuestionAnswering,
							        TFRoFormerForSequenceClassification,
							        TFRoFormerForTokenClassification,
							        TFRoFormerForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							__a					:   str									=       (
							    {
							        "feature-extraction": TFRoFormerModel,
							        "fill-mask": TFRoFormerForMaskedLM,
							        "question-answering": TFRoFormerForQuestionAnswering,
							        "text-classification": TFRoFormerForSequenceClassification,
							        "text-generation": TFRoFormerForCausalLM,
							        "token-classification": TFRoFormerForTokenClassification,
							        "zero-shot": TFRoFormerForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)

							__a					:   List[Any]									=       False
							__a					:   str									=       False

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     int       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     List[str]   )					->       Union[str, Any]:







													"""simple docstring"""
													if pipeline_test_casse_name == "TextGenerationPipelineTests":
																			return True

													return False

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  TFRoFormerModelTester(self   )
													UpperCAmelCase_			:      int     						=  ConfigTester(self       ,     config_class=__magic_name__       ,     hidden_size=37   )

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       str:







													"""simple docstring"""
													self.config_tester.run_common_tests()

							def     UpperCAmelCase__	(							self							:     Any   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Any   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_lm_head(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     int   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Any   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*__magic_name__   )

							def     UpperCAmelCase__	(							self							:     int   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*__magic_name__   )



							@slow
							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  TFRoFormerModel.from_pretrained('''junnyu/roformer_chinese_base'''   )
													self.assertIsNotNone(__magic_name__   )





@require_tf
class 					__a      (unittest.TestCase     ):

							@slow
							def     UpperCAmelCase__	(							self							:     str   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  TFRoFormerForMaskedLM.from_pretrained('''junnyu/roformer_chinese_base'''   )
													UpperCAmelCase_			:      Tuple     						=  tf.constant([[0, 1, 2, 3, 4, 5]]   )
													UpperCAmelCase_			:      int     						=  model(__magic_name__   )[0]

													# TODO Replace vocab size
													UpperCAmelCase_			:      List[str]     						=  5_00_00

													UpperCAmelCase_			:      str     						=  [1, 6, vocab_size]
													self.assertEqual(output.shape       ,     __magic_name__   )

													print(output[:, :3, :3]   )

													# TODO Replace values below with what was printed above.
													UpperCAmelCase_			:      str     						=  tf.constant(
													    [
													        [
													            [-0.1_2_0_5_3_3_4_1, -1.0_2_6_4_9_0_1, 0.2_9_2_2_1_9_4_6],
													            [-1.5_1_3_3_7_8_3, 0.1_9_7_4_3_3, 0.1_5_1_9_0_6_0_7],
													            [-5.0_1_3_5_4_0_3, -3.9_0_0_2_5_6, -0.8_4_0_3_8_7_6_4],
													        ]
													    ]   )
													tf.debugging.assert_near(output[:, :3, :3]       ,     __magic_name__       ,     atol=1E-4   )





@require_tf
class 					__a      (unittest.TestCase     ):
							__a					:   int									=       1e-4

							def     UpperCAmelCase__	(							self							:     str   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  tf.constant([[4, 10]]   )
													UpperCAmelCase_			:      int     						=  TFRoFormerSinusoidalPositionalEmbedding(num_positions=6       ,     embedding_dim=6   )

													UpperCAmelCase_			:      str     						=  emba(input_ids.shape   )
													UpperCAmelCase_			:      str     						=  tf.constant(
													    [[0.0_0_0_0, 0.0_0_0_0, 0.0_0_0_0, 1.0_0_0_0, 1.0_0_0_0, 1.0_0_0_0], [0.8_4_1_5, 0.0_4_6_4, 0.0_0_2_2, 0.5_4_0_3, 0.9_9_8_9, 1.0_0_0_0]]   )

													tf.debugging.assert_near(__magic_name__       ,     __magic_name__       ,     atol=self.tolerance   )



							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  tf.constant(
													    [
													        [0.0_0_0_0, 0.0_0_0_0, 0.0_0_0_0, 0.0_0_0_0, 0.0_0_0_0],
													        [0.8_4_1_5, 0.8_2_1_9, 0.8_0_2_0, 0.7_8_1_9, 0.7_6_1_7],
													        [0.9_0_9_3, 0.9_3_6_4, 0.9_5_8_1, 0.9_7_4_9, 0.9_8_7_0],
													    ]   )
													UpperCAmelCase_			:      Union[str, Any]     						=  TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12       ,     embedding_dim=5_12   )
													emba([2, 16, 5_12]   )
													UpperCAmelCase_			:      Any     						=  emba.weight[:3, :5]

													tf.debugging.assert_near(__magic_name__       ,     __magic_name__       ,     atol=self.tolerance   )





@require_tf
class 					__a      (unittest.TestCase     ):
							__a					:   List[str]									=       1e-4

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Tuple:







													"""simple docstring"""
													# 2,12,16,64
													UpperCAmelCase_			:      str     						=  tf.reshape(tf.range(2 * 12 * 16 * 64       ,     dtype=tf.floataa   )       ,     shape=(2, 12, 16, 64)   ) / 1_00

													UpperCAmelCase_			:      str     						=  -tf.reshape(tf.range(2 * 12 * 16 * 64       ,     dtype=tf.floataa   )       ,     shape=(2, 12, 16, 64)   ) / 1_00

													UpperCAmelCase_			:      int     						=  TFRoFormerSinusoidalPositionalEmbedding(num_positions=32       ,     embedding_dim=64   )
													UpperCAmelCase_			:      Dict     						=  embed_positions([2, 16, 7_68]   )[None, None, :, :]

													UpperCAmelCase_						,       UpperCAmelCase_			:      Optional[int]     						=  TFRoFormerSelfAttention.apply_rotary_position_embeddings(
													    __magic_name__       ,     __magic_name__       ,     __magic_name__   )

													UpperCAmelCase_			:      Union[str, Any]     						=  tf.constant(
													    [
													        [0.0_0_0_0, 0.0_1_0_0, 0.0_2_0_0, 0.0_3_0_0, 0.0_4_0_0, 0.0_5_0_0, 0.0_6_0_0, 0.0_7_0_0],
													        [-0.2_0_1_2, 0.8_8_9_7, 0.0_2_6_3, 0.9_4_0_1, 0.2_0_7_4, 0.9_4_6_3, 0.3_4_8_1, 0.9_3_4_3],
													        [-1.7_0_5_7, 0.6_2_7_1, -1.2_1_4_5, 1.3_8_9_7, -0.6_3_0_3, 1.7_6_4_7, -0.1_1_7_3, 1.8_9_8_5],
													        [-2.1_7_3_1, -1.6_3_9_7, -2.7_3_5_8, 0.2_8_5_4, -2.1_8_4_0, 1.7_1_8_3, -1.3_0_1_8, 2.4_8_7_1],
													        [0.2_7_1_7, -3.6_1_7_3, -2.9_2_0_6, -2.1_9_8_8, -3.6_6_3_8, 0.3_8_5_8, -2.9_1_5_5, 2.2_9_8_0],
													        [3.9_8_5_9, -2.1_5_8_0, -0.7_9_8_4, -4.4_9_0_4, -4.1_1_8_1, -2.0_2_5_2, -4.4_7_8_2, 1.1_2_5_3],
													    ]   )
													UpperCAmelCase_			:      Any     						=  tf.constant(
													    [
													        [0.0_0_0_0, -0.0_1_0_0, -0.0_2_0_0, -0.0_3_0_0, -0.0_4_0_0, -0.0_5_0_0, -0.0_6_0_0, -0.0_7_0_0],
													        [0.2_0_1_2, -0.8_8_9_7, -0.0_2_6_3, -0.9_4_0_1, -0.2_0_7_4, -0.9_4_6_3, -0.3_4_8_1, -0.9_3_4_3],
													        [1.7_0_5_7, -0.6_2_7_1, 1.2_1_4_5, -1.3_8_9_7, 0.6_3_0_3, -1.7_6_4_7, 0.1_1_7_3, -1.8_9_8_5],
													        [2.1_7_3_1, 1.6_3_9_7, 2.7_3_5_8, -0.2_8_5_4, 2.1_8_4_0, -1.7_1_8_3, 1.3_0_1_8, -2.4_8_7_1],
													        [-0.2_7_1_7, 3.6_1_7_3, 2.9_2_0_6, 2.1_9_8_8, 3.6_6_3_8, -0.3_8_5_8, 2.9_1_5_5, -2.2_9_8_0],
													        [-3.9_8_5_9, 2.1_5_8_0, 0.7_9_8_4, 4.4_9_0_4, 4.1_1_8_1, 2.0_2_5_2, 4.4_7_8_2, -1.1_2_5_3],
													    ]   )

													tf.debugging.assert_near(query_layer[0, 0, :6, :8]       ,     __magic_name__       ,     atol=self.tolerance   )
													tf.debugging.assert_near(key_layer[0, 0, :6, :8]       ,     __magic_name__       ,     atol=self.tolerance   )












'''simple docstring'''







import io
import os
import unicodedata
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


snake_case_     :       str						=  logging.get_logger(__name__)

snake_case_     :       int						=  "▁"

snake_case_     :       str						=  {"vocab_file": "vocab.txt", "sentencepiece_model_ckpt": "sentencepiece.bpe.model"}

snake_case_     :       int						=  {
    "sentencepiece_model_file": "sentencepiece.bpe.model",
    "vocab_file": "vocab.txt",
}

snake_case_     :       Optional[Any]						=  {
    "vocab_file": {
        "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt",
        "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt",
    },
    "sentencepiece_model_file": {
        "ernie-m-base": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model",
        "ernie-m-large": "https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model",
    },
}

snake_case_     :       Dict						=  {
    "ernie-m-base": 5_14,
    "ernie-m-large": 5_14,
}

snake_case_     :       Any						=  {
    "ernie-m-base": {"do_lower_case": False},
    "ernie-m-large": {"do_lower_case": False},
}


class 					__a      (lowerCamelCase     ):
							__a					:   List[str]									=       ["input_ids"]

							__a					:   Union[str, Any]									=       VOCAB_FILES_NAMES
							__a					:   Tuple									=       PRETRAINED_INIT_CONFIGURATION
							__a					:   Dict									=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							__a					:   Optional[int]									=       PRETRAINED_VOCAB_FILES_MAP
							__a					:   Union[str, Any]									=       RESOURCE_FILES_NAMES

							def __init__(							self							:     Union[str, Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     int=None       ,     __magic_name__							:     str=False       ,     __magic_name__							:     int="utf8"       ,     __magic_name__							:     Optional[int]="[UNK]"       ,     __magic_name__							:     Dict="[SEP]"       ,     __magic_name__							:     List[Any]="[PAD]"       ,     __magic_name__							:     str="[CLS]"       ,     __magic_name__							:     Optional[int]="[MASK]"       ,     __magic_name__							:     Optional[Dict[str, Any]] = None       ,     **__magic_name__							:     Union[str, Any]       ,     )					->       None:







													"""simple docstring"""
													# Mask token behave like a normal word, i.e. include the space before it and
													# is included in the raw text, there should be a match in a non-normalized sentence.

													UpperCAmelCase_			:      List[Any]     						=  {} if sp_model_kwargs is None else sp_model_kwargs
													super().__init__(
													    do_lower_case=__magic_name__       ,     unk_token=__magic_name__       ,     sep_token=__magic_name__       ,     pad_token=__magic_name__       ,     cls_token=__magic_name__       ,     mask_token=__magic_name__       ,     vocab_file=__magic_name__       ,     encoding=__magic_name__       ,     sp_model_kwargs=self.sp_model_kwargs       ,     **__magic_name__       ,     )
													UpperCAmelCase_			:      Optional[Any]     						=  do_lower_case
													UpperCAmelCase_			:      List[str]     						=  sentencepiece_model_ckpt
													UpperCAmelCase_			:      Tuple     						=  spm.SentencePieceProcessor(**self.sp_model_kwargs   )
													self.sp_model.Load(__magic_name__   )

													# to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning
													if vocab_file is not None:
																			UpperCAmelCase_			:      List[Any]     						=  self.load_vocab(filepath=__magic_name__   )
													else:
																			UpperCAmelCase_			:      str     						=  {self.sp_model.id_to_piece(__magic_name__   ): id for id in range(self.sp_model.get_piece_size()   )}
													UpperCAmelCase_			:      int     						=  {v: k for k, v in self.vocab.items()}

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Any   )					->       Any:







													"""simple docstring"""
													if text is None:
																			return None

													UpperCAmelCase_			:      str     						=  self.tokenize(__magic_name__   )
													UpperCAmelCase_						,       UpperCAmelCase_			:      str     						=  '''''', []

													for i, ch in enumerate(__magic_name__   ):
																			if ch in self.SP_CHAR_MAPPING:
																									UpperCAmelCase_			:      Optional[int]     						=  self.SP_CHAR_MAPPING.get(__magic_name__   )
																			else:
																									UpperCAmelCase_			:      Union[str, Any]     						=  unicodedata.normalize('''NFKC'''       ,     __magic_name__   )
																			if self.is_whitespace(__magic_name__   ):
																									continue
																			normalized_text += ch
																			char_mapping.extend([i] * len(__magic_name__   )   )

													UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_			:      Union[str, Any]     						=  normalized_text, [], 0

													if self.do_lower_case:
																			UpperCAmelCase_			:      Optional[int]     						=  text.lower()

													for token in split_tokens:
																			if token[:1] == "▁":
																									UpperCAmelCase_			:      Tuple     						=  token[1:]
																			UpperCAmelCase_			:      int     						=  text[offset:].index(__magic_name__   ) + offset
																			UpperCAmelCase_			:      Optional[int]     						=  start + len(__magic_name__   )

																			token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)   )
																			UpperCAmelCase_			:      int     						=  end
													return token_mapping

							@property
							def     UpperCAmelCase__	(							self							:     Any   )					->       Any:







													"""simple docstring"""
													return len(self.vocab   )

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       int:







													"""simple docstring"""
													return dict(self.vocab       ,     **self.added_tokens_encoder   )

							def __getstate__(							self							:     str   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.__dict__.copy()
													UpperCAmelCase_			:      Optional[Any]     						=  None
													return state

							def __setstate__(							self							:     str       ,     __magic_name__							:     Any   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  d

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

													UpperCAmelCase_			:      List[Any]     						=  spm.SentencePieceProcessor(**self.sp_model_kwargs   )
													self.sp_model.Load(self.sentencepiece_model_ckpt   )

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     Any   )					->       List[str]:







													"""simple docstring"""
													return "".join((self.SP_CHAR_MAPPING.get(__magic_name__       ,     __magic_name__   ) for c in text)   )

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Any=False       ,     __magic_name__							:     List[str]=64       ,     __magic_name__							:     List[str]=0.1   )					->       List[str]:







													"""simple docstring"""

													if self.sp_model_kwargs.get('''enable_sampling'''   ) is True:
																			UpperCAmelCase_			:      Dict     						=  True
													if self.sp_model_kwargs.get('''alpha'''   ) is not None:
																			UpperCAmelCase_			:      Union[str, Any]     						=  self.sp_model_kwargs.get('''alpha'''   )
													if self.sp_model_kwargs.get('''nbest_size'''   ) is not None:
																			UpperCAmelCase_			:      Any     						=  self.sp_model_kwargs.get('''nbest_size'''   )

													if not enable_sampling:
																			UpperCAmelCase_			:      Dict     						=  self.sp_model.EncodeAsPieces(__magic_name__   )
													else:
																			UpperCAmelCase_			:      Dict     						=  self.sp_model.SampleEncodeAsPieces(__magic_name__       ,     __magic_name__       ,     __magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  []
													for pi, piece in enumerate(__magic_name__   ):
																			if piece == SPIECE_UNDERLINE:
																									if not pieces[pi + 1].startswith(__magic_name__   ) and pi != 0:
																															new_pieces.append(__magic_name__   )
																															continue
																									else:
																															continue
																			UpperCAmelCase_			:      List[str]     						=  0
																			for i, chunk in enumerate(__magic_name__   ):
																									if chunk == SPIECE_UNDERLINE:
																															continue
																									if self.is_ch_char(__magic_name__   ) or self.is_punct(__magic_name__   ):
																															if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
																																					new_pieces.append(piece[lst_i:i]   )
																															new_pieces.append(__magic_name__   )
																															UpperCAmelCase_			:      List[Any]     						=  i + 1
																									elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit():
																															if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
																																					new_pieces.append(piece[lst_i:i]   )
																															UpperCAmelCase_			:      List[str]     						=  i
																									elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit():
																															if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
																																					new_pieces.append(piece[lst_i:i]   )
																															UpperCAmelCase_			:      str     						=  i
																			if len(__magic_name__   ) > lst_i:
																									new_pieces.append(piece[lst_i:]   )
													return new_pieces

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Optional[int]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  ''''''.join(__magic_name__   ).replace(__magic_name__       ,     ''' '''   ).strip()
													return out_string

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Union[str, Any]   )					->       List[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  self.convert_ids_to_tokens(__magic_name__   )
													UpperCAmelCase_			:      Optional[Any]     						=  ''''''.join(__magic_name__   ).replace(__magic_name__       ,     ''' '''   ).strip()
													return out_string

							def     UpperCAmelCase__	(							self							:     str       ,     __magic_name__							:     Optional[Any]   )					->       List[Any]:







													"""simple docstring"""
													return self.vocab.get(__magic_name__       ,     self.vocab.get(self.unk_token   )   )

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     Union[str, Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													return self.reverse_vocab.get(__magic_name__       ,     self.unk_token   )

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     Any       ,     __magic_name__							:     Union[str, Any]=None   )					->       Any:







													"""simple docstring"""
													if token_ids_a is None:
																			return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
													UpperCAmelCase_			:      Union[str, Any]     						=  [self.cls_token_id]
													UpperCAmelCase_			:      List[Any]     						=  [self.sep_token_id]
													return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     List[str]=None   )					->       int:







													"""simple docstring"""
													if offset_mapping_a is None:
																			return [(0, 0)] + offset_mapping_a + [(0, 0)]

													return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)]

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     List[str]=None       ,     __magic_name__							:     Optional[Any]=False   )					->       Optional[int]:







													"""simple docstring"""

													if already_has_special_tokens:
																			if token_ids_a is not None:
																									raise ValueError(
																									    '''You should not supply a second sequence if the provided sequence of '''
																									    '''ids is already formatted with special tokens for the model.'''   )
																			return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

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

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     List[int]       ,     __magic_name__							:     Optional[List[int]] = None   )					->       List[int]:







													"""simple docstring"""
													# called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method
													if token_ids_a is None:
																			# [CLS] X [SEP]
																			return (len(__magic_name__   ) + 2) * [0]

													# [CLS] A [SEP] [SEP] B [SEP]
													return [0] * (len(__magic_name__   ) + 1) + [1] * (len(__magic_name__   ) + 3)

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     str   )					->       Tuple:







													"""simple docstring"""
													if "\u4e00" <= char <= "\u9fff":
																			return True
													return False

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Optional[int]   )					->       str:







													"""simple docstring"""
													if ("a" <= char <= "z") or ("A" <= char <= "Z"):
																			return True
													return False

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Optional[Any]   )					->       Dict:







													"""simple docstring"""
													if char in ",;:.?!~，；：。？！《》【】":
																			return True
													return False

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     Any   )					->       Union[str, Any]:







													"""simple docstring"""
													if char == " " or char == "\t" or char == "\n" or char == "\r":
																			return True
													if len(__magic_name__   ) == 1:
																			UpperCAmelCase_			:      Optional[Any]     						=  unicodedata.category(__magic_name__   )
																			if cat == "Zs":
																									return True
													return False

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     Tuple   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  {}
													with io.open(__magic_name__       ,     '''r'''       ,     encoding='''utf-8'''   ) as f:
																			for index, line in enumerate(__magic_name__   ):
																									UpperCAmelCase_			:      List[Any]     						=  line.rstrip('''\n'''   )
																									UpperCAmelCase_			:      Dict     						=  int(__magic_name__   )

													return token_to_idx



							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     str       ,     __magic_name__							:     Optional[str] = None   )					->       Tuple[str]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  0
													if os.path.isdir(__magic_name__   ):
																			UpperCAmelCase_			:      Any     						=  os.path.join(
																			    __magic_name__       ,     (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']   )
													else:
																			UpperCAmelCase_			:      List[str]     						=  (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory
													with open(__magic_name__       ,     '''w'''       ,     encoding='''utf-8'''   ) as writer:
																			for token, token_index in sorted(self.vocab.items()       ,     key=lambda __magic_name__   : kv[1]   ):
																									if index != token_index:
																															logger.warning(
																															    F"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."""
																															    ''' Please check that the vocabulary is not corrupted!'''   )
																															UpperCAmelCase_			:      Dict     						=  token_index
																									writer.write(token + '''\n'''   )
																									index += 1

													UpperCAmelCase_			:      Union[str, Any]     						=  os.path.join(__magic_name__       ,     '''sentencepiece.bpe.model'''   )
													with open(__magic_name__       ,     '''wb'''   ) as fi:
																			UpperCAmelCase_			:      Optional[int]     						=  self.sp_model.serialized_model_proto()
																			fi.write(__magic_name__   )

													return (vocab_file,)





