





'''simple docstring'''






import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
						from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import PoolFormerImageProcessor




class 				UpperCAmelCase  (    snake_case_     ):




						def        lowercase__					(    self    :				Any						)       ->   Union[str, Any]:
												_lowerCAmelCase							  =							self.config_class(**self.inputs_dict						)
												self.parent.assertTrue(hasattr(__snake_case    ,    """hidden_sizes"""						)						)
												self.parent.assertTrue(hasattr(__snake_case    ,    """num_encoder_blocks"""						)						)




class 				UpperCAmelCase  :




						def __init__(    self    :				List[Any]    ,    __snake_case    :				int    ,    __snake_case    :				List[str]=13    ,    __snake_case    :				List[str]=64    ,    __snake_case    :				Optional[int]=3    ,    __snake_case    :				str=4    ,    __snake_case    :				List[Any]=[2, 2, 2, 2]    ,    __snake_case    :				str=[8, 4, 2, 1]    ,    __snake_case    :				Dict=[16, 32, 64, 1_28]    ,    __snake_case    :				List[Any]=[1, 4, 8, 16]    ,    __snake_case    :				int=False    ,    __snake_case    :				str=True    ,    __snake_case    :				Optional[Any]="gelu"    ,    __snake_case    :				Any=0.1    ,    __snake_case    :				str=0.02    ,    __snake_case    :				Optional[Any]=3    ,    __snake_case    :				Tuple=None    ,    )       ->   List[Any]:
												_lowerCAmelCase							  =							parent
												_lowerCAmelCase							  =							batch_size
												_lowerCAmelCase							  =							image_size
												_lowerCAmelCase							  =							num_channels
												_lowerCAmelCase							  =							num_encoder_blocks
												_lowerCAmelCase							  =							sr_ratios
												_lowerCAmelCase							  =							depths
												_lowerCAmelCase							  =							hidden_sizes
												_lowerCAmelCase							  =							downsampling_rates
												_lowerCAmelCase							  =							is_training
												_lowerCAmelCase							  =							use_labels
												_lowerCAmelCase							  =							hidden_act
												_lowerCAmelCase							  =							hidden_dropout_prob
												_lowerCAmelCase							  =							initializer_range
												_lowerCAmelCase							  =							num_labels
												_lowerCAmelCase							  =							scope




						def        lowercase__					(    self    :				Optional[Any]						)       ->   Tuple:
												_lowerCAmelCase							  =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

												_lowerCAmelCase							  =							None
												if self.use_labels:
																		_lowerCAmelCase							  =							ids_tensor([self.batch_size, self.image_size, self.image_size]    ,    self.num_labels						)

												_lowerCAmelCase							  =							PoolFormerConfig(
												    image_size=self.image_size    ,    num_channels=self.num_channels    ,    num_encoder_blocks=self.num_encoder_blocks    ,    depths=self.depths    ,    hidden_sizes=self.hidden_sizes    ,    hidden_act=self.hidden_act    ,    hidden_dropout_prob=self.hidden_dropout_prob    ,    initializer_range=self.initializer_range    ,    )

												return config, pixel_values, labels




						def        lowercase__					(    self    :				List[Any]    ,    __snake_case    :				Tuple    ,    __snake_case    :				Tuple    ,    __snake_case    :				Dict						)       ->   Optional[Any]:
												_lowerCAmelCase							  =							PoolFormerModel(config=__snake_case						)
												model.to(__snake_case						)
												model.eval()
												_lowerCAmelCase							  =							model(__snake_case						)
												_lowerCAmelCase							  =							_lowerCAmelCase							  =							self.image_size // 32.0
												self.parent.assertEqual(
												    result.last_hidden_state.shape    ,    (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)						)




						def        lowercase__					(    self    :				Tuple						)       ->   Optional[Any]:
												_lowerCAmelCase							  =							self.prepare_config_and_inputs()
												_lowerCAmelCase  ,							_lowerCAmelCase  ,							_lowerCAmelCase							  =							config_and_inputs
												_lowerCAmelCase							  =							{"""pixel_values""": pixel_values}
												return config, inputs_dict




@require_torch
class 				UpperCAmelCase  (    snake_case_							,	snake_case_							,	unittest.TestCase     ):
						_lowercase:    int		  =     (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
						_lowercase:    str		  =     (
						    {'''feature-extraction''': PoolFormerModel, '''image-classification''': PoolFormerForImageClassification}
						    if is_torch_available()
						    else {}
						)

						_lowercase:    Any		  =     False
						_lowercase:    Optional[int]		  =     False
						_lowercase:    str		  =     False
						_lowercase:    List[Any]		  =     False
						_lowercase:    str		  =     False




						def        lowercase__					(    self    :				Dict						)       ->   List[str]:
												_lowerCAmelCase							  =							PoolFormerModelTester(self						)
												_lowerCAmelCase							  =							PoolFormerConfigTester(self    ,    config_class=__snake_case						)




						def        lowercase__					(    self    :				int						)       ->   Any:
												self.config_tester.run_common_tests()




						def        lowercase__					(    self    :				Optional[Any]						)       ->   Optional[int]:
												_lowerCAmelCase							  =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__snake_case						)




						@unittest.skip("""PoolFormer does not use inputs_embeds"""						)
						def        lowercase__					(    self    :				str						)       ->   Optional[int]:
												pass




						@unittest.skip("""PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"""						)
						def        lowercase__					(    self    :				Tuple						)       ->   Dict:
												pass




						def        lowercase__					(    self    :				Dict						)       ->   str:







												def check_hidden_states_output(__snake_case    :				Any    ,    __snake_case    :				int    ,    __snake_case    :				str						):
																		_lowerCAmelCase							  =							model_class(__snake_case						)
																		model.to(__snake_case						)
																		model.eval()

																		with torch.no_grad():
																								_lowerCAmelCase							  =							model(**self._prepare_for_class(__snake_case    ,    __snake_case						)						)

																		_lowerCAmelCase							  =							outputs.hidden_states

																		_lowerCAmelCase							  =							self.model_tester.num_encoder_blocks
																		self.assertEqual(len(__snake_case						)    ,    __snake_case						)

																		# verify the first hidden states (first block)
																		self.assertListEqual(
																		    list(hidden_states[0].shape[-3:]						)    ,    [
																		        self.model_tester.hidden_sizes[0],
																		        self.model_tester.image_size // 4,
																		        self.model_tester.image_size // 4,
																		    ]    ,    )

												_lowerCAmelCase  ,							_lowerCAmelCase							  =							self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		_lowerCAmelCase							  =							True
																		check_hidden_states_output(__snake_case    ,    __snake_case    ,    __snake_case						)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		_lowerCAmelCase							  =							True

																		check_hidden_states_output(__snake_case    ,    __snake_case    ,    __snake_case						)




						def        lowercase__					(    self    :				List[str]						)       ->   List[str]:







												if not self.model_tester.is_training:
																		return

												_lowerCAmelCase  ,							_lowerCAmelCase							  =							self.model_tester.prepare_config_and_inputs_for_common()
												_lowerCAmelCase							  =							True

												for model_class in self.all_model_classes:
																		if model_class in get_values(__snake_case						):
																								continue
																		_lowerCAmelCase							  =							model_class(__snake_case						)
																		model.to(__snake_case						)
																		model.train()
																		_lowerCAmelCase							  =							self._prepare_for_class(__snake_case    ,    __snake_case    ,    return_labels=__snake_case						)
																		_lowerCAmelCase							  =							model(**__snake_case						).loss
																		loss.backward()




						def        lowercase__					(    self    :				Optional[Any]						)       ->   Any:
												_lowerCAmelCase  ,							_lowerCAmelCase							  =							self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		_lowerCAmelCase							  =							model_class(__snake_case						)
																		_lowerCAmelCase							  =							inspect.signature(model.forward						)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		_lowerCAmelCase							  =							[*signature.parameters.keys()]

																		_lowerCAmelCase							  =							["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]    ,    __snake_case						)




						@slow
						def        lowercase__					(    self    :				List[str]						)       ->   List[str]:







												for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_lowerCAmelCase							  =							PoolFormerModel.from_pretrained(__snake_case						)
																		self.assertIsNotNone(__snake_case						)




def      UpperCamelCase__   (	):





						"""simple docstring"""


						_lowerCAmelCase							  =							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
						return image





@require_torch
class 				UpperCAmelCase  (    unittest.TestCase     ):




						@slow
						def        lowercase__					(    self    :				str						)       ->   Dict:
												_lowerCAmelCase							  =							PoolFormerImageProcessor()
												_lowerCAmelCase							  =							PoolFormerForImageClassification.from_pretrained("""sail/poolformer_s12"""						).to(__snake_case						)

												_lowerCAmelCase							  =							image_processor(images=prepare_img()    ,    return_tensors="""pt"""						).to(__snake_case						)

												# forward pass
												with torch.no_grad():
																		_lowerCAmelCase							  =							model(**__snake_case						)

												# verify the logits
												_lowerCAmelCase							  =							torch.Size((1, 10_00)						)
												self.assertEqual(outputs.logits.shape    ,    __snake_case						)

												_lowerCAmelCase							  =							torch.tensor([-0.61_13, 0.16_85, -0.04_92]						).to(__snake_case						)
												self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,    __snake_case    ,    atol=1E-4						)						)













'''simple docstring'''






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




      def __init__(    self    :				str    ,    __snake_case    :				str    ,    __snake_case    :				str=13    ,    __snake_case    :				Optional[Any]=7    ,    __snake_case    :				str=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=True    ,    __snake_case    :				Any=True    ,    __snake_case    :				List[str]=99    ,    __snake_case    :				Dict=24    ,    __snake_case    :				Any=32    ,    __snake_case    :				List[str]=5    ,    __snake_case    :				Any=4    ,    __snake_case    :				Optional[Any]=37    ,    __snake_case    :				Union[str, Any]="gelu"    ,    __snake_case    :				Dict=0.1    ,    __snake_case    :				int=0.1    ,    __snake_case    :				Dict=5_12    ,    __snake_case    :				List[Any]=16    ,    __snake_case    :				Dict=2    ,    __snake_case    :				str=0.02    ,    __snake_case    :				Any=4    ,    )       ->   Union[str, Any]:
            _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        lowercase__					(    self    :				Optional[Any]						)       ->   Optional[Any]:
            _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        lowercase__					(    self    :				Any						)       ->   Optional[int]:
            _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 				UpperCAmelCase  (    snake_case_							,	unittest.TestCase     ):
      _lowercase:    str		  =     True

      _lowercase:    Optional[Any]		  =     (
          (
              FlaxElectraModel,
              FlaxElectraForCausalLM,
              FlaxElectraForMaskedLM,
              FlaxElectraForPreTraining,
              FlaxElectraForTokenClassification,
              FlaxElectraForQuestionAnswering,
              FlaxElectraForMultipleChoice,
              FlaxElectraForSequenceClassification,
          )
          if is_flax_available()
          else ()
      )




      def        lowercase__					(    self    :				Tuple						)       ->   Optional[int]:
            _lowerCAmelCase							  =							FlaxElectraModelTester(self						)




      @slow
      def        lowercase__					(    self    :				List[Any]						)       ->   str:
            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(__snake_case						)






