





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      A_	(a_      ):
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    self.config_class(**self.inputs_dict		)
				self.parent.assertTrue(hasattr(_A							, '''hidden_sizes'''		)		)
				self.parent.assertTrue(hasattr(_A							, '''num_encoder_blocks'''		)		)

class      A_	:
	def __init__(   self							, _A							, _A=1_3							, _A=6_4							, _A=3							, _A=4							, _A=[2, 2, 2, 2]							, _A=[8, 4, 2, 1]							, _A=[1_6, 3_2, 6_4, 1_2_8]							, _A=[1, 4, 8, 1_6]							, _A=False							, _A=True							, _A="gelu"							, _A=0.1							, _A=0.02							, _A=3							, _A=None							, ):






				'''simple docstring'''







				UpperCAmelCase       		=    parent
				UpperCAmelCase       		=    batch_size
				UpperCAmelCase       		=    image_size
				UpperCAmelCase       		=    num_channels
				UpperCAmelCase       		=    num_encoder_blocks
				UpperCAmelCase       		=    sr_ratios
				UpperCAmelCase       		=    depths
				UpperCAmelCase       		=    hidden_sizes
				UpperCAmelCase       		=    downsampling_rates
				UpperCAmelCase       		=    is_training
				UpperCAmelCase       		=    use_labels
				UpperCAmelCase       		=    hidden_act
				UpperCAmelCase       		=    hidden_dropout_prob
				UpperCAmelCase       		=    initializer_range
				UpperCAmelCase       		=    num_labels
				UpperCAmelCase       		=    scope
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

				UpperCAmelCase       		=    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							, _A							, _A							, _A		):






				'''simple docstring'''







				UpperCAmelCase       		=    PoolFormerModel(config=_A		)
				model.to(_A		)
				model.eval()
				UpperCAmelCase       		=    model(_A		)
				UpperCAmelCase       		=    UpperCAmelCase       		=    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		):






				'''simple docstring'''







				UpperCAmelCase       		=    self.prepare_config_and_inputs()
				UpperCAmelCase		,      UpperCAmelCase		,      UpperCAmelCase       		=    config_and_inputs
				UpperCAmelCase       		=    {'''pixel_values''': pixel_values}
				return config, inputs_dict

@require_torch
class      A_	(a_					,							a_					,							unittest.TestCase      ):
	UpperCAmelCase__ =      (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
	UpperCAmelCase__ =      (
	    {'''feature-extraction''': PoolFormerModel, '''image-classification''': PoolFormerForImageClassification}
	    if is_torch_available()
	    else {}
	)

	UpperCAmelCase__ =      False
	UpperCAmelCase__ =      False
	UpperCAmelCase__ =      False
	UpperCAmelCase__ =      False
	UpperCAmelCase__ =      False
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    PoolFormerModelTester(self		)
				UpperCAmelCase       		=    PoolFormerConfigTester(self							, config_class=_A		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







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






				'''simple docstring'''







				UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A		)
	@unittest.skip('''PoolFormer does not use inputs_embeds'''		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







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






				'''simple docstring'''







				pass
	def 					_lowercase    (   self		):






				'''simple docstring'''







				def check_hidden_states_output(_A							, _A							, _A		):
							UpperCAmelCase       		=    model_class(_A		)
							model.to(_A		)
							model.eval()

							with torch.no_grad():
										UpperCAmelCase       		=    model(**self._prepare_for_class(_A							, _A		)		)

							UpperCAmelCase       		=    outputs.hidden_states

							UpperCAmelCase       		=    self.model_tester.num_encoder_blocks
							self.assertEqual(len(_A		)							, _A		)

							# 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,
							    ]							, )

				UpperCAmelCase		,      UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							UpperCAmelCase       		=    True
							check_hidden_states_output(_A							, _A							, _A		)

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

							check_hidden_states_output(_A							, _A							, _A		)
	def 					_lowercase    (   self		):






				'''simple docstring'''







				if not self.model_tester.is_training:
							return

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

				for model_class in self.all_model_classes:
							if model_class in get_values(_A		):
										continue
							UpperCAmelCase       		=    model_class(_A		)
							model.to(_A		)
							model.train()
							UpperCAmelCase       		=    self._prepare_for_class(_A							, _A							, return_labels=_A		)
							UpperCAmelCase       		=    model(**_A		).loss
							loss.backward()
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase		,      UpperCAmelCase       		=    self.model_tester.prepare_config_and_inputs_for_common()

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

							UpperCAmelCase       		=    ['''pixel_values''']
							self.assertListEqual(arg_names[:1]							, _A		)







	@slow
	def 					_lowercase    (   self		):






				'''simple docstring'''







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





def 						__SCREAMING_SNAKE_CASE  (    )  ->    Dict:







			'''simple docstring'''

			UpperCAmelCase       		=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
			return image






@require_torch
class      A_	(unittest.TestCase      ):
	@slow
	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    PoolFormerImageProcessor()
				UpperCAmelCase       		=    PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''		).to(_A		)

				UpperCAmelCase       		=    image_processor(images=prepare_img()							, return_tensors='''pt'''		).to(_A		)

				# forward pass
				with torch.no_grad():
							UpperCAmelCase       		=    model(**_A		)

				# verify the logits
				UpperCAmelCase       		=    torch.Size((1, 1_0_0_0)		)
				self.assertEqual(outputs.logits.shape							, _A		)

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










import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


__A    :   Optional[int]          =       logging.get_logger(__name__)

class      A_	(a_      ):
	UpperCAmelCase__ =      '''speech-encoder-decoder'''
	UpperCAmelCase__ =      True
	def __init__(   self							, **_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				if "encoder" not in kwargs or "decoder" not in kwargs:
							raise ValueError(
							    F"""A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and"""
							    F""" `decoder` sub-configurations are passed, but only {kwargs}"""		)

				UpperCAmelCase       		=    kwargs.pop('''encoder'''		)
				UpperCAmelCase       		=    encoder_config.pop('''model_type'''		)
				UpperCAmelCase       		=    kwargs.pop('''decoder'''		)
				UpperCAmelCase       		=    decoder_config.pop('''model_type'''		)

				UpperCAmelCase       		=    AutoConfig.for_model(_A							, **_A		)
				UpperCAmelCase       		=    AutoConfig.for_model(_A							, **_A		)
				UpperCAmelCase       		=    True
	@classmethod
	def 					_lowercase    (   cls							, _A							, _A							, **_A		):






				'''simple docstring'''







				logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''		)
				UpperCAmelCase       		=    True
				UpperCAmelCase       		=    True

				return cls(encoder=encoder_config.to_dict()							, decoder=decoder_config.to_dict()							, **_A		)







	def 					_lowercase    (   self		):






				'''simple docstring'''







				UpperCAmelCase       		=    copy.deepcopy(self.__dict__		)
				UpperCAmelCase       		=    self.encoder.to_dict()
				UpperCAmelCase       		=    self.decoder.to_dict()
				UpperCAmelCase       		=    self.__class__.model_type
				return output



