


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

# General docstring
__lowerCAmelCase	: int  				='PoolFormerConfig'

# Base docstring
__lowerCAmelCase	: Tuple  				='sail/poolformer_s12'
__lowerCAmelCase	: str  				=[1, 5_1_2, 7, 7]

# Image classification docstring
__lowerCAmelCase	: Any  				='sail/poolformer_s12'
__lowerCAmelCase	: Dict  				='tabby, tabby cat'

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


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






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:Optional[float] = None	)		->			None:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     int					=		drop_prob


				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:torch.Tensor	)		->			torch.Tensor:
										return drop_path(lowerCAmelCase__			,			self.drop_prob			,			self.training	)



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




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:str			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:Tuple			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Dict=None	)		->			Any:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		patch_size if isinstance(lowerCAmelCase__			,			collections.abc.Iterable	) else (patch_size, patch_size)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		stride if isinstance(lowerCAmelCase__			,			collections.abc.Iterable	) else (stride, stride)
										__SCREAMING_SNAKE_CASE						:     Any					=		padding if isinstance(lowerCAmelCase__			,			collections.abc.Iterable	) else (padding, padding)

										__SCREAMING_SNAKE_CASE						:     Any					=		nn.Convad(lowerCAmelCase__			,			lowerCAmelCase__			,			kernel_size=lowerCAmelCase__			,			stride=lowerCAmelCase__			,			padding=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		norm_layer(lowerCAmelCase__	) if norm_layer else nn.Identity()



				def 		__magic_name__(       self			:Any			,			lowerCAmelCase__			:str	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.projection(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		self.norm(lowerCAmelCase__	)
										return embeddings




class        _lowercase      (			nn.GroupNorm       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:List[str]			,			**lowerCAmelCase__			:Optional[int]	)		->			Any:
										super().__init__(1			,			lowerCAmelCase__			,			**lowerCAmelCase__	)




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:Dict			,			lowerCAmelCase__			:Dict	)		->			str:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     List[str]					=		nn.AvgPoolad(lowerCAmelCase__			,			stride=1			,			padding=pool_size // 2			,			count_include_pad=lowerCAmelCase__	)



				def 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:Tuple	)		->			str:
										return self.pool(lowerCAmelCase__	) - hidden_states




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:List[Any]			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:List[Any]	)		->			List[str]:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     List[str]					=		nn.Convad(lowerCAmelCase__			,			lowerCAmelCase__			,			1	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		nn.Convad(lowerCAmelCase__			,			lowerCAmelCase__			,			1	)
										__SCREAMING_SNAKE_CASE						:     Dict					=		PoolFormerDropPath(lowerCAmelCase__	)
										if isinstance(config.hidden_act			,			lowerCAmelCase__	):
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ACTaFN[config.hidden_act]
										else:
																__SCREAMING_SNAKE_CASE						:     Optional[int]					=		config.hidden_act



				def 		__magic_name__(       self			:Any			,			lowerCAmelCase__			:Dict	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.conva(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     int					=		self.act_fn(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.drop(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.conva(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.drop(lowerCAmelCase__	)

										return hidden_states




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:int			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Optional[int]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[str]	)		->			Any:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		PoolFormerPooling(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     List[str]					=		PoolFormerOutput(lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__			,			lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		PoolFormerGroupNorm(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     List[str]					=		PoolFormerGroupNorm(lowerCAmelCase__	)

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



				def 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Optional[int]	)		->			Optional[Any]:
										if self.use_layer_scale:
																__SCREAMING_SNAKE_CASE						:     int					=		self.pooling(self.before_norm(lowerCAmelCase__	)	)
																__SCREAMING_SNAKE_CASE						:     Dict					=		self.layer_scale_a.unsqueeze(-1	).unsqueeze(-1	) * pooling_output
																# First residual connection
																__SCREAMING_SNAKE_CASE						:     Any					=		hidden_states + self.drop_path(lowerCAmelCase__	)
																__SCREAMING_SNAKE_CASE						:     int					=		()

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

																__SCREAMING_SNAKE_CASE						:     List[Any]					=		(output,) + outputs
																return outputs

										else:
																__SCREAMING_SNAKE_CASE						:     Tuple					=		self.drop_path(self.pooling(self.before_norm(lowerCAmelCase__	)	)	)
																# First residual connection
																__SCREAMING_SNAKE_CASE						:     Dict					=		pooling_output + hidden_states
																__SCREAMING_SNAKE_CASE						:     List[Any]					=		()

																# Second residual connection inside the PoolFormerOutput block
																__SCREAMING_SNAKE_CASE						:     int					=		self.drop_path(self.output(self.after_norm(lowerCAmelCase__	)	)	)
																__SCREAMING_SNAKE_CASE						:     str					=		hidden_states + layer_output

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




class        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:Union[str, Any]			,			lowerCAmelCase__			:Dict	)		->			Optional[int]:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		config
										# stochastic depth decay rule
										__SCREAMING_SNAKE_CASE						:     List[str]					=		[x.item() for x in torch.linspace(0			,			config.drop_path_rate			,			sum(config.depths	)	)]

										# patch embeddings
										__SCREAMING_SNAKE_CASE						:     Dict					=		[]
										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]			,			)	)
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		nn.ModuleList(lowerCAmelCase__	)

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

										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		nn.ModuleList(lowerCAmelCase__	)



				def 		__magic_name__(       self			:Union[str, Any]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Dict=False			,			lowerCAmelCase__			:str=True	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		() if output_hidden_states else None

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

																if output_hidden_states:
																						__SCREAMING_SNAKE_CASE						:     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=lowerCAmelCase__			,			hidden_states=lowerCAmelCase__	)




class        _lowercase      (			A__       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		PoolFormerConfig
				SCREAMING_SNAKE_CASE__				:			Optional[int]						=		'''poolformer'''
				SCREAMING_SNAKE_CASE__				:			Any						=		'''pixel_values'''
				SCREAMING_SNAKE_CASE__				:			Tuple						=		True


				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:int	)		->			Optional[int]:
										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 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Union[str, Any]=False	)		->			str:
										if isinstance(lowerCAmelCase__			,			lowerCAmelCase__	):
																__SCREAMING_SNAKE_CASE						:     Tuple					=		value




__lowerCAmelCase	: Optional[int]  				=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	: Dict  				=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        _lowercase      (			A__       ):






				'''simple docstring'''



				def __init__(       self			:List[str]			,			lowerCAmelCase__			:List[str]	)		->			str:
										super().__init__(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		config

										__SCREAMING_SNAKE_CASE						:     Dict					=		PoolFormerEncoder(lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Dict	)		->			int:
										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 		__magic_name__(       self			:List[Any]			,			lowerCAmelCase__			:Optional[torch.FloatTensor] = None			,			lowerCAmelCase__			:Optional[bool] = None			,			lowerCAmelCase__			:Optional[bool] = None			,			)		->			Union[Tuple, BaseModelOutputWithNoAttention]:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		(
										    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
										)
										__SCREAMING_SNAKE_CASE						:     int					=		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'''	)

										__SCREAMING_SNAKE_CASE						:     str					=		self.encoder(
										    lowerCAmelCase__			,			output_hidden_states=lowerCAmelCase__			,			return_dict=lowerCAmelCase__			,			)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		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        _lowercase      (			nn.Module       ):






				'''simple docstring'''



				def __init__(       self			:str			,			lowerCAmelCase__			:Any	)		->			List[str]:
										super().__init__()
										__SCREAMING_SNAKE_CASE						:     int					=		nn.Linear(config.hidden_size			,			config.hidden_size	)



				def 		__magic_name__(       self			:Union[str, Any]			,			lowerCAmelCase__			:Optional[int]	)		->			List[str]:
										__SCREAMING_SNAKE_CASE						:     Dict					=		self.dense(lowerCAmelCase__	)
										return output




@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''						,			A__						,			)
class        _lowercase      (			A__       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:Any	)		->			Tuple:
										super().__init__(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		config.num_labels
										__SCREAMING_SNAKE_CASE						:     str					=		PoolFormerModel(lowerCAmelCase__	)

										# Final norm
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		PoolFormerGroupNorm(config.hidden_sizes[-1]	)
										# Classifier head
										__SCREAMING_SNAKE_CASE						:     int					=		(
										    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 		__magic_name__(       self			:Optional[Any]			,			lowerCAmelCase__			:Optional[torch.FloatTensor] = None			,			lowerCAmelCase__			:Optional[torch.LongTensor] = None			,			lowerCAmelCase__			:Optional[bool] = None			,			lowerCAmelCase__			:Optional[bool] = None			,			)		->			Union[Tuple, ImageClassifierOutputWithNoAttention]:
										__SCREAMING_SNAKE_CASE						:     Any					=		return_dict if return_dict is not None else self.config.use_return_dict

										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		self.poolformer(
										    lowerCAmelCase__			,			output_hidden_states=lowerCAmelCase__			,			return_dict=lowerCAmelCase__			,			)

										__SCREAMING_SNAKE_CASE						:     Dict					=		outputs[0]

										__SCREAMING_SNAKE_CASE						:     Dict					=		self.classifier(self.norm(lowerCAmelCase__	).mean([-2, -1]	)	)

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

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

										if not return_dict:
																__SCREAMING_SNAKE_CASE						:     int					=		(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 unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_a      	=  {'''LayoutLMv2Config''', '''LayoutLMv3Config'''}





@is_pipeline_test
class __lowerCamelCase (      unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=      MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
	UpperCamelCase__								=      TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

	if model_mapping is not None:
		UpperCamelCase__								=      {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
	if tf_model_mapping is not None:
		UpperCamelCase__								=      {
		    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
		}

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			ZeroShotClassificationPipeline(
							    model=UpperCAmelCase       ,    tokenizer=UpperCAmelCase       ,    candidate_labels=['polics', 'health']    )
							return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			classifier('Who are you voting for in 2020?'       ,    candidate_labels='politics'    )
							self.assertEqual(UpperCAmelCase       ,    {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    )]}    )

							# No kwarg
							_UpperCAmelCase									=			classifier('Who are you voting for in 2020?'       ,    ['politics']    )
							self.assertEqual(UpperCAmelCase       ,    {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    )]}    )

							_UpperCAmelCase									=			classifier('Who are you voting for in 2020?'       ,    candidate_labels=['politics']    )
							self.assertEqual(UpperCAmelCase       ,    {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    )]}    )

							_UpperCAmelCase									=			classifier('Who are you voting for in 2020?'       ,    candidate_labels='politics, public health'    )
							self.assertEqual(
							    UpperCAmelCase       ,    {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )]}    )
							self.assertAlmostEqual(sum(nested_simplify(outputs['scores']    )    )       ,    1.0    )

							_UpperCAmelCase									=			classifier('Who are you voting for in 2020?'       ,    candidate_labels=['politics', 'public health']    )
							self.assertEqual(
							    UpperCAmelCase       ,    {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )]}    )
							self.assertAlmostEqual(sum(nested_simplify(outputs['scores']    )    )       ,    1.0    )

							_UpperCAmelCase									=			classifier(
							    'Who are you voting for in 2020?'       ,    candidate_labels='politics'       ,    hypothesis_template='This text is about {}'    )
							self.assertEqual(UpperCAmelCase       ,    {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    )]}    )

							# https://github.com/huggingface/transformers/issues/13846
							_UpperCAmelCase									=			classifier(['I am happy']       ,    ['positive', 'negative']    )
							self.assertEqual(
							    UpperCAmelCase       ,    [
							        {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )]}
							        for i in range(1    )
							    ]       ,    )
							_UpperCAmelCase									=			classifier(['I am happy', 'I am sad']       ,    ['positive', 'negative']    )
							self.assertEqual(
							    UpperCAmelCase       ,    [
							        {'sequence': ANY(UpperCAmelCase    ), 'labels': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )], 'scores': [ANY(UpperCAmelCase    ), ANY(UpperCAmelCase    )]}
							        for i in range(2    )
							    ]       ,    )

							with self.assertRaises(UpperCAmelCase    ):
													classifier(''       ,    candidate_labels='politics'    )

							with self.assertRaises(UpperCAmelCase    ):
													classifier(UpperCAmelCase       ,    candidate_labels='politics'    )

							with self.assertRaises(UpperCAmelCase    ):
													classifier('Who are you voting for in 2020?'       ,    candidate_labels=''    )

							with self.assertRaises(UpperCAmelCase    ):
													classifier('Who are you voting for in 2020?'       ,    candidate_labels=UpperCAmelCase    )

							with self.assertRaises(UpperCAmelCase    ):
													classifier(
													    'Who are you voting for in 2020?'       ,    candidate_labels='politics'       ,    hypothesis_template='Not formatting template'       ,    )

							with self.assertRaises(UpperCAmelCase    ):
													classifier(
													    'Who are you voting for in 2020?'       ,    candidate_labels='politics'       ,    hypothesis_template=UpperCAmelCase       ,    )

							self.run_entailment_id(UpperCAmelCase    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			zero_shot_classifier.model.config
							_UpperCAmelCase									=			config.labelaid
							_UpperCAmelCase									=			zero_shot_classifier.entailment_id

							_UpperCAmelCase									=			{'LABEL_0': 0, 'LABEL_1': 1, 'LABEL_2': 2}
							self.assertEqual(zero_shot_classifier.entailment_id       ,    -1    )

							_UpperCAmelCase									=			{'entailment': 0, 'neutral': 1, 'contradiction': 2}
							self.assertEqual(zero_shot_classifier.entailment_id       ,    0    )

							_UpperCAmelCase									=			{'ENTAIL': 0, 'NON-ENTAIL': 1}
							self.assertEqual(zero_shot_classifier.entailment_id       ,    0    )

							_UpperCAmelCase									=			{'ENTAIL': 2, 'NEUTRAL': 1, 'CONTR': 0}
							self.assertEqual(zero_shot_classifier.entailment_id       ,    2    )

							_UpperCAmelCase									=			original_labelaid
							self.assertEqual(UpperCAmelCase       ,    zero_shot_classifier.entailment_id    )

	@require_torch
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			pipeline(
							    'zero-shot-classification'       ,    model='sshleifer/tiny-distilbert-base-cased-distilled-squad'       ,    framework='pt'       ,    )
							# There was a regression in 4.10 for this
							# Adding a test so we don't make the mistake again.
							# https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
							zero_shot_classifier(
							    'Who are you voting for in 2020?' * 100       ,    candidate_labels=['politics', 'public health', 'science']    )

	@require_torch
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			pipeline(
							    'zero-shot-classification'       ,    model='sshleifer/tiny-distilbert-base-cased-distilled-squad'       ,    framework='pt'       ,    )
							_UpperCAmelCase									=			zero_shot_classifier(
							    'Who are you voting for in 2020?'       ,    candidate_labels=['politics', 'public health', 'science']    )

							self.assertEqual(
							    nested_simplify(UpperCAmelCase    )       ,    {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['science', 'public health', 'politics'],
							        'scores': [0.3_33, 0.3_33, 0.3_33],
							    }       ,    )

	@require_tf
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			pipeline(
							    'zero-shot-classification'       ,    model='sshleifer/tiny-distilbert-base-cased-distilled-squad'       ,    framework='tf'       ,    )
							_UpperCAmelCase									=			zero_shot_classifier(
							    'Who are you voting for in 2020?'       ,    candidate_labels=['politics', 'public health', 'science']    )

							self.assertEqual(
							    nested_simplify(UpperCAmelCase    )       ,    {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['science', 'public health', 'politics'],
							        'scores': [0.3_33, 0.3_33, 0.3_33],
							    }       ,    )

	@slow
	@require_torch
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			pipeline('zero-shot-classification'       ,    model='roberta-large-mnli'       ,    framework='pt'    )
							_UpperCAmelCase									=			zero_shot_classifier(
							    'Who are you voting for in 2020?'       ,    candidate_labels=['politics', 'public health', 'science']    )

							self.assertEqual(
							    nested_simplify(UpperCAmelCase    )       ,    {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['politics', 'public health', 'science'],
							        'scores': [0.9_76, 0.0_15, 0.0_09],
							    }       ,    )
							_UpperCAmelCase									=			zero_shot_classifier(
							    'The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'
							    ' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'
							    ' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'
							    ' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'
							    ' machine translation tasks show these models to be superior in quality while being more parallelizable'
							    ' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'
							    ' English-to-German translation task, improving over the existing best results, including ensembles by'
							    ' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'
							    ' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'
							    ' fraction of the training costs of the best models from the literature. We show that the Transformer'
							    ' generalizes well to other tasks by applying it successfully to English constituency parsing both with'
							    ' large and limited training data.'       ,    candidate_labels=['machine learning', 'statistics', 'translation', 'vision']       ,    multi_label=UpperCAmelCase       ,    )
							self.assertEqual(
							    nested_simplify(UpperCAmelCase    )       ,    {
							        'sequence': (
							            'The dominant sequence transduction models are based on complex recurrent or convolutional neural'
							            ' networks in an encoder-decoder configuration. The best performing models also connect the'
							            ' encoder and decoder through an attention mechanism. We propose a new simple network'
							            ' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'
							            ' and convolutions entirely. Experiments on two machine translation tasks show these models to be'
							            ' superior in quality while being more parallelizable and requiring significantly less time to'
							            ' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'
							            ' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'
							            ' English-to-French translation task, our model establishes a new single-model state-of-the-art'
							            ' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'
							            ' costs of the best models from the literature. We show that the Transformer generalizes well to'
							            ' other tasks by applying it successfully to English constituency parsing both with large and'
							            ' limited training data.'
							        ),
							        'labels': ['translation', 'machine learning', 'vision', 'statistics'],
							        'scores': [0.8_17, 0.7_13, 0.0_18, 0.0_18],
							    }       ,    )

	@slow
	@require_tf
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			pipeline('zero-shot-classification'       ,    model='roberta-large-mnli'       ,    framework='tf'    )
							_UpperCAmelCase									=			zero_shot_classifier(
							    'Who are you voting for in 2020?'       ,    candidate_labels=['politics', 'public health', 'science']    )

							self.assertEqual(
							    nested_simplify(UpperCAmelCase    )       ,    {
							        'sequence': 'Who are you voting for in 2020?',
							        'labels': ['politics', 'public health', 'science'],
							        'scores': [0.9_76, 0.0_15, 0.0_09],
							    }       ,    )
							_UpperCAmelCase									=			zero_shot_classifier(
							    'The dominant sequence transduction models are based on complex recurrent or convolutional neural networks'
							    ' in an encoder-decoder configuration. The best performing models also connect the encoder and decoder'
							    ' through an attention mechanism. We propose a new simple network architecture, the Transformer, based'
							    ' solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two'
							    ' machine translation tasks show these models to be superior in quality while being more parallelizable'
							    ' and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014'
							    ' English-to-German translation task, improving over the existing best results, including ensembles by'
							    ' over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new'
							    ' single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small'
							    ' fraction of the training costs of the best models from the literature. We show that the Transformer'
							    ' generalizes well to other tasks by applying it successfully to English constituency parsing both with'
							    ' large and limited training data.'       ,    candidate_labels=['machine learning', 'statistics', 'translation', 'vision']       ,    multi_label=UpperCAmelCase       ,    )
							self.assertEqual(
							    nested_simplify(UpperCAmelCase    )       ,    {
							        'sequence': (
							            'The dominant sequence transduction models are based on complex recurrent or convolutional neural'
							            ' networks in an encoder-decoder configuration. The best performing models also connect the'
							            ' encoder and decoder through an attention mechanism. We propose a new simple network'
							            ' architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence'
							            ' and convolutions entirely. Experiments on two machine translation tasks show these models to be'
							            ' superior in quality while being more parallelizable and requiring significantly less time to'
							            ' train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,'
							            ' improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014'
							            ' English-to-French translation task, our model establishes a new single-model state-of-the-art'
							            ' BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training'
							            ' costs of the best models from the literature. We show that the Transformer generalizes well to'
							            ' other tasks by applying it successfully to English constituency parsing both with large and'
							            ' limited training data.'
							        ),
							        'labels': ['translation', 'machine learning', 'vision', 'statistics'],
							        'scores': [0.8_17, 0.7_13, 0.0_18, 0.0_18],
							    }       ,    )
