

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


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

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

# Base docstring
__snake_case				:      str   		=			"""facebook/regnet-y-040"""
__snake_case				:      Dict   		=			[1, 10_88, 7, 7]

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

__snake_case				:      Optional[Any]   		=			[
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]



class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase = 3  ,      _lowercase = 1  ,      _lowercase = 1  ,      _lowercase = "relu"  ,      )     -> Optional[int]:
									super().__init__()
									a_						:						Tuple 							= nn.Convad(
									    _lowercase  ,      _lowercase  ,      kernel_size=_lowercase  ,      stride=_lowercase  ,      padding=kernel_size // 2  ,      groups=_lowercase  ,      bias=_lowercase  ,      )
									a_						:						Any 							= nn.BatchNormad(_lowercase    )
									a_						:						Optional[int] 							= ACTaFN[activation] if activation is not None else nn.Identity()





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> List[str]:
									a_						:						int 							= self.convolution(_lowercase    )
									a_						:						Union[str, Any] 							= self.normalization(_lowercase    )
									a_						:						List[Any] 							= self.activation(_lowercase    )
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase    )     -> Optional[Any]:
									super().__init__()
									a_						:						Optional[int] 							= RegNetConvLayer(
									    config.num_channels  ,      config.embedding_size  ,      kernel_size=3  ,      stride=2  ,      activation=config.hidden_act    )
									a_						:						Dict 							= config.num_channels





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> Optional[Any]:
									a_						:						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."""    )
									a_						:						Union[str, Any] 							= self.embedder(_lowercase    )
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase = 2    )     -> Any:
									super().__init__()
									a_						:						Dict 							= nn.Convad(_lowercase  ,      _lowercase  ,      kernel_size=1  ,      stride=_lowercase  ,      bias=_lowercase    )
									a_						:						Any 							= nn.BatchNormad(_lowercase    )





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> Tensor:
									a_						:						Tuple 							= self.convolution(_lowercase    )
									a_						:						int 							= self.normalization(_lowercase    )
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase    )     -> Any:
									super().__init__()

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





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> Optional[Any]:
									# b c h w -> b c 1 1
									a_						:						List[Any] 							= self.pooler(_lowercase    )
									a_						:						Optional[int] 							= self.attention(_lowercase    )
									a_						:						int 							= hidden_state * attention
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase = 1    )     -> Dict:
									super().__init__()
									a_						:						str 							= in_channels != out_channels or stride != 1
									a_						:						Tuple 							= max(1  ,      out_channels // config.groups_width    )
									a_						:						List[Any] 							= (
									    RegNetShortCut(_lowercase  ,      _lowercase  ,      stride=_lowercase    ) if should_apply_shortcut else nn.Identity()
									)
									a_						:						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    )  ,      )
									a_						:						Tuple 							= ACTaFN[config.hidden_act]





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> Optional[int]:
									a_						:						int 							= hidden_state
									a_						:						Optional[int] 							= self.layer(_lowercase    )
									a_						:						int 							= self.shortcut(_lowercase    )
									hidden_state += residual
									a_						:						int 							= self.activation(_lowercase    )
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase = 1    )     -> Optional[int]:
									super().__init__()
									a_						:						Union[str, Any] 							= in_channels != out_channels or stride != 1
									a_						:						Any 							= max(1  ,      out_channels // config.groups_width    )
									a_						:						List[str] 							= (
									    RegNetShortCut(_lowercase  ,      _lowercase  ,      stride=_lowercase    ) if should_apply_shortcut else nn.Identity()
									)
									a_						:						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    )  ,      )
									a_						:						List[Any] 							= ACTaFN[config.hidden_act]





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> Any:
									a_						:						List[Any] 							= hidden_state
									a_						:						Union[str, Any] 							= self.layer(_lowercase    )
									a_						:						Any 							= self.shortcut(_lowercase    )
									hidden_state += residual
									a_						:						int 							= self.activation(_lowercase    )
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase = 2  ,      _lowercase = 2  ,      )     -> int:
									super().__init__()

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

									a_						:						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 			UpperCamelCase__					(    self  ,      _lowercase    )     -> str:
									a_						:						int 							= self.layers(_lowercase    )
									return hidden_state







class        A__(nn.Module							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase    )     -> List[str]:
									super().__init__()
									a_						:						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]  ,      )    )
									a_						:						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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase = False  ,      _lowercase = True    )     -> BaseModelOutputWithNoAttention:
									a_						:						Union[str, Any] 							= () if output_hidden_states else None

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

												a_						:						List[str] 							= stage_module(_lowercase    )

									if output_hidden_states:
												a_						:						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_							):
						"""simple docstring"""






						_A    :				int       					=			RegNetConfig
						_A    :				List[Any]       					=			'''regnet'''
						_A    :				Tuple       					=			'''pixel_values'''
						_A    :				Optional[int]       					=			True





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> List[str]:
									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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase=False    )     -> str:
									if isinstance(_lowercase  ,      _lowercase    ):
												a_						:						Dict 							= value







__snake_case				:      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.
"""

__snake_case				:      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_							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase    )     -> List[Any]:
									super().__init__(_lowercase    )
									a_						:						Union[str, Any] 							= config
									a_						:						Tuple 							= RegNetEmbeddings(_lowercase    )
									a_						:						int 							= RegNetEncoder(_lowercase    )
									a_						:						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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase = None  ,      _lowercase = None    )     -> BaseModelOutputWithPoolingAndNoAttention:
									a_						:						Dict 							= (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									a_						:						int 							= return_dict if return_dict is not None else self.config.use_return_dict

									a_						:						Any 							= self.embedder(_lowercase    )

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

									a_						:						Optional[int] 							= encoder_outputs[0]

									a_						:						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(
    '''
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    ''',  a_,  )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class        A__(a_							):
						"""simple docstring"""






						def __init__(    self  ,      _lowercase    )     -> int:
									super().__init__(_lowercase    )
									a_						:						Union[str, Any] 							= config.num_labels
									a_						:						Optional[int] 							= RegNetModel(_lowercase    )
									# classification head
									a_						:						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 			UpperCamelCase__					(    self  ,      _lowercase = None  ,      _lowercase = None  ,      _lowercase = None  ,      _lowercase = None  ,      )     -> ImageClassifierOutputWithNoAttention:
									a_						:						Dict 							= return_dict if return_dict is not None else self.config.use_return_dict

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

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

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

									a_						:						Optional[Any] 							= None

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

									if not return_dict:
												a_						:						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    )





import logging
from dataclasses import dataclass, field
from typing import Optional

from seqaseq_trainer import arg_to_scheduler

from transformers import TrainingArguments


__snake_case				:      Union[str, Any]   		=			logging.getLogger(__name__)



@dataclass
class        A__(a_							):
						"""simple docstring"""






						_A    :				Optional[float]       					=			field(
						    default=0.0,  metadata={'''help''': '''The label smoothing epsilon to apply (if not zero).'''}							)
						_A    :				bool       					=			field(default=a_,  metadata={'''help''': '''Whether to SortishSamler or not.'''}							)
						_A    :				bool       					=			field(
						    default=a_,  metadata={'''help''': '''Whether to use generate to calculate generative metrics (ROUGE, BLEU).'''}							)
						_A    :				bool       					=			field(default=a_,  metadata={'''help''': '''whether to use adafactor'''}							)
						_A    :				Optional[float]       					=			field(
						    default=a_,  metadata={'''help''': '''Encoder layer dropout probability. Goes into model.config.'''}							)
						_A    :				Optional[float]       					=			field(
						    default=a_,  metadata={'''help''': '''Decoder layer dropout probability. Goes into model.config.'''}							)
						_A    :				Optional[float]       					=			field(default=a_,  metadata={'''help''': '''Dropout probability. Goes into model.config.'''}							)
						_A    :				Optional[float]       					=			field(
						    default=a_,  metadata={'''help''': '''Attention dropout probability. Goes into model.config.'''}							)
						_A    :				Optional[str]       					=			field(
						    default='''linear''',  metadata={'''help''': f'''Which lr scheduler to use. Selected in {sorted(arg_to_scheduler.keys()							)}'''},  )


