



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    _lowercase (a_   ):
				'''simple docstring'''







				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= self.config_class(**self.inputs_dict     )
									self.parent.assertTrue(hasattr(snake_case__  ,			"hidden_sizes"     )     )
									self.parent.assertTrue(hasattr(snake_case__  ,			"num_encoder_blocks"     )     )




class    _lowercase :
				'''simple docstring'''







				def __init__(			self  ,			snake_case__  ,			snake_case__=13  ,			snake_case__=64  ,			snake_case__=3  ,			snake_case__=4  ,			snake_case__=[2, 2, 2, 2]  ,			snake_case__=[8, 4, 2, 1]  ,			snake_case__=[16, 32, 64, 128]  ,			snake_case__=[1, 4, 8, 16]  ,			snake_case__=False  ,			snake_case__=True  ,			snake_case__="gelu"  ,			snake_case__=0.1  ,			snake_case__=0.02  ,			snake_case__=3  ,			snake_case__=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    _lowerCamelCase       (			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    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__  ,			snake_case__     ):

									'''simple docstring'''


									UpperCamelCase_			= PoolFormerModel(config=snake_case__     )
									model.to(snake_case__     )
									model.eval()
									UpperCamelCase_			= model(snake_case__     )
									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    _lowerCamelCase       (			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    _lowercase (a_   , a_   , 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    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= PoolFormerModelTester(self     )
									UpperCamelCase_			= PoolFormerConfigTester(self  ,			config_class=snake_case__     )

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									self.config_tester.run_common_tests()

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= 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    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									pass

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

									'''simple docstring'''


									pass

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									def check_hidden_states_output(snake_case__  ,			snake_case__  ,			snake_case__     ):
														UpperCamelCase_			= model_class(snake_case__     )
														model.to(snake_case__     )
														model.eval()

														with torch.no_grad():
																			UpperCamelCase_			= model(**self._prepare_for_class(snake_case__  ,			snake_case__     )     )

														UpperCamelCase_			= outputs.hidden_states

														UpperCamelCase_			= 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,
														    ]  ,			)

									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(snake_case__  ,			snake_case__  ,			snake_case__     )

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

														check_hidden_states_output(snake_case__  ,			snake_case__  ,			snake_case__     )

				def    _lowerCamelCase       (			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(snake_case__     ):
																			continue
														UpperCamelCase_			= model_class(snake_case__     )
														model.to(snake_case__     )
														model.train()
														UpperCamelCase_			= self._prepare_for_class(snake_case__  ,			snake_case__  ,			return_labels=snake_case__     )
														UpperCamelCase_			= model(**snake_case__     ).loss
														loss.backward()

				def    _lowerCamelCase       (			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(snake_case__     )
														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]  ,			snake_case__     )

				@slow
				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


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




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






@require_torch
class    _lowercase (unittest.TestCase   ):
				'''simple docstring'''







				@slow
				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= PoolFormerImageProcessor()
									UpperCamelCase_			= PoolFormerForImageClassification.from_pretrained("sail/poolformer_s12"     ).to(snake_case__     )

									UpperCamelCase_			= image_processor(images=prepare_img()  ,			return_tensors="pt"     ).to(snake_case__     )

									# forward pass
									with torch.no_grad():
														UpperCamelCase_			= model(**snake_case__     )

									# verify the logits
									UpperCamelCase_			= torch.Size((1, 1000)     )
									self.assertEqual(outputs.logits.shape  ,			snake_case__     )

									UpperCamelCase_			= torch.tensor([-0.6_113, 0.1_685, -0.0_492]     ).to(snake_case__     )
									self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,			snake_case__  ,			atol=1e-4     )     )





import copy

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


UpperCAmelCase :				Union[str, Any]							=logging.get_logger(__name__)


class    _lowercase (a_   ):
				'''simple docstring'''







				lowercase__     =    """speech-encoder-decoder"""
				lowercase__     =    True

				def __init__(			self  ,			**snake_case__     ):

									'''simple docstring'''


									super().__init__(**snake_case__     )
									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(snake_case__  ,			**snake_case__     )
									UpperCamelCase_			= AutoConfig.for_model(snake_case__  ,			**snake_case__     )
									UpperCamelCase_			= True

				@classmethod
				def    _lowerCamelCase       (			cls  ,			snake_case__  ,			snake_case__  ,			**snake_case__     ):

									'''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()  ,			**snake_case__     )

				def    _lowerCamelCase       (			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
