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

# General docstring
A__           =							'''PoolFormerConfig'''

# Base docstring
A__           =							'''sail/poolformer_s12'''
A__           =							[1, 512, 7, 7]

# Image classification docstring
A__           =							'''sail/poolformer_s12'''
A__           =							'''tabby, tabby cat'''

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





def  _lowerCamelCase					(   a_				: Optional[Any]						,			a_				: float = 0.0						,			a_				: bool = False):

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







							def __init__(				self     :    Tuple	,					__snake_case     :    Optional[float] = None		):
										super().__init__()
										lowerCamelCase						:Tuple						  =     drop_prob







							def 				snake_case  (				self     :    Union[str, Any]	,					__snake_case     :    torch.Tensor		):
										return drop_path(__lowerCAmelCase	,					self.drop_prob	,					self.training		)







							def 				snake_case  (				self     :    List[str]		):
										return "p={}".format(self.drop_prob		)


class   _lowerCAmelCase		(     nn.Module				):







							def __init__(				self     :    Union[str, Any]	,					__snake_case     :    Tuple	,					__snake_case     :    Union[str, Any]	,					__snake_case     :    Any	,					__snake_case     :    Union[str, Any]	,					__snake_case     :    Dict	,					__snake_case     :    Dict=None		):
										super().__init__()
										lowerCamelCase						:int						  =     patch_size if isinstance(__lowerCAmelCase	,					collections.abc.Iterable		) else (patch_size, patch_size)
										lowerCamelCase						:str						  =     stride if isinstance(__lowerCAmelCase	,					collections.abc.Iterable		) else (stride, stride)
										lowerCamelCase						:Union[str, Any]						  =     padding if isinstance(__lowerCAmelCase	,					collections.abc.Iterable		) else (padding, padding)

										lowerCamelCase						:Union[str, Any]						  =     nn.Convad(__lowerCAmelCase	,					__lowerCAmelCase	,					kernel_size=__lowerCAmelCase	,					stride=__lowerCAmelCase	,					padding=__lowerCAmelCase		)
										lowerCamelCase						:Union[str, Any]						  =     norm_layer(__lowerCAmelCase		) if norm_layer else nn.Identity()







							def 				snake_case  (				self     :    List[str]	,					__snake_case     :    Union[str, Any]		):
										lowerCamelCase						:Union[str, Any]						  =     self.projection(__lowerCAmelCase		)
										lowerCamelCase						:Tuple						  =     self.norm(__lowerCAmelCase		)
										return embeddings


class   _lowerCAmelCase		(     nn.GroupNorm				):







							def __init__(				self     :    Tuple	,					__snake_case     :    int	,					**__snake_case     :    Optional[Any]		):
										super().__init__(1	,					__lowerCAmelCase	,					**__lowerCAmelCase		)


class   _lowerCAmelCase		(     nn.Module				):







							def __init__(				self     :    int	,					__snake_case     :    Optional[Any]		):
										super().__init__()
										lowerCamelCase						:List[str]						  =     nn.AvgPoolad(__lowerCAmelCase	,					stride=1	,					padding=pool_size // 2	,					count_include_pad=__lowerCAmelCase		)







							def 				snake_case  (				self     :    Optional[Any]	,					__snake_case     :    List[Any]		):
										return self.pool(__lowerCAmelCase		) - hidden_states


class   _lowerCAmelCase		(     nn.Module				):







							def __init__(				self     :    Tuple	,					__snake_case     :    int	,					__snake_case     :    Optional[int]	,					__snake_case     :    Optional[int]	,					__snake_case     :    Any		):
										super().__init__()
										lowerCamelCase						:Tuple						  =     nn.Convad(__lowerCAmelCase	,					__lowerCAmelCase	,					1		)
										lowerCamelCase						:List[Any]						  =     nn.Convad(__lowerCAmelCase	,					__lowerCAmelCase	,					1		)
										lowerCamelCase						:int						  =     PoolFormerDropPath(__lowerCAmelCase		)
										if isinstance(config.hidden_act	,					__lowerCAmelCase		):
													lowerCamelCase						:Dict						  =     ACTaFN[config.hidden_act]
										else:
													lowerCamelCase						:List[str]						  =     config.hidden_act







							def 				snake_case  (				self     :    Tuple	,					__snake_case     :    List[str]		):
										lowerCamelCase						:Optional[Any]						  =     self.conva(__lowerCAmelCase		)
										lowerCamelCase						:Tuple						  =     self.act_fn(__lowerCAmelCase		)
										lowerCamelCase						:str						  =     self.drop(__lowerCAmelCase		)
										lowerCamelCase						:List[str]						  =     self.conva(__lowerCAmelCase		)
										lowerCamelCase						:List[Any]						  =     self.drop(__lowerCAmelCase		)

										return hidden_states


class   _lowerCAmelCase		(     nn.Module				):







							def __init__(				self     :    Dict	,					__snake_case     :    Optional[Any]	,					__snake_case     :    int	,					__snake_case     :    Dict	,					__snake_case     :    Dict	,					__snake_case     :    Tuple	,					__snake_case     :    Union[str, Any]		):
										super().__init__()
										lowerCamelCase						:Tuple						  =     PoolFormerPooling(__lowerCAmelCase		)
										lowerCamelCase						:str						  =     PoolFormerOutput(__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase		)
										lowerCamelCase						:int						  =     PoolFormerGroupNorm(__lowerCAmelCase		)
										lowerCamelCase						:List[str]						  =     PoolFormerGroupNorm(__lowerCAmelCase		)

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







							def 				snake_case  (				self     :    Dict	,					__snake_case     :    Dict		):






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

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

													lowerCamelCase						:int						  =     (output,) + outputs
													return outputs

										else:
													lowerCamelCase						:Optional[int]						  =     self.drop_path(self.pooling(self.before_norm(__lowerCAmelCase		)		)		)
													# First residual connection
													lowerCamelCase						:Optional[int]						  =     pooling_output + hidden_states
													lowerCamelCase						:Optional[int]						  =     ()

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

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


class   _lowerCAmelCase		(     nn.Module				):







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

										# patch embeddings
										lowerCamelCase						: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]	,					)		)
										lowerCamelCase						:str						  =     nn.ModuleList(__lowerCAmelCase		)

										# Transformer blocks
										lowerCamelCase						:Optional[Any]						  =     []
										lowerCamelCase						:int						  =     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(
																        __lowerCAmelCase	,					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(__lowerCAmelCase		)		)

										lowerCamelCase						:Optional[Any]						  =     nn.ModuleList(__lowerCAmelCase		)







							def 				snake_case  (				self     :    Any	,					__snake_case     :    Dict	,					__snake_case     :    Optional[Any]=False	,					__snake_case     :    List[str]=True		):
										lowerCamelCase						:Any						  =     () if output_hidden_states else None

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

													if output_hidden_states:
																lowerCamelCase						:int						  =     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=__lowerCAmelCase	,					hidden_states=__lowerCAmelCase		)


class   _lowerCAmelCase		(     UpperCAmelCase__				):
							_UpperCAmelCase						    =     PoolFormerConfig
							_UpperCAmelCase						    =     'poolformer'
							_UpperCAmelCase						    =     'pixel_values'
							_UpperCAmelCase						    =     True







							def 				snake_case  (				self     :    List[str]	,					__snake_case     :    List[str]		):






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







							def 				snake_case  (				self     :    Optional[int]	,					__snake_case     :    Optional[int]	,					__snake_case     :    Optional[Any]=False		):






										if isinstance(__lowerCAmelCase	,					__lowerCAmelCase		):
													lowerCamelCase						:Dict						  =     value


A__           =							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.
'''

A__           =							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.'      ,   UpperCAmelCase__      ,   )
class   _lowerCAmelCase		(     UpperCAmelCase__				):







							def __init__(				self     :    Any	,					__snake_case     :    Union[str, Any]		):
										super().__init__(__lowerCAmelCase		)
										lowerCamelCase						:Optional[int]						  =     config

										lowerCamelCase						:int						  =     PoolFormerEncoder(__lowerCAmelCase		)

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







							def 				snake_case  (				self     :    List[Any]		):
										return self.embeddings.patch_embeddings







							@add_start_docstrings_to_model_forward(__lowerCAmelCase		)
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC	,					output_type=__lowerCAmelCase	,					config_class=_CONFIG_FOR_DOC	,					modality='''vision'''	,					expected_output=_EXPECTED_OUTPUT_SHAPE	,					)
							def 				snake_case  (				self     :    int	,					__snake_case     :    Optional[torch.FloatTensor] = None	,					__snake_case     :    Optional[bool] = None	,					__snake_case     :    Optional[bool] = None	,					):
										lowerCamelCase						:Union[str, Any]						  =     (
										    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
										)
										lowerCamelCase						:Optional[Any]						  =     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						:int						  =     self.encoder(
										    __lowerCAmelCase	,					output_hidden_states=__lowerCAmelCase	,					return_dict=__lowerCAmelCase	,					)
										lowerCamelCase						:Dict						  =     encoder_outputs[0]

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

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


class   _lowerCAmelCase		(     nn.Module				):







							def __init__(				self     :    Union[str, Any]	,					__snake_case     :    Any		):
										super().__init__()
										lowerCamelCase						:Optional[Any]						  =     nn.Linear(config.hidden_size	,					config.hidden_size		)







							def 				snake_case  (				self     :    int	,					__snake_case     :    List[Any]		):
										lowerCamelCase						:Optional[int]						  =     self.dense(__lowerCAmelCase		)
										return output


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







							def __init__(				self     :    Dict	,					__snake_case     :    Optional[int]		):
										super().__init__(__lowerCAmelCase		)
										lowerCamelCase						:Tuple						  =     config.num_labels
										lowerCamelCase						:Tuple						  =     PoolFormerModel(__lowerCAmelCase		)

										# Final norm
										lowerCamelCase						:Optional[int]						  =     PoolFormerGroupNorm(config.hidden_sizes[-1]		)
										# Classifier head
										lowerCamelCase						:Union[str, 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(__lowerCAmelCase		)
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT	,					output_type=__lowerCAmelCase	,					config_class=_CONFIG_FOR_DOC	,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT	,					)
							def 				snake_case  (				self     :    Union[str, Any]	,					__snake_case     :    Optional[torch.FloatTensor] = None	,					__snake_case     :    Optional[torch.LongTensor] = None	,					__snake_case     :    Optional[bool] = None	,					__snake_case     :    Optional[bool] = None	,					):
										lowerCamelCase						:Union[str, Any]						  =     return_dict if return_dict is not None else self.config.use_return_dict

										lowerCamelCase						:List[Any]						  =     self.poolformer(
										    __lowerCAmelCase	,					output_hidden_states=__lowerCAmelCase	,					return_dict=__lowerCAmelCase	,					)

										lowerCamelCase						:str						  =     outputs[0]

										lowerCamelCase						:str						  =     self.classifier(self.norm(__lowerCAmelCase		).mean([-2, -1]		)		)

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

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

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

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




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A__           =							{
    """configuration_nllb_moe""": [
        """NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """NllbMoeConfig""",
    ]
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			A__           =							[
			    """NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST""",
			    """NllbMoeForConditionalGeneration""",
			    """NllbMoeModel""",
			    """NllbMoePreTrainedModel""",
			    """NllbMoeTop2Router""",
			    """NllbMoeSparseMLP""",
			]


if TYPE_CHECKING:
			from .configuration_nllb_moe import (
			    NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    NllbMoeConfig,
			)

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_nllb_moe import (
						    NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
						    NllbMoeForConditionalGeneration,
						    NllbMoeModel,
						    NllbMoePreTrainedModel,
						    NllbMoeSparseMLP,
						    NllbMoeTopaRouter,
						)


else:
			import sys

			A__           =							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



