


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   (       UpperCAmelCase    ):
		def 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





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




									"""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 			UpperCAmelCase__       (    self		)				-> Dict:




									"""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 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    PoolFormerModel(config=_SCREAMING_SNAKE_CASE		)
									model.to(_SCREAMING_SNAKE_CASE		)
									model.eval()
									_UpperCAmelCase					=    model(_SCREAMING_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 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""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   (       UpperCAmelCase					,   UpperCAmelCase					,   unittest.TestCase    ):
		_a			:						List[str]    			=			(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
		_a			:						Tuple    			=			(
		    {'feature-extraction': PoolFormerModel, 'image-classification': PoolFormerForImageClassification}
		    if is_torch_available()
		    else {}
		)

		_a			:						Union[str, Any]    			=			False
		_a			:						Optional[Any]    			=			False
		_a			:						List[Any]    			=			False
		_a			:						List[str]    			=			False
		_a			:						Dict    			=			False
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    PoolFormerModelTester(self		)
									_UpperCAmelCase					=    PoolFormerConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									self.config_tester.run_common_tests()
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    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 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									pass
		@unittest.skip('PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									pass
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





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

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

																_UpperCAmelCase					=    outputs.hidden_states

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

									_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(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)

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

																check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""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(_SCREAMING_SNAKE_CASE		):
																							continue
																_UpperCAmelCase					=    model_class(_SCREAMING_SNAKE_CASE		)
																model.to(_SCREAMING_SNAKE_CASE		)
																model.train()
																_UpperCAmelCase					=    self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					return_labels=_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase					=    model(**_SCREAMING_SNAKE_CASE		).loss
																loss.backward()
		def 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





									_UpperCAmelCase	, _UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_UpperCAmelCase					=    model_class(_SCREAMING_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]    ,					_SCREAMING_SNAKE_CASE		)






		@slow
		def 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""simple docstring"""





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



def  lowerCAmelCase__				(    )						->	Tuple:


							'''simple docstring'''





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







@require_torch
class 	__a   (       unittest.TestCase    ):
		@slow
		def 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    PoolFormerImageProcessor()
									_UpperCAmelCase					=    PoolFormerForImageClassification.from_pretrained('sail/poolformer_s12'		).to(_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase					=    image_processor(images=prepare_img()    ,					return_tensors='pt'		).to(_SCREAMING_SNAKE_CASE		)

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

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

									_UpperCAmelCase					=    torch.tensor([-0.6113, 0.1685, -0.0492]		).to(_SCREAMING_SNAKE_CASE		)
									self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,					_SCREAMING_SNAKE_CASE    ,					atol=1e-4		)		)




from __future__ import annotations

def  lowerCAmelCase__				(    a__:	list[int]    ,						a__:	int )						->	list[int]:


							'''simple docstring'''





							_UpperCAmelCase					=    {}
							for index, val in enumerate(a__ ):
														_UpperCAmelCase					=    target - val
														if compl in chk_map:
																					return [chk_map[compl], index]
														_UpperCAmelCase					=    index
							return []


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		print(f'''{two_sum([2, 7, 1_1, 1_5], 9) = }''')
