import unittest

from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


__lowerCamelCase					:	str       =  get_tests_dir('''fixtures/test_sentencepiece.model''')






@require_sentencepiece
@require_tokenizers
class 		a__						(					A__    ,		unittest.TestCase					):
					A	       =							ReformerTokenizer
					A	       =							ReformerTokenizerFast
					A	       =							True
					A	       =							False
					A	       =							True

					def 				__UpperCamelCase   (    self      :       Dict    ):







							"""simple docstring"""






							super().setUp()

							SCREAMING_SNAKE_CASE_  :		int				      =    ReformerTokenizer(_A,keep_accents=_A    )
							tokenizer.save_pretrained(self.tmpdirname    )

					def 				__UpperCamelCase   (    self      :       List[str]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int				      =    "<s>"
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    1

							self.assertEqual(self.get_tokenizer()._convert_token_to_id(_A    ),_A    )
							self.assertEqual(self.get_tokenizer()._convert_id_to_token(_A    ),_A    )

					def 				__UpperCamelCase   (    self      :       Dict    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Tuple				      =    list(self.get_tokenizer().get_vocab().keys()    )

							self.assertEqual(vocab_keys[0],"<unk>"    )
							self.assertEqual(vocab_keys[1],"<s>"    )
							self.assertEqual(vocab_keys[-1],"j"    )
							self.assertEqual(len(_A    ),1000    )

					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							self.assertEqual(self.get_tokenizer().vocab_size,1000    )

					def 				__UpperCamelCase   (    self      :       Dict    ):







							"""simple docstring"""






							if not self.test_rust_tokenizer:
									return

							SCREAMING_SNAKE_CASE_  :		Any				      =    self.get_tokenizer()
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    self.get_rust_tokenizer()

							SCREAMING_SNAKE_CASE_  :		int				      =    "I was born in 92000, and this is falsé."

							SCREAMING_SNAKE_CASE_  :		Dict				      =    tokenizer.tokenize(_A    )
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    rust_tokenizer.tokenize(_A    )
							self.assertListEqual(_A,_A    )

							SCREAMING_SNAKE_CASE_  :		int				      =    tokenizer.encode(_A,add_special_tokens=_A    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    rust_tokenizer.encode(_A,add_special_tokens=_A    )
							self.assertListEqual(_A,_A    )

							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    self.get_rust_tokenizer()
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    tokenizer.encode(_A    )
							SCREAMING_SNAKE_CASE_  :		str				      =    rust_tokenizer.encode(_A    )
							self.assertListEqual(_A,_A    )

					def 				__UpperCamelCase   (    self      :       str,_A      :       List[str]=15    ):







							"""simple docstring"""






							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
									with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})'    ):
											SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.rust_tokenizer_class.from_pretrained(_A,**_A    )

											# Simple input
											SCREAMING_SNAKE_CASE_  :		int				      =    "This is a simple input"
											SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    ["This is a simple input 1", "This is a simple input 2"]
											SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    ("This is a simple input", "This is a pair")
											SCREAMING_SNAKE_CASE_  :		List[Any]				      =    [
											    ("This is a simple input 1", "This is a simple input 2"),
											    ("This is a simple pair 1", "This is a simple pair 2"),
											]

											# Simple input tests
											self.assertRaises(_A,tokenizer_r.encode,_A,max_length=_A,padding="max_length"    )

											# Simple input
											self.assertRaises(_A,tokenizer_r.encode_plus,_A,max_length=_A,padding="max_length"    )

											# Simple input
											self.assertRaises(
											    _A,tokenizer_r.batch_encode_plus,_A,max_length=_A,padding="max_length",)

											# Pair input
											self.assertRaises(_A,tokenizer_r.encode,_A,max_length=_A,padding="max_length"    )

											# Pair input
											self.assertRaises(_A,tokenizer_r.encode_plus,_A,max_length=_A,padding="max_length"    )

											# Pair input
											self.assertRaises(
											    _A,tokenizer_r.batch_encode_plus,_A,max_length=_A,padding="max_length",)

					def 				__UpperCamelCase   (    self      :       int    ):







							"""simple docstring"""






							pass

					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int				      =    ReformerTokenizer(_A,keep_accents=_A    )

							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    tokenizer.tokenize("This is a test"    )
							self.assertListEqual(_A,["▁This", "▁is", "▁a", "▁t", "est"]    )

							self.assertListEqual(
							    tokenizer.convert_tokens_to_ids(_A    ),[285, 46, 10, 170, 382],)

							SCREAMING_SNAKE_CASE_  :		str				      =    tokenizer.tokenize("I was born in 92000, and this is falsé."    )
							self.assertListEqual(
							    _A,[
							        SPIECE_UNDERLINE + "I",
							        SPIECE_UNDERLINE + "was",
							        SPIECE_UNDERLINE + "b",
							        "or",
							        "n",
							        SPIECE_UNDERLINE + "in",
							        SPIECE_UNDERLINE + "",
							        "9",
							        "2",
							        "0",
							        "0",
							        "0",
							        ",",
							        SPIECE_UNDERLINE + "and",
							        SPIECE_UNDERLINE + "this",
							        SPIECE_UNDERLINE + "is",
							        SPIECE_UNDERLINE + "f",
							        "al",
							        "s",
							        "é",
							        ".",
							    ],)
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    tokenizer.convert_tokens_to_ids(_A    )
							self.assertListEqual(
							    _A,[8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4],)

							SCREAMING_SNAKE_CASE_  :		int				      =    tokenizer.convert_ids_to_tokens(_A    )
							self.assertListEqual(
							    _A,[
							        SPIECE_UNDERLINE + "I",
							        SPIECE_UNDERLINE + "was",
							        SPIECE_UNDERLINE + "b",
							        "or",
							        "n",
							        SPIECE_UNDERLINE + "in",
							        SPIECE_UNDERLINE + "",
							        "<unk>",
							        "2",
							        "0",
							        "0",
							        "0",
							        ",",
							        SPIECE_UNDERLINE + "and",
							        SPIECE_UNDERLINE + "this",
							        SPIECE_UNDERLINE + "is",
							        SPIECE_UNDERLINE + "f",
							        "al",
							        "s",
							        "<unk>",
							        ".",
							    ],)

					@cached_property
					def 				__UpperCamelCase   (    self      :       Optional[Any]    ):







							"""simple docstring"""






							return ReformerTokenizer.from_pretrained("google/reformer-crime-and-punishment"    )

					@slow
					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    "Hello World!"
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    [126, 32, 262, 152, 38, 72, 287]

							self.assertListEqual(_A,self.big_tokenizer.encode(_A    )    )

					@slow
					def 				__UpperCamelCase   (    self      :       List[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int				      =    (
							    "This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will"
							    " add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth"
							)
							SCREAMING_SNAKE_CASE_  :		str				      =    [
							    108,
							    265,
							    24,
							    111,
							    4,
							    258,
							    156,
							    35,
							    28,
							    275,
							    3,
							    259,
							    297,
							    260,
							    84,
							    4,
							    35,
							    110,
							    44,
							    8,
							    259,
							    91,
							    268,
							    21,
							    11,
							    209,
							    274,
							    109,
							    266,
							    277,
							    117,
							    86,
							    93,
							    315,
							    258,
							    278,
							    258,
							    277,
							    258,
							    0,
							    258,
							    288,
							    258,
							    319,
							    258,
							    0,
							    258,
							    0,
							    258,
							    0,
							    258,
							    0,
							    258,
							    287,
							    258,
							    315,
							    258,
							    289,
							    258,
							    278,
							    99,
							    269,
							    266,
							    262,
							    8,
							    259,
							    241,
							    4,
							    217,
							    230,
							    268,
							    266,
							    55,
							    168,
							    106,
							    75,
							    193,
							    266,
							    223,
							    27,
							    49,
							    26,
							    282,
							    25,
							    264,
							    299,
							    19,
							    26,
							    0,
							    258,
							    277,
							    117,
							    86,
							    93,
							    176,
							    183,
							    270,
							    11,
							    262,
							    42,
							    61,
							    265,
							]

							self.assertListEqual(_A,self.big_tokenizer.encode(_A    )    )

					@require_torch
					@slow
					def 				__UpperCamelCase   (    self      :       List[str]    ):







							"""simple docstring"""






							import torch

							from transformers import ReformerConfig, ReformerModel

							# Build sequence
							SCREAMING_SNAKE_CASE_  :		Any				      =    list(self.big_tokenizer.get_vocab().keys()    )[:10]
							SCREAMING_SNAKE_CASE_  :		str				      =    " ".join(_A    )
							SCREAMING_SNAKE_CASE_  :		str				      =    self.big_tokenizer.encode_plus(_A,return_tensors="pt"    )
							SCREAMING_SNAKE_CASE_  :		Any				      =    self.big_tokenizer.batch_encode_plus([sequence, sequence],return_tensors="pt"    )

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    ReformerConfig()
							# The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024)
							SCREAMING_SNAKE_CASE_  :		int				      =    encoded_sequence["input_ids"].shape
							SCREAMING_SNAKE_CASE_  :		Any				      =    ReformerModel(_A    )

							# Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320)
							assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size

							with torch.no_grad():
									model(**_A    )
									model(**_A    )






					@slow
					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    {"input_ids": [[108, 265, 24, 111, 4, 258, 156, 7, 51, 279, 58, 7, 76, 25, 69, 278], [140, 243, 264, 134, 17, 267, 77, 263, 22, 262, 297, 258, 304, 177, 279, 266, 14, 89, 13, 35, 261, 299, 272, 137, 275, 278]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
							# fmt: on

							# This tokenizer does not know some characters like ")".
							# That is the reason why we use very simple texts here.
							# Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    [
							    "This is a very simple sentence.",
							    "The quick brown fox jumps over the lazy dog.",
							]

							self.tokenizer_integration_test_util(
							    expected_encoding=_A,model_name="google/reformer-crime-and-punishment",revision="0e6c3decb8211d49bf881013425dc8b0448b3f5a",padding=_A,sequences=_A,)

from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 (
		    TFConvBertForMaskedLM,
		    TFConvBertForMultipleChoice,
		    TFConvBertForQuestionAnswering,
		    TFConvBertForSequenceClassification,
		    TFConvBertForTokenClassification,
		    TFConvBertModel,
		)






class 		a__						:

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







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    parent
							SCREAMING_SNAKE_CASE_  :		Any				      =    13
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    7
							SCREAMING_SNAKE_CASE_  :		Dict				      =    True
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    True
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    True
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    True
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    99
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    384
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    2
							SCREAMING_SNAKE_CASE_  :		Any				      =    4
							SCREAMING_SNAKE_CASE_  :		str				      =    37
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    "gelu"
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    0.1
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    0.1
							SCREAMING_SNAKE_CASE_  :		Dict				      =    512
							SCREAMING_SNAKE_CASE_  :		int				      =    16
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    2
							SCREAMING_SNAKE_CASE_  :		Any				      =    0.02
							SCREAMING_SNAKE_CASE_  :		str				      =    3
							SCREAMING_SNAKE_CASE_  :		int				      =    4
							SCREAMING_SNAKE_CASE_  :		Dict				      =    128
							SCREAMING_SNAKE_CASE_  :		Any				      =    2
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    9
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    1
							SCREAMING_SNAKE_CASE_  :		Any				      =    None

					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






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

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

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

							SCREAMING_SNAKE_CASE_  :		Dict				      =    None
							SCREAMING_SNAKE_CASE_  :		Dict				      =    None
							SCREAMING_SNAKE_CASE_  :		str				      =    None
							if self.use_labels:
									SCREAMING_SNAKE_CASE_  :		List[str]				      =    ids_tensor([self.batch_size],self.type_sequence_label_size    )
									SCREAMING_SNAKE_CASE_  :		Dict				      =    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_  :		Any				      =    ConvBertConfig(
							    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 				__UpperCamelCase   (    self      :       Optional[int],_A      :       List[Any],_A      :       int,_A      :       Tuple,_A      :       Optional[int],_A      :       Union[str, Any],_A      :       Union[str, Any],_A      :       List[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    TFConvBertModel(config=_A    )
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

							SCREAMING_SNAKE_CASE_  :		str				      =    [input_ids, input_mask]
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    model(_A    )

							SCREAMING_SNAKE_CASE_  :		Dict				      =    model(_A    )

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

					def 				__UpperCamelCase   (    self      :       Dict,_A      :       Dict,_A      :       int,_A      :       Union[str, Any],_A      :       List[Any],_A      :       int,_A      :       str,_A      :       int    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Dict				      =    TFConvBertForMaskedLM(config=_A    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    model(_A    )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.vocab_size)    )

					def 				__UpperCamelCase   (    self      :       Any,_A      :       Optional[int],_A      :       List[Any],_A      :       Union[str, Any],_A      :       List[Any],_A      :       Union[str, Any],_A      :       Optional[int],_A      :       Dict    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    self.num_labels
							SCREAMING_SNAKE_CASE_  :		Any				      =    TFConvBertForSequenceClassification(config=_A    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    model(_A    )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)    )

					def 				__UpperCamelCase   (    self      :       int,_A      :       int,_A      :       Dict,_A      :       List[str],_A      :       Tuple,_A      :       Dict,_A      :       Optional[int],_A      :       int    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    self.num_choices
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    TFConvBertForMultipleChoice(config=_A    )
							SCREAMING_SNAKE_CASE_  :		Any				      =    tf.tile(tf.expand_dims(_A,1    ),(1, self.num_choices, 1)    )
							SCREAMING_SNAKE_CASE_  :		Any				      =    tf.tile(tf.expand_dims(_A,1    ),(1, self.num_choices, 1)    )
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    tf.tile(tf.expand_dims(_A,1    ),(1, self.num_choices, 1)    )
							SCREAMING_SNAKE_CASE_  :		int				      =    {
							    "input_ids": multiple_choice_inputs_ids,
							    "attention_mask": multiple_choice_input_mask,
							    "token_type_ids": multiple_choice_token_type_ids,
							}
							SCREAMING_SNAKE_CASE_  :		int				      =    model(_A    )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_choices)    )

					def 				__UpperCamelCase   (    self      :       List[Any],_A      :       Union[str, Any],_A      :       int,_A      :       Optional[int],_A      :       str,_A      :       str,_A      :       Tuple,_A      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.num_labels
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    TFConvBertForTokenClassification(config=_A    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							SCREAMING_SNAKE_CASE_  :		str				      =    model(_A    )
							self.parent.assertEqual(result.logits.shape,(self.batch_size, self.seq_length, self.num_labels)    )

					def 				__UpperCamelCase   (    self      :       List[Any],_A      :       int,_A      :       List[str],_A      :       List[Any],_A      :       Any,_A      :       Optional[int],_A      :       List[str],_A      :       Optional[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    TFConvBertForQuestionAnswering(config=_A    )
							SCREAMING_SNAKE_CASE_  :		Dict				      =    {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							SCREAMING_SNAKE_CASE_  :		Any				      =    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 				__UpperCamelCase   (    self      :       Tuple    ):







							"""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_
							)      ,    (
							    SCREAMING_SNAKE_CASE_
							)      ,    
							)  :		List[Any]				      =    config_and_inputs
							SCREAMING_SNAKE_CASE_  :		Any				      =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict







@require_tf
class 		a__						(					A__    ,		A__    ,		unittest.TestCase					):
					A	       =							(
					    (
					        TFConvBertModel,
					        TFConvBertForMaskedLM,
					        TFConvBertForQuestionAnswering,
					        TFConvBertForSequenceClassification,
					        TFConvBertForTokenClassification,
					        TFConvBertForMultipleChoice,
					    )
					    if is_tf_available()
					    else ()
					)
					A	       =							(
					    {
					        'feature-extraction': TFConvBertModel,
					        'fill-mask': TFConvBertForMaskedLM,
					        'question-answering': TFConvBertForQuestionAnswering,
					        'text-classification': TFConvBertForSequenceClassification,
					        'token-classification': TFConvBertForTokenClassification,
					        'zero-shot': TFConvBertForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)
					A	       =							False
					A	       =							False
					A	       =							False

					def 				__UpperCamelCase   (    self      :       List[str]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		str				      =    TFConvBertModelTester(self    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    ConfigTester(self,config_class=_A,hidden_size=37    )

					def 				__UpperCamelCase   (    self      :       Optional[int]    ):







							"""simple docstring"""






							self.config_tester.run_common_tests()

					def 				__UpperCamelCase   (    self      :       Optional[Any]    ):







							"""simple docstring"""






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

					def 				__UpperCamelCase   (    self      :       Optional[int]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int				      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_A    )

					def 				__UpperCamelCase   (    self      :       Optional[int]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Dict				      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*_A    )

					def 				__UpperCamelCase   (    self      :       str    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_A    )

					def 				__UpperCamelCase   (    self      :       List[str]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_A    )

					def 				__UpperCamelCase   (    self      :       Optional[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_A    )

					@slow
					def 				__UpperCamelCase   (    self      :       Optional[int]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    self.model_tester.prepare_config_and_inputs_for_common()
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    True
							SCREAMING_SNAKE_CASE_  :		Any				      =    True

							if hasattr(_A,"use_cache"    ):
									SCREAMING_SNAKE_CASE_  :		List[Any]				      =    True

							SCREAMING_SNAKE_CASE_  :		int				      =    getattr(self.model_tester,"encoder_seq_length",self.model_tester.seq_length    )
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    getattr(self.model_tester,"key_length",_A    )

							for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE_  :		List[str]				      =    self._prepare_for_class(_A,_A    )
									SCREAMING_SNAKE_CASE_  :		List[Any]				      =    model_class(_A    )
									SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    len(model(_A    )    )

									with tempfile.TemporaryDirectory() as tmpdirname:
											model.save_pretrained(_A,saved_model=_A    )
											SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    os.path.join(_A,"saved_model","1"    )
											SCREAMING_SNAKE_CASE_  :		Tuple				      =    tf.keras.models.load_model(_A    )
											SCREAMING_SNAKE_CASE_  :		str				      =    model(_A    )

											if self.is_encoder_decoder:
													SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    outputs["encoder_hidden_states"]
													SCREAMING_SNAKE_CASE_  :		str				      =    outputs["encoder_attentions"]
											else:
													SCREAMING_SNAKE_CASE_  :		Any				      =    outputs["hidden_states"]
													SCREAMING_SNAKE_CASE_  :		List[str]				      =    outputs["attentions"]

											self.assertEqual(len(_A    ),_A    )

											SCREAMING_SNAKE_CASE_  :		Any				      =    getattr(
											    self.model_tester,"expected_num_hidden_layers",self.model_tester.num_hidden_layers + 1    )

											self.assertEqual(len(_A    ),_A    )
											self.assertListEqual(
											    list(output_hidden_states[0].shape[-2:]    ),[self.model_tester.seq_length, self.model_tester.hidden_size],)

											self.assertEqual(len(_A    ),self.model_tester.num_hidden_layers    )
											self.assertListEqual(
											    list(output_attentions[0].shape[-3:]    ),[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length],)

					@slow
					def 				__UpperCamelCase   (    self      :       List[Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		str				      =    TFConvBertModel.from_pretrained("YituTech/conv-bert-base"    )
							self.assertIsNotNone(_A    )






					def 				__UpperCamelCase   (    self      :       Tuple    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Any				      =    self.model_tester.prepare_config_and_inputs_for_common()
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    True
							SCREAMING_SNAKE_CASE_  :		List[str]				      =    getattr(self.model_tester,"decoder_seq_length",self.model_tester.seq_length    )
							SCREAMING_SNAKE_CASE_  :		Any				      =    getattr(self.model_tester,"encoder_seq_length",self.model_tester.seq_length    )
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    getattr(self.model_tester,"key_length",_A    )
							SCREAMING_SNAKE_CASE_  :		int				      =    getattr(self.model_tester,"key_length",_A    )

							def check_decoder_attentions_output(_A      :       Dict    ):
									SCREAMING_SNAKE_CASE_  :		int				      =    len(_A    )
									self.assertEqual(out_len % 2,0    )
									SCREAMING_SNAKE_CASE_  :		Tuple				      =    outputs.decoder_attentions
									self.assertEqual(len(_A    ),self.model_tester.num_hidden_layers    )
									self.assertListEqual(
									    list(decoder_attentions[0].shape[-3:]    ),[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length],)

							def check_encoder_attentions_output(_A      :       Tuple    ):
									SCREAMING_SNAKE_CASE_  :		int				      =    [
									    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
									]
									self.assertEqual(len(_A    ),self.model_tester.num_hidden_layers    )
									self.assertListEqual(
									    list(attentions[0].shape[-3:]    ),[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length],)

							for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    True
									SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    False
									SCREAMING_SNAKE_CASE_  :		Tuple				      =    model_class(_A    )
									SCREAMING_SNAKE_CASE_  :		Any				      =    model(self._prepare_for_class(_A,_A    )    )
									SCREAMING_SNAKE_CASE_  :		Tuple				      =    len(_A    )
									self.assertEqual(config.output_hidden_states,_A    )
									check_encoder_attentions_output(_A    )

									if self.is_encoder_decoder:
											SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    model_class(_A    )
											SCREAMING_SNAKE_CASE_  :		int				      =    model(self._prepare_for_class(_A,_A    )    )
											self.assertEqual(config.output_hidden_states,_A    )
											check_decoder_attentions_output(_A    )

									# Check that output attentions can also be changed via the config
									del inputs_dict["output_attentions"]
									SCREAMING_SNAKE_CASE_  :		str				      =    True
									SCREAMING_SNAKE_CASE_  :		int				      =    model_class(_A    )
									SCREAMING_SNAKE_CASE_  :		List[str]				      =    model(self._prepare_for_class(_A,_A    )    )
									self.assertEqual(config.output_hidden_states,_A    )
									check_encoder_attentions_output(_A    )

									# Check attention is always last and order is fine
									SCREAMING_SNAKE_CASE_  :		str				      =    True
									SCREAMING_SNAKE_CASE_  :		int				      =    True
									SCREAMING_SNAKE_CASE_  :		Dict				      =    model_class(_A    )
									SCREAMING_SNAKE_CASE_  :		str				      =    model(self._prepare_for_class(_A,_A    )    )

									self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1),len(_A    )    )
									self.assertEqual(model.config.output_hidden_states,_A    )
									check_encoder_attentions_output(_A    )







@require_tf
class 		a__						(					unittest.TestCase					):

					@slow
					def 				__UpperCamelCase   (    self      :       Union[str, Any]    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]				      =    TFConvBertModel.from_pretrained("YituTech/conv-bert-base"    )
							SCREAMING_SNAKE_CASE_  :		int				      =    tf.constant([[0, 1, 2, 3, 4, 5]]    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    model(_A    )[0]

							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    [1, 6, 768]
							self.assertEqual(output.shape,_A    )

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    tf.constant(
							    [
							        [
							            [-0.03475493, -0.4686034, -0.30638832],
							            [0.22637248, -0.26988646, -0.7423424],
							            [0.10324868, -0.45013508, -0.58280784],
							        ]
							    ]    )
							tf.debugging.assert_near(output[:, :3, :3],_A,atol=1E-4    )
