

'''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


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 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		Dict		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int = 3		,					UpperCAmelCase__     :		int = 1		,					UpperCAmelCase__     :		int = 1		,					UpperCAmelCase__     :		Optional[str] = "relu"		,					):
										'''simple docstring'''
										super().__init__()
										lowercase :							Tuple    		=nn.Convad(
										    UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=UpperCAmelCase__		,					stride=UpperCAmelCase__		,					padding=kernel_size // 2		,					groups=UpperCAmelCase__		,					bias=UpperCAmelCase__		,					)
										lowercase :							Optional[int]    		=nn.BatchNormad(UpperCAmelCase__   )
										lowercase :							Any    		=ACTaFN[activation] if activation is not None else nn.Identity()







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		Any   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.convolution(UpperCAmelCase__   )
										lowercase :							int    		=self.normalization(UpperCAmelCase__   )
										lowercase :							Tuple    		=self.activation(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		str		,					UpperCAmelCase__     :		RegNetConfig   ):
										'''simple docstring'''
										super().__init__()
										lowercase :							Tuple    		=RegNetConvLayer(
										    config.num_channels		,					config.embedding_size		,					kernel_size=3		,					stride=2		,					activation=config.hidden_act   )
										lowercase :							Optional[int]    		=config.num_channels







				def  lowerCamelCase_	(       self     :		Union[str, Any]		,					UpperCAmelCase__     :		List[str]   ):
										'''simple docstring'''
										lowercase :							Optional[Any]    		=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 :							List[Any]    		=self.embedder(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		List[Any]		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int = 2   ):
										'''simple docstring'''
										super().__init__()
										lowercase :							Tuple    		=nn.Convad(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1		,					stride=UpperCAmelCase__		,					bias=UpperCAmelCase__   )
										lowercase :							List[Any]    		=nn.BatchNormad(UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		Tuple		,					UpperCAmelCase__     :		Tensor   ):
										'''simple docstring'''
										lowercase :							List[str]    		=self.convolution(UpperCAmelCase__   )
										lowercase :							Any    		=self.normalization(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		Union[str, Any]		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int   ):
										'''simple docstring'''
										super().__init__()

										lowercase :							Union[str, Any]    		=nn.AdaptiveAvgPoolad((1, 1)   )
										lowercase :							Dict    		=nn.Sequential(
										    nn.Convad(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1   )		,					nn.ReLU()		,					nn.Convad(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1   )		,					nn.Sigmoid()		,					)







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		List[Any]   ):
										'''simple docstring'''
										# b c h w -> b c 1 1
										lowercase :							int    		=self.pooler(UpperCAmelCase__   )
										lowercase :							List[str]    		=self.attention(UpperCAmelCase__   )
										lowercase :							List[str]    		=hidden_state * attention
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		str		,					UpperCAmelCase__     :		RegNetConfig		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int = 1   ):
										'''simple docstring'''
										super().__init__()
										lowercase :							Any    		=in_channels != out_channels or stride != 1
										lowercase :							int    		=max(1		,					out_channels // config.groups_width   )
										lowercase :							Optional[int]    		=(
										    RegNetShortCut(UpperCAmelCase__		,					UpperCAmelCase__		,					stride=UpperCAmelCase__   ) if should_apply_shortcut else nn.Identity()
										)
										lowercase :							Tuple    		=nn.Sequential(
										    RegNetConvLayer(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1		,					activation=config.hidden_act   )		,					RegNetConvLayer(UpperCAmelCase__		,					UpperCAmelCase__		,					stride=UpperCAmelCase__		,					groups=UpperCAmelCase__		,					activation=config.hidden_act   )		,					RegNetConvLayer(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1		,					activation=UpperCAmelCase__   )		,					)
										lowercase :							str    		=ACTaFN[config.hidden_act]







				def  lowerCamelCase_	(       self     :		List[Any]		,					UpperCAmelCase__     :		Optional[Any]   ):
										'''simple docstring'''
										lowercase :							str    		=hidden_state
										lowercase :							int    		=self.layer(UpperCAmelCase__   )
										lowercase :							Any    		=self.shortcut(UpperCAmelCase__   )
										hidden_state += residual
										lowercase :							Union[str, Any]    		=self.activation(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		List[Any]		,					UpperCAmelCase__     :		RegNetConfig		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int = 1   ):
										'''simple docstring'''
										super().__init__()
										lowercase :							str    		=in_channels != out_channels or stride != 1
										lowercase :							Optional[int]    		=max(1		,					out_channels // config.groups_width   )
										lowercase :							str    		=(
										    RegNetShortCut(UpperCAmelCase__		,					UpperCAmelCase__		,					stride=UpperCAmelCase__   ) if should_apply_shortcut else nn.Identity()
										)
										lowercase :							Union[str, Any]    		=nn.Sequential(
										    RegNetConvLayer(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1		,					activation=config.hidden_act   )		,					RegNetConvLayer(UpperCAmelCase__		,					UpperCAmelCase__		,					stride=UpperCAmelCase__		,					groups=UpperCAmelCase__		,					activation=config.hidden_act   )		,					RegNetSELayer(UpperCAmelCase__		,					reduced_channels=int(round(in_channels / 4   )   )   )		,					RegNetConvLayer(UpperCAmelCase__		,					UpperCAmelCase__		,					kernel_size=1		,					activation=UpperCAmelCase__   )		,					)
										lowercase :							Union[str, Any]    		=ACTaFN[config.hidden_act]







				def  lowerCamelCase_	(       self     :		Any		,					UpperCAmelCase__     :		Tuple   ):
										'''simple docstring'''
										lowercase :							Dict    		=hidden_state
										lowercase :							Any    		=self.layer(UpperCAmelCase__   )
										lowercase :							Dict    		=self.shortcut(UpperCAmelCase__   )
										hidden_state += residual
										lowercase :							str    		=self.activation(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		Dict		,					UpperCAmelCase__     :		RegNetConfig		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int = 2		,					UpperCAmelCase__     :		int = 2		,					):
										'''simple docstring'''
										super().__init__()

										lowercase :							Union[str, Any]    		=RegNetXLayer if config.layer_type == '''x''' else RegNetYLayer

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







				def  lowerCamelCase_	(       self     :		List[Any]		,					UpperCAmelCase__     :		Union[str, Any]   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.layers(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		int		,					UpperCAmelCase__     :		RegNetConfig   ):
										'''simple docstring'''
										super().__init__()
										lowercase :							str    		=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(
										        UpperCAmelCase__		,					config.embedding_size		,					config.hidden_sizes[0]		,					stride=2 if config.downsample_in_first_stage else 1		,					depth=config.depths[0]		,					)   )
										lowercase :							List[str]    		=zip(config.hidden_sizes		,					config.hidden_sizes[1:]   )
										for (in_channels, out_channels), depth in zip(UpperCAmelCase__		,					config.depths[1:]   ):
																self.stages.append(RegNetStage(UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__		,					depth=UpperCAmelCase__   )   )







				def  lowerCamelCase_	(       self     :		List[str]		,					UpperCAmelCase__     :		Tensor		,					UpperCAmelCase__     :		bool = False		,					UpperCAmelCase__     :		bool = True   ):
										'''simple docstring'''
										lowercase :							List[str]    		=() if output_hidden_states else None

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

																lowercase :							int    		=stage_module(UpperCAmelCase__   )

										if output_hidden_states:
																lowercase :							Tuple    		=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=UpperCAmelCase__		,					hidden_states=UpperCAmelCase__   )





class 		__SCREAMING_SNAKE_CASE							(			lowercase__      ):
				lowerCamelCase_		       =    RegNetConfig
				lowerCamelCase_		       =    'regnet'
				lowerCamelCase_		       =    'pixel_values'
				lowerCamelCase_		       =    True







				def  lowerCamelCase_	(       self     :		Union[str, Any]		,					UpperCAmelCase__     :		Tuple   ):
										'''simple docstring'''
										if isinstance(UpperCAmelCase__		,					nn.Convad   ):
																nn.init.kaiming_normal_(module.weight		,					mode='''fan_out'''		,					nonlinearity='''relu'''   )
										elif isinstance(UpperCAmelCase__		,					(nn.BatchNormad, nn.GroupNorm)   ):
																nn.init.constant_(module.weight		,					1   )
																nn.init.constant_(module.bias		,					0   )







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		Optional[int]=False   ):
										'''simple docstring'''
										if isinstance(UpperCAmelCase__		,					UpperCAmelCase__   ):
																lowercase :							int    		=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.'						,							lowercase__						,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 		__SCREAMING_SNAKE_CASE							(			lowercase__      ):







				def __init__(       self     :		Any		,					UpperCAmelCase__     :		List[Any]   ):
										'''simple docstring'''
										super().__init__(UpperCAmelCase__   )
										lowercase :							str    		=config
										lowercase :							Any    		=RegNetEmbeddings(UpperCAmelCase__   )
										lowercase :							Union[str, Any]    		=RegNetEncoder(UpperCAmelCase__   )
										lowercase :							Any    		=nn.AdaptiveAvgPoolad((1, 1)   )
										# Initialize weights and apply final processing
										self.post_init()







				@add_start_docstrings_to_model_forward(UpperCAmelCase__   )
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC		,					output_type=UpperCAmelCase__		,					config_class=_CONFIG_FOR_DOC		,					modality='''vision'''		,					expected_output=_EXPECTED_OUTPUT_SHAPE		,					)
				def  lowerCamelCase_	(       self     :		List[str]		,					UpperCAmelCase__     :		Tensor		,					UpperCAmelCase__     :		Optional[bool] = None		,					UpperCAmelCase__     :		Optional[bool] = None   ):
										'''simple docstring'''
										lowercase :							str    		=(
										    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
										)
										lowercase :							Dict    		=return_dict if return_dict is not None else self.config.use_return_dict

										lowercase :							Tuple    		=self.embedder(UpperCAmelCase__   )

										lowercase :							Optional[Any]    		=self.encoder(
										    UpperCAmelCase__		,					output_hidden_states=UpperCAmelCase__		,					return_dict=UpperCAmelCase__   )

										lowercase :							Optional[int]    		=encoder_outputs[0]

										lowercase :							Optional[int]    		=self.pooler(UpperCAmelCase__   )

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

										return BaseModelOutputWithPoolingAndNoAttention(
										    last_hidden_state=UpperCAmelCase__		,					pooler_output=UpperCAmelCase__		,					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    '						,							lowercase__						,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 		__SCREAMING_SNAKE_CASE							(			lowercase__      ):







				def __init__(       self     :		str		,					UpperCAmelCase__     :		Any   ):
										'''simple docstring'''
										super().__init__(UpperCAmelCase__   )
										lowercase :							Dict    		=config.num_labels
										lowercase :							Union[str, Any]    		=RegNetModel(UpperCAmelCase__   )
										# classification head
										lowercase :							Optional[int]    		=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(UpperCAmelCase__   )
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT		,					output_type=UpperCAmelCase__		,					config_class=_CONFIG_FOR_DOC		,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,					)
				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		Optional[torch.FloatTensor] = None		,					UpperCAmelCase__     :		Optional[torch.LongTensor] = None		,					UpperCAmelCase__     :		Optional[bool] = None		,					UpperCAmelCase__     :		Optional[bool] = None		,					):
										'''simple docstring'''
										lowercase :							Dict    		=return_dict if return_dict is not None else self.config.use_return_dict

										lowercase :							Union[str, Any]    		=self.regnet(UpperCAmelCase__		,					output_hidden_states=UpperCAmelCase__		,					return_dict=UpperCAmelCase__   )

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

										lowercase :							Optional[int]    		=self.classifier(UpperCAmelCase__   )

										lowercase :							Any    		=None

										if labels is not None:
																if self.config.problem_type is None:
																						if self.num_labels == 1:
																												lowercase :							Optional[int]    		='''regression'''
																						elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																												lowercase :							List[str]    		='''single_label_classification'''
																						else:
																												lowercase :							int    		='''multi_label_classification'''
																if self.config.problem_type == "regression":
																						lowercase :							str    		=MSELoss()
																						if self.num_labels == 1:
																												lowercase :							Union[str, Any]    		=loss_fct(logits.squeeze()		,					labels.squeeze()   )
																						else:
																												lowercase :							str    		=loss_fct(UpperCAmelCase__		,					UpperCAmelCase__   )
																elif self.config.problem_type == "single_label_classification":
																						lowercase :							Tuple    		=CrossEntropyLoss()
																						lowercase :							Union[str, 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(UpperCAmelCase__		,					UpperCAmelCase__   )

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

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








'''simple docstring'''





import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
UpperCamelCase_    						=       object()

# For specifying empty leaf dict `{}`
UpperCamelCase_    						=       object()






def 				_lowerCAmelCase			(   __magic_name__						:     int   , __magic_name__						:     int						)				->  Optional[int]:
						lowercase :							Optional[Any]    		=tuple((re.compile(x + '''$'''						) for x in qs)						)
						for i in range(len(__magic_name__						) - len(__magic_name__						) + 1						):
												lowercase :							Union[str, Any]    		=[x.match(__magic_name__						) for x, y in zip(__magic_name__   , ks[i:]						)]
												if matches and all(__magic_name__						):
																		return True
						return False






def 				_lowerCAmelCase			(   __magic_name__						:     Dict						)				->  List[str]:
						def replace(__magic_name__						:     Optional[int]   , __magic_name__						:     Optional[Any]						):
												for rule, replacement in rules:
																		if _match(__magic_name__   , __magic_name__						):
																								return replacement
												return val

						return replace






def 				_lowerCAmelCase			(   )				->  int:
						return [
						    # embeddings
						    (("transformer", "wpe", "embedding"), P('''mp'''   , __magic_name__						)),
						    (("transformer", "wte", "embedding"), P('''mp'''   , __magic_name__						)),
						    # atention
						    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__magic_name__   , '''mp'''						)),
						    (("attention", "out_proj", "kernel"), P('''mp'''   , __magic_name__						)),
						    (("attention", "out_proj", "bias"), None),
						    # mlp
						    (("mlp", "c_fc", "kernel"), P(__magic_name__   , '''mp'''						)),
						    (("mlp", "c_fc", "bias"), P('''mp'''						)),
						    (("mlp", "c_proj", "kernel"), P('''mp'''   , __magic_name__						)),
						    (("mlp", "c_proj", "bias"), None),
						    # layer norms
						    ((r"ln_\d+", "bias"), None),
						    ((r"\d+", r"ln_\d+", "scale"), None),
						    (("ln_f", "bias"), None),
						    (("ln_f", "scale"), None),
						]







def 				_lowerCAmelCase			(   __magic_name__						:     str						)				->  int:
						lowercase :							int    		=_get_partition_rules()
						lowercase :							Tuple    		=_replacement_rules(__magic_name__						)
						lowercase :							Any    		={k: _unmatched for k in flatten_dict(__magic_name__						)}
						lowercase :							Any    		={k: replace(__magic_name__   , __magic_name__						) for k, v in initd.items()}
						assert _unmatched not in result.values(), "Incomplete partition spec."
						return freeze(unflatten_dict(__magic_name__						)						)





