





import random
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():
	import PIL

	from transformers import FlavaImageProcessor
	from transformers.image_utils import PILImageResampling
	from transformers.models.flava.image_processing_flava import (
	    FLAVA_CODEBOOK_MEAN,
	    FLAVA_CODEBOOK_STD,
	    FLAVA_IMAGE_MEAN,
	    FLAVA_IMAGE_STD,
	)
else:
	__UpperCamelCase										=  __UpperCamelCase										=  __UpperCamelCase										=  __UpperCamelCase										=  None




class    _A   (				unittest.TestCase		):

		def __init__(      self				:      Union[str, Any]     ,			__magic_name__				:      Tuple     ,			__magic_name__				:      List[Any]=7     ,			__magic_name__				:      str=3     ,			__magic_name__				:      Any=30     ,			__magic_name__				:      Dict=4_00     ,			__magic_name__				:      int=True     ,			__magic_name__				:      Tuple=None     ,			__magic_name__				:      List[Any]=True     ,			__magic_name__				:      List[Any]=None     ,			__magic_name__				:      Any=None     ,			__magic_name__				:      Tuple=True     ,			__magic_name__				:      Any=1 / 2_55     ,			__magic_name__				:      Any=True     ,			__magic_name__				:      Optional[Any]=FLAVA_IMAGE_MEAN     ,			__magic_name__				:      Dict=FLAVA_IMAGE_STD     ,			__magic_name__				:      int=14     ,			__magic_name__				:      Union[str, Any]=75     ,			__magic_name__				:      Optional[Any]=None     ,			__magic_name__				:      str=16     ,			__magic_name__				:      Union[str, Any]=0.3     ,			__magic_name__				:      List[Any]=None     ,			__magic_name__				:      Dict=True     ,			__magic_name__				:      Any=None     ,			__magic_name__				:      Dict=None     ,			__magic_name__				:      List[str]=True     ,			__magic_name__				:      Any=None     ,			__magic_name__				:      str=True     ,			__magic_name__				:      List[str]=True     ,			__magic_name__				:      Any=FLAVA_CODEBOOK_MEAN     ,			__magic_name__				:      str=FLAVA_CODEBOOK_STD     ,			)				->						Any:


					"""simple docstring"""

					__snake_case				:			List[Any]       			=    size if size is not None else {"""height""": 2_24, """width""": 2_24}
					__snake_case				:			int       			=    crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
					__snake_case				:			int       			=    codebook_size if codebook_size is not None else {"""height""": 1_12, """width""": 1_12}
					__snake_case				:			Dict       			=    codebook_crop_size if codebook_crop_size is not None else {"""height""": 1_12, """width""": 1_12}

					__snake_case				:			Tuple       			=    parent
					__snake_case				:			Optional[int]       			=    batch_size
					__snake_case				:			Tuple       			=    num_channels
					__snake_case				:			List[Any]       			=    do_resize
					__snake_case				:			str       			=    do_rescale
					__snake_case				:			Dict       			=    rescale_factor
					__snake_case				:			Optional[Any]       			=    min_resolution
					__snake_case				:			Dict       			=    max_resolution
					__snake_case				:			Tuple       			=    size
					__snake_case				:			Optional[Any]       			=    resample if resample is not None else PILImageResampling.BICUBIC
					__snake_case				:			str       			=    do_normalize
					__snake_case				:			Dict       			=    image_mean
					__snake_case				:			List[Any]       			=    image_std
					__snake_case				:			Optional[Any]       			=    do_center_crop
					__snake_case				:			Tuple       			=    crop_size

					__snake_case				:			Dict       			=    input_size_patches
					__snake_case				:			Union[str, Any]       			=    total_mask_patches
					__snake_case				:			str       			=    mask_group_max_patches
					__snake_case				:			Optional[Any]       			=    mask_group_min_patches
					__snake_case				:			Tuple       			=    mask_group_min_aspect_ratio
					__snake_case				:			str       			=    mask_group_max_aspect_ratio

					__snake_case				:			Optional[int]       			=    codebook_do_resize
					__snake_case				:			Tuple       			=    codebook_size
					__snake_case				:			Tuple       			=    codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
					__snake_case				:			Dict       			=    codebook_do_center_crop
					__snake_case				:			List[str]       			=    codebook_crop_size
					__snake_case				:			Dict       			=    codebook_do_map_pixels
					__snake_case				:			str       			=    codebook_do_normalize
					__snake_case				:			Dict       			=    codebook_image_mean
					__snake_case				:			Tuple       			=    codebook_image_std

		def   lowercase__    (      self				:      Optional[int] )				->						Union[str, Any]:


					"""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,
					    "resample": self.resample,
					    "do_rescale": self.do_rescale,
					    "rescale_factor": self.rescale_factor,
					    "do_center_crop": self.do_center_crop,
					    "crop_size": self.crop_size,
					    "input_size_patches": self.input_size_patches,
					    "total_mask_patches": self.total_mask_patches,
					    "mask_group_max_patches": self.mask_group_max_patches,
					    "mask_group_min_patches": self.mask_group_min_patches,
					    "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
					    "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
					    "codebook_do_resize": self.codebook_do_resize,
					    "codebook_size": self.codebook_size,
					    "codebook_resample": self.codebook_resample,
					    "codebook_do_center_crop": self.codebook_do_center_crop,
					    "codebook_crop_size": self.codebook_crop_size,
					    "codebook_do_map_pixels": self.codebook_do_map_pixels,
					    "codebook_do_normalize": self.codebook_do_normalize,
					    "codebook_image_mean": self.codebook_image_mean,
					    "codebook_image_std": self.codebook_image_std,
					}

		def   lowercase__    (      self				:      Optional[int] )				->						Union[str, Any]:


					"""simple docstring"""

					return (self.size["height"], self.size["width"])

		def   lowercase__    (      self				:      str )				->						Union[str, Any]:


					"""simple docstring"""

					return (
					    (self.input_size_patches, self.input_size_patches)
					    if not isinstance(self.input_size_patches     ,			__magic_name__ )
					    else self.input_size_patches
					)

		def   lowercase__    (      self				:      List[str] )				->						Optional[int]:


					"""simple docstring"""

					return (self.codebook_size["height"], self.codebook_size["width"])





@require_torch
@require_vision
class    _A   (				__lowercase	,				unittest.TestCase		):
		lowercase__:      Optional[int]							    =      FlavaImageProcessor if is_vision_available() else None
		lowercase__:      str							    =      None

		def   lowercase__    (      self				:      List[Any] )				->						Optional[Any]:


					"""simple docstring"""

					__snake_case				:			Dict       			=    FlavaImageProcessingTester(self )

		@property
		def   lowercase__    (      self				:      List[Any] )				->						Any:


					"""simple docstring"""

					return self.image_processor_tester.prepare_image_processor_dict()

		def   lowercase__    (      self				:      Tuple )				->						Tuple:


					"""simple docstring"""

					__snake_case				:			List[str]       			=    self.image_processing_class(**self.image_processor_dict )
					self.assertTrue(hasattr(__magic_name__     ,			"""image_mean""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""image_std""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""do_normalize""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""do_resize""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""resample""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""crop_size""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""do_center_crop""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""do_rescale""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""rescale_factor""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""masking_generator""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_do_resize""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_size""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_resample""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_do_center_crop""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_crop_size""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_do_map_pixels""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_do_normalize""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_image_mean""" ) )
					self.assertTrue(hasattr(__magic_name__     ,			"""codebook_image_std""" ) )

		def   lowercase__    (      self				:      Optional[Any] )				->						str:


					"""simple docstring"""

					__snake_case				:			Optional[int]       			=    self.image_processing_class.from_dict(self.image_processor_dict )
					self.assertEqual(image_processor.size     ,			{"""height""": 2_24, """width""": 2_24} )
					self.assertEqual(image_processor.crop_size     ,			{"""height""": 2_24, """width""": 2_24} )
					self.assertEqual(image_processor.codebook_size     ,			{"""height""": 1_12, """width""": 1_12} )
					self.assertEqual(image_processor.codebook_crop_size     ,			{"""height""": 1_12, """width""": 1_12} )

					__snake_case				:			Optional[Any]       			=    self.image_processing_class.from_dict(
					    self.image_processor_dict     ,			size=42     ,			crop_size=84     ,			codebook_size=33     ,			codebook_crop_size=66 )
					self.assertEqual(image_processor.size     ,			{"""height""": 42, """width""": 42} )
					self.assertEqual(image_processor.crop_size     ,			{"""height""": 84, """width""": 84} )
					self.assertEqual(image_processor.codebook_size     ,			{"""height""": 33, """width""": 33} )
					self.assertEqual(image_processor.codebook_crop_size     ,			{"""height""": 66, """width""": 66} )

		def   lowercase__    (      self				:      Union[str, Any] )				->						List[Any]:


					"""simple docstring"""

					pass

		def   lowercase__    (      self				:      str )				->						Optional[Any]:


					"""simple docstring"""

					__snake_case				:			Any       			=    self.image_processing_class(**self.image_processor_dict )
					# create random PIL images
					__snake_case				:			Optional[int]       			=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__magic_name__ )
					for image in image_inputs:
								self.assertIsInstance(__magic_name__     ,			PIL.Image.Image )

					# Test not batched input
					__snake_case				:			Any       			=    image_processing(image_inputs[0]     ,			return_tensors="""pt""" )

					# Test no bool masked pos
					self.assertFalse("""bool_masked_pos""" in encoded_images )

					__snake_case				:			Optional[int]       			=    self.image_processor_tester.get_expected_image_size()

					self.assertEqual(
					    encoded_images.pixel_values.shape     ,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,			)

					# Test batched
					__snake_case				:			Union[str, Any]       			=    image_processing(__magic_name__     ,			return_tensors="""pt""" )
					__snake_case				:			Union[str, Any]       			=    self.image_processor_tester.get_expected_image_size()

					# Test no bool masked pos
					self.assertFalse("""bool_masked_pos""" in encoded_images )

					self.assertEqual(
					    encoded_images.pixel_values.shape     ,			(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        expected_height,
					        expected_width,
					    )     ,			)

		def   lowercase__    (      self				:      List[str]     ,			__magic_name__				:      int     ,			__magic_name__				:      Optional[Any] )				->						Any:


					"""simple docstring"""

					__snake_case				:			Tuple       			=    self.image_processing_class(**self.image_processor_dict )
					# create random tensors
					__snake_case				:			int       			=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__magic_name__     ,			**__magic_name__ )
					for image in image_inputs:
								self.assertIsInstance(__magic_name__     ,			__magic_name__ )

					# Test not batched input
					__snake_case				:			List[Any]       			=    image_processing(image_inputs[0]     ,			return_tensors="""pt""" )

					__snake_case				:			Optional[int]       			=    self.image_processor_tester.get_expected_image_size()
					self.assertEqual(
					    encoded_images.pixel_values.shape     ,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,			)

					__snake_case				:			Optional[Any]       			=    image_processing(__magic_name__     ,			return_image_mask=__magic_name__     ,			return_tensors="""pt""" )

					__snake_case				:			List[str]       			=    self.image_processor_tester.get_expected_image_size()
					self.assertEqual(
					    encoded_images.pixel_values.shape     ,			(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        expected_height,
					        expected_width,
					    )     ,			)

					__snake_case				:			Optional[Any]       			=    self.image_processor_tester.get_expected_mask_size()
					self.assertEqual(
					    encoded_images.bool_masked_pos.shape     ,			(
					        self.image_processor_tester.batch_size,
					        expected_height,
					        expected_width,
					    )     ,			)

					# Test batched
					__snake_case				:			Optional[int]       			=    image_processing(__magic_name__     ,			return_tensors="""pt""" ).pixel_values

					__snake_case				:			int       			=    self.image_processor_tester.get_expected_image_size()
					self.assertEqual(
					    encoded_images.shape     ,			(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        expected_height,
					        expected_width,
					    )     ,			)

					# Test masking
					__snake_case				:			List[str]       			=    image_processing(__magic_name__     ,			return_image_mask=__magic_name__     ,			return_tensors="""pt""" )

					__snake_case				:			Optional[Any]       			=    self.image_processor_tester.get_expected_image_size()
					self.assertEqual(
					    encoded_images.pixel_values.shape     ,			(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        expected_height,
					        expected_width,
					    )     ,			)

					__snake_case				:			List[Any]       			=    self.image_processor_tester.get_expected_mask_size()
					self.assertEqual(
					    encoded_images.bool_masked_pos.shape     ,			(
					        self.image_processor_tester.batch_size,
					        expected_height,
					        expected_width,
					    )     ,			)

		def   lowercase__    (      self				:      Dict )				->						Union[str, Any]:


					"""simple docstring"""

					self._test_call_framework(np.ndarray     ,			prepare_kwargs={"""numpify""": True} )

		def   lowercase__    (      self				:      Tuple )				->						Optional[int]:


					"""simple docstring"""

					self._test_call_framework(torch.Tensor     ,			prepare_kwargs={"""torchify""": True} )

		def   lowercase__    (      self				:      int )				->						Union[str, Any]:


					"""simple docstring"""

					random.seed(12_34 )
					__snake_case				:			Dict       			=    self.image_processing_class(**self.image_processor_dict )
					__snake_case				:			int       			=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__magic_name__     ,			torchify=__magic_name__ )

					# Test not batched input
					__snake_case				:			Any       			=    image_processing(image_inputs[0]     ,			return_image_mask=__magic_name__     ,			return_tensors="""pt""" )
					self.assertEqual(encoded_images.bool_masked_pos.sum().item()     ,			75 )

		def   lowercase__    (      self				:      List[str] )				->						Tuple:


					"""simple docstring"""

					__snake_case				:			List[str]       			=    self.image_processing_class(**self.image_processor_dict )
					# create random PIL images
					__snake_case				:			Tuple       			=    prepare_image_inputs(self.image_processor_tester     ,			equal_resolution=__magic_name__ )
					for image in image_inputs:
								self.assertIsInstance(__magic_name__     ,			PIL.Image.Image )

					# Test not batched input
					__snake_case				:			Optional[int]       			=    image_processing(image_inputs[0]     ,			return_codebook_pixels=__magic_name__     ,			return_tensors="""pt""" )
					__snake_case				:			str       			=    self.image_processor_tester.get_expected_codebook_image_size()
					self.assertEqual(
					    encoded_images.codebook_pixel_values.shape     ,			(1, self.image_processor_tester.num_channels, expected_height, expected_width)     ,			)

					# Test batched
					__snake_case				:			Tuple       			=    image_processing(__magic_name__     ,			return_codebook_pixels=__magic_name__     ,			return_tensors="""pt""" )
					__snake_case				:			Union[str, Any]       			=    self.image_processor_tester.get_expected_codebook_image_size()
					self.assertEqual(
					    encoded_images.codebook_pixel_values.shape     ,			(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_channels,
					        expected_height,
					        expected_width,
					    )     ,			)









'''simple docstring'''






import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase




@require_tokenizers
class    _A   (				unittest.TestCase		):
		lowercase__:      Tuple							    =      EsmTokenizer

		def   lowercase__    (      self				:      Tuple )				->						Optional[int]:


					"""simple docstring"""

					super().setUp()
					__snake_case				:			str       			=    tempfile.mkdtemp()
					# fmt: off
					__snake_case				:			List[str]       			=    ["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
					# fmt: on
					__snake_case				:			Tuple       			=    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   lowercase__    (      self				:      List[Any]     ,			**__magic_name__				:      Tuple )				->						List[PreTrainedTokenizerBase]:


					"""simple docstring"""

					return [self.get_tokenizer(**__magic_name__ )]

		def   lowercase__    (      self				:      int     ,			**__magic_name__				:      Dict )				->						PreTrainedTokenizer:


					"""simple docstring"""

					return self.tokenizer_class.from_pretrained(self.tmpdirname     ,			**__magic_name__ )

		def   lowercase__    (      self				:      Union[str, Any] )				->						Dict:


					"""simple docstring"""

					__snake_case				:			Dict       			=    self.tokenizer_class(self.vocab_file )

					__snake_case				:			Optional[int]       			=    tokenizer.tokenize("""LAGVS""" )
					self.assertListEqual(__magic_name__     ,			["""L""", """A""", """G""", """V""", """S"""] )
					self.assertListEqual(tokenizer.convert_tokens_to_ids(__magic_name__ )     ,			[4, 5, 6, 7, 8] )

		def   lowercase__    (      self				:      List[str] )				->						Optional[Any]:


					"""simple docstring"""

					__snake_case				:			str       			=    self.tokenizer_class(self.vocab_file )

					__snake_case				:			Optional[int]       			=    """LAGVS"""
					self.assertListEqual(tokenizer.encode(__magic_name__ )     ,			[0, 4, 5, 6, 7, 8, 2] )

		def   lowercase__    (      self				:      str )				->						Optional[Any]:


					"""simple docstring"""

					__snake_case				:			Dict       			=    self.tokenizer_class(self.vocab_file )

					__snake_case				:			Tuple       			=    ["""LAGVS""", """WCB"""]
					__snake_case				:			Dict       			=    tokenizer(__magic_name__     ,			padding=__magic_name__ )["""input_ids"""]

					self.assertListEqual(__magic_name__     ,			[[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]] )

		def   lowercase__    (      self				:      str )				->						List[Any]:


					"""simple docstring"""

					__snake_case				:			int       			=    self.tokenizer_class(self.vocab_file )

					__snake_case				:			List[Any]       			=    ["""LAGVS""", """WCB"""]
					__snake_case				:			Any       			=    tokenizer(__magic_name__     ,			padding=__magic_name__ )["""input_ids"""]

					self.assertListEqual(__magic_name__     ,			[[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]] )

		def   lowercase__    (      self				:      List[str] )				->						Tuple:


					"""simple docstring"""

					__snake_case				:			Any       			=    self.get_tokenizers(fast=__magic_name__ )
					for tokenizer in tokenizers:
								with self.subTest(f'''{tokenizer.__class__.__name__}''' ):
											__snake_case				:			int       			=    """<unk>"""
											__snake_case				:			Optional[Any]       			=    """<mask>"""

											__snake_case				:			Tuple       			=    tokenizer.tokenize(__magic_name__ )
											__snake_case				:			List[Any]       			=    tokenizer.tokenize(__magic_name__ )

											self.assertEqual(len(__magic_name__ )     ,			1 )
											self.assertEqual(len(__magic_name__ )     ,			1 )
											self.assertEqual(token_a[0]     ,			__magic_name__ )
											self.assertEqual(token_a[0]     ,			__magic_name__ )


