



"""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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import ChineseCLIPImageProcessor



class     UpperCAmelCase    (	unittest.TestCase  ):



   def __init__(       self    :	Dict							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Optional[Any]=7							,       __lowerCamelCase    :	Union[str, Any]=3							,       __lowerCamelCase    :	str=1_8							,       __lowerCamelCase    :	Any=3_0							,       __lowerCamelCase    :	Any=4_0_0							,       __lowerCamelCase    :	Tuple=True							,       __lowerCamelCase    :	List[Any]=None							,       __lowerCamelCase    :	Dict=True							,       __lowerCamelCase    :	Tuple=None							,       __lowerCamelCase    :	Tuple=True							,       __lowerCamelCase    :	List[str]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]							,       __lowerCamelCase    :	List[Any]=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]							,       __lowerCamelCase    :	int=True							,       ):



     """simple docstring"""
     _snake_case   	=      size if size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
     _snake_case   	=      crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
     _snake_case   	=      parent
     _snake_case   	=      batch_size
     _snake_case   	=      num_channels
     _snake_case   	=      image_size
     _snake_case   	=      min_resolution
     _snake_case   	=      max_resolution
     _snake_case   	=      do_resize
     _snake_case   	=      size
     _snake_case   	=      do_center_crop
     _snake_case   	=      crop_size
     _snake_case   	=      do_normalize
     _snake_case   	=      image_mean
     _snake_case   	=      image_std
     _snake_case   	=      do_convert_rgb



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_center_crop": self.do_center_crop,
         "crop_size": self.crop_size,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_convert_rgb": self.do_convert_rgb,
     }





   def    __UpperCAmelCase    (       self    :	List[Any]							,       __lowerCamelCase    :	List[str]=False							,       __lowerCamelCase    :	int=False							,       __lowerCamelCase    :	Any=False				):



     """simple docstring"""
     assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

     if equal_resolution:
       _snake_case   	=      []
       for i in range(self.batch_size				):
         image_inputs.append(
             np.random.randint(
                 2_5_5							,       size=(self.num_channels, self.max_resolution, self.max_resolution)							,       dtype=np.uinta				)				)
     else:
       _snake_case   	=      []
       for i in range(self.batch_size				):
         _snake_case	,     _snake_case   	=      np.random.choice(np.arange(self.min_resolution							,       self.max_resolution				)							,       2				)
         image_inputs.append(np.random.randint(2_5_5							,       size=(self.num_channels, width, height)							,       dtype=np.uinta				)				)

     if not numpify and not torchify:
       # PIL expects the channel dimension as last dimension
       _snake_case   	=      [Image.fromarray(np.moveaxis(__lowerCamelCase							,       0							,       -1				)				) for x in image_inputs]

     if torchify:
       _snake_case   	=      [torch.from_numpy(__lowerCamelCase				) for x in image_inputs]

     return image_inputs



@require_torch
@require_vision
class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,unittest.TestCase  ):
   A__		:	Dict           =  ChineseCLIPImageProcessor if is_vision_available() else None



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _snake_case   	=      ChineseCLIPImageProcessingTester(self							,       do_center_crop=__lowerCamelCase				)



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



     """simple docstring"""
     return self.image_processor_tester.prepare_image_processor_dict()



   def    __UpperCAmelCase    (       self    :	Tuple				):



     """simple docstring"""
     _snake_case   	=      self.image_processing_class(**self.image_processor_dict				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_resize'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''size'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_center_crop'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''center_crop'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_normalize'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''image_mean'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''image_std'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_convert_rgb'''				)				)



   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     _snake_case   	=      self.image_processing_class.from_dict(self.image_processor_dict				)
     self.assertEqual(image_processor.size							,       {'''height''': 2_2_4, '''width''': 2_2_4}				)
     self.assertEqual(image_processor.crop_size							,       {'''height''': 1_8, '''width''': 1_8}				)

     _snake_case   	=      self.image_processing_class.from_dict(self.image_processor_dict							,       size=4_2							,       crop_size=8_4				)
     self.assertEqual(image_processor.size							,       {'''shortest_edge''': 4_2}				)
     self.assertEqual(image_processor.crop_size							,       {'''height''': 8_4, '''width''': 8_4}				)



   def    __UpperCAmelCase    (       self    :	Tuple				):



     """simple docstring"""
     pass



   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     # Initialize image_processing
     _snake_case   	=      self.image_processing_class(**self.image_processor_dict				)
     # create random PIL images
     _snake_case   	=      self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase				)
     for image in image_inputs:
       self.assertIsInstance(__lowerCamelCase							,       Image.Image				)

     # Test not batched input
     _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
     _snake_case   	=      image_processing(__lowerCamelCase							,       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    :	Tuple				):



     """simple docstring"""
     # Initialize image_processing
     _snake_case   	=      self.image_processing_class(**self.image_processor_dict				)
     # create random numpy tensors
     _snake_case   	=      self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase							,       numpify=__lowerCamelCase				)
     for image in image_inputs:
       self.assertIsInstance(__lowerCamelCase							,       np.ndarray				)

     # Test not batched input
     _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
     _snake_case   	=      image_processing(__lowerCamelCase							,       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    :	Tuple				):



     """simple docstring"""
     # Initialize image_processing
     _snake_case   	=      self.image_processing_class(**self.image_processor_dict				)
     # create random PyTorch tensors
     _snake_case   	=      self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase							,       torchify=__lowerCamelCase				)
     for image in image_inputs:
       self.assertIsInstance(__lowerCamelCase							,       torch.Tensor				)

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



@require_torch
@require_vision
class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,unittest.TestCase  ):
   A__		:	int           =  ChineseCLIPImageProcessor if is_vision_available() else None



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      ChineseCLIPImageProcessingTester(self							,       num_channels=4							,       do_center_crop=__lowerCamelCase				)
     _snake_case   	=      3



   @property
   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     return self.image_processor_tester.prepare_image_processor_dict()



   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     _snake_case   	=      self.image_processing_class(**self.image_processor_dict				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_resize'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''size'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_center_crop'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''center_crop'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_normalize'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''image_mean'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''image_std'''				)				)
     self.assertTrue(hasattr(__lowerCamelCase							,       '''do_convert_rgb'''				)				)



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     pass





   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     # Initialize image_processing
     _snake_case   	=      self.image_processing_class(**self.image_processor_dict				)
     # create random PIL images
     _snake_case   	=      self.image_processor_tester.prepare_inputs(equal_resolution=__lowerCamelCase				)
     for image in image_inputs:
       self.assertIsInstance(__lowerCamelCase							,       Image.Image				)

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

     # Test batched
     _snake_case   	=      image_processing(__lowerCamelCase							,       return_tensors='''pt'''				).pixel_values
     self.assertEqual(
         encoded_images.shape							,       (
             self.image_processor_tester.batch_size,
             self.expected_encoded_image_num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         )							,       )










"""simple docstring"""






import unittest

from transformers import AutoTokenizer, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, slow


if is_flax_available():
     import jax.numpy as jnp

     from transformers import FlaxXLMRobertaModel



@require_sentencepiece
@require_tokenizers
@require_flax
class     UpperCAmelCase    (	unittest.TestCase  ):



   @slow
   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      FlaxXLMRobertaModel.from_pretrained('''xlm-roberta-base'''				)
     _snake_case   	=      AutoTokenizer.from_pretrained('''xlm-roberta-base'''				)
     _snake_case   	=      '''The dog is cute and lives in the garden house'''
     _snake_case   	=      jnp.array([tokenizer.encode(__lowerCamelCase				)]				)

     _snake_case   	=      (1, 1_2, 7_6_8)  # batch_size, sequence_length, embedding_vector_dim
     _snake_case   	=      jnp.array(
         [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]]				)

     _snake_case   	=      model(__lowerCamelCase				)['''last_hidden_state''']
     self.assertEqual(output.shape							,       __lowerCamelCase				)
     # compare the actual values for a slice of last dim
     self.assertTrue(jnp.allclose(output[:, :, -1]							,       __lowerCamelCase							,       atol=1E-3				)				)





