



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





	"""simple docstring"""




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.config_class(**self.inputs_dict						)
						self.parent.assertTrue(hasattr(__lowerCAmelCase						,			"hidden_sizes"						)						)
						self.parent.assertTrue(hasattr(__lowerCAmelCase						,			"num_encoder_blocks"						)						)




class       _lowercase				:





	"""simple docstring"""




	def __init__(self						,			lowerCamelCase_						,			lowerCamelCase_=13						,			lowerCamelCase_=64						,			lowerCamelCase_=3						,			lowerCamelCase_=4						,			lowerCamelCase_=[2, 2, 2, 2]						,			lowerCamelCase_=[8, 4, 2, 1]						,			lowerCamelCase_=[16, 32, 64, 128]						,			lowerCamelCase_=[1, 4, 8, 16]						,			lowerCamelCase_=False						,			lowerCamelCase_=True						,			lowerCamelCase_="gelu"						,			lowerCamelCase_=0.1						,			lowerCamelCase_=0.02						,			lowerCamelCase_=3						,			lowerCamelCase_=None						,			):
						"""simple docstring"""

						a   	=  parent
						a   	=  batch_size
						a   	=  image_size
						a   	=  num_channels
						a   	=  num_encoder_blocks
						a   	=  sr_ratios
						a   	=  depths
						a   	=  hidden_sizes
						a   	=  downsampling_rates
						a   	=  is_training
						a   	=  use_labels
						a   	=  hidden_act
						a   	=  hidden_dropout_prob
						a   	=  initializer_range
						a   	=  num_labels
						a   	=  scope




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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

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

						a   	=  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						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  PoolFormerModel(config=__lowerCAmelCase						)
						model.to(__lowerCAmelCase						)
						model.eval()
						a   	=  model(__lowerCAmelCase						)
						a   	=  a   	=  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						):
						"""simple docstring"""

						a   	=  self.prepare_config_and_inputs()
						a	,							a	,							a   	=  config_and_inputs
						a   	=  {"pixel_values": pixel_values}
						return config, inputs_dict




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





	"""simple docstring"""
	__A		      =				(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
	__A		      =				(
	    {"feature-extraction": PoolFormerModel, "image-classification": PoolFormerForImageClassification}
	    if is_torch_available()
	    else {}
	)

	__A		      =				False
	__A		      =				False
	__A		      =				False
	__A		      =				False
	__A		      =				False




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  PoolFormerModelTester(self						)
						a   	=  PoolFormerConfigTester(self						,			config_class=__lowerCAmelCase						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						self.config_tester.run_common_tests()




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowerCAmelCase						)




	@unittest.skip("PoolFormer does not use inputs_embeds"						)
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						pass




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

						pass




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						def check_hidden_states_output(lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
											a   	=  model_class(__lowerCAmelCase						)
											model.to(__lowerCAmelCase						)
											model.eval()

											with torch.no_grad():
																a   	=  model(**self._prepare_for_class(__lowerCAmelCase						,			__lowerCAmelCase						)						)

											a   	=  outputs.hidden_states

											a   	=  self.model_tester.num_encoder_blocks
											self.assertEqual(len(__lowerCAmelCase						)						,			__lowerCAmelCase						)

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

						a	,							a   	=  self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											a   	=  True
											check_hidden_states_output(__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						)

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

											check_hidden_states_output(__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						if not self.model_tester.is_training:
											return

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

						for model_class in self.all_model_classes:
											if model_class in get_values(__lowerCAmelCase						):
																continue
											a   	=  model_class(__lowerCAmelCase						)
											model.to(__lowerCAmelCase						)
											model.train()
											a   	=  self._prepare_for_class(__lowerCAmelCase						,			__lowerCAmelCase						,			return_labels=__lowerCAmelCase						)
											a   	=  model(**__lowerCAmelCase						).loss
											loss.backward()




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a	,							a   	=  self.model_tester.prepare_config_and_inputs_for_common()

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

											a   	=  ["pixel_values"]
											self.assertListEqual(arg_names[:1]						,			__lowerCAmelCase						)




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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






def        a( )						->			List[Any]:
					"""simple docstring"""





					a   	=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
					return image







@require_torch
class       _lowercase				(  unittest.TestCase      ):





	"""simple docstring"""




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  PoolFormerImageProcessor()
						a   	=  PoolFormerForImageClassification.from_pretrained("sail/poolformer_s12"						).to(__lowerCAmelCase						)

						a   	=  image_processor(images=prepare_img()						,			return_tensors="pt"						).to(__lowerCAmelCase						)

						# forward pass
						with torch.no_grad():
											a   	=  model(**__lowerCAmelCase						)

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

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





import functools
import operator

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


A_		:  str										=  logging.get_logger(__name__)

A_		:  Dict										=  {
    '''facebook/hubert-base-ls960''': '''https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json''',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}



class 		_lowercase		(							UpperCAmelCase__						):
   _UpperCAmelCase  						=      '''hubert'''

   def __init__(   self      :							Optional[Any]				, __lowerCAmelCase      :							str=32				, __lowerCAmelCase      :							int=768				, __lowerCAmelCase      :							Optional[int]=12				, __lowerCAmelCase      :							List[Any]=12				, __lowerCAmelCase      :							Optional[Any]=3072				, __lowerCAmelCase      :							Tuple="gelu"				, __lowerCAmelCase      :							Dict=0.1				, __lowerCAmelCase      :							List[Any]=0.1				, __lowerCAmelCase      :							str=0.1				, __lowerCAmelCase      :							Union[str, Any]=True				, __lowerCAmelCase      :							Tuple=0.0				, __lowerCAmelCase      :							Optional[int]=0.1				, __lowerCAmelCase      :							List[str]=0.1				, __lowerCAmelCase      :							Dict=0.0_2				, __lowerCAmelCase      :							Any=1E-5				, __lowerCAmelCase      :							Optional[Any]="group"				, __lowerCAmelCase      :							int="gelu"				, __lowerCAmelCase      :							Dict=(512, 512, 512, 512, 512, 512, 512)				, __lowerCAmelCase      :							Tuple=(5, 2, 2, 2, 2, 2, 2)				, __lowerCAmelCase      :							Tuple=(10, 3, 3, 3, 3, 2, 2)				, __lowerCAmelCase      :							Tuple=False				, __lowerCAmelCase      :							Any=128				, __lowerCAmelCase      :							Tuple=16				, __lowerCAmelCase      :							Tuple=False				, __lowerCAmelCase      :							int=True				, __lowerCAmelCase      :							List[str]=0.0_5				, __lowerCAmelCase      :							List[str]=10				, __lowerCAmelCase      :							int=2				, __lowerCAmelCase      :							List[Any]=0.0				, __lowerCAmelCase      :							Dict=10				, __lowerCAmelCase      :							List[str]=0				, __lowerCAmelCase      :							Union[str, Any]="sum"				, __lowerCAmelCase      :							Dict=False				, __lowerCAmelCase      :							List[str]=False				, __lowerCAmelCase      :							int=256				, __lowerCAmelCase      :							List[str]=0				, __lowerCAmelCase      :							str=1				, __lowerCAmelCase      :							Any=2				, **__lowerCAmelCase      :							Tuple				, )							->					Tuple:


    """simple docstring"""






    super().__init__(**__lowerCAmelCase				, pad_token_id=__lowerCAmelCase				, bos_token_id=__lowerCAmelCase				, eos_token_id=__lowerCAmelCase				)
    a							=		hidden_size
    a							=		feat_extract_norm
    a							=		feat_extract_activation
    a							=		list(__lowerCAmelCase				)
    a							=		list(__lowerCAmelCase				)
    a							=		list(__lowerCAmelCase				)
    a							=		conv_bias
    a							=		num_conv_pos_embeddings
    a							=		num_conv_pos_embedding_groups
    a							=		len(self.conv_dim				)
    a							=		num_hidden_layers
    a							=		intermediate_size
    a							=		hidden_act
    a							=		num_attention_heads
    a							=		hidden_dropout
    a							=		attention_dropout
    a							=		activation_dropout
    a							=		feat_proj_layer_norm
    a							=		feat_proj_dropout
    a							=		final_dropout
    a							=		layerdrop
    a							=		layer_norm_eps
    a							=		initializer_range
    a							=		vocab_size
    a							=		do_stable_layer_norm
    a							=		use_weighted_layer_sum
    a							=		classifier_proj_size

    if (
        (len(self.conv_stride				) != self.num_feat_extract_layers)
        or (len(self.conv_kernel				) != self.num_feat_extract_layers)
        or (len(self.conv_dim				) != self.num_feat_extract_layers)
    ):
     raise ValueError(
         "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
         " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
         f""" {len(self.conv_dim				)}`, `len(config.conv_stride) = {len(self.conv_stride				)}`,"""
         f""" `len(config.conv_kernel) = {len(self.conv_kernel				)}`."""				)

    # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
    a							=		apply_spec_augment
    a							=		mask_time_prob
    a							=		mask_time_length
    a							=		mask_time_min_masks
    a							=		mask_feature_prob
    a							=		mask_feature_length
    a							=		mask_feature_min_masks

    # ctc loss
    a							=		ctc_loss_reduction
    a							=		ctc_zero_infinity




   @property
   def 		A				(   self      :							Tuple				)							->					Optional[Any]:


    """simple docstring"""






    return functools.reduce(operator.mul				, self.conv_stride				, 1				)

