




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 LevitImageProcessor




class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							def __init__(					self       :	Union[str, Any]       ,     a       :	str       ,     a       :	Any=7       ,     a       :	List[str]=3       ,     a       :	Optional[int]=18       ,     a       :	Union[str, Any]=30       ,     a       :	Tuple=400       ,     a       :	List[Any]=True       ,     a       :	str=None       ,     a       :	List[Any]=True       ,     a       :	Optional[int]=None       ,     a       :	int=True       ,     a       :	List[str]=[0.5, 0.5, 0.5]       ,     a       :	Any=[0.5, 0.5, 0.5]       ,     )			->					Optional[Any]:
										SCREAMING_SNAKE_CASE           =					size if size is not None else {"""shortest_edge""": 18}
										SCREAMING_SNAKE_CASE           =					crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
										SCREAMING_SNAKE_CASE           =					parent
										SCREAMING_SNAKE_CASE           =					batch_size
										SCREAMING_SNAKE_CASE           =					num_channels
										SCREAMING_SNAKE_CASE           =					image_size
										SCREAMING_SNAKE_CASE           =					min_resolution
										SCREAMING_SNAKE_CASE           =					max_resolution
										SCREAMING_SNAKE_CASE           =					do_resize
										SCREAMING_SNAKE_CASE           =					size
										SCREAMING_SNAKE_CASE           =					do_center_crop
										SCREAMING_SNAKE_CASE           =					crop_size
										SCREAMING_SNAKE_CASE           =					do_normalize
										SCREAMING_SNAKE_CASE           =					image_mean
										SCREAMING_SNAKE_CASE           =					image_std







							def    _UpperCAmelCase     (					self       :	Tuple			)			->					List[Any]:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "do_center_crop": self.do_center_crop,
										    "size": self.size,
										    "crop_size": self.crop_size,
										}





@require_torch
@require_vision
class 			UpperCAmelCase_      (   A		,      unittest.TestCase       ):




							'''simple docstring'''



							a__						  =     LevitImageProcessor if is_vision_available() else None





							def    _UpperCAmelCase     (					self       :	Optional[Any]			)			->					str:
										SCREAMING_SNAKE_CASE           =					LevitImageProcessingTester(self			)





							@property
							def    _UpperCAmelCase     (					self       :	Tuple			)			->					Optional[int]:
										return self.image_processor_tester.prepare_image_processor_dict()





							def    _UpperCAmelCase     (					self       :	Optional[Any]			)			->					Union[str, Any]:
										SCREAMING_SNAKE_CASE           =					self.image_processing_class(**self.image_processor_dict			)
										self.assertTrue(hasattr(a       ,     """image_mean"""			)			)
										self.assertTrue(hasattr(a       ,     """image_std"""			)			)
										self.assertTrue(hasattr(a       ,     """do_normalize"""			)			)
										self.assertTrue(hasattr(a       ,     """do_resize"""			)			)
										self.assertTrue(hasattr(a       ,     """do_center_crop"""			)			)
										self.assertTrue(hasattr(a       ,     """size"""			)			)





							def    _UpperCAmelCase     (					self       :	Any			)			->					List[Any]:
										SCREAMING_SNAKE_CASE           =					self.image_processing_class.from_dict(self.image_processor_dict			)
										self.assertEqual(image_processor.size       ,     {"""shortest_edge""": 18}			)
										self.assertEqual(image_processor.crop_size       ,     {"""height""": 18, """width""": 18}			)

										SCREAMING_SNAKE_CASE           =					self.image_processing_class.from_dict(self.image_processor_dict       ,     size=42       ,     crop_size=84			)
										self.assertEqual(image_processor.size       ,     {"""shortest_edge""": 42}			)
										self.assertEqual(image_processor.crop_size       ,     {"""height""": 84, """width""": 84}			)





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





							def    _UpperCAmelCase     (					self       :	List[Any]			)			->					str:
										# Initialize image_processing
										SCREAMING_SNAKE_CASE           =					self.image_processing_class(**self.image_processor_dict			)
										# create random PIL images
										SCREAMING_SNAKE_CASE           =					prepare_image_inputs(self.image_processor_tester       ,     equal_resolution=a			)
										for image in image_inputs:
													self.assertIsInstance(a       ,     Image.Image			)

										# Test not batched input
										SCREAMING_SNAKE_CASE           =					image_processing(image_inputs[0]       ,     return_tensors="""pt"""			).pixel_values
										self.assertEqual(
										    encoded_images.shape       ,     (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )       ,     )

										# Test batched
										SCREAMING_SNAKE_CASE           =					image_processing(a       ,     return_tensors="""pt"""			).pixel_values
										self.assertEqual(
										    encoded_images.shape       ,     (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )       ,     )





							def    _UpperCAmelCase     (					self       :	List[str]			)			->					Any:
										# Initialize image_processing
										SCREAMING_SNAKE_CASE           =					self.image_processing_class(**self.image_processor_dict			)
										# create random numpy tensors
										SCREAMING_SNAKE_CASE           =					prepare_image_inputs(self.image_processor_tester       ,     equal_resolution=a       ,     numpify=a			)
										for image in image_inputs:
													self.assertIsInstance(a       ,     np.ndarray			)

										# Test not batched input
										SCREAMING_SNAKE_CASE           =					image_processing(image_inputs[0]       ,     return_tensors="""pt"""			).pixel_values
										self.assertEqual(
										    encoded_images.shape       ,     (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )       ,     )

										# Test batched
										SCREAMING_SNAKE_CASE           =					image_processing(a       ,     return_tensors="""pt"""			).pixel_values
										self.assertEqual(
										    encoded_images.shape       ,     (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )       ,     )







							def    _UpperCAmelCase     (					self       :	int			)			->					str:
										# Initialize image_processing
										SCREAMING_SNAKE_CASE           =					self.image_processing_class(**self.image_processor_dict			)
										# create random PyTorch tensors
										SCREAMING_SNAKE_CASE           =					prepare_image_inputs(self.image_processor_tester       ,     equal_resolution=a       ,     torchify=a			)
										for image in image_inputs:
													self.assertIsInstance(a       ,     torch.Tensor			)

										# Test not batched input
										SCREAMING_SNAKE_CASE           =					image_processing(image_inputs[0]       ,     return_tensors="""pt"""			).pixel_values
										self.assertEqual(
										    encoded_images.shape       ,     (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )       ,     )

										# Test batched
										SCREAMING_SNAKE_CASE           =					image_processing(a       ,     return_tensors="""pt"""			).pixel_values
										self.assertEqual(
										    encoded_images.shape       ,     (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size["""height"""],
										        self.image_processor_tester.crop_size["""width"""],
										    )       ,     )












import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
		from transformers import AutoModelForSeqaSeqLM, AutoTokenizer




@require_torch
@require_sentencepiece
@require_tokenizers
class 			UpperCAmelCase_      (   unittest.TestCase       ):




							'''simple docstring'''



							@slow
							def    _UpperCAmelCase     (					self       :	Optional[int]			)			->					Tuple:
										SCREAMING_SNAKE_CASE           =					AutoModelForSeqaSeqLM.from_pretrained("""google/mt5-small"""       ,     return_dict=a			).to(a			)
										SCREAMING_SNAKE_CASE           =					AutoTokenizer.from_pretrained("""google/mt5-small"""			)

										SCREAMING_SNAKE_CASE           =					tokenizer("""Hello there"""       ,     return_tensors="""pt"""			).input_ids
										SCREAMING_SNAKE_CASE           =					tokenizer("""Hi I am"""       ,     return_tensors="""pt"""			).input_ids

										SCREAMING_SNAKE_CASE           =					model(input_ids.to(a			)       ,     labels=labels.to(a			)			).loss
										SCREAMING_SNAKE_CASE           =					-(labels.shape[-1] * loss.item())

										SCREAMING_SNAKE_CASE           =					-84.9127
										self.assertTrue(abs(mtf_score - EXPECTED_SCORE			) < 1E-4			)






