





'''simple docstring'''




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import ViTImageProcessor




class 			UpperCamelCase__ ( unittest.TestCase       ):




					"""simple docstring"""


					def __init__(		self							:       List[str]	,		lowerCamelCase_							:       Optional[Any]	,		lowerCamelCase_							:       Any=13	,		lowerCamelCase_							:       Dict=3	,		lowerCamelCase_							:       int=2_24	,		lowerCamelCase_							:       Any=30	,		lowerCamelCase_							:       Tuple=4_00	,		lowerCamelCase_							:       int=True	,		lowerCamelCase_							:       List[str]=None	,		lowerCamelCase_							:       Any=True	,		lowerCamelCase_							:       Dict=[0.5, 0.5, 0.5]	,		lowerCamelCase_							:       List[Any]=[0.5, 0.5, 0.5]	,		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							List[Any]						=       size if size is not None else {"""height""": 18, """width""": 18}
							SCREAMING_SNAKE_CASE      :							str						=       parent
							SCREAMING_SNAKE_CASE      :							Optional[int]						=       batch_size
							SCREAMING_SNAKE_CASE      :							Dict						=       num_channels
							SCREAMING_SNAKE_CASE      :							str						=       image_size
							SCREAMING_SNAKE_CASE      :							Dict						=       min_resolution
							SCREAMING_SNAKE_CASE      :							str						=       max_resolution
							SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       do_resize
							SCREAMING_SNAKE_CASE      :							Optional[Any]						=       size
							SCREAMING_SNAKE_CASE      :							Any						=       do_normalize
							SCREAMING_SNAKE_CASE      :							Optional[Any]						=       image_mean
							SCREAMING_SNAKE_CASE      :							Tuple						=       image_std



					def lowerCamelCase_       (		self							:       int		):

							'''simple docstring'''



							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							}







@require_torch
@require_vision
class 			UpperCamelCase__ ( _a		, unittest.TestCase       ):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__            =     ViTImageProcessor if is_vision_available() else None


					def lowerCamelCase_       (		self							:       str		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							Tuple						=       EfficientFormerImageProcessorTester(self		)


					@property
					def lowerCamelCase_       (		self							:       List[str]		):

							'''simple docstring'''



							return self.image_proc_tester.prepare_image_processor_dict()


					def lowerCamelCase_       (		self							:       int		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							List[Any]						=       self.image_processing_class(**self.image_processor_dict		)
							self.assertTrue(hasattr(__lowerCamelCase	,		"""image_mean"""		)		)
							self.assertTrue(hasattr(__lowerCamelCase	,		"""image_std"""		)		)
							self.assertTrue(hasattr(__lowerCamelCase	,		"""do_normalize"""		)		)
							self.assertTrue(hasattr(__lowerCamelCase	,		"""do_resize"""		)		)
							self.assertTrue(hasattr(__lowerCamelCase	,		"""size"""		)		)


					def lowerCamelCase_       (		self							:       Optional[int]		):

							'''simple docstring'''



							pass


					def lowerCamelCase_       (		self							:       str		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       self.image_processing_class(**self.image_processor_dict		)
							# create random PIL images
							SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       prepare_image_inputs(self.image_proc_tester	,		equal_resolution=__lowerCamelCase		)
							for image in image_inputs:
									self.assertIsInstance(__lowerCamelCase	,		Image.Image		)

							# Test not batched input
							SCREAMING_SNAKE_CASE      :							List[str]						=       image_processor(image_inputs[0]	,		return_tensors="""pt"""		).pixel_values
							self.assertEqual(
							    encoded_images.shape	,		(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size["""height"""],
							        self.image_proc_tester.size["""width"""],
							    )	,		)

							# Test batched
							SCREAMING_SNAKE_CASE      :							List[Any]						=       image_processor(__lowerCamelCase	,		return_tensors="""pt"""		).pixel_values
							self.assertEqual(
							    encoded_images.shape	,		(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size["""height"""],
							        self.image_proc_tester.size["""width"""],
							    )	,		)


					def lowerCamelCase_       (		self							:       Union[str, Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							Optional[int]						=       self.image_processing_class(**self.image_processor_dict		)
							# create random numpy tensors
							SCREAMING_SNAKE_CASE      :							List[Any]						=       prepare_image_inputs(self.image_proc_tester	,		equal_resolution=__lowerCamelCase	,		numpify=__lowerCamelCase		)
							for image in image_inputs:
									self.assertIsInstance(__lowerCamelCase	,		np.ndarray		)

							# Test not batched input
							SCREAMING_SNAKE_CASE      :							Dict						=       image_processor(image_inputs[0]	,		return_tensors="""pt"""		).pixel_values
							self.assertEqual(
							    encoded_images.shape	,		(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size["""height"""],
							        self.image_proc_tester.size["""width"""],
							    )	,		)

							# Test batched
							SCREAMING_SNAKE_CASE      :							Tuple						=       image_processor(__lowerCamelCase	,		return_tensors="""pt"""		).pixel_values
							self.assertEqual(
							    encoded_images.shape	,		(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size["""height"""],
							        self.image_proc_tester.size["""width"""],
							    )	,		)



					def lowerCamelCase_       (		self							:       List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE      :							List[str]						=       self.image_processing_class(**self.image_processor_dict		)
							# create random PyTorch tensors
							SCREAMING_SNAKE_CASE      :							Any						=       prepare_image_inputs(self.image_proc_tester	,		equal_resolution=__lowerCamelCase	,		torchify=__lowerCamelCase		)
							for image in image_inputs:
									self.assertIsInstance(__lowerCamelCase	,		torch.Tensor		)

							# Test not batched input
							SCREAMING_SNAKE_CASE      :							List[Any]						=       image_processor(image_inputs[0]	,		return_tensors="""pt"""		).pixel_values
							self.assertEqual(
							    encoded_images.shape	,		(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size["""height"""],
							        self.image_proc_tester.size["""width"""],
							    )	,		)

							# Test batched
							SCREAMING_SNAKE_CASE      :							str						=       image_processor(__lowerCamelCase	,		return_tensors="""pt"""		).pixel_values
							self.assertEqual(
							    encoded_images.shape	,		(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size["""height"""],
							        self.image_proc_tester.size["""width"""],
							    )	,		)











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

if is_tf_available():

	class     _SCREAMING_SNAKE_CASE       (			tf.keras.Model      ):





		def __init__(		self				:					List[str] ,  __lowerCamelCase				:					Union[str, Any]	):
			super().__init__()
			UpperCamelCase     :Any          =	tokenizer
			UpperCamelCase     :List[str]          =	AutoConfig.from_pretrained(__lowerCamelCase	)
			UpperCamelCase     :List[str]          =	TFAutoModel.from_config(__lowerCamelCase	)





		def 						_A (		self				:					Tuple ,  __lowerCamelCase				:					str	):
			UpperCamelCase     :str          =	self.tokenizer(__lowerCamelCase	)
			UpperCamelCase     :Any          =	self.bert(**__lowerCamelCase	)
			return out["pooler_output"]

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





	def 						_A (		self				:					Dict	):
		super().setUp()

		UpperCamelCase     :int          =	[
		    BertTokenizer.from_pretrained(__lowerCamelCase	) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
		]  # repeat for when fast_bert_tokenizer=false
		UpperCamelCase     :Any          =	[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	)

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





	def 						_A (		self				:					Optional[int]	):
		for tokenizer, tf_tokenizer in zip(self.tokenizers ,  self.tf_tokenizers	):
			for test_inputs in (self.test_sentences, self.paired_sentences):
				UpperCamelCase     :Any          =	tokenizer(__lowerCamelCase ,  return_tensors="""tf""" ,  padding="""longest"""	)
				UpperCamelCase     :str          =	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 						_A (		self				:					Dict	):
		for tf_tokenizer in self.tf_tokenizers:
			UpperCamelCase     :str          =	tf_tokenizer(self.paired_sentences	)
			UpperCamelCase     :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				:					List[str]	):
		for tf_tokenizer in self.tf_tokenizers:
			UpperCamelCase     :List[Any]          =	tf.function(__lowerCamelCase	)
			for test_inputs in (self.test_sentences, self.paired_sentences):
				UpperCamelCase     :Any          =	tf.constant(__lowerCamelCase	)
				UpperCamelCase     :List[str]          =	compiled_tokenizer(__lowerCamelCase	)
				UpperCamelCase     :Optional[Any]          =	tf_tokenizer(__lowerCamelCase	)

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





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