






"""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


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		(							UpperCAmelCase__       :					Any,						UpperCAmelCase__       :					float = 0.0,						UpperCAmelCase__       :					bool = False      )		->int:
						if drop_prob == 0.0 or not training:
												return input
						A__						:  Union[str, Any]		  =  1 - drop_prob
						A__						:  str		  =  (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
						A__						:  Any		  =  keep_prob + torch.rand(UpperCAmelCase__,						dtype=input.dtype,						device=input.device      )
						random_tensor.floor_()  # binarize
						A__						:  Optional[int]		  =  input.div(UpperCAmelCase__      ) * random_tensor
						return output






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						int			,    snake_case      :						Optional[float] = None							):
													'''simple docstring'''





													super().__init__()
													A__						:  Tuple		  =  drop_prob


							def 						_UpperCamelCase       (			self      :						Optional[int]			,    snake_case      :						torch.Tensor							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						Tuple							):
													'''simple docstring'''





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






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						int			,    snake_case      :						List[str]			,    snake_case      :						int			,    snake_case      :						Optional[int]			,    snake_case      :						Any			,    snake_case      :						int			,    snake_case      :						int=None							):
													'''simple docstring'''





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

													A__						:  List[str]		  =  nn.Convad(snake_case			,    snake_case			,    kernel_size=snake_case			,    stride=snake_case			,    padding=snake_case							)
													A__						:  Dict		  =  norm_layer(snake_case							) if norm_layer else nn.Identity()


							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.projection(snake_case							)
													A__						:  Optional[int]		  =  self.norm(snake_case							)
													return embeddings






class 							__SCREAMING_SNAKE_CASE						(				nn.GroupNorm		):


							def __init__(			self      :						int			,    snake_case      :						Dict			,    **snake_case      :						Optional[int]							):
													'''simple docstring'''





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






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						int			,    snake_case      :						List[str]							):
													'''simple docstring'''





													super().__init__()
													A__						:  int		  =  nn.AvgPoolad(snake_case			,    stride=1			,    padding=pool_size // 2			,    count_include_pad=snake_case							)


							def 						_UpperCamelCase       (			self      :						Tuple			,    snake_case      :						str							):
													'''simple docstring'''





													return self.pool(snake_case							) - hidden_states






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						List[str]			,    snake_case      :						int			,    snake_case      :						List[str]			,    snake_case      :						List[Any]			,    snake_case      :						List[str]							):
													'''simple docstring'''





													super().__init__()
													A__						:  Dict		  =  nn.Convad(snake_case			,    snake_case			,    1							)
													A__						:  Tuple		  =  nn.Convad(snake_case			,    snake_case			,    1							)
													A__						:  Optional[Any]		  =  PoolFormerDropPath(snake_case							)
													if isinstance(config.hidden_act			,    snake_case							):
																			A__						:  str		  =  ACTaFN[config.hidden_act]
													else:
																			A__						:  List[str]		  =  config.hidden_act


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						List[str]							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.conva(snake_case							)
													A__						:  List[str]		  =  self.act_fn(snake_case							)
													A__						:  Union[str, Any]		  =  self.drop(snake_case							)
													A__						:  List[Any]		  =  self.conva(snake_case							)
													A__						:  Optional[Any]		  =  self.drop(snake_case							)

													return hidden_states






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						Dict			,    snake_case      :						Union[str, Any]			,    snake_case      :						List[Any]			,    snake_case      :						Any			,    snake_case      :						Union[str, Any]			,    snake_case      :						Optional[int]			,    snake_case      :						Any							):
													'''simple docstring'''





													super().__init__()
													A__						:  Dict		  =  PoolFormerPooling(snake_case							)
													A__						:  Optional[int]		  =  PoolFormerOutput(snake_case			,    snake_case			,    snake_case			,    snake_case							)
													A__						:  Dict		  =  PoolFormerGroupNorm(snake_case							)
													A__						:  Optional[Any]		  =  PoolFormerGroupNorm(snake_case							)

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


							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						List[str]							):
													'''simple docstring'''





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

																			A__						:  List[Any]		  =  self.output(self.after_norm(snake_case							)							)
																			A__						:  Any		  =  self.layer_scale_a.unsqueeze(-1							).unsqueeze(-1							) * layer_output
																			# Second residual connection
																			A__						:  Dict		  =  hidden_states + self.drop_path(snake_case							)

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

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

																			# Second residual connection inside the PoolFormerOutput block
																			A__						:  int		  =  self.drop_path(self.output(self.after_norm(snake_case							)							)							)
																			A__						:  Tuple		  =  hidden_states + layer_output

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






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						str			,    snake_case      :						Tuple							):
													'''simple docstring'''





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

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

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

													A__						:  Dict		  =  nn.ModuleList(snake_case							)


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						str			,    snake_case      :						int=False			,    snake_case      :						Optional[Any]=True							):
													'''simple docstring'''





													A__						:  List[Any]		  =  () if output_hidden_states else None

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

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






class 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):
							snake_case_     =							PoolFormerConfig
							snake_case_     =							'poolformer'
							snake_case_     =							'pixel_values'
							snake_case_     =							True


							def 						_UpperCamelCase       (			self      :						Optional[Any]			,    snake_case      :						List[str]							):
													'''simple docstring'''





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


							def 						_UpperCamelCase       (			self      :						str			,    snake_case      :						Union[str, Any]			,    snake_case      :						Union[str, Any]=False							):
													'''simple docstring'''





													if isinstance(snake_case			,    snake_case							):
																			A__						:  Tuple		  =  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 							__SCREAMING_SNAKE_CASE						(				UpperCamelCase		):


							def __init__(			self      :						Tuple			,    snake_case      :						Optional[Any]							):
													'''simple docstring'''





													super().__init__(snake_case							)
													A__						:  Optional[Any]		  =  config

													A__						:  Optional[Any]		  =  PoolFormerEncoder(snake_case							)

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


							def 						_UpperCamelCase       (			self      :						Union[str, Any]							):
													'''simple docstring'''





													return self.embeddings.patch_embeddings


							@add_start_docstrings_to_model_forward(snake_case							)
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC			,    output_type=snake_case			,    config_class=_CONFIG_FOR_DOC			,    modality="""vision"""			,    expected_output=_EXPECTED_OUTPUT_SHAPE			,    )
							def 						_UpperCamelCase       (			self      :						int			,    snake_case      :						Optional[torch.FloatTensor] = None			,    snake_case      :						Optional[bool] = None			,    snake_case      :						Optional[bool] = None			,    ):
													'''simple docstring'''





													A__						:  Optional[int]		  =  (
													    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
													)
													A__						:  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"""							)

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

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

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






class 							__SCREAMING_SNAKE_CASE						(				nn.Module		):


							def __init__(			self      :						Tuple			,    snake_case      :						List[Any]							):
													'''simple docstring'''





													super().__init__()
													A__						:  Union[str, Any]		  =  nn.Linear(config.hidden_size			,    config.hidden_size							)


							def 						_UpperCamelCase       (			self      :						Union[str, Any]			,    snake_case      :						Any							):
													'''simple docstring'''





													A__						:  List[str]		  =  self.dense(snake_case							)
													return output






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


							def __init__(			self      :						Optional[Any]			,    snake_case      :						List[str]							):
													'''simple docstring'''





													super().__init__(snake_case							)
													A__						:  Optional[Any]		  =  config.num_labels
													A__						:  Dict		  =  PoolFormerModel(snake_case							)

													# Final norm
													A__						:  Dict		  =  PoolFormerGroupNorm(config.hidden_sizes[-1]							)
													# Classifier head
													A__						:  Dict		  =  (
													    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(snake_case							)
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT			,    output_type=snake_case			,    config_class=_CONFIG_FOR_DOC			,    expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,    )
							def 						_UpperCamelCase       (			self      :						Dict			,    snake_case      :						Optional[torch.FloatTensor] = None			,    snake_case      :						Optional[torch.LongTensor] = None			,    snake_case      :						Optional[bool] = None			,    snake_case      :						Optional[bool] = None			,    ):
													'''simple docstring'''





													A__						:  Union[str, Any]		  =  return_dict if return_dict is not None else self.config.use_return_dict

													A__						:  str		  =  self.poolformer(
													    snake_case			,    output_hidden_states=snake_case			,    return_dict=snake_case			,    )

													A__						:  str		  =  outputs[0]

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

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

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

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

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














"""simple docstring"""



import os

try:
						from .build_directory_md import good_file_paths
except ImportError:
						from build_directory_md import good_file_paths  # type: ignore

A_    =							list(good_file_paths())
assert filepaths, "good_file_paths() failed!"

A_    =							[file for file in filepaths if file != file.lower()]
if upper_files:
						print(F'{len(upper_files)} files contain uppercase characters:')
						print('''\n'''.join(upper_files) + '''\n''')

A_    =							[file for file in filepaths if ''' ''' in file]
if space_files:
						print(F'{len(space_files)} files contain space characters:')
						print('''\n'''.join(space_files) + '''\n''')

A_    =							[file for file in filepaths if '''-''' in file]
if hyphen_files:
						print(F'{len(hyphen_files)} files contain hyphen characters:')
						print('''\n'''.join(hyphen_files) + '''\n''')

A_    =							[file for file in filepaths if os.sep not in file]
if nodir_files:
						print(F'{len(nodir_files)} files are not in a directory:')
						print('''\n'''.join(nodir_files) + '''\n''')

A_    =							len(upper_files + space_files + hyphen_files + nodir_files)
if bad_files:
						import sys

						sys.exit(bad_files)






