



from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block





@dataclass
class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      42





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



     def __init__(self					,      __a=3					,      __a=3					,      __a=("DownEncoderBlock2D",)					,      __a=(64,)					,      __a=2					,      __a=32					,      __a="silu"					,      __a=True					,      ):






           '''simple docstring'''



           super().__init__()
           lowerCamelCase             =		layers_per_block

           lowerCamelCase             =		torch.nn.Convad(
               __a					,      block_out_channels[0]					,      kernel_size=3					,      stride=1					,      padding=1					,      )

           lowerCamelCase             =		None
           lowerCamelCase             =		nn.ModuleList([]     )

           # down
           lowerCamelCase             =		block_out_channels[0]
           for i, down_block_type in enumerate(__a     ):
                 lowerCamelCase             =		output_channel
                 lowerCamelCase             =		block_out_channels[i]
                 lowerCamelCase             =		i == len(__a     ) - 1

                 lowerCamelCase             =		get_down_block(
                     __a					,      num_layers=self.layers_per_block					,      in_channels=__a					,      out_channels=__a					,      add_downsample=not is_final_block					,      resnet_eps=1E-6					,      downsample_padding=0					,      resnet_act_fn=__a					,      resnet_groups=__a					,      attention_head_dim=__a					,      temb_channels=__a					,      )
                 self.down_blocks.append(__a     )

           # mid
           lowerCamelCase             =		UNetMidBlockaD(
               in_channels=block_out_channels[-1]					,      resnet_eps=1E-6					,      resnet_act_fn=__a					,      output_scale_factor=1					,      resnet_time_scale_shift="default"					,      attention_head_dim=block_out_channels[-1]					,      resnet_groups=__a					,      temb_channels=__a					,      )

           # out
           lowerCamelCase             =		nn.GroupNorm(num_channels=block_out_channels[-1]					,      num_groups=__a					,      eps=1E-6     )
           lowerCamelCase             =		nn.SiLU()

           lowerCamelCase             =		2 * out_channels if double_z else out_channels
           lowerCamelCase             =		nn.Convad(block_out_channels[-1]					,      __a					,      3					,      padding=1     )

           lowerCamelCase             =		False







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		x
           lowerCamelCase             =		self.conv_in(__a     )

           if self.training and self.gradient_checkpointing:

                 def create_custom_forward(__a     ):
                       def custom_forward(*__a     ):
                             return module(*__a     )

                       return custom_forward

                 # down
                 if is_torch_version(">="					,      "1.11.0"     ):
                       for down_block in self.down_blocks:
                             lowerCamelCase             =		torch.utils.checkpoint.checkpoint(
                                 create_custom_forward(__a     )					,      __a					,      use_reentrant=__a     )
                       # middle
                       lowerCamelCase             =		torch.utils.checkpoint.checkpoint(
                           create_custom_forward(self.mid_block     )					,      __a					,      use_reentrant=__a     )
                 else:
                       for down_block in self.down_blocks:
                             lowerCamelCase             =		torch.utils.checkpoint.checkpoint(create_custom_forward(__a     )					,      __a     )
                       # middle
                       lowerCamelCase             =		torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block     )					,      __a     )

           else:
                 # down
                 for down_block in self.down_blocks:
                       lowerCamelCase             =		down_block(__a     )

                 # middle
                 lowerCamelCase             =		self.mid_block(__a     )

           # post-process
           lowerCamelCase             =		self.conv_norm_out(__a     )
           lowerCamelCase             =		self.conv_act(__a     )
           lowerCamelCase             =		self.conv_out(__a     )

           return sample





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



     def __init__(self					,      __a=3					,      __a=3					,      __a=("UpDecoderBlock2D",)					,      __a=(64,)					,      __a=2					,      __a=32					,      __a="silu"					,      __a="group"					,      ):






           '''simple docstring'''



           super().__init__()
           lowerCamelCase             =		layers_per_block

           lowerCamelCase             =		nn.Convad(
               __a					,      block_out_channels[-1]					,      kernel_size=3					,      stride=1					,      padding=1					,      )

           lowerCamelCase             =		None
           lowerCamelCase             =		nn.ModuleList([]     )

           lowerCamelCase             =		in_channels if norm_type == "spatial" else None

           # mid
           lowerCamelCase             =		UNetMidBlockaD(
               in_channels=block_out_channels[-1]					,      resnet_eps=1E-6					,      resnet_act_fn=__a					,      output_scale_factor=1					,      resnet_time_scale_shift="default" if norm_type == "group" else norm_type					,      attention_head_dim=block_out_channels[-1]					,      resnet_groups=__a					,      temb_channels=__a					,      )

           # up
           lowerCamelCase             =		list(reversed(__a     )     )
           lowerCamelCase             =		reversed_block_out_channels[0]
           for i, up_block_type in enumerate(__a     ):
                 lowerCamelCase             =		output_channel
                 lowerCamelCase             =		reversed_block_out_channels[i]

                 lowerCamelCase             =		i == len(__a     ) - 1

                 lowerCamelCase             =		get_up_block(
                     __a					,      num_layers=self.layers_per_block + 1					,      in_channels=__a					,      out_channels=__a					,      prev_output_channel=__a					,      add_upsample=not is_final_block					,      resnet_eps=1E-6					,      resnet_act_fn=__a					,      resnet_groups=__a					,      attention_head_dim=__a					,      temb_channels=__a					,      resnet_time_scale_shift=__a					,      )
                 self.up_blocks.append(__a     )
                 lowerCamelCase             =		output_channel

           # out
           if norm_type == "spatial":
                 lowerCamelCase             =		SpatialNorm(block_out_channels[0]					,      __a     )
           else:
                 lowerCamelCase             =		nn.GroupNorm(num_channels=block_out_channels[0]					,      num_groups=__a					,      eps=1E-6     )
           lowerCamelCase             =		nn.SiLU()
           lowerCamelCase             =		nn.Convad(block_out_channels[0]					,      __a					,      3					,      padding=1     )

           lowerCamelCase             =		False







     def     _a   (self					,      __a					,      __a=None     ):






           '''simple docstring'''



           lowerCamelCase             =		z
           lowerCamelCase             =		self.conv_in(__a     )

           lowerCamelCase             =		next(iter(self.up_blocks.parameters()     )     ).dtype
           if self.training and self.gradient_checkpointing:

                 def create_custom_forward(__a     ):
                       def custom_forward(*__a     ):
                             return module(*__a     )

                       return custom_forward

                 if is_torch_version(">="					,      "1.11.0"     ):
                       # middle
                       lowerCamelCase             =		torch.utils.checkpoint.checkpoint(
                           create_custom_forward(self.mid_block     )					,      __a					,      __a					,      use_reentrant=__a     )
                       lowerCamelCase             =		sample.to(__a     )

                       # up
                       for up_block in self.up_blocks:
                             lowerCamelCase             =		torch.utils.checkpoint.checkpoint(
                                 create_custom_forward(__a     )					,      __a					,      __a					,      use_reentrant=__a     )
                 else:
                       # middle
                       lowerCamelCase             =		torch.utils.checkpoint.checkpoint(
                           create_custom_forward(self.mid_block     )					,      __a					,      __a     )
                       lowerCamelCase             =		sample.to(__a     )

                       # up
                       for up_block in self.up_blocks:
                             lowerCamelCase             =		torch.utils.checkpoint.checkpoint(create_custom_forward(__a     )					,      __a					,      __a     )
           else:
                 # middle
                 lowerCamelCase             =		self.mid_block(__a					,      __a     )
                 lowerCamelCase             =		sample.to(__a     )

                 # up
                 for up_block in self.up_blocks:
                       lowerCamelCase             =		up_block(__a					,      __a     )

        # post-process
           if latent_embeds is None:
                 lowerCamelCase             =		self.conv_norm_out(__a     )
           else:
                 lowerCamelCase             =		self.conv_norm_out(__a					,      __a     )
           lowerCamelCase             =		self.conv_act(__a     )
           lowerCamelCase             =		self.conv_out(__a     )

           return sample





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



     def __init__(self					,      __a					,      __a					,      __a					,      __a=None					,      __a="random"					,      __a=False					,      __a=True     ):






           '''simple docstring'''



           super().__init__()
           lowerCamelCase             =		n_e
           lowerCamelCase             =		vq_embed_dim
           lowerCamelCase             =		beta
           lowerCamelCase             =		legacy

           lowerCamelCase             =		nn.Embedding(self.n_e					,      self.vq_embed_dim     )
           self.embedding.weight.data.uniform_(-1.0 / self.n_e					,      1.0 / self.n_e     )

           lowerCamelCase             =		remap
           if self.remap is not None:
                 self.register_buffer("used"					,      torch.tensor(np.load(self.remap     )     )     )
                 lowerCamelCase             =		self.used.shape[0]
                 lowerCamelCase             =		unknown_index  # "random" or "extra" or integer
                 if self.unknown_index == "extra":
                       lowerCamelCase             =		self.re_embed
                       lowerCamelCase             =		self.re_embed + 1
                 print(
                     F"""Remapping {self.n_e} indices to {self.re_embed} indices. """
                     F"""Using {self.unknown_index} for unknown indices."""     )
           else:
                 lowerCamelCase             =		n_e

           lowerCamelCase             =		sane_index_shape







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		inds.shape
           assert len(__a     ) > 1
           lowerCamelCase             =		inds.reshape(ishape[0]					,      -1     )
           lowerCamelCase             =		self.used.to(__a     )
           lowerCamelCase             =		(inds[:, :, None] == used[None, None, ...]).long()
           lowerCamelCase             =		match.argmax(-1     )
           lowerCamelCase             =		match.sum(2     ) < 1
           if self.unknown_index == "random":
                 lowerCamelCase             =		torch.randint(0					,      self.re_embed					,      size=new[unknown].shape     ).to(device=new.device     )
           else:
                 lowerCamelCase             =		self.unknown_index
           return new.reshape(__a     )







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		inds.shape
           assert len(__a     ) > 1
           lowerCamelCase             =		inds.reshape(ishape[0]					,      -1     )
           lowerCamelCase             =		self.used.to(__a     )
           if self.re_embed > self.used.shape[0]:  # extra token
                 lowerCamelCase             =		0  # simply set to zero
           lowerCamelCase             =		torch.gather(used[None, :][inds.shape[0] * [0], :]					,      1					,      __a     )
           return back.reshape(__a     )







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		z.permute(0					,      2					,      3					,      1     ).contiguous()
           lowerCamelCase             =		z.view(-1					,      self.vq_embed_dim     )

           # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
           lowerCamelCase             =		torch.argmin(torch.cdist(__a					,      self.embedding.weight     )					,      dim=1     )

           lowerCamelCase             =		self.embedding(__a     ).view(z.shape     )
           lowerCamelCase             =		None
           lowerCamelCase             =		None

           # compute loss for embedding
           if not self.legacy:
                 lowerCamelCase             =		self.beta * torch.mean((z_q.detach() - z) ** 2     ) + torch.mean((z_q - z.detach()) ** 2     )
           else:
                 lowerCamelCase             =		torch.mean((z_q.detach() - z) ** 2     ) + self.beta * torch.mean((z_q - z.detach()) ** 2     )

           # preserve gradients
           lowerCamelCase             =		z + (z_q - z).detach()

           # reshape back to match original input shape
           lowerCamelCase             =		z_q.permute(0					,      3					,      1					,      2     ).contiguous()

           if self.remap is not None:
                 lowerCamelCase             =		min_encoding_indices.reshape(z.shape[0]					,      -1     )  # add batch axis
                 lowerCamelCase             =		self.remap_to_used(__a     )
                 lowerCamelCase             =		min_encoding_indices.reshape(-1					,      1     )  # flatten

           if self.sane_index_shape:
                 lowerCamelCase             =		min_encoding_indices.reshape(z_q.shape[0]					,      z_q.shape[2]					,      z_q.shape[3]     )

           return z_q, loss, (perplexity, min_encodings, min_encoding_indices)







     def     _a   (self					,      __a					,      __a     ):






           '''simple docstring'''



           if self.remap is not None:
                 lowerCamelCase             =		indices.reshape(shape[0]					,      -1     )  # add batch axis
                 lowerCamelCase             =		self.unmap_to_all(__a     )
                 lowerCamelCase             =		indices.reshape(-1     )  # flatten again

           # get quantized latent vectors
           lowerCamelCase             =		self.embedding(__a     )

           if shape is not None:
                 lowerCamelCase             =		z_q.view(__a     )
                 # reshape back to match original input shape
                 lowerCamelCase             =		z_q.permute(0					,      3					,      1					,      2     ).contiguous()

           return z_q





class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     def __init__(self					,      __a					,      __a=False     ):






           '''simple docstring'''



           lowerCamelCase             =		parameters
           lowerCamelCase      ,				lowerCamelCase             =		torch.chunk(__a					,      2					,      dim=1     )
           lowerCamelCase             =		torch.clamp(self.logvar					,      -30.0					,      20.0     )
           lowerCamelCase             =		deterministic
           lowerCamelCase             =		torch.exp(0.5 * self.logvar     )
           lowerCamelCase             =		torch.exp(self.logvar     )
           if self.deterministic:
                 lowerCamelCase             =		lowerCamelCase             =		torch.zeros_like(
                     self.mean					,      device=self.parameters.device					,      dtype=self.parameters.dtype     )







     def     _a   (self					,      __a = None     ):






           '''simple docstring'''



           lowerCamelCase             =		randn_tensor(
               self.mean.shape					,      generator=__a					,      device=self.parameters.device					,      dtype=self.parameters.dtype     )
           lowerCamelCase             =		self.mean + self.std * sample
           return x







     def     _a   (self					,      __a=None     ):






           '''simple docstring'''



           if self.deterministic:
                 return torch.Tensor([0.0]     )
           else:
                 if other is None:
                       return 0.5 * torch.sum(torch.pow(self.mean					,      2     ) + self.var - 1.0 - self.logvar					,      dim=[1, 2, 3]     )
                 else:
                       return 0.5 * torch.sum(
                           torch.pow(self.mean - other.mean					,      2     ) / other.var
                           + self.var / other.var
                           - 1.0
                           - self.logvar
                           + other.logvar					,      dim=[1, 2, 3]					,      )







     def     _a   (self					,      __a					,      __a=[1, 2, 3]     ):






           '''simple docstring'''



           if self.deterministic:
                 return torch.Tensor([0.0]     )
           lowerCamelCase             =		np.log(2.0 * np.pi     )
           return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean					,      2     ) / self.var					,      dim=__a     )







     def     _a   (self     ):






           '''simple docstring'''



           return self.mean




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


a_    :  Optional[int]						  =		logging.get_logger(__name__)

# General docstring
a_    :  List[str]						  =		'RegNetConfig'

# Base docstring
a_    :  Union[str, Any]						  =		'facebook/regnet-y-040'
a_    :  Optional[Any]						  =		[1, 1_0_8_8, 7, 7]

# Image classification docstring
a_    :  Dict						  =		'facebook/regnet-y-040'
a_    :  List[Any]						  =		'tabby, tabby cat'

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





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



     def __init__(self					,      __a					,      __a					,      __a = 3					,      __a = 1					,      __a = 1					,      __a = "relu"					,      ):






           '''simple docstring'''



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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.convolution(__a     )
           lowerCamelCase             =		self.normalization(__a     )
           lowerCamelCase             =		self.activation(__a     )
           return hidden_state





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



     def __init__(self					,      __a     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		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."     )
           lowerCamelCase             =		self.embedder(__a     )
           return hidden_state





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



     def __init__(self					,      __a					,      __a					,      __a = 2     ):






           '''simple docstring'''



           super().__init__()
           lowerCamelCase             =		nn.Convad(__a					,      __a					,      kernel_size=1					,      stride=__a					,      bias=__a     )
           lowerCamelCase             =		nn.BatchNormad(__a     )







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.convolution(__a     )
           lowerCamelCase             =		self.normalization(__a     )
           return hidden_state





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



     def __init__(self					,      __a					,      __a     ):






           '''simple docstring'''



           super().__init__()

           lowerCamelCase             =		nn.AdaptiveAvgPoolad((1, 1)     )
           lowerCamelCase             =		nn.Sequential(
               nn.Convad(__a					,      __a					,      kernel_size=1     )					,      nn.ReLU()					,      nn.Convad(__a					,      __a					,      kernel_size=1     )					,      nn.Sigmoid()					,      )







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.pooler(__a     )
           lowerCamelCase             =		self.attention(__a     )
           lowerCamelCase             =		hidden_state * attention
           return hidden_state





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



     def __init__(self					,      __a					,      __a					,      __a					,      __a = 1     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		hidden_state
           lowerCamelCase             =		self.layer(__a     )
           lowerCamelCase             =		self.shortcut(__a     )
           hidden_state += residual
           lowerCamelCase             =		self.activation(__a     )
           return hidden_state





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



     def __init__(self					,      __a					,      __a					,      __a					,      __a = 1     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		hidden_state
           lowerCamelCase             =		self.layer(__a     )
           lowerCamelCase             =		self.shortcut(__a     )
           hidden_state += residual
           lowerCamelCase             =		self.activation(__a     )
           return hidden_state





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



     def __init__(self					,      __a					,      __a					,      __a					,      __a = 2					,      __a = 2					,      ):






           '''simple docstring'''



           super().__init__()

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

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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.layers(__a     )
           return hidden_state





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



     def __init__(self					,      __a     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a					,      __a = False					,      __a = True     ):






           '''simple docstring'''



           lowerCamelCase             =		() if output_hidden_states else None

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

                 lowerCamelCase             =		stage_module(__a     )

           if output_hidden_states:
                 lowerCamelCase             =		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=__a					,      hidden_states=__a     )





class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     _A      =      RegNetConfig
     _A      =      'regnet'
     _A      =      'pixel_values'
     _A      =      True







     def     _a   (self					,      __a     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a					,      __a=False     ):






           '''simple docstring'''



           if isinstance(__a					,      __a     ):
                 lowerCamelCase             =		value



a_    :  Dict						  =		R'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

a_    :  Optional[Any]						  =		R'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.\n'





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



     def __init__(self					,      __a     ):






           '''simple docstring'''



           super().__init__(__a     )
           lowerCamelCase             =		config
           lowerCamelCase             =		RegNetEmbeddings(__a     )
           lowerCamelCase             =		RegNetEncoder(__a     )
           lowerCamelCase             =		nn.AdaptiveAvgPoolad((1, 1)     )
           # Initialize weights and apply final processing
           self.post_init()







     @add_start_docstrings_to_model_forward(__a     )
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC					,      output_type=__a					,      config_class=_CONFIG_FOR_DOC					,      modality="vision"					,      expected_output=_EXPECTED_OUTPUT_SHAPE					,      )
     def     _a   (self					,      __a					,      __a = None					,      __a = None     ):






           '''simple docstring'''



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

           lowerCamelCase             =		self.embedder(__a     )

           lowerCamelCase             =		self.encoder(
               __a					,      output_hidden_states=__a					,      return_dict=__a     )

           lowerCamelCase             =		encoder_outputs[0]

           lowerCamelCase             =		self.pooler(__a     )

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

           return BaseModelOutputWithPoolingAndNoAttention(
               last_hidden_state=__a					,      pooler_output=__a					,      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    '							,     UpperCAmelCase_							,     )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     def __init__(self					,      __a     ):






           '''simple docstring'''



           super().__init__(__a     )
           lowerCamelCase             =		config.num_labels
           lowerCamelCase             =		RegNetModel(__a     )
           # classification head
           lowerCamelCase             =		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(__a     )
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT					,      output_type=__a					,      config_class=_CONFIG_FOR_DOC					,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT					,      )
     def     _a   (self					,      __a = None					,      __a = None					,      __a = None					,      __a = None					,      ):






           '''simple docstring'''



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

           lowerCamelCase             =		self.regnet(__a					,      output_hidden_states=__a					,      return_dict=__a     )

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

           lowerCamelCase             =		self.classifier(__a     )

           lowerCamelCase             =		None

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

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

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