
from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


UpperCAmelCase       =					logging.get_logger(__name__)

# General docstring
UpperCAmelCase       =					'''RegNetConfig'''

# Base docstring
UpperCAmelCase       =					'''facebook/regnet-y-040'''
UpperCAmelCase       =					[1, 1088, 7, 7]

# Image classification docstring
UpperCAmelCase       =					'''facebook/regnet-y-040'''
UpperCAmelCase       =					'''tabby, tabby cat'''

UpperCAmelCase       =					[
    '''facebook/regnet-y-040''',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case						,       snake_case = 3						,       snake_case = 1						,       snake_case = 1						,       snake_case = "relu"						,       ):
		super().__init__()
		lowercase	    =       nn.Convad(
		    snake_case						,       snake_case						,       kernel_size=snake_case						,       stride=snake_case						,       padding=kernel_size // 2						,       groups=snake_case						,       bias=snake_case						,       )
		lowercase	    =       nn.BatchNormad(snake_case			)
		lowercase	    =       ACTaFN[activation] if activation is not None else nn.Identity()





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase	    =       self.convolution(snake_case			)
		lowercase	    =       self.normalization(snake_case			)
		lowercase	    =       self.activation(snake_case			)
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case			):
		super().__init__()
		lowercase	    =       RegNetConvLayer(
		    config.num_channels						,       config.embedding_size						,       kernel_size=3						,       stride=2						,       activation=config.hidden_act			)
		lowercase	    =       config.num_channels





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase	    =       pixel_values.shape[1]
		if num_channels != self.num_channels:
			raise ValueError(
			    'Make sure that the channel dimension of the pixel values match with the one set in the configuration.'			)
		lowercase	    =       self.embedder(snake_case			)
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case						,       snake_case = 2			):
		super().__init__()
		lowercase	    =       nn.Convad(snake_case						,       snake_case						,       kernel_size=1						,       stride=snake_case						,       bias=snake_case			)
		lowercase	    =       nn.BatchNormad(snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase	    =       self.convolution(snake_case			)
		lowercase	    =       self.normalization(snake_case			)
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case			):
		super().__init__()

		lowercase	    =       nn.AdaptiveAvgPoolad((1, 1)			)
		lowercase	    =       nn.Sequential(
		    nn.Convad(snake_case						,       snake_case						,       kernel_size=1			)						,       nn.ReLU()						,       nn.Convad(snake_case						,       snake_case						,       kernel_size=1			)						,       nn.Sigmoid()						,       )





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		# b c h w -> b c 1 1
		lowercase	    =       self.pooler(snake_case			)
		lowercase	    =       self.attention(snake_case			)
		lowercase	    =       hidden_state * attention
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case = 1			):
		super().__init__()
		lowercase	    =       in_channels != out_channels or stride != 1
		lowercase	    =       max(1						,       out_channels // config.groups_width			)
		lowercase	    =       (
		    RegNetShortCut(snake_case						,       snake_case						,       stride=snake_case			) if should_apply_shortcut else nn.Identity()
		)
		lowercase	    =       nn.Sequential(
		    RegNetConvLayer(snake_case						,       snake_case						,       kernel_size=1						,       activation=config.hidden_act			)						,       RegNetConvLayer(snake_case						,       snake_case						,       stride=snake_case						,       groups=snake_case						,       activation=config.hidden_act			)						,       RegNetConvLayer(snake_case						,       snake_case						,       kernel_size=1						,       activation=snake_case			)						,       )
		lowercase	    =       ACTaFN[config.hidden_act]





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase	    =       hidden_state
		lowercase	    =       self.layer(snake_case			)
		lowercase	    =       self.shortcut(snake_case			)
		hidden_state += residual
		lowercase	    =       self.activation(snake_case			)
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case = 1			):
		super().__init__()
		lowercase	    =       in_channels != out_channels or stride != 1
		lowercase	    =       max(1						,       out_channels // config.groups_width			)
		lowercase	    =       (
		    RegNetShortCut(snake_case						,       snake_case						,       stride=snake_case			) if should_apply_shortcut else nn.Identity()
		)
		lowercase	    =       nn.Sequential(
		    RegNetConvLayer(snake_case						,       snake_case						,       kernel_size=1						,       activation=config.hidden_act			)						,       RegNetConvLayer(snake_case						,       snake_case						,       stride=snake_case						,       groups=snake_case						,       activation=config.hidden_act			)						,       RegNetSELayer(snake_case						,       reduced_channels=int(round(in_channels / 4			)			)			)						,       RegNetConvLayer(snake_case						,       snake_case						,       kernel_size=1						,       activation=snake_case			)						,       )
		lowercase	    =       ACTaFN[config.hidden_act]





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase	    =       hidden_state
		lowercase	    =       self.layer(snake_case			)
		lowercase	    =       self.shortcut(snake_case			)
		hidden_state += residual
		lowercase	    =       self.activation(snake_case			)
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case = 2						,       snake_case = 2						,       ):
		super().__init__()

		lowercase	    =       RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

		lowercase	    =       nn.Sequential(
		    # downsampling is done in the first layer with stride of 2
		    layer(
		        snake_case						,       snake_case						,       snake_case						,       stride=snake_case						,       )						,       *[layer(snake_case						,       snake_case						,       snake_case			) for _ in range(depth - 1			)]						,       )





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		lowercase	    =       self.layers(snake_case			)
		return hidden_state







class 			A_  (     nn.Module  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case			):
		super().__init__()
		lowercase	    =       nn.ModuleList([]			)
		# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
		self.stages.append(
		    RegNetStage(
		        snake_case						,       config.embedding_size						,       config.hidden_sizes[0]						,       stride=2 if config.downsample_in_first_stage else 1						,       depth=config.depths[0]						,       )			)
		lowercase	    =       zip(config.hidden_sizes						,       config.hidden_sizes[1:]			)
		for (in_channels, out_channels), depth in zip(snake_case						,       config.depths[1:]			):
			self.stages.append(RegNetStage(snake_case						,       snake_case						,       snake_case						,       depth=snake_case			)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case = False						,       snake_case = True			):
		lowercase	    =       () if output_hidden_states else None

		for stage_module in self.stages:
			if output_hidden_states:
				lowercase	    =       hidden_states + (hidden_state,)

			lowercase	    =       stage_module(snake_case			)

		if output_hidden_states:
			lowercase	    =       hidden_states + (hidden_state,)

		if not return_dict:
			return tuple(v for v in [hidden_state, hidden_states] if v is not None			)

		return BaseModelOutputWithNoAttention(last_hidden_state=snake_case						,       hidden_states=snake_case			)







class 			A_  (     __lowerCamelCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	str									=			RegNetConfig
	_UpperCamelCase	:	Optional[Any]									=			"""regnet"""
	_UpperCamelCase	:	Tuple									=			"""pixel_values"""
	_UpperCamelCase	:	str									=			True





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case			):
		if isinstance(snake_case						,       nn.Convad			):
			nn.init.kaiming_normal_(module.weight						,       mode='fan_out'						,       nonlinearity='relu'			)
		elif isinstance(snake_case						,       (nn.BatchNormad, nn.GroupNorm)			):
			nn.init.constant_(module.weight						,       1			)
			nn.init.constant_(module.bias						,       0			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case=False			):
		if isinstance(snake_case						,       snake_case			):
			lowercase	    =       value




UpperCAmelCase       =					R'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`RegNetConfig`]): 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
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.
'''







@add_start_docstrings(
    """The bare RegNet model outputting raw features without any specific head on top.""" ,      __lowerCamelCase ,      )
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 			A_  (     __lowerCamelCase  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case			):
		super().__init__(snake_case			)
		lowercase	    =       config
		lowercase	    =       RegNetEmbeddings(snake_case			)
		lowercase	    =       RegNetEncoder(snake_case			)
		lowercase	    =       nn.AdaptiveAvgPoolad((1, 1)			)
		# Initialize weights and apply final processing
		self.post_init()





	@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       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case = None						,       snake_case = None			):
		lowercase	    =       (
		    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
		)
		lowercase	    =       return_dict if return_dict is not None else self.config.use_return_dict

		lowercase	    =       self.embedder(snake_case			)

		lowercase	    =       self.encoder(
		    snake_case						,       output_hidden_states=snake_case						,       return_dict=snake_case			)

		lowercase	    =       encoder_outputs[0]

		lowercase	    =       self.pooler(snake_case			)

		if not return_dict:
			return (last_hidden_state, pooled_output) + encoder_outputs[1:]

		return BaseModelOutputWithPoolingAndNoAttention(
		    last_hidden_state=snake_case						,       pooler_output=snake_case						,       hidden_states=encoder_outputs.hidden_states						,       )







@add_start_docstrings(
    """
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """ ,      __lowerCamelCase ,      )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 			A_  (     __lowerCamelCase  ):




	'''simple docstring'''


	def __init__(			self						,       snake_case			):
		super().__init__(snake_case			)
		lowercase	    =       config.num_labels
		lowercase	    =       RegNetModel(snake_case			)
		# classification head
		lowercase	    =       nn.Sequential(
		    nn.Flatten()						,       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       SCREAMING_SNAKE_CASE__		(			self						,       snake_case = None						,       snake_case = None						,       snake_case = None						,       snake_case = None						,       ):
		lowercase	    =       return_dict if return_dict is not None else self.config.use_return_dict

		lowercase	    =       self.regnet(snake_case						,       output_hidden_states=snake_case						,       return_dict=snake_case			)

		lowercase	    =       outputs.pooler_output if return_dict else outputs[1]

		lowercase	    =       self.classifier(snake_case			)

		lowercase	    =       None

		if labels is not None:
			if self.config.problem_type is None:
				if self.num_labels == 1:
					lowercase	    =       'regression'
				elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
					lowercase	    =       'single_label_classification'
				else:
					lowercase	    =       'multi_label_classification'
			if self.config.problem_type == "regression":
				lowercase	    =       MSELoss()
				if self.num_labels == 1:
					lowercase	    =       loss_fct(logits.squeeze()						,       labels.squeeze()			)
				else:
					lowercase	    =       loss_fct(snake_case						,       snake_case			)
			elif self.config.problem_type == "single_label_classification":
				lowercase	    =       CrossEntropyLoss()
				lowercase	    =       loss_fct(logits.view(-1						,       self.num_labels			)						,       labels.view(-1			)			)
			elif self.config.problem_type == "multi_label_classification":
				lowercase	    =       BCEWithLogitsLoss()
				lowercase	    =       loss_fct(snake_case						,       snake_case			)

		if not return_dict:
			lowercase	    =       (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 typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


__UpperCAmelCase						=			logging.get_logger(__name__)



class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def       UpperCAmelCase      (		self    :			int	, a_    :			List[Any]    )	->	Optional[Any]:



							'''simple docstring'''



							if isinstance(a_	, a_    ):
										a__  :	Any		 =			[label.strip() for label in labels.split(","    ) if label.strip()]
							return labels






				def __call__(		self    :			Union[str, Any]	, a_    :			Tuple	, a_    :			Optional[Any]	, a_    :			List[str]    )	->	Optional[Any]:



							'''simple docstring'''



							if len(a_    ) == 0 or len(a_    ) == 0:
										raise ValueError("You must include at least one label and at least one sequence."    )
							if hypothesis_template.format(labels[0]    ) == hypothesis_template:
										raise ValueError(
										    (
										        "The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. "
										        "Make sure the passed template includes formatting syntax such as {{}} where the label should go."
										    ).format(a_    )    )

							if isinstance(a_	, a_    ):
										a__  :	str		 =			[sequences]

							a__  :	Optional[int]		 =			[]
							for sequence in sequences:
										sequence_pairs.extend([[sequence, hypothesis_template.format(a_    )] for label in labels]    )

							return sequence_pairs, sequences



@add_end_docstrings(_UpperCamelCase  )
class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def __init__(		self    :			str	, a_    :			Optional[Any]=ZeroShotClassificationArgumentHandler()	, *a_    :			Tuple	, **a_    :			str    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[Any]		 =			args_parser
							super().__init__(*a_	, **a_    )
							if self.entailment_id == -1:
										logger.warning(
										    "Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to "
										    "-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."    )
				@property
				def       UpperCAmelCase      (		self    :			Tuple    )	->	str:



							'''simple docstring'''



							for label, ind in self.model.config.labelaid.items():
										if label.lower().startswith("entail"    ):
													return ind
							return -1
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			List[Any]	, a_    :			int=True	, a_    :			Tuple=True	, a_    :			Tuple=TruncationStrategy.ONLY_FIRST	, **a_    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	str		 =			self.framework
							if self.tokenizer.pad_token is None:
										# Override for tokenizers not supporting padding
										logger.error(
										    "Tokenizer was not supporting padding necessary for zero-shot, attempting to use "
										    " `pad_token=eos_token`"    )
										a__  :	List[str]		 =			self.tokenizer.eos_token
							try:
										a__  :	List[str]		 =			self.tokenizer(
										    a_	, add_special_tokens=a_	, return_tensors=a_	, padding=a_	, truncation=a_	, )
							except Exception as e:
										if "too short" in str(a_    ):
													# tokenizers might yell that we want to truncate
													# to a value that is not even reached by the input.
													# In that case we don't want to truncate.
													# It seems there's not a really better way to catch that
													# exception.

													a__  :	List[str]		 =			self.tokenizer(
													    a_	, add_special_tokens=a_	, return_tensors=a_	, padding=a_	, truncation=TruncationStrategy.DO_NOT_TRUNCATE	, )
										else:
													raise e

							return inputs
				def       UpperCAmelCase      (		self    :			Tuple	, **a_    :			Tuple    )	->	Optional[int]:



							'''simple docstring'''



							if kwargs.get("multi_class"	, a_    ) is not None:
										a__  :	str		 =			kwargs["multi_class"]
										logger.warning(
										    "The `multi_class` argument has been deprecated and renamed to `multi_label`. "
										    "`multi_class` will be removed in a future version of Transformers."    )
							a__  :	Tuple		 =			{}
							if "candidate_labels" in kwargs:
										a__  :	Any		 =			self._args_parser._parse_labels(kwargs["candidate_labels"]    )
							if "hypothesis_template" in kwargs:
										a__  :	str		 =			kwargs["hypothesis_template"]

							a__  :	Tuple		 =			{}
							if "multi_label" in kwargs:
										a__  :	Dict		 =			kwargs["multi_label"]
							return preprocess_params, {}, postprocess_params
				def __call__(		self    :			str	, a_    :			Union[str, List[str]]	, *a_    :			List[str]	, **a_    :			List[Any]	, )	->	Tuple:



							'''simple docstring'''



							if len(a_    ) == 0:
										pass
							elif len(a_    ) == 1 and "candidate_labels" not in kwargs:
										a__  :	Any		 =			args[0]
							else:
										raise ValueError(F"Unable to understand extra arguments {args}"    )

							return super().__call__(a_	, **a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Tuple	, a_    :			Any=None	, a_    :			Dict="This example is {}."    )	->	Optional[int]:



							'''simple docstring'''



							a__ ,	a__  :	Optional[Any]		 =			self._args_parser(a_	, a_	, a_    )

							for i, (candidate_label, sequence_pair) in enumerate(zip(a_	, a_    )    ):
										a__  :	Union[str, Any]		 =			self._parse_and_tokenize([sequence_pair]    )

										yield {
										    "candidate_label": candidate_label,
										    "sequence": sequences[0],
										    "is_last": i == len(a_    ) - 1,
										    **model_input,
										}
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Optional[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Dict		 =			inputs["candidate_label"]
							a__  :	Optional[int]		 =			inputs["sequence"]
							a__  :	Optional[int]		 =			{k: inputs[k] for k in self.tokenizer.model_input_names}
							a__  :	int		 =			self.model(**a_    )

							a__  :	Optional[int]		 =			{
							    "candidate_label": candidate_label,
							    "sequence": sequence,
							    "is_last": inputs["is_last"],
							    **outputs,
							}
							return model_outputs






				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Any	, a_    :			List[str]=False    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	int		 =			[outputs["candidate_label"] for outputs in model_outputs]
							a__  :	Optional[int]		 =			[outputs["sequence"] for outputs in model_outputs]
							a__  :	Union[str, Any]		 =			np.concatenate([output["logits"].numpy() for output in model_outputs]    )
							a__  :	List[str]		 =			logits.shape[0]
							a__  :	Optional[int]		 =			len(a_    )
							a__  :	List[str]		 =			N // n
							a__  :	int		 =			logits.reshape((num_sequences, n, -1)    )

							if multi_label or len(a_    ) == 1:
										# softmax over the entailment vs. contradiction dim for each label independently
										a__  :	str		 =			self.entailment_id
										a__  :	str		 =			-1 if entailment_id == 0 else 0
										a__  :	str		 =			reshaped_outputs[..., [contradiction_id, entailment_id]]
										a__  :	List[Any]		 =			np.exp(a_    ) / np.exp(a_    ).sum(-1	, keepdims=a_    )
										a__  :	str		 =			scores[..., 1]
							else:
										# softmax the "entailment" logits over all candidate labels
										a__  :	str		 =			reshaped_outputs[..., self.entailment_id]
										a__  :	Optional[int]		 =			np.exp(a_    ) / np.exp(a_    ).sum(-1	, keepdims=a_    )

							a__  :	List[str]		 =			list(reversed(scores[0].argsort()    )    )
							return {
							    "sequence": sequences[0],
							    "labels": [candidate_labels[i] for i in top_inds],
							    "scores": scores[0, top_inds].tolist(),
							}