

import unittest

import numpy as np

from transformers import AlbertConfig, 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():
    import jax.numpy as jnp

    from transformers.models.albert.modeling_flax_albert import (
        FlaxAlbertForMaskedLM,
        FlaxAlbertForMultipleChoice,
        FlaxAlbertForPreTraining,
        FlaxAlbertForQuestionAnswering,
        FlaxAlbertForSequenceClassification,
        FlaxAlbertForTokenClassification,
        FlaxAlbertModel,
    )





class 			_SCREAMING_SNAKE_CASE (   unittest.TestCase		):







   '''simple docstring'''




   def __init__(       self					:			Optional[Any]     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			List[str]=13     , __lowerCamelCase					:			List[str]=7     , __lowerCamelCase					:			Union[str, Any]=True     , __lowerCamelCase					:			Union[str, Any]=True     , __lowerCamelCase					:			Dict=True     , __lowerCamelCase					:			Optional[int]=True     , __lowerCamelCase					:			Optional[int]=99     , __lowerCamelCase					:			Tuple=32     , __lowerCamelCase					:			Any=5     , __lowerCamelCase					:			int=4     , __lowerCamelCase					:			Any=37     , __lowerCamelCase					:			Any="gelu"     , __lowerCamelCase					:			int=0.1     , __lowerCamelCase					:			Optional[int]=0.1     , __lowerCamelCase					:			Optional[Any]=512     , __lowerCamelCase					:			Union[str, Any]=16     , __lowerCamelCase					:			Any=2     , __lowerCamelCase					:			List[Any]=0.02     , __lowerCamelCase					:			int=4     , ):
       SCREAMING_SNAKE_CASE										=   parent
       SCREAMING_SNAKE_CASE										=   batch_size
       SCREAMING_SNAKE_CASE										=   seq_length
       SCREAMING_SNAKE_CASE										=   is_training
       SCREAMING_SNAKE_CASE										=   use_attention_mask
       SCREAMING_SNAKE_CASE										=   use_token_type_ids
       SCREAMING_SNAKE_CASE										=   use_labels
       SCREAMING_SNAKE_CASE										=   vocab_size
       SCREAMING_SNAKE_CASE										=   hidden_size
       SCREAMING_SNAKE_CASE										=   num_hidden_layers
       SCREAMING_SNAKE_CASE										=   num_attention_heads
       SCREAMING_SNAKE_CASE										=   intermediate_size
       SCREAMING_SNAKE_CASE										=   hidden_act
       SCREAMING_SNAKE_CASE										=   hidden_dropout_prob
       SCREAMING_SNAKE_CASE										=   attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE										=   max_position_embeddings
       SCREAMING_SNAKE_CASE										=   type_vocab_size
       SCREAMING_SNAKE_CASE										=   type_sequence_label_size
       SCREAMING_SNAKE_CASE										=   initializer_range
       SCREAMING_SNAKE_CASE										=   num_choices





   def      _snake_case		(       self					:			str			):
       SCREAMING_SNAKE_CASE										=   ids_tensor([self.batch_size, self.seq_length]     , self.vocab_size			)

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

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

       SCREAMING_SNAKE_CASE										=   AlbertConfig(
           vocab_size=self.vocab_size     , hidden_size=self.hidden_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     , is_decoder=__lowerCamelCase     , initializer_range=self.initializer_range     , )

       return config, input_ids, token_type_ids, attention_mask





   def      _snake_case		(       self					:			Union[str, Any]			):
       SCREAMING_SNAKE_CASE										=   self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE							,	SCREAMING_SNAKE_CASE										=   config_and_inputs
       SCREAMING_SNAKE_CASE										=   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
       return config, inputs_dict





@require_flax
class 			_SCREAMING_SNAKE_CASE (   __snake_case					,       unittest.TestCase		):







   '''simple docstring'''




   lowerCamelCase__				=      (
       (
           FlaxAlbertModel,
           FlaxAlbertForPreTraining,
           FlaxAlbertForMaskedLM,
           FlaxAlbertForMultipleChoice,
           FlaxAlbertForQuestionAnswering,
           FlaxAlbertForSequenceClassification,
           FlaxAlbertForTokenClassification,
           FlaxAlbertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )





   def      _snake_case		(       self					:			List[str]			):
       SCREAMING_SNAKE_CASE										=   FlaxAlbertModelTester(self			)





   @slow
   def      _snake_case		(       self					:			Any			):
       for model_class_name in self.all_model_classes:
           SCREAMING_SNAKE_CASE										=   model_class_name.from_pretrained("albert-base-v2"			)
           SCREAMING_SNAKE_CASE										=   model(np.ones((1, 1)			)			)
           self.assertIsNotNone(__lowerCamelCase			)





@require_flax
class 			_SCREAMING_SNAKE_CASE (   unittest.TestCase		):







   '''simple docstring'''




   @slow
   def      _snake_case		(       self					:			str			):
       SCREAMING_SNAKE_CASE										=   FlaxAlbertModel.from_pretrained("albert-base-v2"			)
       SCREAMING_SNAKE_CASE										=   np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]			)
       SCREAMING_SNAKE_CASE										=   np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
       SCREAMING_SNAKE_CASE										=   model(__lowerCamelCase     , attention_mask=__lowerCamelCase			)[0]
       SCREAMING_SNAKE_CASE										=   (1, 11, 768)
       self.assertEqual(output.shape     , __lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   np.array(
           [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]			)

       self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]     , __lowerCamelCase     , atol=1e-4			)			)





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 PoolFormerImageProcessor

class     __a(	unittest.TestCase  ):






    """simple docstring"""





    def __init__( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE=7  ,_SCREAMING_SNAKE_CASE=3  ,_SCREAMING_SNAKE_CASE=30  ,_SCREAMING_SNAKE_CASE=400  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=0.9  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]  ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]  ,)					->						Optional[int]:
         UpperCAmelCase_     :				int  =     size if size is not None else {'''shortest_edge''': 30}
         UpperCAmelCase_     :				List[str]  =     crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
         UpperCAmelCase_     :				Dict  =     parent
         UpperCAmelCase_     :				int  =     batch_size
         UpperCAmelCase_     :				int  =     num_channels
         UpperCAmelCase_     :				Any  =     min_resolution
         UpperCAmelCase_     :				Tuple  =     max_resolution
         UpperCAmelCase_     :				Optional[int]  =     do_resize_and_center_crop
         UpperCAmelCase_     :				Tuple  =     size
         UpperCAmelCase_     :				List[str]  =     crop_pct
         UpperCAmelCase_     :				List[str]  =     crop_size
         UpperCAmelCase_     :				Any  =     do_normalize
         UpperCAmelCase_     :				str  =     image_mean
         UpperCAmelCase_     :				List[Any]  =     image_std





    def 				a__ ( self    )					->						str:
         return {
             "size": self.size,
             "do_resize_and_center_crop": self.do_resize_and_center_crop,
             "crop_pct": self.crop_pct,
             "crop_size": self.crop_size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
         }


@require_torch
@require_vision
class     __a(	_a							,		unittest.TestCase  ):






    """simple docstring"""





    lowerCAmelCase         = PoolFormerImageProcessor if is_vision_available() else None





    def 				a__ ( self    )					->						Dict:
         UpperCAmelCase_     :				str  =     PoolFormerImageProcessingTester(self    )





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





    def 				a__ ( self    )					->						Optional[int]:
         UpperCAmelCase_     :				List[Any]  =     self.image_processing_class(**self.image_processor_dict    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''do_resize_and_center_crop'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''size'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''crop_pct'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''do_normalize'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''image_mean'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''image_std'''    )    )





    def 				a__ ( self    )					->						Union[str, Any]:
         UpperCAmelCase_     :				Any  =     self.image_processing_class.from_dict(self.image_processor_dict    )
         self.assertEqual(image_processor.size  ,{'''shortest_edge''': 30}    )
         self.assertEqual(image_processor.crop_size  ,{'''height''': 30, '''width''': 30}    )

         UpperCAmelCase_     :				Optional[Any]  =     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 				a__ ( self    )					->						Optional[int]:
         pass





    def 				a__ ( self    )					->						Dict:
         # Initialize image_processing
         UpperCAmelCase_     :				str  =     self.image_processing_class(**self.image_processor_dict    )
         # create random PIL images
         UpperCAmelCase_     :				int  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,Image.Image    )

         # Test not batched input
         UpperCAmelCase_     :				Optional[int]  =     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
         UpperCAmelCase_     :				Union[str, Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,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 				a__ ( self    )					->						List[Any]:
         # Initialize image_processing
         UpperCAmelCase_     :				Any  =     self.image_processing_class(**self.image_processor_dict    )
         # create random numpy tensors
         UpperCAmelCase_     :				List[str]  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE  ,numpify=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,np.ndarray    )

         # Test not batched input
         UpperCAmelCase_     :				Tuple  =     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
         UpperCAmelCase_     :				List[Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,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 				a__ ( self    )					->						Union[str, Any]:
         # Initialize image_processing
         UpperCAmelCase_     :				List[Any]  =     self.image_processing_class(**self.image_processor_dict    )
         # create random PyTorch tensors
         UpperCAmelCase_     :				Optional[Any]  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE  ,torchify=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,torch.Tensor    )

         # Test not batched input
         UpperCAmelCase_     :				Tuple  =     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
         UpperCAmelCase_     :				List[Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,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'''],
             )  ,)