




'''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 	lowerCamelCase_				(			lowerCAmelCase_				):




					'''simple docstring'''






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case      			=			self.config_class(**self.inputs_dict					)
							self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE,    "hidden_sizes"					)					)
							self.parent.assertTrue(hasattr(_SCREAMING_SNAKE_CASE,    "num_encoder_blocks"					)					)





class 	lowerCamelCase_				:




					'''simple docstring'''






					def __init__(       self,    __UpperCamelCase,    __UpperCamelCase=13,    __UpperCamelCase=64,    __UpperCamelCase=3,    __UpperCamelCase=4,    __UpperCamelCase=[2, 2, 2, 2],    __UpperCamelCase=[8, 4, 2, 1],    __UpperCamelCase=[16, 32, 64, 128],    __UpperCamelCase=[1, 4, 8, 16],    __UpperCamelCase=False,    __UpperCamelCase=True,    __UpperCamelCase="gelu",    __UpperCamelCase=0.1,    __UpperCamelCase=0.02,    __UpperCamelCase=3,    __UpperCamelCase=None,    ):
							_snake_case      			=			parent
							_snake_case      			=			batch_size
							_snake_case      			=			image_size
							_snake_case      			=			num_channels
							_snake_case      			=			num_encoder_blocks
							_snake_case      			=			sr_ratios
							_snake_case      			=			depths
							_snake_case      			=			hidden_sizes
							_snake_case      			=			downsampling_rates
							_snake_case      			=			is_training
							_snake_case      			=			use_labels
							_snake_case      			=			hidden_act
							_snake_case      			=			hidden_dropout_prob
							_snake_case      			=			initializer_range
							_snake_case      			=			num_labels
							_snake_case      			=			scope






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case      			=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

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

							_snake_case      			=			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 							__SCREAMING_SNAKE_CASE      (       self,    __UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase					):
							_snake_case      			=			PoolFormerModel(config=_SCREAMING_SNAKE_CASE					)
							model.to(_SCREAMING_SNAKE_CASE					)
							model.eval()
							_snake_case      			=			model(_SCREAMING_SNAKE_CASE					)
							_snake_case      			=			_snake_case      			=			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 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case      			=			self.prepare_config_and_inputs()
							_snake_case			,       _snake_case			,       _snake_case      			=			config_and_inputs
							_snake_case      			=			{"pixel_values": pixel_values}
							return config, inputs_dict





@require_torch
class 	lowerCamelCase_				(			lowerCAmelCase_,			lowerCAmelCase_,			unittest.TestCase				):




					'''simple docstring'''






					lowercase__       =					(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
					lowercase__       =					(
					    {'''feature-extraction''': PoolFormerModel, '''image-classification''': PoolFormerForImageClassification}
					    if is_torch_available()
					    else {}
					)

					lowercase__       =					False
					lowercase__       =					False
					lowercase__       =					False
					lowercase__       =					False
					lowercase__       =					False






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case      			=			PoolFormerModelTester(self					)
							_snake_case      			=			PoolFormerConfigTester(self,    config_class=_SCREAMING_SNAKE_CASE					)






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							self.config_tester.run_common_tests()






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case      			=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE					)






					@unittest.skip("PoolFormer does not use inputs_embeds"					)
					def 							__SCREAMING_SNAKE_CASE      (       self					):
							pass






					@unittest.skip("PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"					)
					def 							__SCREAMING_SNAKE_CASE      (       self					):
							pass






					def 							__SCREAMING_SNAKE_CASE      (       self					):


							def check_hidden_states_output(__UpperCamelCase,    __UpperCamelCase,    __UpperCamelCase					):
									_snake_case      			=			model_class(_SCREAMING_SNAKE_CASE					)
									model.to(_SCREAMING_SNAKE_CASE					)
									model.eval()

									with torch.no_grad():
											_snake_case      			=			model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE,    _SCREAMING_SNAKE_CASE					)					)

									_snake_case      			=			outputs.hidden_states

									_snake_case      			=			self.model_tester.num_encoder_blocks
									self.assertEqual(len(_SCREAMING_SNAKE_CASE					),    _SCREAMING_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,
									    ],    )

							_snake_case			,       _snake_case      			=			self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case      			=			True
									check_hidden_states_output(_SCREAMING_SNAKE_CASE,    _SCREAMING_SNAKE_CASE,    _SCREAMING_SNAKE_CASE					)

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

									check_hidden_states_output(_SCREAMING_SNAKE_CASE,    _SCREAMING_SNAKE_CASE,    _SCREAMING_SNAKE_CASE					)






					def 							__SCREAMING_SNAKE_CASE      (       self					):


							if not self.model_tester.is_training:
									return

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

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






					def 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case			,       _snake_case      			=			self.model_tester.prepare_config_and_inputs_for_common()

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

									_snake_case      			=			["pixel_values"]
									self.assertListEqual(arg_names[:1],    _SCREAMING_SNAKE_CASE					)






					@slow
					def 							__SCREAMING_SNAKE_CASE      (       self					):


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






def 						_lowercase						(      ):



		'''simple docstring'''







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





@require_torch
class 	lowerCamelCase_				(			unittest.TestCase				):




					'''simple docstring'''






					@slow
					def 							__SCREAMING_SNAKE_CASE      (       self					):
							_snake_case      			=			PoolFormerImageProcessor()
							_snake_case      			=			PoolFormerForImageClassification.from_pretrained("sail/poolformer_s12"					).to(_SCREAMING_SNAKE_CASE					)

							_snake_case      			=			image_processor(images=prepare_img(),    return_tensors="pt"					).to(_SCREAMING_SNAKE_CASE					)

							# forward pass
							with torch.no_grad():
									_snake_case      			=			model(**_SCREAMING_SNAKE_CASE					)

							# verify the logits
							_snake_case      			=			torch.Size((1, 1_000)					)
							self.assertEqual(outputs.logits.shape,    _SCREAMING_SNAKE_CASE					)

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












from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_UpperCAmelCase										=					logging.get_logger(__name__)

_UpperCAmelCase										=					{
    'mnaylor/mega-base-wikitext': 'https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json',
}



class 				_UpperCamelCase       (						lowerCAmelCase_  ):
       _UpperCamelCase					:       Union[str, Any]       = '''mega'''

       def __init__(		self:			Optional[int]       , _SCREAMING_SNAKE_CASE:			Union[str, Any]=30522       , _SCREAMING_SNAKE_CASE:			Optional[Any]=128       , _SCREAMING_SNAKE_CASE:			Any=4       , _SCREAMING_SNAKE_CASE:			Tuple=256       , _SCREAMING_SNAKE_CASE:			List[str]=16       , _SCREAMING_SNAKE_CASE:			Dict=True       , _SCREAMING_SNAKE_CASE:			Optional[int]=64       , _SCREAMING_SNAKE_CASE:			Union[str, Any]=False       , _SCREAMING_SNAKE_CASE:			List[Any]=-1       , _SCREAMING_SNAKE_CASE:			Tuple=None       , _SCREAMING_SNAKE_CASE:			Optional[Any]=True       , _SCREAMING_SNAKE_CASE:			Optional[int]="scalenorm"       , _SCREAMING_SNAKE_CASE:			int=True       , _SCREAMING_SNAKE_CASE:			List[str]="silu"       , _SCREAMING_SNAKE_CASE:			int="softmax"       , _SCREAMING_SNAKE_CASE:			List[Any]=0.1       , _SCREAMING_SNAKE_CASE:			Optional[int]=0.1       , _SCREAMING_SNAKE_CASE:			Optional[Any]=0.1       , _SCREAMING_SNAKE_CASE:			Tuple=False       , _SCREAMING_SNAKE_CASE:			List[Any]=True       , _SCREAMING_SNAKE_CASE:			Tuple=256       , _SCREAMING_SNAKE_CASE:			Any=True       , _SCREAMING_SNAKE_CASE:			List[Any]=0.1       , _SCREAMING_SNAKE_CASE:			Optional[Any]=2048       , _SCREAMING_SNAKE_CASE:			Any=False       , _SCREAMING_SNAKE_CASE:			Optional[Any]=2       , _SCREAMING_SNAKE_CASE:			List[str]=0.02       , _SCREAMING_SNAKE_CASE:			Tuple=0.2       , _SCREAMING_SNAKE_CASE:			Optional[int]=0.02       , _SCREAMING_SNAKE_CASE:			Tuple=1.0       , _SCREAMING_SNAKE_CASE:			List[str]=1       , _SCREAMING_SNAKE_CASE:			List[Any]=0       , _SCREAMING_SNAKE_CASE:			Optional[Any]=2       , _SCREAMING_SNAKE_CASE:			Optional[int]="rotary"       , _SCREAMING_SNAKE_CASE:			Optional[Any]=None       , _SCREAMING_SNAKE_CASE:			Any=True       , _SCREAMING_SNAKE_CASE:			Optional[Any]=True       , **_SCREAMING_SNAKE_CASE:			Union[str, Any]       , )							->			Optional[Any]:




              """simple docstring"""


              super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE       , bos_token_id=_SCREAMING_SNAKE_CASE       , eos_token_id=_SCREAMING_SNAKE_CASE       , **_SCREAMING_SNAKE_CASE		)

              UpperCamelCase_ 							=	vocab_size
              UpperCamelCase_ 							=	hidden_size
              UpperCamelCase_ 							=	num_hidden_layers
              UpperCamelCase_ 							=	activation
              UpperCamelCase_ 							=	attention_activation
              UpperCamelCase_ 							=	intermediate_size
              UpperCamelCase_ 							=	ema_projection_size
              UpperCamelCase_ 							=	bidirectional
              UpperCamelCase_ 							=	shared_representation_size
              UpperCamelCase_ 							=	use_chunking
              UpperCamelCase_ 							=	chunk_size
              UpperCamelCase_ 							=	truncation
              UpperCamelCase_ 							=	normalize_before_mega
              UpperCamelCase_ 							=	normalization_type
              UpperCamelCase_ 							=	norm_affine
              UpperCamelCase_ 							=	dropout_prob
              UpperCamelCase_ 							=	hidden_dropout_prob
              UpperCamelCase_ 							=	attention_probs_dropout_prob
              UpperCamelCase_ 							=	use_feature_dropout
              UpperCamelCase_ 							=	use_normalized_ffn
              UpperCamelCase_ 							=	nffn_hidden_size
              UpperCamelCase_ 							=	normalize_before_ffn
              UpperCamelCase_ 							=	nffn_activation_dropout_prob
              UpperCamelCase_ 							=	max_positions
              UpperCamelCase_ 							=	add_token_type_embeddings
              UpperCamelCase_ 							=	type_vocab_size
              UpperCamelCase_ 							=	initializer_range
              UpperCamelCase_ 							=	ema_delta_alpha_range
              UpperCamelCase_ 							=	ema_beta_range
              UpperCamelCase_ 							=	ema_gamma_omega_range
              UpperCamelCase_ 							=	relative_positional_bias
              UpperCamelCase_ 							=	use_cache
              UpperCamelCase_ 							=	classifier_dropout
              UpperCamelCase_ 							=	add_lm_hidden_dense_layer
              UpperCamelCase_ 							=	1  # not used but required by Hugging Face



class 				_UpperCamelCase       (						lowerCAmelCase_  ):

       @property
       def lowercase			(		self:			Optional[int]		)							->			Mapping[str, Mapping[int, str]]:




              """simple docstring"""


              if self.task == "multiple-choice":
                     UpperCamelCase_ 							=	{0: "batch", 1: "choice", 2: "sequence"}
              else:
                     UpperCamelCase_ 							=	{0: "batch", 1: "sequence"}
              return OrderedDict(
                  [
                      ("input_ids", dynamic_axis),
                      ("attention_mask", dynamic_axis),
                  ]		)

