




from collections import OrderedDict
from typing import Mapping

from packaging import version

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


lowerCamelCase__					:  List[str]      							=				logging.get_logger(__name__)

lowerCamelCase__					:  Union[str, Any]      							=				{
    'sail/poolformer_s12': 'https://huggingface.co/sail/poolformer_s12/resolve/main/config.json',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
}





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       "poolformer"



 def __init__(				self    :						List[Any]							,							_lowerCAmelCase    :						Optional[int]=3							,							_lowerCAmelCase    :						int=16							,							_lowerCAmelCase    :						Union[str, Any]=16							,							_lowerCAmelCase    :						Any=3							,							_lowerCAmelCase    :						str=4.0							,							_lowerCAmelCase    :						List[Any]=[2, 2, 6, 2]							,							_lowerCAmelCase    :						Optional[Any]=[64, 128, 320, 512]							,							_lowerCAmelCase    :						Optional[Any]=[7, 3, 3, 3]							,							_lowerCAmelCase    :						Union[str, Any]=[4, 2, 2, 2]							,							_lowerCAmelCase    :						Optional[int]=[2, 1, 1, 1]							,							_lowerCAmelCase    :						List[str]=4							,							_lowerCAmelCase    :						Tuple=0.0							,							_lowerCAmelCase    :						str="gelu"							,							_lowerCAmelCase    :						str=True							,							_lowerCAmelCase    :						Dict=1E-5							,							_lowerCAmelCase    :						Optional[Any]=0.02							,							**_lowerCAmelCase    :						Optional[Any]							,							):
       SCREAMING_SNAKE_CASE_      		=     num_channels
       SCREAMING_SNAKE_CASE_      		=     patch_size
       SCREAMING_SNAKE_CASE_      		=     stride
       SCREAMING_SNAKE_CASE_      		=     padding
       SCREAMING_SNAKE_CASE_      		=     pool_size
       SCREAMING_SNAKE_CASE_      		=     hidden_sizes
       SCREAMING_SNAKE_CASE_      		=     mlp_ratio
       SCREAMING_SNAKE_CASE_      		=     depths
       SCREAMING_SNAKE_CASE_      		=     patch_sizes
       SCREAMING_SNAKE_CASE_      		=     strides
       SCREAMING_SNAKE_CASE_      		=     num_encoder_blocks
       SCREAMING_SNAKE_CASE_      		=     drop_path_rate
       SCREAMING_SNAKE_CASE_      		=     hidden_act
       SCREAMING_SNAKE_CASE_      		=     use_layer_scale
       SCREAMING_SNAKE_CASE_      		=     layer_scale_init_value
       SCREAMING_SNAKE_CASE_      		=     initializer_range
       super().__init__(**_lowerCAmelCase		)





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 lowercase_							       =       version.parse("1.11"   )



 @property
 def  lowerCAmelCase_    (				self    :						str		):
       return OrderedDict(
           [
               ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
           ]		)




 @property
 def  lowerCAmelCase_    (				self    :						List[Any]		):
       return 2E-3





import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch





@require_torch
class 			lowerCamelCase_				:


 '''simple docstring'''







 @property
 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       return self.get_dummy_input()



 @property
 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       if self.block_type == "down":
             return (4, 32, 16, 16)
       elif self.block_type == "mid":
             return (4, 32, 32, 32)
       elif self.block_type == "up":
             return (4, 32, 64, 64)

       raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'."		)



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Optional[Any]=True							,							_lowerCAmelCase    :						str=False							,							_lowerCAmelCase    :						Optional[int]=False							,							_lowerCAmelCase    :						Dict=False							,							):
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     32
       SCREAMING_SNAKE_CASE_      		=     (32, 32)

       SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     torch.device(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     (batch_size, num_channels) + sizes
       SCREAMING_SNAKE_CASE_      		=     randn_tensor(_lowerCAmelCase							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'hidden_states': hidden_states}

       if include_temb:
             SCREAMING_SNAKE_CASE_      		=     128
             SCREAMING_SNAKE_CASE_      		=     randn_tensor((batch_size, temb_channels)							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		)

       if include_res_hidden_states_tuple:
             SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(1		)
             SCREAMING_SNAKE_CASE_      		=     (randn_tensor(_lowerCAmelCase							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		),)

       if include_encoder_hidden_states:
             SCREAMING_SNAKE_CASE_      		=     floats_tensor((batch_size, 32, 32)		).to(_lowerCAmelCase		)

       if include_skip_sample:
             SCREAMING_SNAKE_CASE_      		=     randn_tensor(((batch_size, 3) + sizes)							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		)

       return dummy_input



 def  lowerCAmelCase_    (				self    :						Optional[int]		):
       SCREAMING_SNAKE_CASE_      		=     {
           'in_channels': 32,
           'out_channels': 32,
           'temb_channels': 128,
       }
       if self.block_type == "up":
             SCREAMING_SNAKE_CASE_      		=     32

       if self.block_type == "mid":
             init_dict.pop('out_channels'		)

       SCREAMING_SNAKE_CASE_      		=     self.dummy_input
       return init_dict, inputs_dict



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     self.prepare_init_args_and_inputs_for_common()
       SCREAMING_SNAKE_CASE_      		=     self.block_class(**_lowerCAmelCase		)
       unet_block.to(_lowerCAmelCase		)
       unet_block.eval()

       with torch.no_grad():
             SCREAMING_SNAKE_CASE_      		=     unet_block(**_lowerCAmelCase		)

       if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_      		=     output[0]

       self.assertEqual(output.shape							,							self.output_shape		)

       SCREAMING_SNAKE_CASE_      		=     output[0, -1, -3:, -3:]
       SCREAMING_SNAKE_CASE_      		=     torch.tensor(_lowerCAmelCase		).to(_lowerCAmelCase		)
       assert torch_all_close(output_slice.flatten()							,							_lowerCAmelCase							,							atol=5E-3		)




 @unittest.skipIf(torch_device == 'mps'							,							'Training is not supported in mps'		)
 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     self.prepare_init_args_and_inputs_for_common()
       SCREAMING_SNAKE_CASE_      		=     self.block_class(**_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.train()
       SCREAMING_SNAKE_CASE_      		=     model(**_lowerCAmelCase		)

       if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_      		=     output[0]

       SCREAMING_SNAKE_CASE_      		=     torch.device(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     randn_tensor(output.shape							,							device=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.nn.functional.mse_loss(_lowerCAmelCase							,							_lowerCAmelCase		)
       loss.backward()