






'''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    :      List[Any]  			=		['bert-base-uncased', 'bert-base-cased']
UpperCamelCase    :      List[str]  			=		'hf-internal-testing/tiny-bert-tf-only'

if is_tf_available():




							class 		UpperCamelCase__    (tf.keras.Model			):






									def __init__(      self   ,_lowerCAmelCase       ):
															super().__init__()
															lowerCamelCase__         =  tokenizer
															lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
															lowerCamelCase__         =  TFAutoModel.from_config(_lowerCAmelCase       )






									def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
															lowerCamelCase__         =  self.tokenizer(_lowerCAmelCase       )
															lowerCamelCase__         =  self.bert(**_lowerCAmelCase       )
															return out["pooler_output"]




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






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

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

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






		def 			UpperCamelCase_					(      self       ):
								for tokenizer, tf_tokenizer in zip(self.tokenizers   ,self.tf_tokenizers       ):
														for test_inputs in (self.test_sentences, self.paired_sentences):
																				lowerCamelCase__         =  tokenizer(_lowerCAmelCase   ,return_tensors="""tf"""   ,padding="""longest"""       )
																				lowerCamelCase__         =  tf_tokenizer(_lowerCAmelCase       )

																				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       ):
								for tf_tokenizer in self.tf_tokenizers:
														lowerCamelCase__         =  tf_tokenizer(self.paired_sentences       )
														lowerCamelCase__         =  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       ):
								for tf_tokenizer in self.tf_tokenizers:
														lowerCamelCase__         =  tf.function(_lowerCAmelCase       )
														for test_inputs in (self.test_sentences, self.paired_sentences):
																				lowerCamelCase__         =  tf.constant(_lowerCAmelCase       )
																				lowerCamelCase__         =  compiled_tokenizer(_lowerCAmelCase       )
																				lowerCamelCase__         =  tf_tokenizer(_lowerCAmelCase       )

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






		@slow
		def 			UpperCamelCase_					(      self       ):
								for tf_tokenizer in self.tf_tokenizers:
														lowerCamelCase__         =  ModelToSave(tokenizer=_lowerCAmelCase       )
														lowerCamelCase__         =  tf.convert_to_tensor(self.test_sentences       )
														lowerCamelCase__         =  model(_lowerCAmelCase       )  # Build model with some sample inputs
														with TemporaryDirectory() as tempdir:
																				lowerCamelCase__         =  Path(_lowerCAmelCase       ) / """saved.model"""
																				model.save(_lowerCAmelCase       )
																				lowerCamelCase__         =  tf.keras.models.load_model(_lowerCAmelCase       )
														lowerCamelCase__         =  loaded_model(_lowerCAmelCase       )
														# 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'''







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


UpperCamelCase    :      Tuple  			=		logging.get_logger(__name__)

UpperCamelCase    :      List[str]  			=		{
    'naver-clova-ix/donut-base': 'https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json',
    # See all Donut models at https://huggingface.co/models?filter=donut-swin
}




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					'donut-swin'

		_UpperCamelCase				  =					{
		    'num_attention_heads': 'num_heads',
		    'num_hidden_layers': 'num_layers',
		}






		def __init__(      self   ,_lowerCAmelCase=2_24   ,_lowerCAmelCase=4   ,_lowerCAmelCase=3   ,_lowerCAmelCase=96   ,_lowerCAmelCase=[2, 2, 6, 2]   ,_lowerCAmelCase=[3, 6, 12, 24]   ,_lowerCAmelCase=7   ,_lowerCAmelCase=4.0   ,_lowerCAmelCase=True   ,_lowerCAmelCase=0.0   ,_lowerCAmelCase=0.0   ,_lowerCAmelCase=0.1   ,_lowerCAmelCase="gelu"   ,_lowerCAmelCase=False   ,_lowerCAmelCase=0.02   ,_lowerCAmelCase=1E-5   ,**_lowerCAmelCase   ,):
								super().__init__(**_lowerCAmelCase       )

								lowerCamelCase__         =  image_size
								lowerCamelCase__         =  patch_size
								lowerCamelCase__         =  num_channels
								lowerCamelCase__         =  embed_dim
								lowerCamelCase__         =  depths
								lowerCamelCase__         =  len(_lowerCAmelCase       )
								lowerCamelCase__         =  num_heads
								lowerCamelCase__         =  window_size
								lowerCamelCase__         =  mlp_ratio
								lowerCamelCase__         =  qkv_bias
								lowerCamelCase__         =  hidden_dropout_prob
								lowerCamelCase__         =  attention_probs_dropout_prob
								lowerCamelCase__         =  drop_path_rate
								lowerCamelCase__         =  hidden_act
								lowerCamelCase__         =  use_absolute_embeddings
								lowerCamelCase__         =  layer_norm_eps
								lowerCamelCase__         =  initializer_range
								# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
								# this indicates the channel dimension after the last stage of the model
								lowerCamelCase__         =  int(embed_dim * 2 ** (len(_lowerCAmelCase       ) - 1)       )

