

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


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

# General docstring
A_       : Optional[int]         =			'PoolFormerConfig'

# Base docstring
A_       : Optional[int]         =			'sail/poolformer_s12'
A_       : Optional[Any]         =			[1, 512, 7, 7]

# Image classification docstring
A_       : List[Any]         =			'sail/poolformer_s12'
A_       : List[str]         =			'tabby, tabby cat'

A_       : List[str]         =			[
    'sail/poolformer_s12',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]

def 						UpperCamelCase					(lowercase_: Dict    ,      lowercase_: Tuple = 0.0    ,      lowercase_: Any = False					)     ->      str:
	if drop_prob == 0.0 or not training:
		return input
	A__							:    Optional[Any]        =		1 - drop_prob
	A__							:    Union[str, Any]        =		(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
	A__							:    Union[str, Any]        =		keep_prob + torch.rand(__lowerCamelCase    ,      dtype=input.dtype    ,      device=input.device					)
	random_tensor.floor_()  # binarize
	A__							:    Optional[Any]        =		input.div(__lowerCamelCase					) * random_tensor
	return output



class     _a						(nn.Module				):


					'''simple docstring'''






					def __init__( self    ,       A__ = None			):
						super().__init__()
						A__							:    str        =		drop_prob






					def   __A			( self    ,       A__			):
						return drop_path(lowercase_    ,       self.drop_prob    ,       self.training			)






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



class     _a						(nn.Module				):


					'''simple docstring'''






					def __init__( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__=None			):
						super().__init__()
						A__							:    Union[str, Any]        =		patch_size if isinstance(lowercase_    ,       collections.abc.Iterable			) else (patch_size, patch_size)
						A__							:    Dict        =		stride if isinstance(lowercase_    ,       collections.abc.Iterable			) else (stride, stride)
						A__							:    Optional[int]        =		padding if isinstance(lowercase_    ,       collections.abc.Iterable			) else (padding, padding)

						A__							:    Optional[Any]        =		nn.Convad(lowercase_    ,       lowercase_    ,       kernel_size=lowercase_    ,       stride=lowercase_    ,       padding=lowercase_			)
						A__							:    Optional[int]        =		norm_layer(lowercase_			) if norm_layer else nn.Identity()






					def   __A			( self    ,       A__			):
						A__							:    List[Any]        =		self.projection(lowercase_			)
						A__							:    List[Any]        =		self.norm(lowercase_			)
						return embeddings



class     _a						(nn.GroupNorm				):


					'''simple docstring'''






					def __init__( self    ,       A__    ,       **A__			):
						super().__init__(1    ,       lowercase_    ,       **lowercase_			)



class     _a						(nn.Module				):


					'''simple docstring'''






					def __init__( self    ,       A__			):
						super().__init__()
						A__							:    Optional[Any]        =		nn.AvgPoolad(lowercase_    ,       stride=1    ,       padding=pool_size // 2    ,       count_include_pad=lowercase_			)






					def   __A			( self    ,       A__			):
						return self.pool(lowercase_			) - hidden_states



class     _a						(nn.Module				):


					'''simple docstring'''






					def __init__( self    ,       A__    ,       A__    ,       A__    ,       A__			):
						super().__init__()
						A__							:    Union[str, Any]        =		nn.Convad(lowercase_    ,       lowercase_    ,       1			)
						A__							:    Any        =		nn.Convad(lowercase_    ,       lowercase_    ,       1			)
						A__							:    Union[str, Any]        =		PoolFormerDropPath(lowercase_			)
						if isinstance(config.hidden_act    ,       lowercase_			):
							A__							:    List[str]        =		ACTaFN[config.hidden_act]
						else:
							A__							:    str        =		config.hidden_act






					def   __A			( self    ,       A__			):
						A__							:    Tuple        =		self.conva(lowercase_			)
						A__							:    List[Any]        =		self.act_fn(lowercase_			)
						A__							:    Union[str, Any]        =		self.drop(lowercase_			)
						A__							:    Dict        =		self.conva(lowercase_			)
						A__							:    Tuple        =		self.drop(lowercase_			)

						return hidden_states



class     _a						(nn.Module				):


					'''simple docstring'''






					def __init__( self    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__    ,       A__			):
						super().__init__()
						A__							:    str        =		PoolFormerPooling(lowercase_			)
						A__							:    Dict        =		PoolFormerOutput(lowercase_    ,       lowercase_    ,       lowercase_    ,       lowercase_			)
						A__							:    List[str]        =		PoolFormerGroupNorm(lowercase_			)
						A__							:    Optional[int]        =		PoolFormerGroupNorm(lowercase_			)

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






					def   __A			( self    ,       A__			):
						if self.use_layer_scale:
							A__							:    Union[str, Any]        =		self.pooling(self.before_norm(lowercase_			)			)
							A__							:    Tuple        =		self.layer_scale_a.unsqueeze(-1			).unsqueeze(-1			) * pooling_output
							# First residual connection
							A__							:    Optional[int]        =		hidden_states + self.drop_path(lowercase_			)
							A__							:    List[str]        =		()

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

							A__							:    List[Any]        =		(output,) + outputs
							return outputs

						else:
							A__							:    Optional[int]        =		self.drop_path(self.pooling(self.before_norm(lowercase_			)			)			)
							# First residual connection
							A__							:    Optional[int]        =		pooling_output + hidden_states
							A__							:    Union[str, Any]        =		()

							# Second residual connection inside the PoolFormerOutput block
							A__							:    Optional[Any]        =		self.drop_path(self.output(self.after_norm(lowercase_			)			)			)
							A__							:    Optional[int]        =		hidden_states + layer_output

							A__							:    Tuple        =		(output,) + outputs
							return outputs



class     _a						(nn.Module				):


					'''simple docstring'''






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

						# patch embeddings
						A__							:    List[str]        =		[]
						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]    ,       )			)
						A__							:    int        =		nn.ModuleList(lowercase_			)

						# Transformer blocks
						A__							:    List[str]        =		[]
						A__							:    Dict        =		0
						for i in range(config.num_encoder_blocks			):
							# each block consists of layers
							A__							:    str        =		[]
							if i != 0:
								cur += config.depths[i - 1]
							for j in range(config.depths[i]			):
								layers.append(
								    PoolFormerLayer(
								        lowercase_    ,       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(lowercase_			)			)

						A__							:    int        =		nn.ModuleList(lowercase_			)






					def   __A			( self    ,       A__    ,       A__=False    ,       A__=True			):
						A__							:    int        =		() if output_hidden_states else None

						A__							:    int        =		pixel_values
						for idx, layers in enumerate(zip(self.patch_embeddings    ,       self.block			)			):
							A__							:    Tuple        =		layers
							# Get patch embeddings from hidden_states
							A__							:    List[str]        =		embedding_layer(lowercase_			)
							# Send the embeddings through the blocks
							for _, blk in enumerate(lowercase_			):
								A__							:    Tuple        =		blk(lowercase_			)
								A__							:    int        =		layer_outputs[0]

							if output_hidden_states:
								A__							:    List[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=lowercase_    ,       hidden_states=lowercase_			)



class     _a						(lowercase__				):


					'''simple docstring'''






					UpperCAmelCase__:   str  							=       PoolFormerConfig
					UpperCAmelCase__:   Optional[Any]  							=       """poolformer"""
					UpperCAmelCase__:   Optional[Any]  							=       """pixel_values"""
					UpperCAmelCase__:   Union[str, Any]  							=       True






					def   __A			( self    ,       A__			):
						if isinstance(lowercase_    ,       (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(lowercase_    ,       nn.LayerNorm			):
							module.bias.data.zero_()
							module.weight.data.fill_(1.0			)






					def   __A			( self    ,       A__    ,       A__=False			):
						if isinstance(lowercase_    ,       lowercase_			):
							A__							:    List[Any]        =		value



A_       : Any         =			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'

A_       : 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.'''		,   lowercase__		,   )
class     _a						(lowercase__				):


					'''simple docstring'''






					def __init__( self    ,       A__			):
						super().__init__(lowercase_			)
						A__							:    Tuple        =		config

						A__							:    Dict        =		PoolFormerEncoder(lowercase_			)

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






					def   __A			( self			):
						return self.embeddings.patch_embeddings






					@add_start_docstrings_to_model_forward(lowercase_			)
					@add_code_sample_docstrings(
					    checkpoint=_CHECKPOINT_FOR_DOC    ,       output_type=lowercase_    ,       config_class=_CONFIG_FOR_DOC    ,       modality="""vision"""    ,       expected_output=_EXPECTED_OUTPUT_SHAPE    ,       )
					def   __A			( self    ,       A__ = None    ,       A__ = None    ,       A__ = None    ,       ):
						A__							:    str        =		(
						    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
						)
						A__							:    int        =		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"""			)

						A__							:    Optional[Any]        =		self.encoder(
						    lowercase_    ,       output_hidden_states=lowercase_    ,       return_dict=lowercase_    ,       )
						A__							:    Union[str, Any]        =		encoder_outputs[0]

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

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



class     _a						(nn.Module				):


					'''simple docstring'''






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






					def   __A			( self    ,       A__			):
						A__							:    Union[str, Any]        =		self.dense(lowercase_			)
						return output



@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''		,   lowercase__		,   )
class     _a						(lowercase__				):


					'''simple docstring'''






					def __init__( self    ,       A__			):
						super().__init__(lowercase_			)
						A__							:    Tuple        =		config.num_labels
						A__							:    List[Any]        =		PoolFormerModel(lowercase_			)

						# Final norm
						A__							:    Union[str, Any]        =		PoolFormerGroupNorm(config.hidden_sizes[-1]			)
						# Classifier head
						A__							:    Optional[Any]        =		(
						    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(lowercase_			)
					@add_code_sample_docstrings(
					    checkpoint=_IMAGE_CLASS_CHECKPOINT    ,       output_type=lowercase_    ,       config_class=_CONFIG_FOR_DOC    ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT    ,       )
					def   __A			( self    ,       A__ = None    ,       A__ = None    ,       A__ = None    ,       A__ = None    ,       ):
						A__							:    Optional[Any]        =		return_dict if return_dict is not None else self.config.use_return_dict

						A__							:    Optional[int]        =		self.poolformer(
						    lowercase_    ,       output_hidden_states=lowercase_    ,       return_dict=lowercase_    ,       )

						A__							:    Union[str, Any]        =		outputs[0]

						A__							:    Union[str, Any]        =		self.classifier(self.norm(lowercase_			).mean([-2, -1]			)			)

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

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

						if not return_dict:
							A__							:    Optional[Any]        =		(logits,) + outputs[2:]
							return ((loss,) + output) if loss is not None else output

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





"""simple docstring"""





from __future__ import annotations







def 	__a     (		__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase  ):
   if (voltage, current, resistance).count(0  ) != 1:
      raise ValueError("One and only one argument must be 0"  )
   if resistance < 0:
      raise ValueError("Resistance cannot be negative"  )
   if voltage == 0:
      return {"voltage": float(current * resistance  )}
   elif current == 0:
      return {"current": voltage / resistance}
   elif resistance == 0:
      return {"resistance": voltage / current}
   else:
      raise ValueError("Exactly one argument must be 0"  )


if __name__ == "__main__":
    import doctest

    doctest.testmod()


