"""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      UpperCAmelCase    (		UpperCamelCase__				,			UpperCamelCase__ = 0.0				,			UpperCamelCase__ = False ):






							"""simple docstring"""





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





						def __init__(				self						,__UpperCAmelCase = None   )    -> None:
													super().__init__()
													A__		   =							drop_prob





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> torch.Tensor:
													return drop_path(__UpperCAmelCase						,self.drop_prob						,self.training   )





						def  snake_case__		(				self   )    -> str:
													return "p={}".format(self.drop_prob   )






class 			UpperCamelCase__(	nn.Module       ):





						def __init__(				self						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase=None   )    -> str:
													super().__init__()
													A__		   =							patch_size if isinstance(__UpperCAmelCase						,collections.abc.Iterable   ) else (patch_size, patch_size)
													A__		   =							stride if isinstance(__UpperCAmelCase						,collections.abc.Iterable   ) else (stride, stride)
													A__		   =							padding if isinstance(__UpperCAmelCase						,collections.abc.Iterable   ) else (padding, padding)

													A__		   =							nn.Convad(__UpperCAmelCase						,__UpperCAmelCase						,kernel_size=__UpperCAmelCase						,stride=__UpperCAmelCase						,padding=__UpperCAmelCase   )
													A__		   =							norm_layer(__UpperCAmelCase   ) if norm_layer else nn.Identity()





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Dict:
													A__		   =							self.projection(__UpperCAmelCase   )
													A__		   =							self.norm(__UpperCAmelCase   )
													return embeddings






class 			UpperCamelCase__(	nn.GroupNorm       ):





						def __init__(				self						,__UpperCAmelCase						,**__UpperCAmelCase   )    -> List[Any]:
													super().__init__(1						,__UpperCAmelCase						,**__UpperCAmelCase   )






class 			UpperCamelCase__(	nn.Module       ):





						def __init__(				self						,__UpperCAmelCase   )    -> Any:
													super().__init__()
													A__		   =							nn.AvgPoolad(__UpperCAmelCase						,stride=1						,padding=pool_size // 2						,count_include_pad=__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> List[Any]:
													return self.pool(__UpperCAmelCase   ) - hidden_states






class 			UpperCamelCase__(	nn.Module       ):





						def __init__(				self						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )    -> Dict:
													super().__init__()
													A__		   =							nn.Convad(__UpperCAmelCase						,__UpperCAmelCase						,1   )
													A__		   =							nn.Convad(__UpperCAmelCase						,__UpperCAmelCase						,1   )
													A__		   =							PoolFormerDropPath(__UpperCAmelCase   )
													if isinstance(config.hidden_act						,__UpperCAmelCase   ):
																				A__		   =							ACTaFN[config.hidden_act]
													else:
																				A__		   =							config.hidden_act





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> str:
													A__		   =							self.conva(__UpperCAmelCase   )
													A__		   =							self.act_fn(__UpperCAmelCase   )
													A__		   =							self.drop(__UpperCAmelCase   )
													A__		   =							self.conva(__UpperCAmelCase   )
													A__		   =							self.drop(__UpperCAmelCase   )

													return hidden_states






class 			UpperCamelCase__(	nn.Module       ):





						def __init__(				self						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )    -> str:
													super().__init__()
													A__		   =							PoolFormerPooling(__UpperCAmelCase   )
													A__		   =							PoolFormerOutput(__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase						,__UpperCAmelCase   )
													A__		   =							PoolFormerGroupNorm(__UpperCAmelCase   )
													A__		   =							PoolFormerGroupNorm(__UpperCAmelCase   )

													# Useful for training neural nets
													A__		   =							PoolFormerDropPath(__UpperCAmelCase   ) if drop_path > 0.0 else nn.Identity()
													A__		   =							config.use_layer_scale
													if config.use_layer_scale:
																				A__		   =							nn.Parameter(
																				    config.layer_scale_init_value * torch.ones((__UpperCAmelCase)   )						,requires_grad=__UpperCAmelCase   )
																				A__		   =							nn.Parameter(
																				    config.layer_scale_init_value * torch.ones((__UpperCAmelCase)   )						,requires_grad=__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Any:
													if self.use_layer_scale:
																				A__		   =							self.pooling(self.before_norm(__UpperCAmelCase   )   )
																				A__		   =							self.layer_scale_a.unsqueeze(-1   ).unsqueeze(-1   ) * pooling_output
																				# First residual connection
																				A__		   =							hidden_states + self.drop_path(__UpperCAmelCase   )
																				A__		   =							()

																				A__		   =							self.output(self.after_norm(__UpperCAmelCase   )   )
																				A__		   =							self.layer_scale_a.unsqueeze(-1   ).unsqueeze(-1   ) * layer_output
																				# Second residual connection
																				A__		   =							hidden_states + self.drop_path(__UpperCAmelCase   )

																				A__		   =							(output,) + outputs
																				return outputs

													else:
																				A__		   =							self.drop_path(self.pooling(self.before_norm(__UpperCAmelCase   )   )   )
																				# First residual connection
																				A__		   =							pooling_output + hidden_states
																				A__		   =							()

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

																				A__		   =							(output,) + outputs
																				return outputs






class 			UpperCamelCase__(	nn.Module       ):





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

													# patch embeddings
													A__		   =							[]
													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__		   =							nn.ModuleList(__UpperCAmelCase   )

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

													A__		   =							nn.ModuleList(__UpperCAmelCase   )





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=False						,__UpperCAmelCase=True   )    -> int:
													A__		   =							() if output_hidden_states else None

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

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






class 			UpperCamelCase__(	__A       ):
						lowerCAmelCase__     :  Dict  							=						PoolFormerConfig
						lowerCAmelCase__     :  Dict  							=						'poolformer'
						lowerCAmelCase__     :  Any  							=						'pixel_values'
						lowerCAmelCase__     :  List[str]  							=						True





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> List[str]:
													if isinstance(__UpperCAmelCase						,(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(__UpperCAmelCase						,nn.LayerNorm   ):
																				module.bias.data.zero_()
																				module.weight.data.fill_(1.0   )





						def  snake_case__		(				self						,__UpperCAmelCase						,__UpperCAmelCase=False   )    -> int:
													if isinstance(__UpperCAmelCase						,__UpperCAmelCase   ):
																				A__		   =							value






__lowerCamelCase							 =     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							 =     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.'						,  __A						,  )
class 			UpperCamelCase__(	__A       ):





						def __init__(				self						,__UpperCAmelCase   )    -> List[Any]:
													super().__init__(__UpperCAmelCase   )
													A__		   =							config

													A__		   =							PoolFormerEncoder(__UpperCAmelCase   )

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





						def  snake_case__		(				self   )    -> int:
													return self.embeddings.patch_embeddings





						@add_start_docstrings_to_model_forward(__UpperCAmelCase   )
						@add_code_sample_docstrings(
						    checkpoint=_CHECKPOINT_FOR_DOC						,output_type=__UpperCAmelCase						,config_class=_CONFIG_FOR_DOC						,modality='vision'						,expected_output=_EXPECTED_OUTPUT_SHAPE						,)
						def  snake_case__		(				self						,__UpperCAmelCase = None						,__UpperCAmelCase = None						,__UpperCAmelCase = None						,)    -> Union[Tuple, BaseModelOutputWithNoAttention]:
													A__		   =							(
													    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
													)
													A__		   =							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__		   =							self.encoder(
													    __UpperCAmelCase						,output_hidden_states=__UpperCAmelCase						,return_dict=__UpperCAmelCase						,)
													A__		   =							encoder_outputs[0]

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

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






class 			UpperCamelCase__(	nn.Module       ):





						def __init__(				self						,__UpperCAmelCase   )    -> str:
													super().__init__()
													A__		   =							nn.Linear(config.hidden_size						,config.hidden_size   )





						def  snake_case__		(				self						,__UpperCAmelCase   )    -> str:
													A__		   =							self.dense(__UpperCAmelCase   )
													return output






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





						def __init__(				self						,__UpperCAmelCase   )    -> Optional[Any]:
													super().__init__(__UpperCAmelCase   )
													A__		   =							config.num_labels
													A__		   =							PoolFormerModel(__UpperCAmelCase   )

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

													A__		   =							self.poolformer(
													    __UpperCAmelCase						,output_hidden_states=__UpperCAmelCase						,return_dict=__UpperCAmelCase						,)

													A__		   =							outputs[0]

													A__		   =							self.classifier(self.norm(__UpperCAmelCase   ).mean([-2, -1]   )   )

													A__		   =							None
													if labels is not None:
																				if self.config.problem_type is None:
																											if self.num_labels == 1:
																																		A__		   =							'regression'
																											elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																																		A__		   =							'single_label_classification'
																											else:
																																		A__		   =							'multi_label_classification'

																				if self.config.problem_type == "regression":
																											A__		   =							MSELoss()
																											if self.num_labels == 1:
																																		A__		   =							loss_fct(logits.squeeze()						,labels.squeeze()   )
																											else:
																																		A__		   =							loss_fct(__UpperCAmelCase						,__UpperCAmelCase   )
																				elif self.config.problem_type == "single_label_classification":
																											A__		   =							CrossEntropyLoss()
																											A__		   =							loss_fct(logits.view(-1						,self.num_labels   )						,labels.view(-1   )   )
																				elif self.config.problem_type == "multi_label_classification":
																											A__		   =							BCEWithLogitsLoss()
																											A__		   =							loss_fct(__UpperCAmelCase						,__UpperCAmelCase   )

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

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






"""simple docstring"""




import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
				from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
				from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
				from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


__lowerCamelCase							 =     DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

__lowerCamelCase							 =     "main"
# Default branch name
__lowerCamelCase							 =     "f2c752cfc5c0ab6f4bdec59acea69eefbee381c2"
# One particular commit (not the top of `main`)
__lowerCamelCase							 =     "aaaaaaa"
# This commit does not exist, so we should 404.

__lowerCamelCase							 =     "d9e9f15bc825e4b2c9249e9578f884bbcb5e3684"
# Sha-1 of config.json on the top of `main`, for checking purposes
__lowerCamelCase							 =     "4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3"






@contextlib.contextmanager
def      UpperCAmelCase    (		):






							"""simple docstring"""





							print('Welcome!' )
							yield
							print('Bye!' )






@contextlib.contextmanager
def      UpperCAmelCase    (		):






							"""simple docstring"""





							print('Bonjour!' )
							yield
							print('Au revoir!' )
class 			UpperCamelCase__(	unittest.TestCase       ):





						def  snake_case__		(				self   )    -> List[str]:
													# If the spec is missing, importlib would not be able to import the module dynamically.
													assert transformers.__spec__ is not None
													assert importlib.util.find_spec('transformers'   ) is not None






class 			UpperCamelCase__(	unittest.TestCase       ):





						@unittest.mock.patch('sys.stdout'						,new_callable=io.StringIO   )
						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Dict:
													with ContextManagers([]   ):
																				print('Transformers are awesome!'   )
													# The print statement adds a new line at the end of the output
													self.assertEqual(mock_stdout.getvalue()						,'Transformers are awesome!\n'   )





						@unittest.mock.patch('sys.stdout'						,new_callable=io.StringIO   )
						def  snake_case__		(				self						,__UpperCAmelCase   )    -> List[str]:
													with ContextManagers([context_en()]   ):
																				print('Transformers are awesome!'   )
													# The output should be wrapped with an English welcome and goodbye
													self.assertEqual(mock_stdout.getvalue()						,'Welcome!\nTransformers are awesome!\nBye!\n'   )





						@unittest.mock.patch('sys.stdout'						,new_callable=io.StringIO   )
						def  snake_case__		(				self						,__UpperCAmelCase   )    -> Any:
													with ContextManagers([context_fr(), context_en()]   ):
																				print('Transformers are awesome!'   )
													# The output should be wrapped with an English and French welcome and goodbye
													self.assertEqual(mock_stdout.getvalue()						,'Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'   )





						@require_torch
						def  snake_case__		(				self   )    -> Union[str, Any]:
													self.assertEqual(find_labels(__UpperCAmelCase   )						,['labels']   )
													self.assertEqual(find_labels(__UpperCAmelCase   )						,['labels', 'next_sentence_label']   )
													self.assertEqual(find_labels(__UpperCAmelCase   )						,['start_positions', 'end_positions']   )
													class 			UpperCamelCase__(	__A       ):
																			pass






													self.assertEqual(find_labels(__UpperCAmelCase   )						,['labels']   )





						@require_tf
						def  snake_case__		(				self   )    -> str:
													self.assertEqual(find_labels(__UpperCAmelCase   )						,['labels']   )
													self.assertEqual(find_labels(__UpperCAmelCase   )						,['labels', 'next_sentence_label']   )
													self.assertEqual(find_labels(__UpperCAmelCase   )						,['start_positions', 'end_positions']   )
													class 			UpperCamelCase__(	__A       ):
																			pass






													self.assertEqual(find_labels(__UpperCAmelCase   )						,['labels']   )





						@require_flax
						def  snake_case__		(				self   )    -> List[Any]:
													# Flax models don't have labels
													self.assertEqual(find_labels(__UpperCAmelCase   )						,[]   )
													self.assertEqual(find_labels(__UpperCAmelCase   )						,[]   )
													self.assertEqual(find_labels(__UpperCAmelCase   )						,[]   )
													class 			UpperCamelCase__(	__A       ):
																			pass






													self.assertEqual(find_labels(__UpperCAmelCase   )						,[]   )





