




'''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_      :		List[Any]							      =       logging.get_logger(__name__)

# General docstring
a_      :		str							      =       """PoolFormerConfig"""

# Base docstring
a_      :		Optional[int]							      =       """sail/poolformer_s12"""
a_      :		Optional[int]							      =       [1, 5_12, 7, 7]

# Image classification docstring
a_      :		List[Any]							      =       """sail/poolformer_s12"""
a_      :		int							      =       """tabby, tabby cat"""

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





def      a_							(      __snake_case							:			Dict   ,       __snake_case							:			float = 0.0   ,       __snake_case							:			bool = False   )							->						str:



       """simple docstring"""
       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				):






       def __init__(							self,		lowerCAmelCase = None  ):



              """simple docstring"""







              super().__init__()
              lowerCamelCase_							=drop_prob






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







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





class   __UpperCamelCase       ( nn.Module				):






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase=None  ):



              """simple docstring"""







              super().__init__()
              lowerCamelCase_							=patch_size if isinstance(lowerCAmelCase,		collections.abc.Iterable  ) else (patch_size, patch_size)
              lowerCamelCase_							=stride if isinstance(lowerCAmelCase,		collections.abc.Iterable  ) else (stride, stride)
              lowerCamelCase_							=padding if isinstance(lowerCAmelCase,		collections.abc.Iterable  ) else (padding, padding)

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






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=self.projection(lowerCAmelCase  )
              lowerCamelCase_							=self.norm(lowerCAmelCase  )
              return embeddings





class   __UpperCamelCase       ( nn.GroupNorm				):






       def __init__(							self,		lowerCAmelCase,		**lowerCAmelCase  ):



              """simple docstring"""







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





class   __UpperCamelCase       ( nn.Module				):






       def __init__(							self,		lowerCAmelCase  ):



              """simple docstring"""







              super().__init__()
              lowerCamelCase_							=nn.AvgPoolad(lowerCAmelCase,		stride=1,		padding=pool_size // 2,		count_include_pad=lowerCAmelCase  )






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              return self.pool(lowerCAmelCase  ) - hidden_states





class   __UpperCamelCase       ( nn.Module				):






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







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






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=self.conva(lowerCAmelCase  )
              lowerCamelCase_							=self.act_fn(lowerCAmelCase  )
              lowerCamelCase_							=self.drop(lowerCAmelCase  )
              lowerCamelCase_							=self.conva(lowerCAmelCase  )
              lowerCamelCase_							=self.drop(lowerCAmelCase  )

              return hidden_states





class   __UpperCamelCase       ( nn.Module				):






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              super().__init__()
              lowerCamelCase_							=PoolFormerPooling(lowerCAmelCase  )
              lowerCamelCase_							=PoolFormerOutput(lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  )
              lowerCamelCase_							=PoolFormerGroupNorm(lowerCAmelCase  )
              lowerCamelCase_							=PoolFormerGroupNorm(lowerCAmelCase  )

              # Useful for training neural nets
              lowerCamelCase_							=PoolFormerDropPath(lowerCAmelCase  ) 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((lowerCAmelCase)  ),		requires_grad=lowerCAmelCase  )
                     lowerCamelCase_							=nn.Parameter(
                         config.layer_scale_init_value * torch.ones((lowerCAmelCase)  ),		requires_grad=lowerCAmelCase  )






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







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

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

                     lowerCamelCase_							=(output,) + outputs
                     return outputs

              else:
                     lowerCamelCase_							=self.drop_path(self.pooling(self.before_norm(lowerCAmelCase  )  )  )
                     # 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(lowerCAmelCase  )  )  )
                     lowerCamelCase_							=hidden_states + layer_output

                     lowerCamelCase_							=(output,) + outputs
                     return outputs





class   __UpperCamelCase       ( nn.Module				):






       def __init__(							self,		lowerCAmelCase  ):



              """simple docstring"""







              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(lowerCAmelCase  )

              # 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(
                                    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_							=nn.ModuleList(lowerCAmelCase  )






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase=False,		lowerCAmelCase=True  ):



              """simple docstring"""







              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(lowerCAmelCase  )
                     # Send the embeddings through the blocks
                     for _, blk in enumerate(lowerCAmelCase  ):
                            lowerCamelCase_							=blk(lowerCAmelCase  )
                            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=lowerCAmelCase,		hidden_states=lowerCAmelCase  )





class   __UpperCamelCase       ( lowerCamelCase__				):
       lowercase	:    Tuple      						=PoolFormerConfig
       lowercase	:    Union[str, Any]      						='poolformer'
       lowercase	:    Dict      						='pixel_values'
       lowercase	:    Tuple      						=True






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              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      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase=False  ):



              """simple docstring"""







              if isinstance(lowerCAmelCase,		lowerCAmelCase  ):
                     lowerCamelCase_							=value





a_      :		str							      =       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_      :		Dict							      =       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.'			,					lowerCamelCase__			,					)
class   __UpperCamelCase       ( lowerCamelCase__				):






       def __init__(							self,		lowerCAmelCase  ):



              """simple docstring"""







              super().__init__(lowerCAmelCase  )
              lowerCamelCase_							=config

              lowerCamelCase_							=PoolFormerEncoder(lowerCAmelCase  )

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






       def      lowercase__     (							self  ):



              """simple docstring"""







              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      lowercase__     (							self,		lowerCAmelCase = None,		lowerCAmelCase = None,		lowerCAmelCase = None,		):



              """simple docstring"""







              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(
                  lowerCAmelCase,		output_hidden_states=lowerCAmelCase,		return_dict=lowerCAmelCase,		)
              lowerCamelCase_							=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   __UpperCamelCase       ( nn.Module				):






       def __init__(							self,		lowerCAmelCase  ):



              """simple docstring"""







              super().__init__()
              lowerCamelCase_							=nn.Linear(config.hidden_size,		config.hidden_size  )






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=self.dense(lowerCAmelCase  )
              return output





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






       def __init__(							self,		lowerCAmelCase  ):



              """simple docstring"""







              super().__init__(lowerCAmelCase  )
              lowerCamelCase_							=config.num_labels
              lowerCamelCase_							=PoolFormerModel(lowerCAmelCase  )

              # 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(lowerCAmelCase  )
       @add_code_sample_docstrings(
           checkpoint=_IMAGE_CLASS_CHECKPOINT,		output_type=lowerCAmelCase,		config_class=_CONFIG_FOR_DOC,		expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT,		)
       def      lowercase__     (							self,		lowerCAmelCase = None,		lowerCAmelCase = None,		lowerCAmelCase = None,		lowerCAmelCase = None,		):



              """simple docstring"""







              lowerCamelCase_							=return_dict if return_dict is not None else self.config.use_return_dict

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

              lowerCamelCase_							=outputs[0]

              lowerCamelCase_							=self.classifier(self.norm(lowerCAmelCase  ).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(lowerCAmelCase,		lowerCAmelCase  )
                     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(lowerCAmelCase,		lowerCAmelCase  )

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

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












import re
import string

import numpy as np

import datasets


UpperCAmelCase_ :						Dict      =      '''
Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.
'''

UpperCAmelCase_ :						Any      =      '''
Args:
    predictions: List of predicted texts.
    references: List of reference texts.
    regexes_to_ignore: List, defaults to None. Regex expressions of characters to
        ignore when calculating the exact matches. Note: these regexes are removed
        from the input data before the changes based on the options below (e.g. ignore_case,
        ignore_punctuation, ignore_numbers) are applied.
    ignore_case: Boolean, defaults to False. If true, turns everything
        to lowercase so that capitalization differences are ignored.
    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
Returns:
    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.
Examples:
    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["exact_match"], 1))
    25.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results["exact_match"], 1))
    50.0


    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results["exact_match"], 1))
    75.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)
    >>> print(round(results["exact_match"], 1))
    100.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]
    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["exact_match"], 1))
    33.3

'''

UpperCAmelCase_ :						Tuple      =      '''
'''

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,   _KWARGS_DESCRIPTION      )
class     _SCREAMING_SNAKE_CASE       (			datasets.Metric      ):





	def 						_A (		self				:					Optional[int]	):
		return datasets.MetricInfo(
		    description=_DESCRIPTION ,  citation=_CITATION ,  inputs_description=_KWARGS_DESCRIPTION ,  features=datasets.Features(
		        {
		            """predictions""": datasets.Value("""string""" ,  id="""sequence"""	),
		            """references""": datasets.Value("""string""" ,  id="""sequence"""	),
		        }	) ,  reference_urls=[] ,  )





	def 						_A (		self				:					Optional[int] ,  __lowerCamelCase				:					Union[str, Any] ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					List[str]=None ,  __lowerCamelCase				:					Optional[int]=False ,  __lowerCamelCase				:					int=False ,  __lowerCamelCase				:					Optional[int]=False ,  ):
		if regexes_to_ignore is not None:
			for s in regexes_to_ignore:
				UpperCamelCase     :str          =	np.array([re.sub(__lowerCamelCase ,  """""" ,  __lowerCamelCase	) for x in predictions]	)
				UpperCamelCase     :Tuple          =	np.array([re.sub(__lowerCamelCase ,  """""" ,  __lowerCamelCase	) for x in references]	)
		else:
			UpperCamelCase     :Any          =	np.asarray(__lowerCamelCase	)
			UpperCamelCase     :str          =	np.asarray(__lowerCamelCase	)

		if ignore_case:
			UpperCamelCase     :Tuple          =	np.char.lower(__lowerCamelCase	)
			UpperCamelCase     :Any          =	np.char.lower(__lowerCamelCase	)

		if ignore_punctuation:
			UpperCamelCase     :Optional[int]          =	string.punctuation.maketrans("""""" ,  """""" ,  string.punctuation	)
			UpperCamelCase     :Optional[Any]          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)
			UpperCamelCase     :List[str]          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)

		if ignore_numbers:
			UpperCamelCase     :Tuple          =	string.digits.maketrans("""""" ,  """""" ,  string.digits	)
			UpperCamelCase     :Dict          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)
			UpperCamelCase     :Tuple          =	np.char.translate(__lowerCamelCase ,  table=__lowerCamelCase	)

		UpperCamelCase     :int          =	predictions == references

		return {"exact_match": np.mean(__lowerCamelCase	) * 100}
