



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 CLIPImageProcessor



class 	__lowerCAmelCase       (		unittest.TestCase				):




 def __init__(  self		,			__UpperCAmelCase		,			__UpperCAmelCase=7		,			__UpperCAmelCase=3		,			__UpperCAmelCase=18		,			__UpperCAmelCase=30		,			__UpperCAmelCase=400		,			__UpperCAmelCase=True		,			__UpperCAmelCase=None		,			__UpperCAmelCase=True		,			__UpperCAmelCase=None		,			__UpperCAmelCase=True		,			__UpperCAmelCase=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,			__UpperCAmelCase=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,			__UpperCAmelCase=True		,			):



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




 def     lowerCamelCase (  self			):



    '''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     lowerCamelCase (  self		,			__UpperCAmelCase=False		,			__UpperCAmelCase=False		,			__UpperCAmelCase=False			):



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

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

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

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

    return image_inputs



@require_torch
@require_vision
class 	__lowerCAmelCase       (		lowerCAmelCase__					,		unittest.TestCase				):
 lowerCAmelCase__       =					CLIPImageProcessor if is_vision_available() else None




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      CLIPImageProcessingTester(self			)




 @property
 def     lowerCamelCase (  self			):



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




 def     lowerCamelCase (  self			):



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




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.image_processing_class.from_dict(self.image_processor_dict			)
    self.assertEqual(image_processor.size		,			{'''shortest_edge''': 20}			)
    self.assertEqual(image_processor.crop_size		,			{'''height''': 18, '''width''': 18}			)

    __lowerCamelCase  							=      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			):



    '''simple docstring'''
    pass




 def     lowerCamelCase (  self			):



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

    # Test not batched input
    __lowerCamelCase  							=      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
    __lowerCamelCase  							=      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			):



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

    # Test not batched input
    __lowerCamelCase  							=      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
    __lowerCamelCase  							=      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			):



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

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



@require_torch
@require_vision
class 	__lowerCAmelCase       (		lowerCAmelCase__					,		unittest.TestCase				):
 lowerCAmelCase__       =					CLIPImageProcessor if is_vision_available() else None




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      CLIPImageProcessingTester(self		,			num_channels=4			)
    __lowerCamelCase  							=      3




 @property
 def     lowerCamelCase (  self			):



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




 def     lowerCamelCase (  self			):



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




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    pass




 def     lowerCamelCase (  self			):



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

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











import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
  from transformers.models.electra.modeling_flax_electra import (
      FlaxElectraForCausalLM,
      FlaxElectraForMaskedLM,
      FlaxElectraForMultipleChoice,
      FlaxElectraForPreTraining,
      FlaxElectraForQuestionAnswering,
      FlaxElectraForSequenceClassification,
      FlaxElectraForTokenClassification,
      FlaxElectraModel,
  )



class 	__lowerCAmelCase       (		unittest.TestCase				):




 def __init__(  self		,			__UpperCAmelCase		,			__UpperCAmelCase=13		,			__UpperCAmelCase=7		,			__UpperCAmelCase=True		,			__UpperCAmelCase=True		,			__UpperCAmelCase=True		,			__UpperCAmelCase=True		,			__UpperCAmelCase=99		,			__UpperCAmelCase=24		,			__UpperCAmelCase=32		,			__UpperCAmelCase=5		,			__UpperCAmelCase=4		,			__UpperCAmelCase=37		,			__UpperCAmelCase="gelu"		,			__UpperCAmelCase=0.1		,			__UpperCAmelCase=0.1		,			__UpperCAmelCase=512		,			__UpperCAmelCase=16		,			__UpperCAmelCase=2		,			__UpperCAmelCase=0.02		,			__UpperCAmelCase=4		,			):



    '''simple docstring'''
    __lowerCamelCase  							=      parent
    __lowerCamelCase  							=      batch_size
    __lowerCamelCase  							=      seq_length
    __lowerCamelCase  							=      is_training
    __lowerCamelCase  							=      use_attention_mask
    __lowerCamelCase  							=      use_token_type_ids
    __lowerCamelCase  							=      use_labels
    __lowerCamelCase  							=      vocab_size
    __lowerCamelCase  							=      hidden_size
    __lowerCamelCase  							=      embedding_size
    __lowerCamelCase  							=      num_hidden_layers
    __lowerCamelCase  							=      num_attention_heads
    __lowerCamelCase  							=      intermediate_size
    __lowerCamelCase  							=      hidden_act
    __lowerCamelCase  							=      hidden_dropout_prob
    __lowerCamelCase  							=      attention_probs_dropout_prob
    __lowerCamelCase  							=      max_position_embeddings
    __lowerCamelCase  							=      type_vocab_size
    __lowerCamelCase  							=      type_sequence_label_size
    __lowerCamelCase  							=      initializer_range
    __lowerCamelCase  							=      num_choices




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size			)

    __lowerCamelCase  							=      None
    if self.use_attention_mask:
       __lowerCamelCase  							=      random_attention_mask([self.batch_size, self.seq_length]			)

    __lowerCamelCase  							=      None
    if self.use_token_type_ids:
       __lowerCamelCase  							=      ids_tensor([self.batch_size, self.seq_length]		,			self.type_vocab_size			)

    __lowerCamelCase  							=      ElectraConfig(
        vocab_size=self.vocab_size		,			hidden_size=self.hidden_size		,			embedding_size=self.embedding_size		,			num_hidden_layers=self.num_hidden_layers		,			num_attention_heads=self.num_attention_heads		,			intermediate_size=self.intermediate_size		,			hidden_act=self.hidden_act		,			hidden_dropout_prob=self.hidden_dropout_prob		,			attention_probs_dropout_prob=self.attention_probs_dropout_prob		,			max_position_embeddings=self.max_position_embeddings		,			type_vocab_size=self.type_vocab_size		,			initializer_range=self.initializer_range		,			)

    return config, input_ids, token_type_ids, attention_mask




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      self.prepare_config_and_inputs()
    __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase  							=      config_and_inputs
    __lowerCamelCase  							=      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
    return config, inputs_dict



@require_flax
class 	__lowerCAmelCase       (		lowerCAmelCase__					,		unittest.TestCase				):
 lowerCAmelCase__       =					True

 lowerCAmelCase__       =					(
     (
         FlaxElectraModel,
         FlaxElectraForCausalLM,
         FlaxElectraForMaskedLM,
         FlaxElectraForPreTraining,
         FlaxElectraForTokenClassification,
         FlaxElectraForQuestionAnswering,
         FlaxElectraForMultipleChoice,
         FlaxElectraForSequenceClassification,
     )
     if is_flax_available()
     else ()
 )




 def     lowerCamelCase (  self			):



    '''simple docstring'''
    __lowerCamelCase  							=      FlaxElectraModelTester(self			)




 @slow
 def     lowerCamelCase (  self			):



    '''simple docstring'''
    for model_class_name in self.all_model_classes:
       if model_class_name == FlaxElectraForMaskedLM:
          __lowerCamelCase  							=      model_class_name.from_pretrained('''google/electra-small-generator'''			)
       else:
          __lowerCamelCase  							=      model_class_name.from_pretrained('''google/electra-small-discriminator'''			)
       __lowerCamelCase  							=      model(np.ones((1, 1)			)			)
       self.assertIsNotNone(__UpperCAmelCase			)






