




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


UpperCAmelCase_						=logging.get_logger(__name__)

# General docstring
UpperCAmelCase_						="""PoolFormerConfig"""

# Base docstring
UpperCAmelCase_						="""sail/poolformer_s12"""
UpperCAmelCase_						=[1, 512, 7, 7]

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

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



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


class 							__UpperCamelCase			( nn.Module  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_ = None						):
											super().__init__()
											lowerCAmelCase   =		drop_prob



				def       __snake_case		( self       , UpperCAmelCase_						):
											return drop_path(UpperCAmelCase_       , self.drop_prob       , self.training						)






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




class 							__UpperCamelCase			( nn.Module  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_=None						):
											super().__init__()
											lowerCAmelCase   =		patch_size if isinstance(UpperCAmelCase_       , collections.abc.Iterable						) else (patch_size, patch_size)
											lowerCAmelCase   =		stride if isinstance(UpperCAmelCase_       , collections.abc.Iterable						) else (stride, stride)
											lowerCAmelCase   =		padding if isinstance(UpperCAmelCase_       , collections.abc.Iterable						) else (padding, padding)

											lowerCAmelCase   =		nn.Convad(UpperCAmelCase_       , UpperCAmelCase_       , kernel_size=UpperCAmelCase_       , stride=UpperCAmelCase_       , padding=UpperCAmelCase_						)
											lowerCAmelCase   =		norm_layer(UpperCAmelCase_						) if norm_layer else nn.Identity()






				def       __snake_case		( self       , UpperCAmelCase_						):
											lowerCAmelCase   =		self.projection(UpperCAmelCase_						)
											lowerCAmelCase   =		self.norm(UpperCAmelCase_						)
											return embeddings




class 							__UpperCamelCase			( nn.GroupNorm  ):





				'''simple docstring'''



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




class 							__UpperCamelCase			( nn.Module  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_						):
											super().__init__()
											lowerCAmelCase   =		nn.AvgPoolad(UpperCAmelCase_       , stride=1       , padding=pool_size // 2       , count_include_pad=UpperCAmelCase_						)






				def       __snake_case		( self       , UpperCAmelCase_						):
											return self.pool(UpperCAmelCase_						) - hidden_states




class 							__UpperCamelCase			( nn.Module  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
											super().__init__()
											lowerCAmelCase   =		nn.Convad(UpperCAmelCase_       , UpperCAmelCase_       , 1						)
											lowerCAmelCase   =		nn.Convad(UpperCAmelCase_       , UpperCAmelCase_       , 1						)
											lowerCAmelCase   =		PoolFormerDropPath(UpperCAmelCase_						)
											if isinstance(config.hidden_act       , UpperCAmelCase_						):
																		lowerCAmelCase   =		ACTaFN[config.hidden_act]
											else:
																		lowerCAmelCase   =		config.hidden_act






				def       __snake_case		( self       , UpperCAmelCase_						):
											lowerCAmelCase   =		self.conva(UpperCAmelCase_						)
											lowerCAmelCase   =		self.act_fn(UpperCAmelCase_						)
											lowerCAmelCase   =		self.drop(UpperCAmelCase_						)
											lowerCAmelCase   =		self.conva(UpperCAmelCase_						)
											lowerCAmelCase   =		self.drop(UpperCAmelCase_						)

											return hidden_states




class 							__UpperCamelCase			( nn.Module  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
											super().__init__()
											lowerCAmelCase   =		PoolFormerPooling(UpperCAmelCase_						)
											lowerCAmelCase   =		PoolFormerOutput(UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						)
											lowerCAmelCase   =		PoolFormerGroupNorm(UpperCAmelCase_						)
											lowerCAmelCase   =		PoolFormerGroupNorm(UpperCAmelCase_						)

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






				def       __snake_case		( self       , UpperCAmelCase_						):
											if self.use_layer_scale:
																		lowerCAmelCase   =		self.pooling(self.before_norm(UpperCAmelCase_						)						)
																		lowerCAmelCase   =		self.layer_scale_a.unsqueeze(-1						).unsqueeze(-1						) * pooling_output
																		# First residual connection
																		lowerCAmelCase   =		hidden_states + self.drop_path(UpperCAmelCase_						)
																		lowerCAmelCase   =		()

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

																		lowerCAmelCase   =		(output,) + outputs
																		return outputs

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

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

																		lowerCAmelCase   =		(output,) + outputs
																		return outputs




class 							__UpperCamelCase			( nn.Module  ):





				'''simple docstring'''



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

											# patch embeddings
											lowerCAmelCase   =		[]
											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   =		nn.ModuleList(UpperCAmelCase_						)

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

											lowerCAmelCase   =		nn.ModuleList(UpperCAmelCase_						)






				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_=False       , UpperCAmelCase_=True						):
											lowerCAmelCase   =		() if output_hidden_states else None

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

																		if output_hidden_states:
																									lowerCAmelCase   =		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			( __UpperCAmelCase  ):





				'''simple docstring'''



				__a							: Tuple										=PoolFormerConfig
				__a							: List[Any]										="""poolformer"""
				__a							: Dict										="""pixel_values"""
				__a							: int										=True



				def       __snake_case		( self       , UpperCAmelCase_						):
											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						):
											if isinstance(UpperCAmelCase_       , UpperCAmelCase_						):
																		lowerCAmelCase   =		value




UpperCAmelCase_						=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.
"""

UpperCAmelCase_						=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 							__UpperCamelCase			( __UpperCAmelCase  ):





				'''simple docstring'''



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

											lowerCAmelCase   =		PoolFormerEncoder(UpperCAmelCase_						)

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



				def       __snake_case		( self						):
											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       , ):
											lowerCAmelCase   =		(
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											lowerCAmelCase   =		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   =		self.encoder(
											    UpperCAmelCase_       , output_hidden_states=UpperCAmelCase_       , return_dict=UpperCAmelCase_       , )
											lowerCAmelCase   =		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  ):





				'''simple docstring'''



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






				def       __snake_case		( self       , UpperCAmelCase_						):
											lowerCAmelCase   =		self.dense(UpperCAmelCase_						)
											return output




@add_start_docstrings(
    """
    PoolFormer Model transformer with an image classification head on top
    """     ,					__UpperCAmelCase     ,					)
class 							__UpperCamelCase			( __UpperCAmelCase  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_						):
											super().__init__(UpperCAmelCase_						)
											lowerCAmelCase   =		config.num_labels
											lowerCAmelCase   =		PoolFormerModel(UpperCAmelCase_						)

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

											lowerCAmelCase   =		self.poolformer(
											    UpperCAmelCase_       , output_hidden_states=UpperCAmelCase_       , return_dict=UpperCAmelCase_       , )

											lowerCAmelCase   =		outputs[0]

											lowerCAmelCase   =		self.classifier(self.norm(UpperCAmelCase_						).mean([-2, -1]						)						)

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

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

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

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









from __future__ import annotations

import math
from collections import Counter
from string import ascii_lowercase



def       UpperCAmelCase       (     _snake_case							):
							lowerCAmelCase   ,     lowerCAmelCase   =		analyze_text(_snake_case							)
							lowerCAmelCase   =		list(''' ''' + ascii_lowercase							)
							# what is our total sum of probabilities.
							lowerCAmelCase   =		sum(single_char_strings.values()							)

							# one length string
							lowerCAmelCase   =		0
							# for each alpha we go in our dict and if it is in it we calculate entropy
							for ch in my_alphas:
														if ch in single_char_strings:
																					lowerCAmelCase   =		single_char_strings[ch]
																					lowerCAmelCase   =		my_str / all_sum
																					my_fir_sum += prob * math.loga(_snake_case							)  # entropy formula.

    # print entropy
							print(F"""{round(-1 * my_fir_sum							):.1f}"""							)

							# two len string
							lowerCAmelCase   =		sum(two_char_strings.values()							)
							lowerCAmelCase   =		0
							# for each alpha (two in size) calculate entropy.
							for cha in my_alphas:
														for cha in my_alphas:
																					lowerCAmelCase   =		cha + cha
																					if sequence in two_char_strings:
																												lowerCAmelCase   =		two_char_strings[sequence]
																												lowerCAmelCase   =		int(_snake_case							) / all_sum
																												my_sec_sum += prob * math.loga(_snake_case							)

    # print second entropy
							print(F"""{round(-1 * my_sec_sum							):.1f}"""							)

							# print the difference between them
							print(F"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum)							):.1f}"""							)



def       UpperCAmelCase       (     _snake_case							):
							lowerCAmelCase   =		Counter()  # type: ignore
							lowerCAmelCase   =		Counter()  # type: ignore
							single_char_strings[text[-1]] += 1

							# first case when we have space at start.
							two_char_strings[" " + text[0]] += 1
							for i in range(0						, len(_snake_case							) - 1							):
														single_char_strings[text[i]] += 1
														two_char_strings[text[i : i + 2]] += 1
							return single_char_strings, two_char_strings



def       UpperCAmelCase       (     ):
							import doctest

							doctest.testmod()
							# text = (
							#     "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark "
							#     "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest "
							#     "jointure saw horrible. He private he on be imagine suppose. Fertile "
							#     "beloved evident through no service elderly is. Blind there if every no so "
							#     "at. Own neglected you preferred way sincerity delivered his attempted. To "
							#     "of message cottage windows do besides against uncivil.  Delightful "
							#     "unreserved impossible few estimating men favourable see entreaties. She "
							#     "propriety immediate was improving. He or entrance humoured likewise "
							#     "moderate. Much nor game son say feel. Fat make met can must form into "
							#     "gate. Me we offending prevailed discovery. "
							# )

							# calculate_prob(text)


if __name__ == "__main__":
		main()



