





'''simple docstring'''






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									=   logging.get_logger(__name__)

# General docstring
lowerCAmelCase									=   """PoolFormerConfig"""

# Base docstring
lowerCAmelCase									=   """sail/poolformer_s12"""
lowerCAmelCase									=   [1, 5_12, 7, 7]

# Image classification docstring
lowerCAmelCase									=   """sail/poolformer_s12"""
lowerCAmelCase									=   """tabby, tabby cat"""

lowerCAmelCase									=   [
    """sail/poolformer_s12""",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]






def   __A    (				a_    :	Tuple   ,a_    :	float = 0.0   ,a_    :	bool = False						):
		if drop_prob == 0.0 or not training:
				return input
		lowerCAmelCase  :     Optional[Any]       						= 1 - drop_prob
		lowerCAmelCase  :     List[str]       						= (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
		lowerCAmelCase  :     List[Any]       						= keep_prob + torch.rand(a_   ,dtype=input.dtype   ,device=input.device						)
		random_tensor.floor_()  # binarize
		lowerCAmelCase  :     Dict       						= input.div(a_						) * random_tensor
		return output
class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_ = None						):
			super().__init__()
			lowerCAmelCase  :     List[str]       						= drop_prob



	def 	_lowerCamelCase     ( self		,   a_						):
			return drop_path(a_		,   self.drop_prob		,   self.training						)





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






class 		lowerCamelCase	(   nn.Module							):



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

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





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Optional[Any]       						= self.projection(a_						)
			lowerCAmelCase  :     int       						= self.norm(a_						)
			return embeddings






class 		lowerCamelCase	(   nn.GroupNorm							):



	def __init__( self		,   a_		,   **a_						):
			super().__init__(1		,   a_		,   **a_						)






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_						):
			super().__init__()
			lowerCAmelCase  :     List[str]       						= nn.AvgPoolad(a_		,   stride=1		,   padding=pool_size // 2		,   count_include_pad=a_						)





	def 	_lowerCamelCase     ( self		,   a_						):
			return self.pool(a_						) - hidden_states






class 		lowerCamelCase	(   nn.Module							):



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





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Tuple       						= self.conva(a_						)
			lowerCAmelCase  :     Union[str, Any]       						= self.act_fn(a_						)
			lowerCAmelCase  :     Tuple       						= self.drop(a_						)
			lowerCAmelCase  :     List[str]       						= self.conva(a_						)
			lowerCAmelCase  :     str       						= self.drop(a_						)

			return hidden_states






class 		lowerCamelCase	(   nn.Module							):



	def __init__( self		,   a_		,   a_		,   a_		,   a_		,   a_		,   a_						):
			super().__init__()
			lowerCAmelCase  :     List[str]       						= PoolFormerPooling(a_						)
			lowerCAmelCase  :     List[Any]       						= PoolFormerOutput(a_		,   a_		,   a_		,   a_						)
			lowerCAmelCase  :     List[Any]       						= PoolFormerGroupNorm(a_						)
			lowerCAmelCase  :     int       						= PoolFormerGroupNorm(a_						)

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





	def 	_lowerCamelCase     ( self		,   a_						):
			if self.use_layer_scale:
					lowerCAmelCase  :     List[str]       						= self.pooling(self.before_norm(a_						)						)
					lowerCAmelCase  :     List[str]       						= self.layer_scale_a.unsqueeze(-1						).unsqueeze(-1						) * pooling_output
					# First residual connection
					lowerCAmelCase  :     List[Any]       						= hidden_states + self.drop_path(a_						)
					lowerCAmelCase  :     str       						= ()

					lowerCAmelCase  :     str       						= self.output(self.after_norm(a_						)						)
					lowerCAmelCase  :     Tuple       						= self.layer_scale_a.unsqueeze(-1						).unsqueeze(-1						) * layer_output
					# Second residual connection
					lowerCAmelCase  :     Optional[int]       						= hidden_states + self.drop_path(a_						)

					lowerCAmelCase  :     Optional[int]       						= (output,) + outputs
					return outputs

			else:
					lowerCAmelCase  :     Tuple       						= self.drop_path(self.pooling(self.before_norm(a_						)						)						)
					# First residual connection
					lowerCAmelCase  :     Optional[Any]       						= pooling_output + hidden_states
					lowerCAmelCase  :     int       						= ()

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

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






class 		lowerCamelCase	(   nn.Module							):



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

			# patch embeddings
			lowerCAmelCase  :     Union[str, 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]		,   )						)
			lowerCAmelCase  :     Tuple       						= nn.ModuleList(a_						)

			# Transformer blocks
			lowerCAmelCase  :     List[str]       						= []
			lowerCAmelCase  :     Any       						= 0
			for i in range(config.num_encoder_blocks						):
					# each block consists of layers
					lowerCAmelCase  :     int       						= []
					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_						)						)

			lowerCAmelCase  :     Dict       						= nn.ModuleList(a_						)





	def 	_lowerCamelCase     ( self		,   a_		,   a_=False		,   a_=True						):
			lowerCAmelCase  :     Tuple       						= () if output_hidden_states else None

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

					if output_hidden_states:
							lowerCAmelCase  :     Dict       						= 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	(   _A							):
	snake_case_   = PoolFormerConfig
	snake_case_   = "poolformer"
	snake_case_   = "pixel_values"
	snake_case_   = True



	def 	_lowerCamelCase     ( self		,   a_						):
			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		,   a_		,   a_=False						):
			if isinstance(a_		,   a_						):
					lowerCAmelCase  :     str       						= value






lowerCAmelCase									=   R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

lowerCAmelCase									=   R"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`PoolFormerImageProcessor.__call__`] for details.
"""
@add_start_docstrings(
    "The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top."   , _A   , )
class 		lowerCamelCase	(   _A							):



	def __init__( self		,   a_						):
			super().__init__(a_						)
			lowerCAmelCase  :     str       						= config

			lowerCAmelCase  :     str       						= PoolFormerEncoder(a_						)

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



	def 	_lowerCamelCase     ( self						):
			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		,   a_ = None		,   a_ = None		,   a_ = None		,   ):
			lowerCAmelCase  :     Any       						= (
			    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
			)
			lowerCAmelCase  :     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"						)

			lowerCAmelCase  :     Any       						= self.encoder(
			    a_		,   output_hidden_states=a_		,   return_dict=a_		,   )
			lowerCAmelCase  :     Dict       						= 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							):



	def __init__( self		,   a_						):
			super().__init__()
			lowerCAmelCase  :     str       						= nn.Linear(config.hidden_size		,   config.hidden_size						)





	def 	_lowerCamelCase     ( self		,   a_						):
			lowerCAmelCase  :     Optional[Any]       						= self.dense(a_						)
			return output






@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "   , _A   , )
class 		lowerCamelCase	(   _A							):



	def __init__( self		,   a_						):
			super().__init__(a_						)
			lowerCAmelCase  :     Optional[Any]       						= config.num_labels
			lowerCAmelCase  :     Dict       						= PoolFormerModel(a_						)

			# Final norm
			lowerCAmelCase  :     Optional[Any]       						= PoolFormerGroupNorm(config.hidden_sizes[-1]						)
			# Classifier head
			lowerCAmelCase  :     List[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		,   a_ = None		,   a_ = None		,   a_ = None		,   a_ = None		,   ):
			lowerCAmelCase  :     List[str]       						= return_dict if return_dict is not None else self.config.use_return_dict

			lowerCAmelCase  :     Dict       						= self.poolformer(
			    a_		,   output_hidden_states=a_		,   return_dict=a_		,   )

			lowerCAmelCase  :     List[str]       						= outputs[0]

			lowerCAmelCase  :     Optional[Any]       						= self.classifier(self.norm(a_						).mean([-2, -1]						)						)

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

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

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

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










'''simple docstring'''






from __future__ import annotations






def   __A    (				a_    :	float   ,a_    :	float   ,a_    :	float   ,):
		if (electron_conc, hole_conc, intrinsic_conc).count(0						) != 1:
				raise ValueError("You cannot supply more or less than 2 values"						)
		elif electron_conc < 0:
				raise ValueError("Electron concentration cannot be negative in a semiconductor"						)
		elif hole_conc < 0:
				raise ValueError("Hole concentration cannot be negative in a semiconductor"						)
		elif intrinsic_conc < 0:
				raise ValueError(
				    "Intrinsic concentration cannot be negative in a semiconductor"						)
		elif electron_conc == 0:
				return (
				    "electron_conc",
				    intrinsic_conc**2 / hole_conc,
				)
		elif hole_conc == 0:
				return (
				    "hole_conc",
				    intrinsic_conc**2 / electron_conc,
				)
		elif intrinsic_conc == 0:
				return (
				    "intrinsic_conc",
				    (electron_conc * hole_conc) ** 0.5,
				)
		else:
				return (-1, -1)


if __name__ == "__main__":
					import doctest

					doctest.testmod()



