





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


A 			=  ['''bert-base-uncased''', '''bert-base-cased''']
A 			=  '''hf-internal-testing/tiny-bert-tf-only'''

if is_tf_available():




		class 				__lowercase			(			tf.keras.Model							):







							'''simple docstring'''




							def __init__(						self	,			_UpperCAmelCase     ):
											super().__init__()
											__a :						Any			       =  tokenizer
											__a :						Optional[Any]			       =  AutoConfig.from_pretrained(_UpperCAmelCase     )
											__a :						str			       =  TFAutoModel.from_config(_UpperCAmelCase     )







							def  _lowerCamelCase		(						self	,			_UpperCAmelCase     ):
											__a :						Any			       =  self.tokenizer(_UpperCAmelCase     )
											__a :						int			       =  self.bert(**_UpperCAmelCase     )
											return out["pooler_output"]




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







					'''simple docstring'''




					def  _lowerCamelCase		(						self     ):
									super().setUp()

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

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







					def  _lowerCamelCase		(						self     ):
									for tokenizer, tf_tokenizer in zip(self.tokenizers	,			self.tf_tokenizers     ):
													for test_inputs in (self.test_sentences, self.paired_sentences):
																	__a :						List[Any]			       =  tokenizer(_UpperCAmelCase	,			return_tensors='''tf'''	,			padding='''longest'''     )
																	__a :						List[str]			       =  tf_tokenizer(_UpperCAmelCase     )

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

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







					@slow
					def  _lowerCamelCase		(						self     ):
									for tf_tokenizer in self.tf_tokenizers:
													__a :						Dict			       =  ModelToSave(tokenizer=_UpperCAmelCase     )
													__a :						Tuple			       =  tf.convert_to_tensor(self.test_sentences     )
													__a :						List[str]			       =  model(_UpperCAmelCase     )  # Build model with some sample inputs
													with TemporaryDirectory() as tempdir:
																	__a :						Tuple			       =  Path(_UpperCAmelCase     ) / '''saved.model'''
																	model.save(_UpperCAmelCase     )
																	__a :						Tuple			       =  tf.keras.models.load_model(_UpperCAmelCase     )
													__a :						int			       =  loaded_model(_UpperCAmelCase     )
													# 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 copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A 			=  logging.get_logger(__name__)

A 			=  {
    '''microsoft/git-base''': '''https://huggingface.co/microsoft/git-base/resolve/main/config.json''',
}




class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  '''git_vision_model'''







					def __init__(						self	,			_UpperCAmelCase=768	,			_UpperCAmelCase=3072	,			_UpperCAmelCase=12	,			_UpperCAmelCase=12	,			_UpperCAmelCase=3	,			_UpperCAmelCase=224	,			_UpperCAmelCase=16	,			_UpperCAmelCase="quick_gelu"	,			_UpperCAmelCase=1e-5	,			_UpperCAmelCase=0.0	,			_UpperCAmelCase=0.0_2	,			**_UpperCAmelCase	,			):
									super().__init__(**_UpperCAmelCase     )

									__a :						Union[str, Any]			       =  hidden_size
									__a :						Optional[int]			       =  intermediate_size
									__a :						Tuple			       =  num_hidden_layers
									__a :						Tuple			       =  num_attention_heads
									__a :						Optional[int]			       =  num_channels
									__a :						Tuple			       =  patch_size
									__a :						Any			       =  image_size
									__a :						Optional[Any]			       =  initializer_range
									__a :						Optional[int]			       =  attention_dropout
									__a :						Any			       =  layer_norm_eps
									__a :						Dict			       =  hidden_act







					@classmethod
					def  _lowerCamelCase		(						cls	,			_UpperCAmelCase	,			**_UpperCAmelCase     ):
									cls._set_token_in_kwargs(_UpperCAmelCase     )

									__a       ,						__a :						Dict			       =  cls.get_config_dict(_UpperCAmelCase	,			**_UpperCAmelCase     )

									# get the vision config dict if we are loading from GITConfig
									if config_dict.get('''model_type'''     ) == "git":
													__a :						int			       =  config_dict['''vision_config''']

									if "model_type" in config_dict and hasattr(cls	,			'''model_type'''     ) and config_dict["model_type"] != cls.model_type:
													logger.warning(
													    f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
													    f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""     )

									return cls.from_dict(_UpperCAmelCase	,			**_UpperCAmelCase     )






class 				__lowercase			(			_UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase  =  '''git'''







					def __init__(						self	,			_UpperCAmelCase=None	,			_UpperCAmelCase=30522	,			_UpperCAmelCase=768	,			_UpperCAmelCase=6	,			_UpperCAmelCase=12	,			_UpperCAmelCase=3072	,			_UpperCAmelCase="gelu"	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=0.1	,			_UpperCAmelCase=1024	,			_UpperCAmelCase=0.0_2	,			_UpperCAmelCase=1e-1_2	,			_UpperCAmelCase=0	,			_UpperCAmelCase="absolute"	,			_UpperCAmelCase=True	,			_UpperCAmelCase=False	,			_UpperCAmelCase=101	,			_UpperCAmelCase=102	,			_UpperCAmelCase=None	,			**_UpperCAmelCase	,			):
									super().__init__(bos_token_id=_UpperCAmelCase	,			eos_token_id=_UpperCAmelCase	,			pad_token_id=_UpperCAmelCase	,			**_UpperCAmelCase     )

									if vision_config is None:
													__a :						Union[str, Any]			       =  {}
													logger.info('''vision_config is None. initializing the GitVisionConfig with default values.'''     )

									__a :						Union[str, Any]			       =  GitVisionConfig(**_UpperCAmelCase     )
									__a :						List[str]			       =  vocab_size
									__a :						Any			       =  hidden_size
									__a :						Dict			       =  num_hidden_layers
									__a :						Tuple			       =  num_attention_heads
									__a :						List[str]			       =  hidden_act
									__a :						Any			       =  intermediate_size
									__a :						int			       =  hidden_dropout_prob
									__a :						Dict			       =  attention_probs_dropout_prob
									__a :						Tuple			       =  max_position_embeddings
									__a :						int			       =  initializer_range
									__a :						List[str]			       =  layer_norm_eps
									__a :						Tuple			       =  position_embedding_type
									__a :						Optional[int]			       =  use_cache
									__a :						str			       =  tie_word_embeddings
									__a :						Optional[Any]			       =  num_image_with_embedding

									__a :						str			       =  bos_token_id
									__a :						Dict			       =  eos_token_id







					def  _lowerCamelCase		(						self     ):
									__a :						Optional[int]			       =  copy.deepcopy(self.__dict__     )
									__a :						int			       =  self.vision_config.to_dict()
									__a :						Tuple			       =  self.__class__.model_type
									return output