


"""simple docstring"""




import itertools
import json
import os
import unittest

from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast
from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 			__snake_case   (lowerCamelCase			,		unittest.TestCase  ):
							__a					 =       LongformerTokenizer
							__a					 =       True
							__a					 =       LongformerTokenizerFast
							__a					 =       True




							def   __a   (     self:    Union[str, Any]   ):
										super().setUp()

										# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
										__lowerCamelCase          =      [
										    """l""",
										    """o""",
										    """w""",
										    """e""",
										    """r""",
										    """s""",
										    """t""",
										    """i""",
										    """d""",
										    """n""",
										    """\u0120""",
										    """\u0120l""",
										    """\u0120n""",
										    """\u0120lo""",
										    """\u0120low""",
										    """er""",
										    """\u0120lowest""",
										    """\u0120newer""",
										    """\u0120wider""",
										    """<unk>""",
										]
										__lowerCamelCase          =      dict(zip(A_     ,							range(len(A_   )   )   )   )
										__lowerCamelCase          =      ["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
										__lowerCamelCase          =      {"""unk_token""": """<unk>"""}

										__lowerCamelCase          =      os.path.join(self.tmpdirname     ,							VOCAB_FILES_NAMES["""vocab_file"""]   )
										__lowerCamelCase          =      os.path.join(self.tmpdirname     ,							VOCAB_FILES_NAMES["""merges_file"""]   )
										with open(self.vocab_file     ,							"""w"""     ,							encoding="""utf-8"""   ) as fp:
													fp.write(json.dumps(A_   ) + """\n"""   )
										with open(self.merges_file     ,							"""w"""     ,							encoding="""utf-8"""   ) as fp:
													fp.write("""\n""".join(A_   )   )




							def   __a   (     self:    Optional[int]     ,							**A_:    List[Any]   ):
										kwargs.update(self.special_tokens_map   )
										return self.tokenizer_class.from_pretrained(self.tmpdirname     ,							**A_   )




							def   __a   (     self:    List[Any]     ,							**A_:    Optional[Any]   ):
										kwargs.update(self.special_tokens_map   )
										return self.rust_tokenizer_class.from_pretrained(self.tmpdirname     ,							**A_   )




							def   __a   (     self:    int     ,							A_:    Any   ):
										__lowerCamelCase          =      """lower newer"""
										__lowerCamelCase          =      """lower newer"""
										return input_text, output_text




							def   __a   (     self:    str   ):
										__lowerCamelCase          =      self.tokenizer_class(self.vocab_file     ,							self.merges_file     ,							**self.special_tokens_map   )
										__lowerCamelCase          =      """lower newer"""
										__lowerCamelCase          =      ["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""]
										__lowerCamelCase          =      tokenizer.tokenize(A_   )  # , add_prefix_space=True)
										self.assertListEqual(A_     ,							A_   )

										__lowerCamelCase          =      tokens + [tokenizer.unk_token]
										__lowerCamelCase          =      [0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
										self.assertListEqual(tokenizer.convert_tokens_to_ids(A_   )     ,							A_   )




							def   __a   (     self:    int   ):
										__lowerCamelCase          =      self.get_tokenizer()

										self.assertListEqual(tokenizer.encode("""Hello world!"""     ,							add_special_tokens=A_   )     ,							[0, 3_14_14, 2_32, 3_28, 2]   )
										self.assertListEqual(
										    tokenizer.encode("""Hello world! cécé herlolip 418"""     ,							add_special_tokens=A_   )     ,							[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]     ,							)




							@slow
							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      self.tokenizer_class.from_pretrained("""allenai/longformer-base-4096"""   )

										__lowerCamelCase          =      tokenizer.encode("""sequence builders"""     ,							add_special_tokens=A_   )
										__lowerCamelCase          =      tokenizer.encode("""multi-sequence build"""     ,							add_special_tokens=A_   )

										__lowerCamelCase          =      tokenizer.encode(
										    """sequence builders"""     ,							add_special_tokens=A_     ,							add_prefix_space=A_   )
										__lowerCamelCase          =      tokenizer.encode(
										    """sequence builders"""     ,							"""multi-sequence build"""     ,							add_special_tokens=A_     ,							add_prefix_space=A_   )

										__lowerCamelCase          =      tokenizer.build_inputs_with_special_tokens(A_   )
										__lowerCamelCase          =      tokenizer.build_inputs_with_special_tokens(A_     ,							A_   )

										assert encoded_sentence == encoded_text_from_decode
										assert encoded_pair == encoded_pair_from_decode




							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      self.get_tokenizer()

										__lowerCamelCase          =      """Encode this sequence."""
										__lowerCamelCase          =      tokenizer.byte_encoder[""" """.encode("""utf-8"""   )[0]]

										# Testing encoder arguments
										__lowerCamelCase          =      tokenizer.encode(A_     ,							add_special_tokens=A_     ,							add_prefix_space=A_   )
										__lowerCamelCase          =      tokenizer.convert_ids_to_tokens(encoded[0]   )[0]
										self.assertNotEqual(A_     ,							A_   )

										__lowerCamelCase          =      tokenizer.encode(A_     ,							add_special_tokens=A_     ,							add_prefix_space=A_   )
										__lowerCamelCase          =      tokenizer.convert_ids_to_tokens(encoded[0]   )[0]
										self.assertEqual(A_     ,							A_   )

										tokenizer.add_special_tokens({"""bos_token""": """<s>"""}   )
										__lowerCamelCase          =      tokenizer.encode(A_     ,							add_special_tokens=A_   )
										__lowerCamelCase          =      tokenizer.convert_ids_to_tokens(encoded[1]   )[0]
										self.assertNotEqual(A_     ,							A_   )

										# Testing spaces after special tokens
										__lowerCamelCase          =      """<mask>"""
										tokenizer.add_special_tokens(
										    {"""mask_token""": AddedToken(A_     ,							lstrip=A_     ,							rstrip=A_   )}   )  # mask token has a left space
										__lowerCamelCase          =      tokenizer.convert_tokens_to_ids(A_   )

										__lowerCamelCase          =      """Encode <mask> sequence"""
										__lowerCamelCase          =      """Encode <mask>sequence"""

										__lowerCamelCase          =      tokenizer.encode(A_   )
										__lowerCamelCase          =      encoded.index(A_   )
										__lowerCamelCase          =      tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]   )[0]
										self.assertEqual(A_     ,							A_   )

										__lowerCamelCase          =      tokenizer.encode(A_   )
										__lowerCamelCase          =      encoded.index(A_   )
										__lowerCamelCase          =      tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]   )[0]
										self.assertNotEqual(A_     ,							A_   )




							def   __a   (     self:    Optional[int]   ):
										pass




							def   __a   (     self:    Tuple   ):
										for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'   ):
																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(A_     ,							**A_   )
																__lowerCamelCase          =      self.tokenizer_class.from_pretrained(A_     ,							**A_   )
																__lowerCamelCase          =      """A, <mask> AllenNLP sentence."""
																__lowerCamelCase          =      tokenizer_r.encode_plus(A_     ,							add_special_tokens=A_     ,							return_token_type_ids=A_   )
																__lowerCamelCase          =      tokenizer_p.encode_plus(A_     ,							add_special_tokens=A_     ,							return_token_type_ids=A_   )

																# token_type_ids should put 0 everywhere
																self.assertEqual(sum(tokens_r["""token_type_ids"""]   )     ,							sum(tokens_p["""token_type_ids"""]   )   )

																# attention_mask should put 1 everywhere, so sum over length should be 1
																self.assertEqual(
																    sum(tokens_r["""attention_mask"""]   ) / len(tokens_r["""attention_mask"""]   )     ,							sum(tokens_p["""attention_mask"""]   ) / len(tokens_p["""attention_mask"""]   )     ,							)

																__lowerCamelCase          =      tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""]   )
																__lowerCamelCase          =      tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""]   )

																# Rust correctly handles the space before the mask while python doesnt
																self.assertSequenceEqual(tokens_p["""input_ids"""]     ,							[0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2]   )
																self.assertSequenceEqual(tokens_r["""input_ids"""]     ,							[0, 2_50, 6, 5_02_64, 38_23, 4_87, 2_19_92, 36_45, 4, 2]   )

																self.assertSequenceEqual(
																    A_     ,							["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]   )
																self.assertSequenceEqual(
																    A_     ,							["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]   )




							def   __a   (     self:    List[Any]   ):
										for trim_offsets, add_prefix_space in itertools.product([True, False]     ,							repeat=2   ):
													__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
													    self.tmpdirname     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )

													__lowerCamelCase          =      json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__()   )
													__lowerCamelCase          =      json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__()   )

													self.assertEqual(pre_tokenizer_state["""add_prefix_space"""]     ,							A_   )

													self.assertEqual(post_processor_state["""add_prefix_space"""]     ,							A_   )
													self.assertEqual(post_processor_state["""trim_offsets"""]     ,							A_   )




							def   __a   (     self:    Tuple   ):
										# Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and
										# `trim_offsets`
										for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'   ):
																__lowerCamelCase          =      """hello"""  # `hello` is a token in the vocabulary of `pretrained_name`
																__lowerCamelCase          =      f'{text_of_1_token} {text_of_1_token}'

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(0, len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(len(A_   ) + 1, len(A_   ) + 1 + len(A_   ))     ,							)

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(0, len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(len(A_   ) + 1, len(A_   ) + 1 + len(A_   ))     ,							)

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(0, len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(len(A_   ), len(A_   ) + 1 + len(A_   ))     ,							)

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(0, len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(len(A_   ), len(A_   ) + 1 + len(A_   ))     ,							)

																__lowerCamelCase          =      f' {text}'

																# tokenizer_r = self.rust_tokenizer_class.from_pretrained(
																#     pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True
																# )
																# encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False)
																# self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token)))
																# self.assertEqual(
																#     encoding.offset_mapping[1],
																#     (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)),
																# )

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(1, 1 + len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(1 + len(A_   ) + 1, 1 + len(A_   ) + 1 + len(A_   ))     ,							)

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(0, 1 + len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(1 + len(A_   ), 1 + len(A_   ) + 1 + len(A_   ))     ,							)

																__lowerCamelCase          =      self.rust_tokenizer_class.from_pretrained(
																    A_     ,							use_fast=A_     ,							add_prefix_space=A_     ,							trim_offsets=A_   )
																__lowerCamelCase          =      tokenizer_r(A_     ,							return_offsets_mapping=A_     ,							add_special_tokens=A_   )
																self.assertEqual(encoding.offset_mapping[0]     ,							(0, 1 + len(A_   ))   )
																self.assertEqual(
																    encoding.offset_mapping[1]     ,							(1 + len(A_   ), 1 + len(A_   ) + 1 + len(A_   ))     ,							)








"""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 			__snake_case   :




							def __init__(     self:    Any     ,							A_:    str     ,							A_:    str=13     ,							A_:    Dict=7     ,							A_:    Optional[int]=True     ,							A_:    int=True     ,							A_:    List[str]=True     ,							A_:    Any=True     ,							A_:    List[Any]=99     ,							A_:    Tuple=32     ,							A_:    Union[str, Any]=2     ,							A_:    Dict=4     ,							A_:    Tuple=37     ,							A_:    Dict="gelu"     ,							A_:    List[Any]=0.1     ,							A_:    int=0.1     ,							A_:    Union[str, Any]=5_12     ,							A_:    List[Any]=16     ,							A_:    Optional[Any]=2     ,							A_:    int=0.02     ,							A_:    List[str]=3     ,							A_:    Optional[int]=4     ,							A_:    List[str]=None     ,							):
										__lowerCamelCase          =      parent
										__lowerCamelCase          =      13
										__lowerCamelCase          =      7
										__lowerCamelCase          =      True
										__lowerCamelCase          =      True
										__lowerCamelCase          =      True
										__lowerCamelCase          =      True
										__lowerCamelCase          =      99
										__lowerCamelCase          =      32
										__lowerCamelCase          =      2
										__lowerCamelCase          =      4
										__lowerCamelCase          =      37
										__lowerCamelCase          =      """gelu"""
										__lowerCamelCase          =      0.1
										__lowerCamelCase          =      0.1
										__lowerCamelCase          =      5_12
										__lowerCamelCase          =      16
										__lowerCamelCase          =      2
										__lowerCamelCase          =      0.02
										__lowerCamelCase          =      3
										__lowerCamelCase          =      4
										__lowerCamelCase          =      None




							def   __a   (     self:    Optional[Any]   ):
										__lowerCamelCase          =      ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size   )

										__lowerCamelCase          =      None
										if self.use_input_mask:
													__lowerCamelCase          =      random_attention_mask([self.batch_size, self.seq_length]   )

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

										__lowerCamelCase          =      None
										__lowerCamelCase          =      None
										__lowerCamelCase          =      None
										if self.use_labels:
													__lowerCamelCase          =      ids_tensor([self.batch_size]     ,							self.type_sequence_label_size   )
													__lowerCamelCase          =      ids_tensor([self.batch_size, self.seq_length]     ,							self.num_labels   )
													__lowerCamelCase          =      ids_tensor([self.batch_size]     ,							self.num_choices   )

										__lowerCamelCase          =      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=A_     ,							)

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




							def   __a   (     self:    Union[str, Any]     ,							A_:    str     ,							A_:    Dict     ,							A_:    Union[str, Any]     ,							A_:    Optional[int]     ,							A_:    Any     ,							A_:    Dict     ,							A_:    int   ):
										__lowerCamelCase          =      TFRoFormerModel(config=A_   )
										__lowerCamelCase          =      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

										__lowerCamelCase          =      [input_ids, input_mask]
										__lowerCamelCase          =      model(A_   )

										__lowerCamelCase          =      model(A_   )

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




							def   __a   (     self:    List[str]     ,							A_:    int     ,							A_:    str     ,							A_:    Any     ,							A_:    int     ,							A_:    str     ,							A_:    str     ,							A_:    Union[str, Any]   ):
										__lowerCamelCase          =      True
										__lowerCamelCase          =      TFRoFormerForCausalLM(config=A_   )
										__lowerCamelCase          =      {
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}
										__lowerCamelCase          =      model(A_   )["""logits"""]
										self.parent.assertListEqual(
										    list(prediction_scores.numpy().shape   )     ,							[self.batch_size, self.seq_length, self.vocab_size]   )




							def   __a   (     self:    Any     ,							A_:    Optional[Any]     ,							A_:    Optional[Any]     ,							A_:    Union[str, Any]     ,							A_:    str     ,							A_:    str     ,							A_:    Optional[Any]     ,							A_:    int   ):
										__lowerCamelCase          =      TFRoFormerForMaskedLM(config=A_   )
										__lowerCamelCase          =      {
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}
										__lowerCamelCase          =      model(A_   )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.vocab_size)   )




							def   __a   (     self:    Optional[Any]     ,							A_:    Dict     ,							A_:    Union[str, Any]     ,							A_:    List[Any]     ,							A_:    str     ,							A_:    int     ,							A_:    Optional[Any]     ,							A_:    int   ):
										__lowerCamelCase          =      self.num_labels
										__lowerCamelCase          =      TFRoFormerForSequenceClassification(config=A_   )
										__lowerCamelCase          =      {
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}

										__lowerCamelCase          =      model(A_   )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)   )




							def   __a   (     self:    List[str]     ,							A_:    Tuple     ,							A_:    List[str]     ,							A_:    Union[str, Any]     ,							A_:    Optional[Any]     ,							A_:    List[str]     ,							A_:    Optional[int]     ,							A_:    List[Any]   ):
										__lowerCamelCase          =      self.num_choices
										__lowerCamelCase          =      TFRoFormerForMultipleChoice(config=A_   )
										__lowerCamelCase          =      tf.tile(tf.expand_dims(A_     ,							1   )     ,							(1, self.num_choices, 1)   )
										__lowerCamelCase          =      tf.tile(tf.expand_dims(A_     ,							1   )     ,							(1, self.num_choices, 1)   )
										__lowerCamelCase          =      tf.tile(tf.expand_dims(A_     ,							1   )     ,							(1, self.num_choices, 1)   )
										__lowerCamelCase          =      {
										    """input_ids""": multiple_choice_inputs_ids,
										    """attention_mask""": multiple_choice_input_mask,
										    """token_type_ids""": multiple_choice_token_type_ids,
										}
										__lowerCamelCase          =      model(A_   )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_choices)   )




							def   __a   (     self:    Union[str, Any]     ,							A_:    Dict     ,							A_:    List[Any]     ,							A_:    List[Any]     ,							A_:    Union[str, Any]     ,							A_:    str     ,							A_:    Union[str, Any]     ,							A_:    List[str]   ):
										__lowerCamelCase          =      self.num_labels
										__lowerCamelCase          =      TFRoFormerForTokenClassification(config=A_   )
										__lowerCamelCase          =      {
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}
										__lowerCamelCase          =      model(A_   )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.num_labels)   )




							def   __a   (     self:    int     ,							A_:    Union[str, Any]     ,							A_:    Any     ,							A_:    Any     ,							A_:    Dict     ,							A_:    str     ,							A_:    int     ,							A_:    Tuple   ):
										__lowerCamelCase          =      TFRoFormerForQuestionAnswering(config=A_   )
										__lowerCamelCase          =      {
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}

										__lowerCamelCase          =      model(A_   )
										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   __a   (     self:    Union[str, Any]   ):
										__lowerCamelCase          =      self.prepare_config_and_inputs()
										(
										    (
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,
										)          =      config_and_inputs
										__lowerCamelCase          =      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
										return config, inputs_dict




@require_tf
class 			__snake_case   (lowerCamelCase			,		lowerCamelCase			,		unittest.TestCase  ):
							__a					 =       (
							    (
							        TFRoFormerModel,
							        TFRoFormerForCausalLM,
							        TFRoFormerForMaskedLM,
							        TFRoFormerForQuestionAnswering,
							        TFRoFormerForSequenceClassification,
							        TFRoFormerForTokenClassification,
							        TFRoFormerForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							__a					 =       (
							    {
							        '''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					 =       False
							__a					 =       False




							def   __a   (     self:    List[str]     ,							A_:    List[Any]     ,							A_:    List[str]     ,							A_:    str     ,							A_:    Union[str, Any]     ,							A_:    Dict   ):
										if pipeline_test_casse_name == "TextGenerationPipelineTests":
													return True

										return False




							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      TFRoFormerModelTester(self   )
										__lowerCamelCase          =      ConfigTester(self     ,							config_class=A_     ,							hidden_size=37   )




							def   __a   (     self:    int   ):
										self.config_tester.run_common_tests()




							def   __a   (     self:    Optional[int]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*A_   )




							def   __a   (     self:    Union[str, Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*A_   )




							def   __a   (     self:    Optional[Any]   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head(*A_   )




							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*A_   )




							def   __a   (     self:    Any   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*A_   )




							def   __a   (     self:    int   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*A_   )




							def   __a   (     self:    int   ):
										__lowerCamelCase          =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*A_   )




							@slow
							def   __a   (     self:    Optional[int]   ):
										__lowerCamelCase          =      TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base"""   )
										self.assertIsNotNone(A_   )




@require_tf
class 			__snake_case   (unittest.TestCase  ):




							@slow
							def   __a   (     self:    Optional[Any]   ):
										__lowerCamelCase          =      TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base"""   )
										__lowerCamelCase          =      tf.constant([[0, 1, 2, 3, 4, 5]]   )
										__lowerCamelCase          =      model(A_   )[0]

										# TODO Replace vocab size
										__lowerCamelCase          =      5_00_00

										__lowerCamelCase          =      [1, 6, vocab_size]
										self.assertEqual(output.shape     ,							A_   )

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

										# TODO Replace values below with what was printed above.
										__lowerCamelCase          =      tf.constant(
										    [
										        [
										            [-0.12_053_341, -1.0_264_901, 0.29_221_946],
										            [-1.5_133_783, 0.197_433, 0.15_190_607],
										            [-5.0_135_403, -3.900_256, -0.84_038_764],
										        ]
										    ]   )
										tf.debugging.assert_near(output[:, :3, :3]     ,							A_     ,							atol=1E-4   )




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




							def   __a   (     self:    List[Any]   ):
										__lowerCamelCase          =      tf.constant([[4, 10]]   )
										__lowerCamelCase          =      TFRoFormerSinusoidalPositionalEmbedding(num_positions=6     ,							embedding_dim=6   )

										__lowerCamelCase          =      emba(input_ids.shape   )
										__lowerCamelCase          =      tf.constant(
										    [[0.0_000, 0.0_000, 0.0_000, 1.0_000, 1.0_000, 1.0_000], [0.8_415, 0.0_464, 0.0_022, 0.5_403, 0.9_989, 1.0_000]]   )

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




							def   __a   (     self:    Optional[Any]   ):
										__lowerCamelCase          =      tf.constant(
										    [
										        [0.0_000, 0.0_000, 0.0_000, 0.0_000, 0.0_000],
										        [0.8_415, 0.8_219, 0.8_020, 0.7_819, 0.7_617],
										        [0.9_093, 0.9_364, 0.9_581, 0.9_749, 0.9_870],
										    ]   )
										__lowerCamelCase          =      TFRoFormerSinusoidalPositionalEmbedding(num_positions=5_12     ,							embedding_dim=5_12   )
										emba([2, 16, 5_12]   )
										__lowerCamelCase          =      emba.weight[:3, :5]

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




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




							def   __a   (     self:    str   ):
										# 2,12,16,64
										__lowerCamelCase          =      tf.reshape(tf.range(2 * 12 * 16 * 64     ,							dtype=tf.floataa   )     ,							shape=(2, 12, 16, 64)   ) / 1_00

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

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

										__lowerCamelCase     ,__lowerCamelCase          =      TFRoFormerSelfAttention.apply_rotary_position_embeddings(
										    A_     ,							A_     ,							A_   )

										__lowerCamelCase          =      tf.constant(
										    [
										        [0.0_000, 0.0_100, 0.0_200, 0.0_300, 0.0_400, 0.0_500, 0.0_600, 0.0_700],
										        [-0.2_012, 0.8_897, 0.0_263, 0.9_401, 0.2_074, 0.9_463, 0.3_481, 0.9_343],
										        [-1.7_057, 0.6_271, -1.2_145, 1.3_897, -0.6_303, 1.7_647, -0.1_173, 1.8_985],
										        [-2.1_731, -1.6_397, -2.7_358, 0.2_854, -2.1_840, 1.7_183, -1.3_018, 2.4_871],
										        [0.2_717, -3.6_173, -2.9_206, -2.1_988, -3.6_638, 0.3_858, -2.9_155, 2.2_980],
										        [3.9_859, -2.1_580, -0.7_984, -4.4_904, -4.1_181, -2.0_252, -4.4_782, 1.1_253],
										    ]   )
										__lowerCamelCase          =      tf.constant(
										    [
										        [0.0_000, -0.0_100, -0.0_200, -0.0_300, -0.0_400, -0.0_500, -0.0_600, -0.0_700],
										        [0.2_012, -0.8_897, -0.0_263, -0.9_401, -0.2_074, -0.9_463, -0.3_481, -0.9_343],
										        [1.7_057, -0.6_271, 1.2_145, -1.3_897, 0.6_303, -1.7_647, 0.1_173, -1.8_985],
										        [2.1_731, 1.6_397, 2.7_358, -0.2_854, 2.1_840, -1.7_183, 1.3_018, -2.4_871],
										        [-0.2_717, 3.6_173, 2.9_206, 2.1_988, 3.6_638, -0.3_858, 2.9_155, -2.2_980],
										        [-3.9_859, 2.1_580, 0.7_984, 4.4_904, 4.1_181, 2.0_252, 4.4_782, -1.1_253],
										    ]   )

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




