





import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


lowerCamelCase : Optional[Any]						   =       logging.get_logger(__name__)

# General docstring
lowerCamelCase : Union[str, Any]						   =       '''PoolFormerConfig'''

# Base docstring
lowerCamelCase : Union[str, Any]						   =       '''sail/poolformer_s12'''
lowerCamelCase : str						   =       [1, 5_12, 7, 7]

# Image classification docstring
lowerCamelCase : int						   =       '''sail/poolformer_s12'''
lowerCamelCase : str						   =       '''tabby, tabby cat'''

lowerCamelCase : int						   =       [
    '''sail/poolformer_s12''',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]
def       snake_case_		(     lowerCAmelCase_       :						str       ,       lowerCAmelCase_       :						str = 0.0       ,       lowerCAmelCase_       :						Dict = False      ):
							if drop_prob == 0.0 or not training:
														return input
							__lowercase      :							List[str]				=     1 - drop_prob
							__lowercase      :							Optional[int]				=     (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
							__lowercase      :							str				=     keep_prob + torch.rand(snake_case_       ,       dtype=input.dtype       ,       device=input.device      )
							random_tensor.floor_()  # binarize
							__lowercase      :							List[str]				=     input.div(snake_case_      ) * random_tensor
							return output

class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	Tuple		,     __a				:	int = None							)			->  None:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							List[str]				=     drop_prob



					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	List[Any]							)			->  torch.Tensor:


												"""simple docstring"""

												return drop_path(_a		,     self.drop_prob		,     self.training							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  str:


												"""simple docstring"""

												return "p={}".format(self.drop_prob							)







class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	Optional[int]		,     __a				:	List[str]		,     __a				:	Tuple		,     __a				:	Dict		,     __a				:	List[Any]		,     __a				:	Dict		,     __a				:	Optional[Any]=None							)			->  List[str]:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							int				=     patch_size if isinstance(_a		,     collections.abc.Iterable							) else (patch_size, patch_size)
												__lowercase      :							Optional[Any]				=     stride if isinstance(_a		,     collections.abc.Iterable							) else (stride, stride)
												__lowercase      :							Dict				=     padding if isinstance(_a		,     collections.abc.Iterable							) else (padding, padding)

												__lowercase      :							Optional[Any]				=     nn.Convad(_a		,     _a		,     kernel_size=_a		,     stride=_a		,     padding=_a							)
												__lowercase      :							str				=     norm_layer(_a							) if norm_layer else nn.Identity()



					def       lowerCAmelCase				(  self				:	Optional[int]		,     __a				:	Optional[Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Any				=     self.projection(_a							)
												__lowercase      :							List[Any]				=     self.norm(_a							)
												return embeddings







class 		lowerCAmelCase     ( nn.GroupNorm			):

					'''simple docstring'''

					def __init__(  self				:	Union[str, Any]		,     __a				:	Optional[int]		,     **__a				:	Tuple							)			->  List[Any]:


												"""simple docstring"""

												super().__init__(1		,     _a		,     **_a							)







class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	Optional[int]		,     __a				:	Union[str, Any]							)			->  int:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							Union[str, Any]				=     nn.AvgPoolad(_a		,     stride=1		,     padding=pool_size // 2		,     count_include_pad=_a							)



					def       lowerCAmelCase				(  self				:	Any		,     __a				:	int							)			->  Tuple:


												"""simple docstring"""

												return self.pool(_a							) - hidden_states







class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	str		,     __a				:	Dict		,     __a				:	List[Any]		,     __a				:	int		,     __a				:	List[str]							)			->  int:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							Tuple				=     nn.Convad(_a		,     _a		,     1							)
												__lowercase      :							Tuple				=     nn.Convad(_a		,     _a		,     1							)
												__lowercase      :							Any				=     PoolFormerDropPath(_a							)
												if isinstance(config.hidden_act		,     _a							):
																			__lowercase      :							Optional[int]				=     ACTaFN[config.hidden_act]
												else:
																			__lowercase      :							Optional[Any]				=     config.hidden_act



					def       lowerCAmelCase				(  self				:	List[str]		,     __a				:	Optional[int]							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Tuple				=     self.conva(_a							)
												__lowercase      :							int				=     self.act_fn(_a							)
												__lowercase      :							List[str]				=     self.drop(_a							)
												__lowercase      :							Union[str, Any]				=     self.conva(_a							)
												__lowercase      :							Optional[int]				=     self.drop(_a							)

												return hidden_states







class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	List[Any]		,     __a				:	Dict		,     __a				:	Optional[Any]		,     __a				:	Any		,     __a				:	str		,     __a				:	Optional[int]		,     __a				:	Union[str, Any]							)			->  Union[str, Any]:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							List[str]				=     PoolFormerPooling(_a							)
												__lowercase      :							int				=     PoolFormerOutput(_a		,     _a		,     _a		,     _a							)
												__lowercase      :							Optional[Any]				=     PoolFormerGroupNorm(_a							)
												__lowercase      :							Optional[Any]				=     PoolFormerGroupNorm(_a							)

												# Useful for training neural nets
												__lowercase      :							List[Any]				=     PoolFormerDropPath(_a							) if drop_path > 0.0 else nn.Identity()
												__lowercase      :							List[str]				=     config.use_layer_scale
												if config.use_layer_scale:
																			__lowercase      :							Tuple				=     nn.Parameter(
																			    config.layer_scale_init_value * torch.ones((_a)							)		,     requires_grad=_a							)
																			__lowercase      :							Tuple				=     nn.Parameter(
																			    config.layer_scale_init_value * torch.ones((_a)							)		,     requires_grad=_a							)



					def       lowerCAmelCase				(  self				:	Optional[Any]		,     __a				:	Any							)			->  Union[str, Any]:


												"""simple docstring"""

												if self.use_layer_scale:
																			__lowercase      :							Optional[Any]				=     self.pooling(self.before_norm(_a							)							)
																			__lowercase      :							Union[str, Any]				=     self.layer_scale_a.unsqueeze(-1							).unsqueeze(-1							) * pooling_output
																			# First residual connection
																			__lowercase      :							Union[str, Any]				=     hidden_states + self.drop_path(_a							)
																			__lowercase      :							List[Any]				=     ()

																			__lowercase      :							List[str]				=     self.output(self.after_norm(_a							)							)
																			__lowercase      :							Optional[Any]				=     self.layer_scale_a.unsqueeze(-1							).unsqueeze(-1							) * layer_output
																			# Second residual connection
																			__lowercase      :							List[Any]				=     hidden_states + self.drop_path(_a							)

																			__lowercase      :							Union[str, Any]				=     (output,) + outputs
																			return outputs

												else:
																			__lowercase      :							int				=     self.drop_path(self.pooling(self.before_norm(_a							)							)							)
																			# First residual connection
																			__lowercase      :							int				=     pooling_output + hidden_states
																			__lowercase      :							Dict				=     ()

																			# Second residual connection inside the PoolFormerOutput block
																			__lowercase      :							int				=     self.drop_path(self.output(self.after_norm(_a							)							)							)
																			__lowercase      :							Union[str, Any]				=     hidden_states + layer_output

																			__lowercase      :							Dict				=     (output,) + outputs
																			return outputs







class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	Optional[int]		,     __a				:	Tuple							)			->  Optional[int]:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							Union[str, Any]				=     config
												# stochastic depth decay rule
												__lowercase      :							Union[str, Any]				=     [x.item() for x in torch.linspace(0		,     config.drop_path_rate		,     sum(config.depths							)							)]

												# patch embeddings
												__lowercase      :							Optional[Any]				=     []
												for i in range(config.num_encoder_blocks							):
																			embeddings.append(
																			    PoolFormerEmbeddings(
																			        patch_size=config.patch_sizes[i]		,     stride=config.strides[i]		,     padding=config.padding[i]		,     num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]		,     hidden_size=config.hidden_sizes[i]		,     )							)
												__lowercase      :							Optional[int]				=     nn.ModuleList(_a							)

												# Transformer blocks
												__lowercase      :							Optional[Any]				=     []
												__lowercase      :							Tuple				=     0
												for i in range(config.num_encoder_blocks							):
																			# each block consists of layers
																			__lowercase      :							List[Any]				=     []
																			if i != 0:
																										cur += config.depths[i - 1]
																			for j in range(config.depths[i]							):
																										layers.append(
																										    PoolFormerLayer(
																										        _a		,     num_channels=config.hidden_sizes[i]		,     pool_size=config.pool_size		,     hidden_size=config.hidden_sizes[i]		,     intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio							)		,     drop_path=dpr[cur + j]		,     )							)
																			blocks.append(nn.ModuleList(_a							)							)

												__lowercase      :							List[Any]				=     nn.ModuleList(_a							)



					def       lowerCAmelCase				(  self				:	List[str]		,     __a				:	List[Any]		,     __a				:	str=False		,     __a				:	Dict=True							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							Tuple				=     () if output_hidden_states else None

												__lowercase      :							List[str]				=     pixel_values
												for idx, layers in enumerate(zip(self.patch_embeddings		,     self.block							)							):
																			__lowercase      :							Union[str, Any]				=     layers
																			# Get patch embeddings from hidden_states
																			__lowercase      :							str				=     embedding_layer(_a							)
																			# Send the embeddings through the blocks
																			for _, blk in enumerate(_a							):
																										__lowercase      :							Optional[Any]				=     blk(_a							)
																										__lowercase      :							List[str]				=     layer_outputs[0]

																			if output_hidden_states:
																										__lowercase      :							Any				=     all_hidden_states + (hidden_states,)

												if not return_dict:
																			return tuple(v for v in [hidden_states, all_hidden_states] if v is not None							)

												return BaseModelOutputWithNoAttention(last_hidden_state=_a		,     hidden_states=_a							)







class 		lowerCAmelCase     ( UpperCamelCase__			):

					'''simple docstring'''

					_A						:			Any									=      PoolFormerConfig
					_A						:			Optional[Any]									=      '''poolformer'''
					_A						:			Optional[Any]									=      '''pixel_values'''
					_A						:			Optional[Any]									=      True



					def       lowerCAmelCase				(  self				:	Dict		,     __a				:	Tuple							)			->  Tuple:


												"""simple docstring"""

												if isinstance(_a		,     (nn.Linear, nn.Convad)							):
																			module.weight.data.normal_(mean=0.0		,     std=self.config.initializer_range							)
																			if module.bias is not None:
																										module.bias.data.zero_()
												elif isinstance(_a		,     nn.LayerNorm							):
																			module.bias.data.zero_()
																			module.weight.data.fill_(1.0							)



					def       lowerCAmelCase				(  self				:	Optional[Any]		,     __a				:	List[str]		,     __a				:	Union[str, Any]=False							)			->  Union[str, Any]:


												"""simple docstring"""

												if isinstance(_a		,     _a							):
																			__lowercase      :							List[str]				=     value







lowerCamelCase : Optional[int]						   =       r'''\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'''

lowerCamelCase : List[str]						   =       r'''\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n'''

@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'''      ,       UpperCamelCase__      ,       )
class 		lowerCAmelCase     ( UpperCamelCase__			):

					'''simple docstring'''

					def __init__(  self				:	str		,     __a				:	int							)			->  List[str]:


												"""simple docstring"""

												super().__init__(_a							)
												__lowercase      :							Any				=     config

												__lowercase      :							Union[str, Any]				=     PoolFormerEncoder(_a							)

												# Initialize weights and apply final processing
												self.post_init()



					def       lowerCAmelCase				(  self				:	List[Any]							)			->  str:


												"""simple docstring"""

												return self.embeddings.patch_embeddings



					@add_start_docstrings_to_model_forward(_a							)
					@add_code_sample_docstrings(
					    checkpoint=_CHECKPOINT_FOR_DOC		,     output_type=_a		,     config_class=_CONFIG_FOR_DOC		,     modality="""vision"""		,     expected_output=_EXPECTED_OUTPUT_SHAPE		,     )
					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	Dict = None		,     __a				:	List[Any] = None		,     __a				:	Tuple = None		,     )			->  Union[Tuple, BaseModelOutputWithNoAttention]:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     (
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												__lowercase      :							List[str]				=     return_dict if return_dict is not None else self.config.use_return_dict

												if pixel_values is None:
																			raise ValueError("""You have to specify pixel_values"""							)

												__lowercase      :							Any				=     self.encoder(
												    _a		,     output_hidden_states=_a		,     return_dict=_a		,     )
												__lowercase      :							Optional[int]				=     encoder_outputs[0]

												if not return_dict:
																			return (sequence_output, None) + encoder_outputs[1:]

												return BaseModelOutputWithNoAttention(
												    last_hidden_state=_a		,     hidden_states=encoder_outputs.hidden_states		,     )







class 		lowerCAmelCase     ( nn.Module			):

					'''simple docstring'''

					def __init__(  self				:	Any		,     __a				:	Any							)			->  Tuple:


												"""simple docstring"""

												super().__init__()
												__lowercase      :							List[str]				=     nn.Linear(config.hidden_size		,     config.hidden_size							)



					def       lowerCAmelCase				(  self				:	List[str]		,     __a				:	Tuple							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							str				=     self.dense(_a							)
												return output







@add_start_docstrings(
    '''\n    PoolFormer Model transformer with an image classification head on top\n    '''      ,       UpperCamelCase__      ,       )
class 		lowerCAmelCase     ( UpperCamelCase__			):

					'''simple docstring'''

					def __init__(  self				:	Optional[Any]		,     __a				:	Dict							)			->  List[Any]:


												"""simple docstring"""

												super().__init__(_a							)
												__lowercase      :							Tuple				=     config.num_labels
												__lowercase      :							Optional[Any]				=     PoolFormerModel(_a							)

												# Final norm
												__lowercase      :							Optional[int]				=     PoolFormerGroupNorm(config.hidden_sizes[-1]							)
												# Classifier head
												__lowercase      :							str				=     (
												    nn.Linear(config.hidden_sizes[-1]		,     config.num_labels							) if config.num_labels > 0 else nn.Identity()
												)

												# Initialize weights and apply final processing
												self.post_init()



					@add_start_docstrings_to_model_forward(_a							)
					@add_code_sample_docstrings(
					    checkpoint=_IMAGE_CLASS_CHECKPOINT		,     output_type=_a		,     config_class=_CONFIG_FOR_DOC		,     expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,     )
					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	Any = None		,     __a				:	Union[str, Any] = None		,     __a				:	List[Any] = None		,     __a				:	Optional[int] = None		,     )			->  Union[Tuple, ImageClassifierOutputWithNoAttention]:


												"""simple docstring"""

												__lowercase      :							Optional[Any]				=     return_dict if return_dict is not None else self.config.use_return_dict

												__lowercase      :							Optional[Any]				=     self.poolformer(
												    _a		,     output_hidden_states=_a		,     return_dict=_a		,     )

												__lowercase      :							Tuple				=     outputs[0]

												__lowercase      :							Any				=     self.classifier(self.norm(_a							).mean([-2, -1]							)							)

												__lowercase      :							Dict				=     None
												if labels is not None:
																			if self.config.problem_type is None:
																										if self.num_labels == 1:
																																	__lowercase      :							Union[str, Any]				=     """regression"""
																										elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																																	__lowercase      :							Optional[int]				=     """single_label_classification"""
																										else:
																																	__lowercase      :							List[Any]				=     """multi_label_classification"""

																			if self.config.problem_type == "regression":
																										__lowercase      :							Union[str, Any]				=     MSELoss()
																										if self.num_labels == 1:
																																	__lowercase      :							str				=     loss_fct(logits.squeeze()		,     labels.squeeze()							)
																										else:
																																	__lowercase      :							Optional[Any]				=     loss_fct(_a		,     _a							)
																			elif self.config.problem_type == "single_label_classification":
																										__lowercase      :							List[str]				=     CrossEntropyLoss()
																										__lowercase      :							Any				=     loss_fct(logits.view(-1		,     self.num_labels							)		,     labels.view(-1							)							)
																			elif self.config.problem_type == "multi_label_classification":
																										__lowercase      :							List[Any]				=     BCEWithLogitsLoss()
																										__lowercase      :							Dict				=     loss_fct(_a		,     _a							)

												if not return_dict:
																			__lowercase      :							List[str]				=     (logits,) + outputs[2:]
																			return ((loss,) + output) if loss is not None else output

												return ImageClassifierOutputWithNoAttention(loss=_a		,     logits=_a		,     hidden_states=outputs.hidden_states							)







def       snake_case_		(     lowerCAmelCase_       :						int       ,       lowerCAmelCase_       :						int      ):
							return int((input_a, input_a).count(1      ) != 0      )
def       snake_case_		(     ):
							assert or_gate(0       ,       0      ) == 0
							assert or_gate(0       ,       1      ) == 1
							assert or_gate(1       ,       0      ) == 1
							assert or_gate(1       ,       1      ) == 1


if __name__ == "__main__":
	print(or_gate(0, 1))
	print(or_gate(1, 0))
	print(or_gate(0, 0))
	print(or_gate(1, 1))