



'''simple docstring'''





import unittest

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


if is_torch_available():
       import torch

       from transformers import XLMRobertaModel







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







    @slow
    def lowerCamelCase       (    self					:     str						):
          snake_case__  :    str    						=      XLMRobertaModel.from_pretrained("""xlm-roberta-base"""						)
          snake_case__  :    Union[str, Any]    						=      torch.tensor([[0, 581, 10_269, 83, 99_942, 136, 60_742, 23, 70, 80_583, 18_276, 2]]						)
          # The dog is cute and lives in the garden house

          snake_case__  :    List[Any]    						=      torch.Size((1, 12, 768)						)  # batch_size, sequence_length, embedding_vector_dim
          snake_case__  :    Dict    						=      torch.tensor(
              [[-0.0101, 0.1218, -0.0803, 0.0801, 0.1327, 0.0776, -0.1215, 0.2383, 0.3338, 0.3106, 0.0300, 0.0252]]						)
          #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
          #  xlmr.eval()
          #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
          with torch.no_grad():
                snake_case__  :    str    						=      model(snake_case_						)["""last_hidden_state"""].detach()
          self.assertEqual(output.shape ,     snake_case_						)
          # compare the actual values for a slice of last dim
          self.assertTrue(torch.allclose(output[:, :, -1] ,     snake_case_ ,     atol=1E-3						)						)




    @slow
    def lowerCamelCase       (    self					:     Union[str, Any]						):
          snake_case__  :    List[str]    						=      XLMRobertaModel.from_pretrained("""xlm-roberta-large"""						)
          snake_case__  :    Union[str, Any]    						=      torch.tensor([[0, 581, 10_269, 83, 99_942, 136, 60_742, 23, 70, 80_583, 18_276, 2]]						)
          # The dog is cute and lives in the garden house

          snake_case__  :    Optional[int]    						=      torch.Size((1, 12, 1_024)						)  # batch_size, sequence_length, embedding_vector_dim
          snake_case__  :    int    						=      torch.tensor(
              [[-0.0699, -0.0318, 0.0705, -0.1241, 0.0999, -0.0520, 0.1004, -0.1838, -0.4704, 0.1437, 0.0821, 0.0126]]						)
          #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
          #  xlmr.eval()
          #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
          with torch.no_grad():
                snake_case__  :    List[Any]    						=      model(snake_case_						)["""last_hidden_state"""].detach()
          self.assertEqual(output.shape ,     snake_case_						)
          # compare the actual values for a slice of last dim
          self.assertTrue(torch.allclose(output[:, :, -1] ,     snake_case_ ,     atol=1E-3						)						)




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

							def __init__(							self:	Dict    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	Dict=7    ,						UpperCamelCase_:	Optional[int]=3    ,						UpperCamelCase_:	List[Any]=18    ,						UpperCamelCase_:	Dict=30    ,						UpperCamelCase_:	Optional[int]=400    ,						UpperCamelCase_:	Optional[int]=True    ,						UpperCamelCase_:	Union[str, Any]=None    ,						UpperCamelCase_:	List[str]=True    ,						UpperCamelCase_:	Dict=None    ,						UpperCamelCase_:	Optional[int]=True    ,						UpperCamelCase_:	Tuple=[0.5, 0.5, 0.5]    ,						UpperCamelCase_:	List[str]=[0.5, 0.5, 0.5]    ,						)			->   int:





									"""simple docstring"""







									lowercase__            =      size if size is not None else {'''shortest_edge''': 18}
									lowercase__            =      crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
									lowercase__            =      parent
									lowercase__            =      batch_size
									lowercase__            =      num_channels
									lowercase__            =      image_size
									lowercase__            =      min_resolution
									lowercase__            =      max_resolution
									lowercase__            =      do_resize
									lowercase__            =      size
									lowercase__            =      do_center_crop
									lowercase__            =      crop_size
									lowercase__            =      do_normalize
									lowercase__            =      image_mean
									lowercase__            =      image_std


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





									"""simple docstring"""







									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        _a							(			UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			List[str]        = LevitImageProcessor if is_vision_available() else None

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





									"""simple docstring"""







									lowercase__            =      LevitImageProcessingTester(self							)

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





									"""simple docstring"""







									return self.image_processor_tester.prepare_image_processor_dict()

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





									"""simple docstring"""







									lowercase__            =      self.image_processing_class(**self.image_processor_dict							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''image_mean'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''image_std'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_normalize'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_resize'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''do_center_crop'''							)							)
									self.assertTrue(hasattr(UpperCamelCase_    ,						'''size'''							)							)

							def       lowerCamelCase_ (							self:	str							)			->   Tuple:





									"""simple docstring"""







									lowercase__            =      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}							)

									lowercase__            =      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       lowerCamelCase_ (							self:	List[str]							)			->   Dict:





									"""simple docstring"""







									pass

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





									"""simple docstring"""







									lowercase__            =      self.image_processing_class(**self.image_processor_dict							)
									# create random PIL images
									lowercase__            =      prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=UpperCamelCase_							)
									for image in image_inputs:
											self.assertIsInstance(UpperCamelCase_    ,						Image.Image							)

									# Test not batched input
									lowercase__            =      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
									lowercase__            =      image_processing(UpperCamelCase_    ,						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       lowerCamelCase_ (							self:	Union[str, Any]							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      self.image_processing_class(**self.image_processor_dict							)
									# create random numpy tensors
									lowercase__            =      prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=UpperCamelCase_    ,						numpify=UpperCamelCase_							)
									for image in image_inputs:
											self.assertIsInstance(UpperCamelCase_    ,						np.ndarray							)

									# Test not batched input
									lowercase__            =      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
									lowercase__            =      image_processing(UpperCamelCase_    ,						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       lowerCamelCase_ (							self:	Any							)			->   Optional[Any]:





									"""simple docstring"""







									lowercase__            =      self.image_processing_class(**self.image_processor_dict							)
									# create random PyTorch tensors
									lowercase__            =      prepare_image_inputs(self.image_processor_tester    ,						equal_resolution=UpperCamelCase_    ,						torchify=UpperCamelCase_							)
									for image in image_inputs:
											self.assertIsInstance(UpperCamelCase_    ,						torch.Tensor							)

									# Test not batched input
									lowercase__            =      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
									lowercase__            =      image_processing(UpperCamelCase_    ,						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'''],
									    )    ,						)


