



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__      (      __UpperCamelCase       ):




			'''simple docstring'''






			def 			snake_case__					(       self							:			Dict			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     self.config_class(**self.inputs_dict			)
					self.parent.assertTrue(hasattr(a_	,	'''hidden_sizes'''			)			)
					self.parent.assertTrue(hasattr(a_	,	'''num_encoder_blocks'''			)			)

class 		UpperCAmelCase__      :




			'''simple docstring'''






			def __init__(       self							:			Union[str, Any]	,	a_							:			Union[str, Any]	,	a_							:			List[str]=13	,	a_							:			Union[str, Any]=64	,	a_							:			Optional[Any]=3	,	a_							:			Any=4	,	a_							:			str=[2, 2, 2, 2]	,	a_							:			Union[str, Any]=[8, 4, 2, 1]	,	a_							:			Optional[int]=[16, 32, 64, 1_28]	,	a_							:			str=[1, 4, 8, 16]	,	a_							:			List[str]=False	,	a_							:			Optional[Any]=True	,	a_							:			int="gelu"	,	a_							:			str=0.1	,	a_							:			Union[str, Any]=0.0_2	,	a_							:			List[str]=3	,	a_							:			Dict=None	,	):
					'''simple docstring'''





					__UpperCAmelCase   :					Optional[int]													=     parent
					__UpperCAmelCase   :					List[str]													=     batch_size
					__UpperCAmelCase   :					Any													=     image_size
					__UpperCAmelCase   :					Optional[Any]													=     num_channels
					__UpperCAmelCase   :					Any													=     num_encoder_blocks
					__UpperCAmelCase   :					Optional[int]													=     sr_ratios
					__UpperCAmelCase   :					Tuple													=     depths
					__UpperCAmelCase   :					Optional[Any]													=     hidden_sizes
					__UpperCAmelCase   :					str													=     downsampling_rates
					__UpperCAmelCase   :					Dict													=     is_training
					__UpperCAmelCase   :					Optional[int]													=     use_labels
					__UpperCAmelCase   :					List[str]													=     hidden_act
					__UpperCAmelCase   :					Union[str, Any]													=     hidden_dropout_prob
					__UpperCAmelCase   :					List[str]													=     initializer_range
					__UpperCAmelCase   :					List[str]													=     num_labels
					__UpperCAmelCase   :					Optional[Any]													=     scope




			def 			snake_case__					(       self							:			Optional[Any]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Any													=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

					__UpperCAmelCase   :					str													=     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 			snake_case__					(       self							:			Optional[int]	,	a_							:			Optional[int]	,	a_							:			int	,	a_							:			Tuple			):
					'''simple docstring'''





					__UpperCAmelCase   :					List[str]													=     PoolFormerModel(config=a_			)
					model.to(a_			)
					model.eval()
					__UpperCAmelCase   :					str													=     model(a_			)
					__UpperCAmelCase   :					Optional[Any]													=     self.image_size // 3_2.0
					self.parent.assertEqual(
					    result.last_hidden_state.shape	,	(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)			)




			def 			snake_case__					(       self							:			Any			):
					'''simple docstring'''





					__UpperCAmelCase   :					Tuple													=     self.prepare_config_and_inputs()
					__UpperCAmelCase							, __UpperCAmelCase							, __UpperCAmelCase   :					Dict													=     config_and_inputs
					__UpperCAmelCase   :					Optional[Any]													=     {'''pixel_values''': pixel_values}
					return config, inputs_dict

@require_torch
class 		UpperCAmelCase__      (      __UpperCamelCase						,__UpperCamelCase						,unittest.TestCase       ):




			'''simple docstring'''






			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 			snake_case__					(       self							:			Dict			):
					'''simple docstring'''





					__UpperCAmelCase   :					List[Any]													=     PoolFormerModelTester(self			)
					__UpperCAmelCase   :					Union[str, Any]													=     PoolFormerConfigTester(self	,	config_class=a_			)




			def 			snake_case__					(       self							:			Union[str, Any]			):
					'''simple docstring'''





					self.config_tester.run_common_tests()




			def 			snake_case__					(       self							:			Tuple			):
					'''simple docstring'''





					__UpperCAmelCase   :					Any													=     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*a_			)




			@unittest.skip('''PoolFormer does not use inputs_embeds'''			)
			def 			snake_case__					(       self							:			Any			):
					'''simple docstring'''





					pass




			@unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''			)
			def 			snake_case__					(       self							:			Dict			):
					'''simple docstring'''





					pass




			def 			snake_case__					(       self							:			Union[str, Any]			):
					'''simple docstring'''





					def check_hidden_states_output(a_							:			Any	,	a_							:			List[str]	,	a_							:			Union[str, Any]			):
							__UpperCAmelCase   :					int													=     model_class(a_			)
							model.to(a_			)
							model.eval()

							with torch.no_grad():
									__UpperCAmelCase   :					Optional[int]													=     model(**self._prepare_for_class(a_	,	a_			)			)

							__UpperCAmelCase   :					Tuple													=     outputs.hidden_states

							__UpperCAmelCase   :					List[Any]													=     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   :					int													=     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__UpperCAmelCase   :					Any													=     True
							check_hidden_states_output(a_	,	a_	,	a_			)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							__UpperCAmelCase   :					Optional[Any]													=     True

							check_hidden_states_output(a_	,	a_	,	a_			)




			def 			snake_case__					(       self							:			Optional[Any]			):
					'''simple docstring'''





					if not self.model_tester.is_training:
							return

					__UpperCAmelCase							, __UpperCAmelCase   :					List[Any]													=     self.model_tester.prepare_config_and_inputs_for_common()
					__UpperCAmelCase   :					Union[str, Any]													=     True

					for model_class in self.all_model_classes:
							if model_class in get_values(a_			):
									continue
							__UpperCAmelCase   :					List[str]													=     model_class(a_			)
							model.to(a_			)
							model.train()
							__UpperCAmelCase   :					List[str]													=     self._prepare_for_class(a_	,	a_	,	return_labels=a_			)
							__UpperCAmelCase   :					str													=     model(**a_			).loss
							loss.backward()




			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase							, __UpperCAmelCase   :					Optional[Any]													=     self.model_tester.prepare_config_and_inputs_for_common()

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

							__UpperCAmelCase   :					str													=     ['''pixel_values''']
							self.assertListEqual(arg_names[:1]	,	a_			)




			@slow
			def 			snake_case__					(       self							:			List[str]			):
					'''simple docstring'''





					for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__UpperCAmelCase   :					int													=     PoolFormerModel.from_pretrained(a_			)
							self.assertIsNotNone(a_			)

def 	a						(				):







		'''simple docstring'''





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

@require_torch
class 		UpperCAmelCase__      (      unittest.TestCase       ):




			'''simple docstring'''






			@slow
			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					List[str]													=     PoolFormerImageProcessor()
					__UpperCAmelCase   :					List[str]													=     PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''			).to(a_			)

					__UpperCAmelCase   :					Optional[int]													=     image_processor(images=prepare_img()	,	return_tensors='''pt'''			).to(a_			)

					# forward pass
					with torch.no_grad():
							__UpperCAmelCase   :					Tuple													=     model(**a_			)

					# verify the logits
					__UpperCAmelCase   :					Union[str, Any]													=     torch.Size((1, 10_00)			)
					self.assertEqual(outputs.logits.shape	,	a_			)

					__UpperCAmelCase   :					Union[str, Any]													=     torch.tensor([-0.6_1_1_3, 0.1_6_8_5, -0.0_4_9_2]			).to(a_			)
					self.assertTrue(torch.allclose(outputs.logits[0, :3]	,	a_	,	atol=1e-4			)			)










from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A             =logging.get_logger(__name__)

__A             ={
    "tau/splinter-base": "https://huggingface.co/tau/splinter-base/resolve/main/config.json",
    "tau/splinter-base-qass": "https://huggingface.co/tau/splinter-base-qass/resolve/main/config.json",
    "tau/splinter-large": "https://huggingface.co/tau/splinter-large/resolve/main/config.json",
    "tau/splinter-large-qass": "https://huggingface.co/tau/splinter-large-qass/resolve/main/config.json",
    # See all Splinter models at https://huggingface.co/models?filter=splinter
}

class 		UpperCAmelCase__      (      __UpperCamelCase       ):




			'''simple docstring'''






			UpperCamelCase      						=	"""splinter"""




			def __init__(       self							:			Any	,	a_							:			Optional[int]=3_05_22	,	a_							:			Union[str, Any]=7_68	,	a_							:			int=12	,	a_							:			int=12	,	a_							:			List[Any]=30_72	,	a_							:			Union[str, Any]="gelu"	,	a_							:			Any=0.1	,	a_							:			Union[str, Any]=0.1	,	a_							:			Any=5_12	,	a_							:			str=2	,	a_							:			Dict=0.0_2	,	a_							:			int=1e-12	,	a_							:			str=True	,	a_							:			Tuple=0	,	a_							:			List[Any]=1_04	,	**a_							:			str	,	):
					'''simple docstring'''





					super().__init__(pad_token_id=a_	,	**a_			)

					__UpperCAmelCase   :					Union[str, Any]													=     vocab_size
					__UpperCAmelCase   :					List[Any]													=     max_position_embeddings
					__UpperCAmelCase   :					str													=     hidden_size
					__UpperCAmelCase   :					Union[str, Any]													=     num_hidden_layers
					__UpperCAmelCase   :					Any													=     num_attention_heads
					__UpperCAmelCase   :					Any													=     intermediate_size
					__UpperCAmelCase   :					Tuple													=     hidden_act
					__UpperCAmelCase   :					str													=     hidden_dropout_prob
					__UpperCAmelCase   :					Any													=     attention_probs_dropout_prob
					__UpperCAmelCase   :					Optional[int]													=     initializer_range
					__UpperCAmelCase   :					Any													=     type_vocab_size
					__UpperCAmelCase   :					int													=     layer_norm_eps
					__UpperCAmelCase   :					List[Any]													=     use_cache
					__UpperCAmelCase   :					Optional[int]													=     question_token_id





