






"""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			:    Any				 =		[1, 512, 7, 7]

# Image classification docstring
a			:    int				 =		"""sail/poolformer_s12"""
a			:    int				 =		"""tabby, tabby cat"""

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





def 						lowercase__(A ,		A = 0.0 ,		A = False						)    ->Any:




						"""simple docstring"""



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


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__ = None      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Union[str, Any]=	drop_prob




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												return drop_path(snake_case__	,  self.drop_prob	,  self.training      )




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												return "p={}".format(self.drop_prob      )


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__=None      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				str=	patch_size if isinstance(snake_case__	,  collections.abc.Iterable      ) else (patch_size, patch_size)
												lowercase__    :				List[Any]=	stride if isinstance(snake_case__	,  collections.abc.Iterable      ) else (stride, stride)
												lowercase__    :				str=	padding if isinstance(snake_case__	,  collections.abc.Iterable      ) else (padding, padding)

												lowercase__    :				List[Any]=	nn.Convad(snake_case__	,  snake_case__	,  kernel_size=snake_case__	,  stride=snake_case__	,  padding=snake_case__      )
												lowercase__    :				Tuple=	norm_layer(snake_case__      ) if norm_layer else nn.Identity()




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.projection(snake_case__      )
												lowercase__    :				Dict=	self.norm(snake_case__      )
												return embeddings


class    __UpperCAmelCase(       nn.GroupNorm							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  **snake_case__      ):
												'''simple docstring'''
												super().__init__(1	,  snake_case__	,  **snake_case__      )


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				int=	nn.AvgPoolad(snake_case__	,  stride=1	,  padding=pool_size // 2	,  count_include_pad=snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												return self.pool(snake_case__      ) - hidden_states


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				int=	nn.Convad(snake_case__	,  snake_case__	,  1      )
												lowercase__    :				List[str]=	nn.Convad(snake_case__	,  snake_case__	,  1      )
												lowercase__    :				List[str]=	PoolFormerDropPath(snake_case__      )
												if isinstance(config.hidden_act	,  snake_case__      ):
																		lowercase__    :				Optional[Any]=	ACTaFN[config.hidden_act]
												else:
																		lowercase__    :				str=	config.hidden_act




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Any=	self.conva(snake_case__      )
												lowercase__    :				Any=	self.act_fn(snake_case__      )
												lowercase__    :				List[Any]=	self.drop(snake_case__      )
												lowercase__    :				Optional[Any]=	self.conva(snake_case__      )
												lowercase__    :				int=	self.drop(snake_case__      )

												return hidden_states


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Dict=	PoolFormerPooling(snake_case__      )
												lowercase__    :				Any=	PoolFormerOutput(snake_case__	,  snake_case__	,  snake_case__	,  snake_case__      )
												lowercase__    :				List[str]=	PoolFormerGroupNorm(snake_case__      )
												lowercase__    :				Dict=	PoolFormerGroupNorm(snake_case__      )

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




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												if self.use_layer_scale:
																		lowercase__    :				Any=	self.pooling(self.before_norm(snake_case__      )      )
																		lowercase__    :				Dict=	self.layer_scale_a.unsqueeze(-1      ).unsqueeze(-1      ) * pooling_output
																		# First residual connection
																		lowercase__    :				Any=	hidden_states + self.drop_path(snake_case__      )
																		lowercase__    :				List[Any]=	()

																		lowercase__    :				Union[str, Any]=	self.output(self.after_norm(snake_case__      )      )
																		lowercase__    :				List[str]=	self.layer_scale_a.unsqueeze(-1      ).unsqueeze(-1      ) * layer_output
																		# Second residual connection
																		lowercase__    :				int=	hidden_states + self.drop_path(snake_case__      )

																		lowercase__    :				str=	(output,) + outputs
																		return outputs

												else:
																		lowercase__    :				str=	self.drop_path(self.pooling(self.before_norm(snake_case__      )      )      )
																		# First residual connection
																		lowercase__    :				Optional[Any]=	pooling_output + hidden_states
																		lowercase__    :				str=	()

																		# Second residual connection inside the PoolFormerOutput block
																		lowercase__    :				Dict=	self.drop_path(self.output(self.after_norm(snake_case__      )      )      )
																		lowercase__    :				Any=	hidden_states + layer_output

																		lowercase__    :				Union[str, Any]=	(output,) + outputs
																		return outputs


class    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				Tuple=	config
												# stochastic depth decay rule
												lowercase__    :				Optional[int]=	[x.item() for x in torch.linspace(0	,  config.drop_path_rate	,  sum(config.depths      )      )]

												# patch embeddings
												lowercase__    :				Tuple=	[]
												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]	,  )      )
												lowercase__    :				int=	nn.ModuleList(snake_case__      )

												# Transformer blocks
												lowercase__    :				int=	[]
												lowercase__    :				Dict=	0
												for i in range(config.num_encoder_blocks      ):
																		# each block consists of layers
																		lowercase__    :				List[Any]=	[]
																		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__      )      )

												lowercase__    :				Tuple=	nn.ModuleList(snake_case__      )




						def 		UpperCAmelCase_	(       self	,  snake_case__	,  snake_case__=False	,  snake_case__=True      ):
												'''simple docstring'''
												lowercase__    :				int=	() if output_hidden_states else None

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

																		if output_hidden_states:
																								lowercase__    :				Optional[Any]=	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    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						__lowerCamelCase    	= PoolFormerConfig
						__lowerCamelCase    	= "poolformer"
						__lowerCamelCase    	= "pixel_values"
						__lowerCamelCase    	= True




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''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	,  snake_case__	,  snake_case__=False      ):
												'''simple docstring'''
												if isinstance(snake_case__	,  snake_case__      ):
																		lowercase__    :				str=	value


a			:    int				 =		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			:    Optional[int]				 =		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."  ,  SCREAMING_SNAKE_CASE__  ,  )
class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__(snake_case__      )
												lowercase__    :				List[str]=	config

												lowercase__    :				Union[str, Any]=	PoolFormerEncoder(snake_case__      )

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




						def 		UpperCAmelCase_	(       self      ):
												'''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	,  snake_case__ = None	,  snake_case__ = None	,  snake_case__ = None	,  ):
												'''simple docstring'''
												lowercase__    :				Union[str, Any]=	(
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												lowercase__    :				Union[str, 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"      )

												lowercase__    :				Any=	self.encoder(
												    snake_case__	,  output_hidden_states=snake_case__	,  return_dict=snake_case__	,  )
												lowercase__    :				List[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    __UpperCAmelCase(       nn.Module							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__()
												lowercase__    :				str=	nn.Linear(config.hidden_size	,  config.hidden_size      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	self.dense(snake_case__      )
												return output


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




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												super().__init__(snake_case__      )
												lowercase__    :				List[str]=	config.num_labels
												lowercase__    :				int=	PoolFormerModel(snake_case__      )

												# Final norm
												lowercase__    :				Union[str, Any]=	PoolFormerGroupNorm(config.hidden_sizes[-1]      )
												# Classifier head
												lowercase__    :				Tuple=	(
												    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	,  snake_case__ = None	,  snake_case__ = None	,  snake_case__ = None	,  snake_case__ = None	,  ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	return_dict if return_dict is not None else self.config.use_return_dict

												lowercase__    :				List[str]=	self.poolformer(
												    snake_case__	,  output_hidden_states=snake_case__	,  return_dict=snake_case__	,  )

												lowercase__    :				Dict=	outputs[0]

												lowercase__    :				List[str]=	self.classifier(self.norm(snake_case__      ).mean([-2, -1]      )      )

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

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

												if not return_dict:
																		lowercase__    :				Optional[Any]=	(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"""







from collections import defaultdict
from pathlib import Path

import pandas as pd
from rouge_cli import calculate_rouge_path

from utils import calculate_rouge


a			:    Optional[Any]				 =		[
    """Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of the"""
    """ final seconds on board Flight 9525. The Germanwings co-pilot says he had a \"previous episode of severe"""
    """ depression\" German airline confirms it knew of Andreas Lubitz's depression years before he took control.""",
    """The Palestinian Authority officially becomes the 123rd member of the International Criminal Court. The formal"""
    """ accession was marked with a ceremony at The Hague, in the Netherlands. The Palestinians signed the ICC's"""
    """ founding Rome Statute in January. Israel and the United States opposed the Palestinians' efforts to join the"""
    """ body.""",
    """Amnesty International releases its annual report on the death penalty. The report catalogs the use of"""
    """ state-sanctioned killing as a punitive measure across the globe. At least 607 people were executed around the"""
    """ world in 2014, compared to 778 in 2013. The U.S. remains one of the worst offenders for imposing capital"""
    """ punishment.""",
]

a			:    str				 =		[
    """Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports ."""
    """ Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz"""
    """ had informed his Lufthansa training school of an episode of severe depression, airline says .""",
    """Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June ."""
    """ Israel and the United States opposed the move, which could open the door to war crimes investigations against"""
    """ Israelis .""",
    """Amnesty's annual death penalty report catalogs encouraging signs, but setbacks in numbers of those sentenced to"""
    """ death . Organization claims that governments around the world are using the threat of terrorism to advance"""
    """ executions . The number of executions worldwide has gone down by almost 22% compared with 2013, but death"""
    """ sentences up by 28% .""",
]





def 						lowercase__()    ->List[Any]:




						"""simple docstring"""



						lowercase__    :				str=	calculate_rouge(A ,		A ,		bootstrap_aggregation=A ,		rouge_keys=["rouge2", "rougeL"]						)
						assert isinstance(A ,		A						)
						lowercase__    :				Optional[int]=	calculate_rouge(A ,		A ,		bootstrap_aggregation=A ,		rouge_keys=["rouge2"]						)
						assert (
						    pd.DataFrame(no_aggregation["rouge2"]						).fmeasure.mean()
						    == pd.DataFrame(no_aggregation_just_ra["rouge2"]						).fmeasure.mean()
						)





def 						lowercase__()    ->int:




						"""simple docstring"""



						lowercase__    :				Optional[int]=	"rougeLsum"
						lowercase__    :				str=	calculate_rouge(A ,		A ,		newline_sep=A ,		rouge_keys=[k]						)[k]
						lowercase__    :				Union[str, Any]=	calculate_rouge(A ,		A ,		newline_sep=A ,		rouge_keys=[k]						)[k]
						assert score > score_no_sep





def 						lowercase__()    ->Tuple:




						"""simple docstring"""



						lowercase__    :				Tuple=	["rouge1", "rouge2", "rougeL"]
						lowercase__    :				Optional[Any]=	calculate_rouge(A ,		A ,		newline_sep=A ,		rouge_keys=A						)
						lowercase__    :				Dict=	calculate_rouge(A ,		A ,		newline_sep=A ,		rouge_keys=A						)
						assert score_sep == score_no_sep





def 						lowercase__()    ->Optional[int]:




						"""simple docstring"""



						lowercase__    :				int=	[
						    "Her older sister, Margot Frank, died in 1945, a month earlier than previously thought.",
						    "Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports .",
						]
						lowercase__    :				Dict=	[
						    "Margot Frank, died in 1945, a month earlier than previously thought.",
						    "Prosecutor: \"No videos were used in the crash investigation\" German papers say they saw a cell phone video of"
						    " the final seconds on board Flight 9525.",
						]
						assert calculate_rouge(A ,		A ,		newline_sep=A						) == calculate_rouge(A ,		A ,		newline_sep=A						)





def 						lowercase__()    ->Dict:




						"""simple docstring"""



						lowercase__    :				List[str]=	[
						    "\" \"a person who has such a video needs to immediately give it to the investigators,\" prosecutor says .<n> \"it is a very disturbing scene,\" editor-in-chief of bild online tells \"erin burnett: outfront\" "
						]
						lowercase__    :				Union[str, Any]=	[
						    " Marseille prosecutor says \"so far no videos were used in the crash investigation\" despite media reports . Journalists at Bild and Paris Match are \"very confident\" the video clip is real, an editor says . Andreas Lubitz had informed his Lufthansa training school of an episode of severe depression, airline says ."
						]

						lowercase__    :				List[Any]=	calculate_rouge(A ,		A ,		rouge_keys=["rougeLsum"] ,		newline_sep=A						)["rougeLsum"]
						lowercase__    :				Optional[Any]=	calculate_rouge(A ,		A ,		rouge_keys=["rougeLsum"]						)["rougeLsum"]
						assert new_score > prev_score







def 						lowercase__()    ->Optional[Any]:




						"""simple docstring"""



						lowercase__    :				Optional[Any]=	Path("examples/seq2seq/test_data/wmt_en_ro"						)
						lowercase__    :				Union[str, Any]=	calculate_rouge_path(data_dir.joinpath("test.source"						) ,		data_dir.joinpath("test.target"						)						)
						assert isinstance(A ,		A						)
						lowercase__    :				List[Any]=	calculate_rouge_path(
						    data_dir.joinpath("test.source"						) ,		data_dir.joinpath("test.target"						) ,		bootstrap_aggregation=A						)
						assert isinstance(A ,		A						)






