





'''simple docstring'''


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


lowerCAmelCase   :	Optional[Any]   				=logging.get_logger(__name__)

# General docstring
lowerCAmelCase   :	Union[str, Any]   				="""RegNetConfig"""

# Base docstring
lowerCAmelCase   :	str   				="""facebook/regnet-y-040"""
lowerCAmelCase   :	Dict   				=[1, 1_088, 7, 7]

# Image classification docstring
lowerCAmelCase   :	Union[str, Any]   				="""facebook/regnet-y-040"""
lowerCAmelCase   :	Union[str, Any]   				="""tabby, tabby cat"""

lowerCAmelCase   :	Optional[Any]   				=[
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]
class  a_  (							nn.Module				):





     def __init__(					self						:   Tuple      , lowercase						:   Any      , lowercase						:   Optional[Any]      , lowercase						:   Tuple = 3      , lowercase						:   Dict = 1      , lowercase						:   List[Any] = 1      , lowercase						:   Tuple = "relu"      , ):





        """simple docstring"""




        super().__init__()
        lowercase_     :Tuple            =					nn.Convad(
            _lowercase      , _lowercase      , kernel_size=_lowercase      , stride=_lowercase      , padding=kernel_size // 2      , groups=_lowercase      , bias=_lowercase      , )
        lowercase_     :Any            =					nn.BatchNormad(_lowercase		)
        lowercase_     :Optional[int]            =					ACTaFN[activation] if activation is not None else nn.Identity()







     def 				lowercase__ (					self						:   int      , lowercase						:   Union[str, Any]		):





        """simple docstring"""




        lowercase_     :int            =					self.convolution(_lowercase		)
        lowercase_     :Union[str, Any]            =					self.normalization(_lowercase		)
        lowercase_     :List[Any]            =					self.activation(_lowercase		)
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   List[Any]      , lowercase						:   Tuple		):





        """simple docstring"""




        super().__init__()
        lowercase_     :Optional[int]            =					RegNetConvLayer(
            config.num_channels      , config.embedding_size      , kernel_size=3      , stride=2      , activation=config.hidden_act		)
        lowercase_     :Dict            =					config.num_channels







     def 				lowercase__ (					self						:   str      , lowercase						:   Optional[int]		):





        """simple docstring"""




        lowercase_     :List[str]            =					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_     :Union[str, Any]            =					self.embedder(_lowercase		)
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   Any      , lowercase						:   Tuple      , lowercase						:   Optional[Any]      , lowercase						:   Any = 2		):





        """simple docstring"""




        super().__init__()
        lowercase_     :Dict            =					nn.Convad(_lowercase      , _lowercase      , kernel_size=1      , stride=_lowercase      , bias=_lowercase		)
        lowercase_     :Any            =					nn.BatchNormad(_lowercase		)







     def 				lowercase__ (					self						:   int      , lowercase						:   Any		):





        """simple docstring"""




        lowercase_     :Tuple            =					self.convolution(_lowercase		)
        lowercase_     :int            =					self.normalization(_lowercase		)
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   List[str]      , lowercase						:   List[str]      , lowercase						:   Dict		):





        """simple docstring"""




        super().__init__()

        lowercase_     :str            =					nn.AdaptiveAvgPoolad((1, 1)		)
        lowercase_     :Optional[Any]            =					nn.Sequential(
            nn.Convad(_lowercase      , _lowercase      , kernel_size=1		)      , nn.ReLU()      , nn.Convad(_lowercase      , _lowercase      , kernel_size=1		)      , nn.Sigmoid()      , )







     def 				lowercase__ (					self						:   Tuple      , lowercase						:   Union[str, Any]		):





        """simple docstring"""




        lowercase_     :List[Any]            =					self.pooler(_lowercase		)
        lowercase_     :Optional[int]            =					self.attention(_lowercase		)
        lowercase_     :int            =					hidden_state * attention
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   Optional[int]      , lowercase						:   List[str]      , lowercase						:   List[Any]      , lowercase						:   int      , lowercase						:   List[str] = 1		):





        """simple docstring"""




        super().__init__()
        lowercase_     :str            =					in_channels != out_channels or stride != 1
        lowercase_     :Tuple            =					max(1      , out_channels // config.groups_width		)
        lowercase_     :List[Any]            =					(
            RegNetShortCut(_lowercase      , _lowercase      , stride=_lowercase		) if should_apply_shortcut else nn.Identity()
        )
        lowercase_     :List[Any]            =					nn.Sequential(
            RegNetConvLayer(_lowercase      , _lowercase      , kernel_size=1      , activation=config.hidden_act		)      , RegNetConvLayer(_lowercase      , _lowercase      , stride=_lowercase      , groups=_lowercase      , activation=config.hidden_act		)      , RegNetConvLayer(_lowercase      , _lowercase      , kernel_size=1      , activation=_lowercase		)      , )
        lowercase_     :Tuple            =					ACTaFN[config.hidden_act]







     def 				lowercase__ (					self						:   Dict      , lowercase						:   Optional[int]		):





        """simple docstring"""




        lowercase_     :int            =					hidden_state
        lowercase_     :Optional[int]            =					self.layer(_lowercase		)
        lowercase_     :int            =					self.shortcut(_lowercase		)
        hidden_state += residual
        lowercase_     :int            =					self.activation(_lowercase		)
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   str      , lowercase						:   Dict      , lowercase						:   List[Any]      , lowercase						:   Optional[int]      , lowercase						:   Union[str, Any] = 1		):





        """simple docstring"""




        super().__init__()
        lowercase_     :Union[str, Any]            =					in_channels != out_channels or stride != 1
        lowercase_     :Any            =					max(1      , out_channels // config.groups_width		)
        lowercase_     :List[str]            =					(
            RegNetShortCut(_lowercase      , _lowercase      , stride=_lowercase		) if should_apply_shortcut else nn.Identity()
        )
        lowercase_     :int            =					nn.Sequential(
            RegNetConvLayer(_lowercase      , _lowercase      , kernel_size=1      , activation=config.hidden_act		)      , RegNetConvLayer(_lowercase      , _lowercase      , stride=_lowercase      , groups=_lowercase      , activation=config.hidden_act		)      , RegNetSELayer(_lowercase      , reduced_channels=int(round(in_channels / 4		)		)		)      , RegNetConvLayer(_lowercase      , _lowercase      , kernel_size=1      , activation=_lowercase		)      , )
        lowercase_     :List[Any]            =					ACTaFN[config.hidden_act]







     def 				lowercase__ (					self						:   Any      , lowercase						:   Any		):





        """simple docstring"""




        lowercase_     :List[Any]            =					hidden_state
        lowercase_     :Union[str, Any]            =					self.layer(_lowercase		)
        lowercase_     :Any            =					self.shortcut(_lowercase		)
        hidden_state += residual
        lowercase_     :int            =					self.activation(_lowercase		)
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   int      , lowercase						:   Optional[Any]      , lowercase						:   str      , lowercase						:   Dict      , lowercase						:   List[Any] = 2      , lowercase						:   Optional[int] = 2      , ):





        """simple docstring"""




        super().__init__()

        lowercase_     :Optional[int]            =					RegNetXLayer if config.layer_type == """x""" else RegNetYLayer

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







     def 				lowercase__ (					self						:   int      , lowercase						:   str		):





        """simple docstring"""




        lowercase_     :int            =					self.layers(_lowercase		)
        return hidden_state







class  a_  (							nn.Module				):





     def __init__(					self						:   List[str]      , lowercase						:   List[str]		):





        """simple docstring"""




        super().__init__()
        lowercase_     :Dict            =					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(
                _lowercase      , config.embedding_size      , config.hidden_sizes[0]      , stride=2 if config.downsample_in_first_stage else 1      , depth=config.depths[0]      , )		)
        lowercase_     :int            =					zip(config.hidden_sizes      , config.hidden_sizes[1:]		)
        for (in_channels, out_channels), depth in zip(_lowercase      , config.depths[1:]		):
           self.stages.append(RegNetStage(_lowercase      , _lowercase      , _lowercase      , depth=_lowercase		)		)







     def 				lowercase__ (					self						:   Optional[Any]      , lowercase						:   Optional[Any]      , lowercase						:   List[Any] = False      , lowercase						:   Any = True		):





        """simple docstring"""




        lowercase_     :Union[str, Any]            =					() if output_hidden_states else None

        for stage_module in self.stages:
           if output_hidden_states:
              lowercase_     :Union[str, Any]            =					hidden_states + (hidden_state,)

           lowercase_     :List[str]            =					stage_module(_lowercase		)

        if output_hidden_states:
           lowercase_     :List[Any]            =					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=_lowercase      , hidden_states=_lowercase		)







class  a_  (							a_				):
     __A						=     RegNetConfig
     __A						=     '''regnet'''
     __A						=     '''pixel_values'''
     __A						=     True





     def 				lowercase__ (					self						:   str      , lowercase						:   Optional[Any]		):





        """simple docstring"""




        if isinstance(_lowercase      , nn.Convad		):
           nn.init.kaiming_normal_(module.weight      , mode="fan_out"      , nonlinearity="relu"		)
        elif isinstance(_lowercase      , (nn.BatchNormad, nn.GroupNorm)		):
           nn.init.constant_(module.weight      , 1		)
           nn.init.constant_(module.bias      , 0		)







     def 				lowercase__ (					self						:   List[Any]      , lowercase						:   Tuple      , lowercase						:   int=False		):





        """simple docstring"""




        if isinstance(_lowercase      , _lowercase		):
           lowercase_     :Dict            =					value







lowerCAmelCase   :	List[str]   				=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.
"""

lowerCAmelCase   :	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
            [`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."      ,		a_      ,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class  a_  (							a_				):





     def __init__(					self						:   Union[str, Any]      , lowercase						:   int		):





        """simple docstring"""




        super().__init__(_lowercase		)
        lowercase_     :Union[str, Any]            =					config
        lowercase_     :Tuple            =					RegNetEmbeddings(_lowercase		)
        lowercase_     :int            =					RegNetEncoder(_lowercase		)
        lowercase_     :Dict            =					nn.AdaptiveAvgPoolad((1, 1)		)
        # Initialize weights and apply final processing
        self.post_init()







     @add_start_docstrings_to_model_forward(_lowercase		)
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC      , output_type=_lowercase      , config_class=_CONFIG_FOR_DOC      , modality="vision"      , expected_output=_EXPECTED_OUTPUT_SHAPE      , )
     def 				lowercase__ (					self						:   Tuple      , lowercase						:   List[Any]      , lowercase						:   Optional[int] = None      , lowercase						:   str = None		):





        """simple docstring"""




        lowercase_     :Dict            =					(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowercase_     :int            =					return_dict if return_dict is not None else self.config.use_return_dict

        lowercase_     :Any            =					self.embedder(_lowercase		)

        lowercase_     :str            =					self.encoder(
            _lowercase      , output_hidden_states=_lowercase      , return_dict=_lowercase		)

        lowercase_     :Optional[int]            =					encoder_outputs[0]

        lowercase_     :str            =					self.pooler(_lowercase		)

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

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







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





     def __init__(					self						:   Union[str, Any]      , lowercase						:   Tuple		):





        """simple docstring"""




        super().__init__(_lowercase		)
        lowercase_     :Union[str, Any]            =					config.num_labels
        lowercase_     :Optional[int]            =					RegNetModel(_lowercase		)
        # classification head
        lowercase_     :Optional[Any]            =					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(_lowercase		)
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT      , output_type=_lowercase      , config_class=_CONFIG_FOR_DOC      , expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      , )
     def 				lowercase__ (					self						:   Any      , lowercase						:   Optional[int] = None      , lowercase						:   Any = None      , lowercase						:   Tuple = None      , lowercase						:   List[str] = None      , ):





        """simple docstring"""




        lowercase_     :Dict            =					return_dict if return_dict is not None else self.config.use_return_dict

        lowercase_     :Optional[int]            =					self.regnet(_lowercase      , output_hidden_states=_lowercase      , return_dict=_lowercase		)

        lowercase_     :Tuple            =					outputs.pooler_output if return_dict else outputs[1]

        lowercase_     :Union[str, Any]            =					self.classifier(_lowercase		)

        lowercase_     :Optional[Any]            =					None

        if labels is not None:
           if self.config.problem_type is None:
              if self.num_labels == 1:
                 lowercase_     :List[str]            =					"""regression"""
              elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                 lowercase_     :List[Any]            =					"""single_label_classification"""
              else:
                 lowercase_     :str            =					"""multi_label_classification"""
           if self.config.problem_type == "regression":
              lowercase_     :int            =					MSELoss()
              if self.num_labels == 1:
                 lowercase_     :Optional[int]            =					loss_fct(logits.squeeze()      , labels.squeeze()		)
              else:
                 lowercase_     :Tuple            =					loss_fct(_lowercase      , _lowercase		)
           elif self.config.problem_type == "single_label_classification":
              lowercase_     :List[str]            =					CrossEntropyLoss()
              lowercase_     :Optional[Any]            =					loss_fct(logits.view(-1      , self.num_labels		)      , labels.view(-1		)		)
           elif self.config.problem_type == "multi_label_classification":
              lowercase_     :Optional[int]            =					BCEWithLogitsLoss()
              lowercase_     :List[Any]            =					loss_fct(_lowercase      , _lowercase		)

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

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





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


__snake_case				:      Optional[int]   		=			{
    """configuration_xlm""": ["""XLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLMConfig""", """XLMOnnxConfig"""],
    """tokenization_xlm""": ["""XLMTokenizer"""],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__snake_case				:      Optional[Any]   		=			[
	    """XLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """XLMForMultipleChoice""",
	    """XLMForQuestionAnswering""",
	    """XLMForQuestionAnsweringSimple""",
	    """XLMForSequenceClassification""",
	    """XLMForTokenClassification""",
	    """XLMModel""",
	    """XLMPreTrainedModel""",
	    """XLMWithLMHeadModel""",
	]

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__snake_case				:      Optional[Any]   		=			[
	    """TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """TFXLMForMultipleChoice""",
	    """TFXLMForQuestionAnsweringSimple""",
	    """TFXLMForSequenceClassification""",
	    """TFXLMForTokenClassification""",
	    """TFXLMMainLayer""",
	    """TFXLMModel""",
	    """TFXLMPreTrainedModel""",
	    """TFXLMWithLMHeadModel""",
	]


if TYPE_CHECKING:
	from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
	from .tokenization_xlm import XLMTokenizer

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_xlm import (
		    XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    XLMForMultipleChoice,
		    XLMForQuestionAnswering,
		    XLMForQuestionAnsweringSimple,
		    XLMForSequenceClassification,
		    XLMForTokenClassification,
		    XLMModel,
		    XLMPreTrainedModel,
		    XLMWithLMHeadModel,
		)

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_xlm import (
		    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFXLMForMultipleChoice,
		    TFXLMForQuestionAnsweringSimple,
		    TFXLMForSequenceClassification,
		    TFXLMForTokenClassification,
		    TFXLMMainLayer,
		    TFXLMModel,
		    TFXLMPreTrainedModel,
		    TFXLMWithLMHeadModel,
		)

else:
	import sys

	__snake_case				:      Union[str, Any]   		=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


