






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     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	def __init__(		self				:					List[str] ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					Any=13 ,  __lowerCamelCase				:					Dict=3 ,  __lowerCamelCase				:					int=224 ,  __lowerCamelCase				:					Any=30 ,  __lowerCamelCase				:					Tuple=400 ,  __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] ,  ):
		UpperCamelCase     :List[Any]          =	size if size is not None else {"""height""": 18, """width""": 18}
		UpperCamelCase     :str          =	parent
		UpperCamelCase     :Optional[int]          =	batch_size
		UpperCamelCase     :Dict          =	num_channels
		UpperCamelCase     :str          =	image_size
		UpperCamelCase     :Dict          =	min_resolution
		UpperCamelCase     :str          =	max_resolution
		UpperCamelCase     :Union[str, Any]          =	do_resize
		UpperCamelCase     :Optional[Any]          =	size
		UpperCamelCase     :Any          =	do_normalize
		UpperCamelCase     :Optional[Any]          =	image_mean
		UpperCamelCase     :Tuple          =	image_std





	def 						_A (		self				:					int	):
		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     _SCREAMING_SNAKE_CASE       (			_a		,   unittest.TestCase      ):
	snake_case__							:     List[Any]     	=  ViTImageProcessor if is_vision_available() else None





	def 						_A (		self				:					str	):
		UpperCamelCase     :Tuple          =	EfficientFormerImageProcessorTester(self	)





	@property
	def 						_A (		self				:					List[str]	):
		return self.image_proc_tester.prepare_image_processor_dict()





	def 						_A (		self				:					int	):
		UpperCamelCase     :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 						_A (		self				:					Optional[int]	):
		pass





	def 						_A (		self				:					str	):
		# Initialize image_processor
		UpperCamelCase     :Union[str, Any]          =	self.image_processing_class(**self.image_processor_dict	)
		# create random PIL images
		UpperCamelCase     :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
		UpperCamelCase     :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
		UpperCamelCase     :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 						_A (		self				:					Union[str, Any]	):
		# Initialize image_processor
		UpperCamelCase     :Optional[int]          =	self.image_processing_class(**self.image_processor_dict	)
		# create random numpy tensors
		UpperCamelCase     :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
		UpperCamelCase     :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
		UpperCamelCase     :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 						_A (		self				:					List[Any]	):
		# Initialize image_processor
		UpperCamelCase     :List[str]          =	self.image_processing_class(**self.image_processor_dict	)
		# create random PyTorch tensors
		UpperCamelCase     :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
		UpperCamelCase     :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
		UpperCamelCase     :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 os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin

class     _SCREAMING_SNAKE_CASE       (			_a		,   unittest.TestCase      ):
	snake_case__							:     Optional[Any]     	=  TransfoXLTokenizer
	snake_case__							:     List[Any]     	=  False
	snake_case__							:     Tuple     	=  False





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

		UpperCamelCase     :Dict          =	[
		    """<unk>""",
		    """[CLS]""",
		    """[SEP]""",
		    """want""",
		    """unwanted""",
		    """wa""",
		    """un""",
		    """running""",
		    """,""",
		    """low""",
		    """l""",
		]
		UpperCamelCase     :str          =	os.path.join(self.tmpdirname ,  VOCAB_FILES_NAMES["""vocab_file"""]	)
		with open(self.vocab_file ,  """w""" ,  encoding="""utf-8"""	) as vocab_writer:
			vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]	)	)





	def 						_A (		self				:					List[str] ,  **__lowerCamelCase				:					Any	):
		UpperCamelCase     :Any          =	True
		return TransfoXLTokenizer.from_pretrained(self.tmpdirname ,  **__lowerCamelCase	)





	def 						_A (		self				:					Any ,  __lowerCamelCase				:					int	):
		UpperCamelCase     :List[Any]          =	"""<unk> UNwanted , running"""
		UpperCamelCase     :int          =	"""<unk> unwanted, running"""
		return input_text, output_text





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :List[str]          =	TransfoXLTokenizer(vocab_file=self.vocab_file ,  lower_case=__lowerCamelCase	)

		UpperCamelCase     :Any          =	tokenizer.tokenize("""<unk> UNwanted , running"""	)
		self.assertListEqual(__lowerCamelCase ,  ["""<unk>""", """unwanted""", """,""", """running"""]	)

		self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowerCamelCase	) ,  [0, 4, 8, 7]	)





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :List[Any]          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

		self.assertListEqual(
		    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """	) ,  ["""hello""", """!""", """how""", """are""", """you""", """?"""]	)





	def 						_A (		self				:					Union[str, Any]	):
		UpperCamelCase     :int          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)

		self.assertListEqual(
		    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """	) ,  ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]	)





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :Any          =	TransfoXLTokenizer(lower_case=__lowerCamelCase	)
		UpperCamelCase     :Optional[int]          =	"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
		UpperCamelCase     :Optional[int]          =	[
		    """Hello""",
		    """(""",
		    """bracket""",
		    """)""",
		    """and""",
		    """side""",
		    """@-@""",
		    """scrolled""",
		    """[""",
		    """and""",
		    """]""",
		    """Henry""",
		    """'s""",
		    """$""",
		    """5""",
		    """@,@""",
		    """000""",
		    """with""",
		    """3""",
		    """@.@""",
		    """34""",
		    """m""",
		    """.""",
		    """What""",
		    """'s""",
		    """up""",
		    """!""",
		    """?""",
		]

		self.assertListEqual(tokenizer.tokenize(__lowerCamelCase	) ,  __lowerCamelCase	)

		self.assertEqual(tokenizer.convert_tokens_to_string(__lowerCamelCase	) ,  __lowerCamelCase	)





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :Any          =	self.get_tokenizer()
		UpperCamelCase     :List[str]          =	len(__lowerCamelCase	)

		tokenizer.add_tokens(["""new1""", """new2"""]	)
		tokenizer.move_added_token("""new1""" ,  1	)

		# Check that moved token is not copied (duplicate)
		self.assertEqual(len(__lowerCamelCase	) ,  original_len + 2	)
		# Check that token is moved to specified id
		self.assertEqual(tokenizer.encode("""new1"""	) ,  [1]	)
		self.assertEqual(tokenizer.decode([1]	) ,  """new1"""	)
