






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

# General docstring
lowerCamelCase_				: int    	=							'''RegNetConfig'''

# Base docstring
lowerCamelCase_				: Optional[Any]    	=							'''facebook/regnet-y-040'''
lowerCamelCase_				: Any    	=							[1, 1088, 7, 7]

# Image classification docstring
lowerCamelCase_				: List[str]    	=							'''facebook/regnet-y-040'''
lowerCamelCase_				: Tuple    	=							'''tabby, tabby cat'''

lowerCamelCase_				: Tuple    	=							[
    '''facebook/regnet-y-040''',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





			def __init__(   self							:						Dict ,      lowercase							:						int ,      lowercase							:						int ,      lowercase							:						int = 3 ,      lowercase							:						int = 1 ,      lowercase							:						int = 1 ,      lowercase							:						Optional[str] = "relu" ,      )							->    Optional[Any]:

									'''simple docstring'''







									super().__init__()
									UpperCamelCase__         = nn.Convad(
									    lowercase ,      lowercase ,      kernel_size=lowercase ,      stride=lowercase ,      padding=kernel_size // 2 ,      groups=lowercase ,      bias=lowercase ,      )
									UpperCamelCase__         = nn.BatchNormad(lowercase			)
									UpperCamelCase__         = ACTaFN[activation] if activation is not None else nn.Identity()







			def 				A      (   self							:						Tuple ,      lowercase							:						Any			)							->    Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__         = self.convolution(lowercase			)
									UpperCamelCase__         = self.normalization(lowercase			)
									UpperCamelCase__         = self.activation(lowercase			)
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





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

									'''simple docstring'''







									super().__init__()
									UpperCamelCase__         = RegNetConvLayer(
									    config.num_channels ,      config.embedding_size ,      kernel_size=3 ,      stride=2 ,      activation=config.hidden_act			)
									UpperCamelCase__         = config.num_channels







			def 				A      (   self							:						List[Any] ,      lowercase							:						str			)							->    Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__         = 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."""			)
									UpperCamelCase__         = self.embedder(lowercase			)
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





			def __init__(   self							:						int ,      lowercase							:						int ,      lowercase							:						int ,      lowercase							:						int = 2			)							->    Union[str, Any]:

									'''simple docstring'''







									super().__init__()
									UpperCamelCase__         = nn.Convad(lowercase ,      lowercase ,      kernel_size=1 ,      stride=lowercase ,      bias=lowercase			)
									UpperCamelCase__         = nn.BatchNormad(lowercase			)







			def 				A      (   self							:						int ,      lowercase							:						Tensor			)							->    Tensor:

									'''simple docstring'''







									UpperCamelCase__         = self.convolution(lowercase			)
									UpperCamelCase__         = self.normalization(lowercase			)
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





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

									'''simple docstring'''







									super().__init__()

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







			def 				A      (   self							:						Union[str, Any] ,      lowercase							:						Optional[Any]			)							->    Dict:

									'''simple docstring'''







									UpperCamelCase__         = self.pooler(lowercase			)
									UpperCamelCase__         = self.attention(lowercase			)
									UpperCamelCase__         = hidden_state * attention
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





			def __init__(   self							:						str ,      lowercase							:						RegNetConfig ,      lowercase							:						int ,      lowercase							:						int ,      lowercase							:						int = 1			)							->    int:

									'''simple docstring'''







									super().__init__()
									UpperCamelCase__         = in_channels != out_channels or stride != 1
									UpperCamelCase__         = max(1 ,      out_channels // config.groups_width			)
									UpperCamelCase__         = (
									    RegNetShortCut(lowercase ,      lowercase ,      stride=lowercase			) if should_apply_shortcut else nn.Identity()
									)
									UpperCamelCase__         = 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			) ,      )
									UpperCamelCase__         = ACTaFN[config.hidden_act]







			def 				A      (   self							:						Optional[Any] ,      lowercase							:						Any			)							->    Any:

									'''simple docstring'''







									UpperCamelCase__         = hidden_state
									UpperCamelCase__         = self.layer(lowercase			)
									UpperCamelCase__         = self.shortcut(lowercase			)
									hidden_state += residual
									UpperCamelCase__         = self.activation(lowercase			)
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





			def __init__(   self							:						Optional[int] ,      lowercase							:						RegNetConfig ,      lowercase							:						int ,      lowercase							:						int ,      lowercase							:						int = 1			)							->    Any:

									'''simple docstring'''







									super().__init__()
									UpperCamelCase__         = in_channels != out_channels or stride != 1
									UpperCamelCase__         = max(1 ,      out_channels // config.groups_width			)
									UpperCamelCase__         = (
									    RegNetShortCut(lowercase ,      lowercase ,      stride=lowercase			) if should_apply_shortcut else nn.Identity()
									)
									UpperCamelCase__         = 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			) ,      )
									UpperCamelCase__         = ACTaFN[config.hidden_act]







			def 				A      (   self							:						List[str] ,      lowercase							:						Optional[Any]			)							->    str:

									'''simple docstring'''







									UpperCamelCase__         = hidden_state
									UpperCamelCase__         = self.layer(lowercase			)
									UpperCamelCase__         = self.shortcut(lowercase			)
									hidden_state += residual
									UpperCamelCase__         = self.activation(lowercase			)
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





			def __init__(   self							:						int ,      lowercase							:						RegNetConfig ,      lowercase							:						int ,      lowercase							:						int ,      lowercase							:						int = 2 ,      lowercase							:						int = 2 ,      )							->    Any:

									'''simple docstring'''







									super().__init__()

									UpperCamelCase__         = RegNetXLayer if config.layer_type == """x""" else RegNetYLayer

									UpperCamelCase__         = 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 				A      (   self							:						Optional[int] ,      lowercase							:						Any			)							->    Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__         = self.layers(lowercase			)
									return hidden_state
class    _SCREAMING_SNAKE_CASE							(		nn.Module	):


			'''simple docstring'''





			def __init__(   self							:						int ,      lowercase							:						RegNetConfig			)							->    Any:

									'''simple docstring'''







									super().__init__()
									UpperCamelCase__         = 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] ,      )			)
									UpperCamelCase__         = 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 				A      (   self							:						int ,      lowercase							:						Tensor ,      lowercase							:						bool = False ,      lowercase							:						bool = True			)							->    BaseModelOutputWithNoAttention:

									'''simple docstring'''







									UpperCamelCase__         = () if output_hidden_states else None

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

															UpperCamelCase__         = stage_module(lowercase			)

									if output_hidden_states:
															UpperCamelCase__         = 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    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							List[Any]					     =					RegNetConfig
			__a							:							Dict					     =					"regnet"
			__a							:							Union[str, Any]					     =					"pixel_values"
			__a							:							str					     =					True






			def 				A      (   self							:						Dict ,      lowercase							:						Dict			)							->    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 				A      (   self							:						List[Any] ,      lowercase							:						List[str] ,      lowercase							:						Dict=False			)							->    Tuple:

									'''simple docstring'''







									if isinstance(lowercase ,      lowercase			):
															UpperCamelCase__         = value
lowerCamelCase_				: Dict    	=							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."       ,SCREAMING_SNAKE_CASE       ,)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			def __init__(   self							:						Optional[int] ,      lowercase							:						int			)							->    Optional[Any]:

									'''simple docstring'''







									super().__init__(lowercase			)
									UpperCamelCase__         = config
									UpperCamelCase__         = RegNetEmbeddings(lowercase			)
									UpperCamelCase__         = RegNetEncoder(lowercase			)
									UpperCamelCase__         = 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 				A      (   self							:						Tuple ,      lowercase							:						Tensor ,      lowercase							:						Optional[bool] = None ,      lowercase							:						Optional[bool] = None			)							->    BaseModelOutputWithPoolingAndNoAttention:

									'''simple docstring'''







									UpperCamelCase__         = (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									UpperCamelCase__         = return_dict if return_dict is not None else self.config.use_return_dict

									UpperCamelCase__         = self.embedder(lowercase			)

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

									UpperCamelCase__         = encoder_outputs[0]

									UpperCamelCase__         = 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    "       ,SCREAMING_SNAKE_CASE       ,)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





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

									'''simple docstring'''







									super().__init__(lowercase			)
									UpperCamelCase__         = config.num_labels
									UpperCamelCase__         = RegNetModel(lowercase			)
									# classification head
									UpperCamelCase__         = 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 				A      (   self							:						Any ,      lowercase							:						Optional[torch.FloatTensor] = None ,      lowercase							:						Optional[torch.LongTensor] = None ,      lowercase							:						Optional[bool] = None ,      lowercase							:						Optional[bool] = None ,      )							->    ImageClassifierOutputWithNoAttention:

									'''simple docstring'''







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

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

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

									UpperCamelCase__         = self.classifier(lowercase			)

									UpperCamelCase__         = None

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

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

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








'''simple docstring'''


import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, ClassLabel, Features
from .base import TaskTemplate
@dataclass(frozen=SCREAMING_SNAKE_CASE	)
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							str					     =					field(default="audio-classification"       ,metadata={"include_in_asdict_even_if_is_default": True}	)
			__a							:							ClassVar[Features]					     =					Features({"audio": Audio()}	)
			__a							:							ClassVar[Features]					     =					Features({"labels": ClassLabel}	)
			__a							:							str					     =					"audio"
			__a							:							str					     =					"labels"






			def 				A      (   self							:						List[Any] ,      lowercase							:						List[Any]			)							->    Any:

									'''simple docstring'''







									if self.label_column not in features:
															raise ValueError(f"Column {self.label_column} is not present in features."			)
									if not isinstance(features[self.label_column] ,      lowercase			):
															raise ValueError(f"Column {self.label_column} is not a ClassLabel."			)
									UpperCamelCase__         = copy.deepcopy(self			)
									UpperCamelCase__         = self.label_schema.copy()
									UpperCamelCase__         = features[self.label_column]
									UpperCamelCase__         = label_schema
									return task_template







			@property
			def 				A      (   self							:						int			)							->    Dict[str, str]:

									'''simple docstring'''







									return {
									    self.audio_column: "audio",
									    self.label_column: "labels",
									}
