



"""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_    					=	["bert-base-uncased", "bert-base-cased"]
snake_case_    					=	"hf-internal-testing/tiny-bert-tf-only"

if is_tf_available():







				class 				A_      (					tf.keras.Model    ):



										"""simple docstring"""






										def __init__( self			:str       ,  lowercase_			:str )    ->      Dict:
																super().__init__()
																UpperCAmelCase					=			tokenizer
																UpperCAmelCase					=			AutoConfig.from_pretrained(__A )
																UpperCAmelCase					=			TFAutoModel.from_config(__A )






										def      UpperCAmelCase__					( self			:Any       ,  lowercase_			:Optional[int] )    ->      Optional[int]:
																UpperCAmelCase					=			self.tokenizer(__A )
																UpperCAmelCase					=			self.bert(**__A )
																return out["pooler_output"]







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



						"""simple docstring"""






						def      UpperCAmelCase__					( self			:Optional[int] )    ->      str:
												super().setUp()

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

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






						def      UpperCAmelCase__					( self			:Optional[Any] )    ->      List[str]:
												for tokenizer, tf_tokenizer in zip(self.tokenizers       ,  self.tf_tokenizers ):
																		for test_inputs in (self.test_sentences, self.paired_sentences):
																								UpperCAmelCase					=			tokenizer(__A       ,  return_tensors='tf'       ,  padding='longest' )
																								UpperCAmelCase					=			tf_tokenizer(__A )

																								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      UpperCAmelCase__					( self			:str )    ->      Any:
												for tf_tokenizer in self.tf_tokenizers:
																		UpperCAmelCase					=			tf_tokenizer(self.paired_sentences )
																		UpperCAmelCase					=			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      UpperCAmelCase__					( self			:Union[str, Any] )    ->      Dict:
												for tf_tokenizer in self.tf_tokenizers:
																		UpperCAmelCase					=			tf.function(__A )
																		for test_inputs in (self.test_sentences, self.paired_sentences):
																								UpperCAmelCase					=			tf.constant(__A )
																								UpperCAmelCase					=			compiled_tokenizer(__A )
																								UpperCAmelCase					=			tf_tokenizer(__A )

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






						@slow
						def      UpperCAmelCase__					( self			:Tuple )    ->      Optional[Any]:
												for tf_tokenizer in self.tf_tokenizers:
																		UpperCAmelCase					=			ModelToSave(tokenizer=__A )
																		UpperCAmelCase					=			tf.convert_to_tensor(self.test_sentences )
																		UpperCAmelCase					=			model(__A )  # Build model with some sample inputs
																		with TemporaryDirectory() as tempdir:
																								UpperCAmelCase					=			Path(__A ) / '''saved.model'''
																								model.save(__A )
																								UpperCAmelCase					=			tf.keras.models.load_model(__A )
																		UpperCAmelCase					=			loaded_model(__A )
																		# 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 operator as op


snake_case_    					=	"""scaler.pt"""
snake_case_    					=	"""pytorch_model"""
snake_case_    					=	"""random_states"""
snake_case_    					=	"""optimizer"""
snake_case_    					=	"""scheduler"""
snake_case_    					=	"""pytorch_model.bin"""
snake_case_    					=	"""pytorch_model.bin.index.json"""
snake_case_    					=	"""model.safetensors"""
snake_case_    					=	"""model.safetensors.index.json"""
snake_case_    					=	"""1.10.2"""
snake_case_    					=	"""py38"""
snake_case_    					=	"""4.17.0"""
snake_case_    					=	["""ml.p3.16xlarge""", """ml.p3dn.24xlarge""", """ml.p4dn.24xlarge"""]
snake_case_    					=	["""FULL_SHARD""", """SHARD_GRAD_OP""", """NO_SHARD""", """HYBRID_SHARD""", """HYBRID_SHARD_ZERO2"""]
snake_case_    					=	["""TRANSFORMER_BASED_WRAP""", """SIZE_BASED_WRAP""", """NO_WRAP"""]
snake_case_    					=	["""BACKWARD_PRE""", """BACKWARD_POST""", """NO_PREFETCH"""]
snake_case_    					=	["""FULL_STATE_DICT""", """LOCAL_STATE_DICT""", """SHARDED_STATE_DICT"""]
snake_case_    					=	"""2.0.1"""
snake_case_    					=	["""pdsh""", """standard""", """openmpi""", """mvapich"""]
snake_case_    					=	["""default""", """reduce-overhead""", """max-autotune"""]

snake_case_    					=	{""">""": op.gt, """>=""": op.ge, """==""": op.eq, """!=""": op.ne, """<=""": op.le, """<""": op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
snake_case_    					=	[
    """nnodes""",
    """nproc_per_node""",
    """rdzv_backend""",
    """rdzv_endpoint""",
    """rdzv_id""",
    """rdzv_conf""",
    """standalone""",
    """max_restarts""",
    """monitor_interval""",
    """start_method""",
    """role""",
    """module""",
    """m""",
    """no_python""",
    """run_path""",
    """log_dir""",
    """r""",
    """redirects""",
    """t""",
    """tee""",
    """node_rank""",
    """master_addr""",
    """master_port""",
]

snake_case_    					=	["""DEEPSPEED""", """MULTI_GPU""", """FSDP""", """MEGATRON_LM"""]
snake_case_    					=	["""DEEPSPEED""", """MULTI_XPU""", """FSDP"""]





