






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        __snake_case(					__UpperCAmelCase      ):



	def 						UpperCAmelCase    (	self   :   List[str]	):

				"""simple docstring"""






				lowercase__						:	Optional[int]         = self.config_class(**self.inputs_dict	)
				self.parent.assertTrue(hasattr(lowercase_ ,		"""hidden_sizes"""	)	)
				self.parent.assertTrue(hasattr(lowercase_ ,		"""num_encoder_blocks"""	)	)
class        __snake_case:



	def __init__(	self   :   Union[str, Any] ,		UpperCAmelCase__   :   Optional[Any] ,		UpperCAmelCase__   :   Dict=13 ,		UpperCAmelCase__   :   str=64 ,		UpperCAmelCase__   :   List[Any]=3 ,		UpperCAmelCase__   :   Optional[Any]=4 ,		UpperCAmelCase__   :   Any=[2, 2, 2, 2] ,		UpperCAmelCase__   :   str=[8, 4, 2, 1] ,		UpperCAmelCase__   :   Union[str, Any]=[16, 32, 64, 128] ,		UpperCAmelCase__   :   Tuple=[1, 4, 8, 16] ,		UpperCAmelCase__   :   Optional[int]=False ,		UpperCAmelCase__   :   Dict=True ,		UpperCAmelCase__   :   Optional[Any]="gelu" ,		UpperCAmelCase__   :   str=0.1 ,		UpperCAmelCase__   :   List[str]=0.02 ,		UpperCAmelCase__   :   Optional[int]=3 ,		UpperCAmelCase__   :   List[str]=None ,		):

				"""simple docstring"""






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



	def 						UpperCAmelCase    (	self   :   Union[str, Any]	):

				"""simple docstring"""






				lowercase__						:	int         = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

				lowercase__						:	int         = 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   :   Optional[int] ,		UpperCAmelCase__   :   Optional[Any] ,		UpperCAmelCase__   :   List[str] ,		UpperCAmelCase__   :   List[str]	):

				"""simple docstring"""






				lowercase__						:	Optional[Any]         = PoolFormerModel(config=lowercase_	)
				model.to(lowercase_	)
				model.eval()
				lowercase__						:	Any         = model(lowercase_	)
				lowercase__						:	Union[str, Any]         = 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   :   Tuple	):

				"""simple docstring"""






				lowercase__						:	Tuple         = self.prepare_config_and_inputs()
				lowercase__						:	int         = config_and_inputs
				lowercase__						:	Optional[int]         = {'''pixel_values''': pixel_values}
				return config, inputs_dict
@require_torch
class        __snake_case(					__UpperCAmelCase     ,       __UpperCAmelCase     ,       unittest.TestCase      ):
	lowercase :       List[Any]		      =		(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
	lowercase :       Any		      =		(
	    {'''feature-extraction''': PoolFormerModel, '''image-classification''': PoolFormerForImageClassification}
	    if is_torch_available()
	    else {}
	)

	lowercase :       Dict		      =		False
	lowercase :       Any		      =		False
	lowercase :       int		      =		False
	lowercase :       int		      =		False
	lowercase :       Dict		      =		False



	def 						UpperCAmelCase    (	self   :   Union[str, Any]	):

				"""simple docstring"""






				lowercase__						:	int         = PoolFormerModelTester(self	)
				lowercase__						:	int         = PoolFormerConfigTester(self ,		config_class=lowercase_	)



	def 						UpperCAmelCase    (	self   :   List[Any]	):

				"""simple docstring"""






				self.config_tester.run_common_tests()



	def 						UpperCAmelCase    (	self   :   List[str]	):

				"""simple docstring"""






				lowercase__						:	List[str]         = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase_	)



	@unittest.skip("""PoolFormer does not use inputs_embeds"""	)
	def 						UpperCAmelCase    (	self   :   Any	):

				"""simple docstring"""






				pass



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

				"""simple docstring"""






				pass



	def 						UpperCAmelCase    (	self   :   Optional[Any]	):

				"""simple docstring"""






				def check_hidden_states_output(UpperCAmelCase__   :   Tuple ,		UpperCAmelCase__   :   int ,		UpperCAmelCase__   :   Optional[int]	):
							lowercase__						:	str         = model_class(lowercase_	)
							model.to(lowercase_	)
							model.eval()

							with torch.no_grad():
										lowercase__						:	str         = model(**self._prepare_for_class(lowercase_ ,		lowercase_	)	)

							lowercase__						:	int         = outputs.hidden_states

							lowercase__						:	Optional[int]         = self.model_tester.num_encoder_blocks
							self.assertEqual(len(lowercase_	) ,		lowercase_	)

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

				lowercase__						:	Any         = self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							lowercase__						:	Union[str, Any]         = True
							check_hidden_states_output(lowercase_ ,		lowercase_ ,		lowercase_	)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							lowercase__						:	Union[str, Any]         = True

							check_hidden_states_output(lowercase_ ,		lowercase_ ,		lowercase_	)



	def 						UpperCAmelCase    (	self   :   Dict	):

				"""simple docstring"""






				if not self.model_tester.is_training:
							return

				lowercase__						:	Any         = self.model_tester.prepare_config_and_inputs_for_common()
				lowercase__						:	Any         = True

				for model_class in self.all_model_classes:
							if model_class in get_values(lowercase_	):
										continue
							lowercase__						:	int         = model_class(lowercase_	)
							model.to(lowercase_	)
							model.train()
							lowercase__						:	Dict         = self._prepare_for_class(lowercase_ ,		lowercase_ ,		return_labels=lowercase_	)
							lowercase__						:	str         = model(**lowercase_	).loss
							loss.backward()



	def 						UpperCAmelCase    (	self   :   Any	):

				"""simple docstring"""






				lowercase__						:	int         = self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							lowercase__						:	List[str]         = model_class(lowercase_	)
							lowercase__						:	Optional[int]         = inspect.signature(model.forward	)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							lowercase__						:	Any         = [*signature.parameters.keys()]

							lowercase__						:	Optional[int]         = ['''pixel_values''']
							self.assertListEqual(arg_names[:1] ,		lowercase_	)







	@slow
	def 						UpperCAmelCase    (	self   :   Optional[Any]	):

				"""simple docstring"""






				for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowercase__						:	Dict         = PoolFormerModel.from_pretrained(lowercase_	)
							self.assertIsNotNone(lowercase_	)
def       a__						(  ):
			lowercase__						:	Any         = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
			return image






@require_torch
class        __snake_case(					unittest.TestCase      ):



	@slow
	def 						UpperCAmelCase    (	self   :   Any	):

				"""simple docstring"""






				lowercase__						:	Optional[int]         = PoolFormerImageProcessor()
				lowercase__						:	List[str]         = PoolFormerForImageClassification.from_pretrained("""sail/poolformer_s12"""	).to(lowercase_	)

				lowercase__						:	Tuple         = image_processor(images=prepare_img() ,		return_tensors="""pt"""	).to(lowercase_	)

				# forward pass
				with torch.no_grad():
							lowercase__						:	int         = model(**lowercase_	)

				# verify the logits
				lowercase__						:	Optional[Any]         = torch.Size((1, 1_000)	)
				self.assertEqual(outputs.logits.shape ,		lowercase_	)

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










from collections import OrderedDict
from typing import Mapping

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


a    =       logging.get_logger(__name__)

a    =       {
    'nghuyong/ernie-1.0-base-zh': 'https://huggingface.co/nghuyong/ernie-1.0-base-zh/resolve/main/config.json',
    'nghuyong/ernie-2.0-base-en': 'https://huggingface.co/nghuyong/ernie-2.0-base-en/resolve/main/config.json',
    'nghuyong/ernie-2.0-large-en': 'https://huggingface.co/nghuyong/ernie-2.0-large-en/resolve/main/config.json',
    'nghuyong/ernie-3.0-base-zh': 'https://huggingface.co/nghuyong/ernie-3.0-base-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-medium-zh': 'https://huggingface.co/nghuyong/ernie-3.0-medium-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-mini-zh': 'https://huggingface.co/nghuyong/ernie-3.0-mini-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-micro-zh': 'https://huggingface.co/nghuyong/ernie-3.0-micro-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-nano-zh': 'https://huggingface.co/nghuyong/ernie-3.0-nano-zh/resolve/main/config.json',
    'nghuyong/ernie-gram-zh': 'https://huggingface.co/nghuyong/ernie-gram-zh/resolve/main/config.json',
    'nghuyong/ernie-health-zh': 'https://huggingface.co/nghuyong/ernie-health-zh/resolve/main/config.json',
}
class        __snake_case(					__UpperCamelCase      ):
 lowercase :       Dict		      =		'''ernie'''



 def __init__(	self   :   Tuple ,		UpperCAmelCase__   :   int=30_522 ,		UpperCAmelCase__   :   List[str]=768 ,		UpperCAmelCase__   :   Tuple=12 ,		UpperCAmelCase__   :   Optional[int]=12 ,		UpperCAmelCase__   :   Any=3_072 ,		UpperCAmelCase__   :   Tuple="gelu" ,		UpperCAmelCase__   :   Union[str, Any]=0.1 ,		UpperCAmelCase__   :   str=0.1 ,		UpperCAmelCase__   :   List[str]=512 ,		UpperCAmelCase__   :   str=2 ,		UpperCAmelCase__   :   Optional[int]=3 ,		UpperCAmelCase__   :   Dict=False ,		UpperCAmelCase__   :   Any=0.02 ,		UpperCAmelCase__   :   Any=1e-12 ,		UpperCAmelCase__   :   str=0 ,		UpperCAmelCase__   :   Any="absolute" ,		UpperCAmelCase__   :   List[str]=True ,		UpperCAmelCase__   :   Tuple=None ,		**UpperCAmelCase__   :   Dict ,		):

    """simple docstring"""






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

    lowercase__						:	List[str]         = vocab_size
    lowercase__						:	Optional[int]         = hidden_size
    lowercase__						:	Union[str, Any]         = num_hidden_layers
    lowercase__						:	Any         = num_attention_heads
    lowercase__						:	Optional[int]         = hidden_act
    lowercase__						:	str         = intermediate_size
    lowercase__						:	Tuple         = hidden_dropout_prob
    lowercase__						:	Tuple         = attention_probs_dropout_prob
    lowercase__						:	str         = max_position_embeddings
    lowercase__						:	List[Any]         = type_vocab_size
    lowercase__						:	Any         = task_type_vocab_size
    lowercase__						:	Optional[Any]         = use_task_id
    lowercase__						:	Optional[Any]         = initializer_range
    lowercase__						:	Union[str, Any]         = layer_norm_eps
    lowercase__						:	Dict         = position_embedding_type
    lowercase__						:	Union[str, Any]         = use_cache
    lowercase__						:	int         = classifier_dropout
class        __snake_case(					__UpperCamelCase      ):



 @property
 def 						UpperCAmelCase    (	self   :   List[Any]	):

    """simple docstring"""






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


