



'''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


UpperCamelCase__:     Optional[int]										=    ["bert-base-uncased", "bert-base-cased"]
UpperCamelCase__:     Any										=    "hf-internal-testing/tiny-bert-tf-only"

if is_tf_available():

						class        SCREAMING_SNAKE_CASE(							tf.keras.Model    ):







							"""simple docstring"""






							def __init__(		self    :  List[Any]	,    __snake_case    :  str	)    ->	Tuple:
														super().__init__()
														UpperCAmelCase				: Optional[Any]											=	tokenizer
														UpperCAmelCase				: List[Any]											=	AutoConfig.from_pretrained(__snake_case	)
														UpperCAmelCase				: Union[str, Any]											=	TFAutoModel.from_config(__snake_case	)







							def   A			(		self    :  List[str]	,    __snake_case    :  Dict	)    ->	Optional[int]:
														UpperCAmelCase				: List[Any]											=	self.tokenizer(__snake_case	)
														UpperCAmelCase				: Tuple											=	self.bert(**__snake_case	)
														return out["pooler_output"]

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







	"""simple docstring"""






	def   A			(		self    :  Any	)    ->	Tuple:
								super().setUp()

								UpperCAmelCase				: List[Any]											=	[
								    BertTokenizer.from_pretrained(__snake_case	) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
								]  # repeat for when fast_bert_tokenizer=false
								UpperCAmelCase				: List[Any]											=	[TFBertTokenizer.from_pretrained(__snake_case	) for checkpoint in TOKENIZER_CHECKPOINTS] + [
								    TFBertTokenizer.from_pretrained(__snake_case	,    use_fast_bert_tokenizer=__snake_case	)
								    for checkpoint in TOKENIZER_CHECKPOINTS
								]
								assert len(self.tokenizers	) == len(self.tf_tokenizers	)

								UpperCAmelCase				: Any											=	[
								    '''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ċ, ꝼ''',
								]
								UpperCAmelCase				: Tuple											=	list(zip(self.test_sentences	,    self.test_sentences[::-1]	)	)







	def   A			(		self    :  Tuple	)    ->	Dict:
								for tokenizer, tf_tokenizer in zip(self.tokenizers	,    self.tf_tokenizers	):
															for test_inputs in (self.test_sentences, self.paired_sentences):
																						UpperCAmelCase				: Any											=	tokenizer(__snake_case	,    return_tensors='''tf'''	,    padding='''longest'''	)
																						UpperCAmelCase				: Optional[Any]											=	tf_tokenizer(__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   A			(		self    :  List[Any]	)    ->	Tuple:
								for tf_tokenizer in self.tf_tokenizers:
															UpperCAmelCase				: Any											=	tf_tokenizer(self.paired_sentences	)
															UpperCAmelCase				: Union[str, Any]											=	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   A			(		self    :  int	)    ->	int:
								for tf_tokenizer in self.tf_tokenizers:
															UpperCAmelCase				: Tuple											=	tf.function(__snake_case	)
															for test_inputs in (self.test_sentences, self.paired_sentences):
																						UpperCAmelCase				: Optional[Any]											=	tf.constant(__snake_case	)
																						UpperCAmelCase				: str											=	compiled_tokenizer(__snake_case	)
																						UpperCAmelCase				: List[Any]											=	tf_tokenizer(__snake_case	)

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







	@slow
	def   A			(		self    :  Union[str, Any]	)    ->	List[str]:
								for tf_tokenizer in self.tf_tokenizers:
															UpperCAmelCase				: Union[str, Any]											=	ModelToSave(tokenizer=__snake_case	)
															UpperCAmelCase				: Dict											=	tf.convert_to_tensor(self.test_sentences	)
															UpperCAmelCase				: Any											=	model(__snake_case	)  # Build model with some sample inputs
															with TemporaryDirectory() as tempdir:
																						UpperCAmelCase				: Union[str, Any]											=	Path(__snake_case	) / '''saved.model'''
																						model.save(__snake_case	)
																						UpperCAmelCase				: List[Any]											=	tf.keras.models.load_model(__snake_case	)
															UpperCAmelCase				: Tuple											=	loaded_model(__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 math

import tensorflow as tf
from packaging import version


def 				snake_case_  (      _lowerCAmelCase						:		List[str]						)   ->     Dict:
							UpperCAmelCase				: Tuple											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Dict											=	0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)     ,   x.dtype						)						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		int						)   ->     List[Any]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(math.pi     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: List[Any]											=	0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(_lowerCAmelCase     ,   3						))						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Any:
							UpperCAmelCase				: List[Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)

							return x * tf.tanh(tf.math.softplus(_lowerCAmelCase						)						)


def 				snake_case_  (      _lowerCAmelCase						:		str						)   ->     Tuple:
							UpperCAmelCase				: Union[str, Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.7_9_7_8_8_4_5_6_0_8     ,   x.dtype						)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Optional[int]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Optional[int]											=	tf.cast(1.7_0_2     ,   x.dtype						)
							return x * tf.math.sigmoid(coeff * x						)


def 				snake_case_  (      _lowerCAmelCase						:		Any						)   ->     Any:
							return tf.clip_by_value(_gelu(_lowerCAmelCase						)     ,   -10     ,   10						)


def 				snake_case_  (      _lowerCAmelCase						:		Dict     ,   _lowerCAmelCase						:		Optional[Any]=-1						)   ->     Union[str, Any]:
							UpperCAmelCase							,	UpperCAmelCase				: Optional[Any]											=	tf.split(_lowerCAmelCase     ,   2     ,   axis=_lowerCAmelCase						)
							return a * tf.math.sigmoid(_lowerCAmelCase						)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):


						def 				snake_case_  (      _lowerCAmelCase						:		Tuple						)   ->     Any:
													return tf.keras.activations.gelu(_lowerCAmelCase     ,   approximate=_lowerCAmelCase						)

						UpperCamelCase__:     List[Any]										=    tf.keras.activations.gelu




						UpperCamelCase__:     List[Any]										=    approximate_gelu_wrap
else:
						UpperCamelCase__:     List[str]										=    _gelu
						UpperCamelCase__:     Tuple										=    _gelu_new


UpperCamelCase__:     List[Any]										=    {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}




def 				snake_case_  (      _lowerCAmelCase						:		Dict						)   ->     List[Any]:
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(f"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"""						)





