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


SCREAMING_SNAKE_CASE__:Tuple			=	logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE__:List[Any]			=	"""RegNetConfig"""

# Base docstring
SCREAMING_SNAKE_CASE__:List[str]			=	"""facebook/regnet-y-040"""
SCREAMING_SNAKE_CASE__:Tuple			=	[1, 1088, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE__:Dict			=	"""facebook/regnet-y-040"""
SCREAMING_SNAKE_CASE__:str			=	"""tabby, tabby cat"""

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






class 				snake_case__     (   nn.Module   ):


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






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  self.convolution(lowerCamelCase		)
        __a     				=  self.normalization(lowerCamelCase		)
        __a     				=  self.activation(lowerCamelCase		)
        return hidden_state






class 				snake_case__     (   nn.Module   ):


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






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  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     				=  self.embedder(lowerCamelCase		)
        return hidden_state






class 				snake_case__     (   nn.Module   ):


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






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  self.convolution(lowerCamelCase		)
        __a     				=  self.normalization(lowerCamelCase		)
        return hidden_state






class 				snake_case__     (   nn.Module   ):


 def __init__(				self  ,						lowerCamelCase  ,						lowerCamelCase		):
        super().__init__()

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






 def 		a__ (				self  ,						lowerCamelCase		):
        # b c h w -> b c 1 1
        __a     				=  self.pooler(lowerCamelCase		)
        __a     				=  self.attention(lowerCamelCase		)
        __a     				=  hidden_state * attention
        return hidden_state






class 				snake_case__     (   nn.Module   ):


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






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  hidden_state
        __a     				=  self.layer(lowerCamelCase		)
        __a     				=  self.shortcut(lowerCamelCase		)
        hidden_state += residual
        __a     				=  self.activation(lowerCamelCase		)
        return hidden_state






class 				snake_case__     (   nn.Module   ):


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






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  hidden_state
        __a     				=  self.layer(lowerCamelCase		)
        __a     				=  self.shortcut(lowerCamelCase		)
        hidden_state += residual
        __a     				=  self.activation(lowerCamelCase		)
        return hidden_state






class 				snake_case__     (   nn.Module   ):


 def __init__(				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase = 2  ,						lowerCamelCase = 2  ,						):
        super().__init__()

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

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






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  self.layers(lowerCamelCase		)
        return hidden_state






class 				snake_case__     (   nn.Module   ):


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






 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = False  ,						lowerCamelCase = True		):
        __a     				=  () if output_hidden_states else None

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

               __a     				=  stage_module(lowerCamelCase		)

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






class 				snake_case__     (   snake_case_   ):
 _snake_case   :			List[Any]           =		RegNetConfig
 _snake_case   :			List[Any]           =		"""regnet"""
 _snake_case   :			Optional[int]           =		"""pixel_values"""
 _snake_case   :			Optional[Any]           =		True


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






 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase=False		):
        if isinstance(lowerCamelCase  ,						lowerCamelCase		):
               __a     				=  value
SCREAMING_SNAKE_CASE__: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.
"""

SCREAMING_SNAKE_CASE__:List[Any]			=	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.""",		snake_case_,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 				snake_case__     (   snake_case_   ):


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






 @add_start_docstrings_to_model_forward(lowerCamelCase		)
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC  ,						output_type=lowerCamelCase  ,						config_class=_CONFIG_FOR_DOC  ,						modality="vision"  ,						expected_output=_EXPECTED_OUTPUT_SHAPE  ,						)
 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None  ,						lowerCamelCase = None		):
        __a     				=  (
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        __a     				=  return_dict if return_dict is not None else self.config.use_return_dict

        __a     				=  self.embedder(lowerCamelCase		)

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

        __a     				=  encoder_outputs[0]

        __a     				=  self.pooler(lowerCamelCase		)

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

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


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

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

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

        __a     				=  self.classifier(lowerCamelCase		)

        __a     				=  None

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

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

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

"""simple docstring"""



from typing import List, Optional, Tuple, Union

import torch

from ...models import UNetaDModel
from ...schedulers import KarrasVeScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput






class 				snake_case__     (   snake_case_   ):
 _snake_case   :			UNetaDModel
 _snake_case   :			KarrasVeScheduler


 def __init__(				self  ,						lowerCamelCase  ,						lowerCamelCase		):
        super().__init__()
        self.register_modules(unet=lowerCamelCase  ,						scheduler=lowerCamelCase		)






 @torch.no_grad()
 def __call__(				self  ,						lowerCamelCase = 1  ,						lowerCamelCase = 50  ,						lowerCamelCase = None  ,						lowerCamelCase = "pil"  ,						lowerCamelCase = True  ,						**lowerCamelCase  ,						):
        __a     				=  self.unet.config.sample_size
        __a     				=  (batch_size, 3, img_size, img_size)

        __a     				=  self.unet

        # sample x_0 ~ N(0, sigma_0^2 * I)
        __a     				=  randn_tensor(lowerCamelCase  ,						generator=lowerCamelCase  ,						device=self.device		) * self.scheduler.init_noise_sigma

        self.scheduler.set_timesteps(lowerCamelCase		)

        for t in self.progress_bar(self.scheduler.timesteps		):
               # here sigma_t == t_i from the paper
               __a     				=  self.scheduler.schedule[t]
               __a     				=  self.scheduler.schedule[t - 1] if t > 0 else 0

               # 1. Select temporarily increased noise level sigma_hat
               # 2. Add new noise to move from sample_i to sample_hat
               __a				, __a     				=  self.scheduler.add_noise_to_input(lowerCamelCase  ,						lowerCamelCase  ,						generator=lowerCamelCase		)

               # 3. Predict the noise residual given the noise magnitude `sigma_hat`
               # The model inputs and output are adjusted by following eq. (213) in [1].
               __a     				=  (sigma_hat / 2) * model((sample_hat + 1) / 2  ,						sigma_hat / 2		).sample

               # 4. Evaluate dx/dt at sigma_hat
               # 5. Take Euler step from sigma to sigma_prev
               __a     				=  self.scheduler.step(lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase		)

               if sigma_prev != 0:
                      # 6. Apply 2nd order correction
                      # The model inputs and output are adjusted by following eq. (213) in [1].
                      __a     				=  (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2  ,						sigma_prev / 2		).sample
                      __a     				=  self.scheduler.step_correct(
                          lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						step_output.prev_sample  ,						step_output["derivative"]  ,						)
               __a     				=  step_output.prev_sample

        __a     				=  (sample / 2 + 0.5).clamp(0  ,						1		)
        __a     				=  sample.cpu().permute(0  ,						2  ,						3  ,						1		).numpy()
        if output_type == "pil":
               __a     				=  self.numpy_to_pil(lowerCamelCase		)

        if not return_dict:
               return (image,)

        return ImagePipelineOutput(images=lowerCamelCase		)
