



"""simple docstring"""






import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
	import tensorflow as tf

if is_tensorflow_text_available():
	from transformers.models.bert import TFBertTokenizer


__snake_case							:     Optional[int]   =					['bert-base-uncased', 'bert-base-cased']
__snake_case							:     str   =					'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():



	class     A__     (						tf.keras.Model							):




		'''simple docstring'''





		def __init__(  self:	List[Any] ,    _SCREAMING_SNAKE_CASE:	List[Any])       -> int:

					"""simple docstring"""

					super().__init__()
					__lowerCAmelCase   :						Optional[int]				= tokenizer
					__lowerCAmelCase   :						Optional[Any]				= AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE)
					__lowerCAmelCase   :						Optional[Any]				= TFAutoModel.from_config(_SCREAMING_SNAKE_CASE)







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

					"""simple docstring"""

					__lowerCAmelCase   :						str				= self.tokenizer(_SCREAMING_SNAKE_CASE)
					__lowerCAmelCase   :						Tuple				= self.bert(**_SCREAMING_SNAKE_CASE)
					return out["pooler_output"]



@require_tf
@require_tensorflow_text
class     A__     (						unittest.TestCase							):




	'''simple docstring'''





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

				"""simple docstring"""

				super().setUp()

				__lowerCAmelCase   :						Tuple				= [
				    BertTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
				]  # repeat for when fast_bert_tokenizer=false
				__lowerCAmelCase   :						str				= [TFBertTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE) for checkpoint in TOKENIZER_CHECKPOINTS] + [
				    TFBertTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE ,    use_fast_bert_tokenizer=_SCREAMING_SNAKE_CASE)
				    for checkpoint in TOKENIZER_CHECKPOINTS
				]
				assert len(self.tokenizers) == len(self.tf_tokenizers)

				__lowerCAmelCase   :						Tuple				= [
				    "This is a straightforward English test sentence.",
				    "This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.",
				    "Now we're going to add some Chinese: 一 二 三 一二三",
				    "And some much more rare Chinese: 齉 堃 齉堃",
				    "Je vais aussi écrire en français pour tester les accents",
				    "Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ",
				]
				__lowerCAmelCase   :						List[Any]				= list(zip(self.test_sentences ,    self.test_sentences[::-1]))

	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any])       -> Any:

				"""simple docstring"""

				for tokenizer, tf_tokenizer in zip(self.tokenizers ,    self.tf_tokenizers):
							for test_inputs in (self.test_sentences, self.paired_sentences):
										__lowerCAmelCase   :						Dict				= tokenizer(_SCREAMING_SNAKE_CASE ,    return_tensors="tf" ,    padding="longest")
										__lowerCAmelCase   :						int				= tf_tokenizer(_SCREAMING_SNAKE_CASE)

										for key in python_outputs.keys():
													self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape))
													self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] ,    tf.intaa) == tf_outputs[key]))

	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	str)       -> Union[str, Any]:

				"""simple docstring"""

				for tf_tokenizer in self.tf_tokenizers:
							__lowerCAmelCase   :						List[str]				= tf_tokenizer(self.paired_sentences)
							__lowerCAmelCase   :						Tuple				= tf_tokenizer(
							    text=[sentence[0] for sentence in self.paired_sentences] ,    text_pair=[sentence[1] for sentence in self.paired_sentences] ,    )
							for key in merged_outputs.keys():
										self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] ,    tf.intaa) == separated_outputs[key]))

	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str])       -> int:

				"""simple docstring"""

				for tf_tokenizer in self.tf_tokenizers:
							__lowerCAmelCase   :						List[Any]				= tf.function(_SCREAMING_SNAKE_CASE)
							for test_inputs in (self.test_sentences, self.paired_sentences):
										__lowerCAmelCase   :						Dict				= tf.constant(_SCREAMING_SNAKE_CASE)
										__lowerCAmelCase   :						Dict				= compiled_tokenizer(_SCREAMING_SNAKE_CASE)
										__lowerCAmelCase   :						List[Any]				= tf_tokenizer(_SCREAMING_SNAKE_CASE)

										for key in eager_outputs.keys():
													self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]))







	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[int])       -> int:

				"""simple docstring"""

				for tf_tokenizer in self.tf_tokenizers:
							__lowerCAmelCase   :						Union[str, Any]				= ModelToSave(tokenizer=_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase   :						List[str]				= tf.convert_to_tensor(self.test_sentences)
							__lowerCAmelCase   :						List[str]				= model(_SCREAMING_SNAKE_CASE)  # Build model with some sample inputs
							with TemporaryDirectory() as tempdir:
										__lowerCAmelCase   :						Optional[int]				= Path(_SCREAMING_SNAKE_CASE) / "saved.model"
										model.save(_SCREAMING_SNAKE_CASE)
										__lowerCAmelCase   :						int				= tf.keras.models.load_model(_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase   :						Any				= loaded_model(_SCREAMING_SNAKE_CASE)
							# We may see small differences because the loaded model is compiled, so we need an epsilon for the test
							self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output)) ,    1e-5)




"""simple docstring"""






import logging

from transformers.configuration_utils import PretrainedConfig


__snake_case							:     Tuple   =					logging.getLogger(__name__)



class     A__     (						__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE					   =			'masked_bert'

	def __init__(  self:	str ,    _SCREAMING_SNAKE_CASE:	Tuple=3_0522 ,    _SCREAMING_SNAKE_CASE:	Optional[int]=768 ,    _SCREAMING_SNAKE_CASE:	List[str]=12 ,    _SCREAMING_SNAKE_CASE:	Any=12 ,    _SCREAMING_SNAKE_CASE:	Optional[int]=3072 ,    _SCREAMING_SNAKE_CASE:	List[str]="gelu" ,    _SCREAMING_SNAKE_CASE:	List[str]=0.1 ,    _SCREAMING_SNAKE_CASE:	str=0.1 ,    _SCREAMING_SNAKE_CASE:	Dict=512 ,    _SCREAMING_SNAKE_CASE:	List[Any]=2 ,    _SCREAMING_SNAKE_CASE:	Tuple=0.02 ,    _SCREAMING_SNAKE_CASE:	int=1e-12 ,    _SCREAMING_SNAKE_CASE:	int=0 ,    _SCREAMING_SNAKE_CASE:	Tuple="topK" ,    _SCREAMING_SNAKE_CASE:	Any="constant" ,    _SCREAMING_SNAKE_CASE:	int=0.0 ,    **_SCREAMING_SNAKE_CASE:	Any ,    )       -> List[str]:

				"""simple docstring"""

				super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE ,    **_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						Union[str, Any]				= vocab_size
				__lowerCAmelCase   :						List[str]				= hidden_size
				__lowerCAmelCase   :						Any				= num_hidden_layers
				__lowerCAmelCase   :						Dict				= num_attention_heads
				__lowerCAmelCase   :						Union[str, Any]				= hidden_act
				__lowerCAmelCase   :						Optional[Any]				= intermediate_size
				__lowerCAmelCase   :						int				= hidden_dropout_prob
				__lowerCAmelCase   :						List[Any]				= attention_probs_dropout_prob
				__lowerCAmelCase   :						int				= max_position_embeddings
				__lowerCAmelCase   :						str				= type_vocab_size
				__lowerCAmelCase   :						Any				= initializer_range
				__lowerCAmelCase   :						Optional[int]				= layer_norm_eps
				__lowerCAmelCase   :						Tuple				= pruning_method
				__lowerCAmelCase   :						Union[str, Any]				= mask_init
				__lowerCAmelCase   :						str				= mask_scale