'''simple docstring'''





from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


_lowerCAmelCase  :List[Any]  				=    logging.get_logger(__name__)

# General docstring
_lowerCAmelCase  :Optional[Any]  				=    """RegNetConfig"""

# Base docstring
_lowerCAmelCase  :int  				=    """facebook/regnet-y-040"""
_lowerCAmelCase  :Any  				=    [1, 1_088, 7, 7]

# Image classification docstring
_lowerCAmelCase  :Optional[Any]  				=    """facebook/regnet-y-040"""
_lowerCAmelCase  :Optional[int]  				=    """tabby, tabby cat"""

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






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







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






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> List[Any]:
									SCREAMING_SNAKE_CASE					:			int    	=       self.convolution(lowercase__ )
									SCREAMING_SNAKE_CASE					:			List[str]    	=       self.normalization(lowercase__ )
									SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.activation(lowercase__ )
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







				def __init__(       self    ,							lowercase__ )							-> str:
									super().__init__()
									SCREAMING_SNAKE_CASE					:			List[Any]    	=       RegNetConvLayer(
									    config.num_channels    ,							config.embedding_size    ,							kernel_size=3    ,							stride=2    ,							activation=config.hidden_act )
									SCREAMING_SNAKE_CASE					:			List[Any]    	=       config.num_channels






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> str:
									SCREAMING_SNAKE_CASE					:			Dict    	=       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.' )
									SCREAMING_SNAKE_CASE					:			str    	=       self.embedder(lowercase__ )
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







				def __init__(       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 2 )							-> Optional[int]:
									super().__init__()
									SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       nn.Convad(lowercase__    ,							lowercase__    ,							kernel_size=1    ,							stride=lowercase__    ,							bias=lowercase__ )
									SCREAMING_SNAKE_CASE					:			Dict    	=       nn.BatchNormad(lowercase__ )






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Tensor:
									SCREAMING_SNAKE_CASE					:			int    	=       self.convolution(lowercase__ )
									SCREAMING_SNAKE_CASE					:			int    	=       self.normalization(lowercase__ )
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







				def __init__(       self    ,							lowercase__    ,							lowercase__ )							-> List[str]:
									super().__init__()

									SCREAMING_SNAKE_CASE					:			Any    	=       nn.AdaptiveAvgPoolad((1, 1) )
									SCREAMING_SNAKE_CASE					:			Tuple    	=       nn.Sequential(
									    nn.Convad(lowercase__    ,							lowercase__    ,							kernel_size=1 )    ,							nn.ReLU()    ,							nn.Convad(lowercase__    ,							lowercase__    ,							kernel_size=1 )    ,							nn.Sigmoid()    ,							)






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Dict:
									# b c h w -> b c 1 1
									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.pooler(lowercase__ )
									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.attention(lowercase__ )
									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       hidden_state * attention
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







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






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Any:
									SCREAMING_SNAKE_CASE					:			Tuple    	=       hidden_state
									SCREAMING_SNAKE_CASE					:			str    	=       self.layer(lowercase__ )
									SCREAMING_SNAKE_CASE					:			int    	=       self.shortcut(lowercase__ )
									hidden_state += residual
									SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self.activation(lowercase__ )
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







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






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Union[str, Any]:
									SCREAMING_SNAKE_CASE					:			int    	=       hidden_state
									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.layer(lowercase__ )
									SCREAMING_SNAKE_CASE					:			Tuple    	=       self.shortcut(lowercase__ )
									hidden_state += residual
									SCREAMING_SNAKE_CASE					:			int    	=       self.activation(lowercase__ )
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







				def __init__(       self    ,							lowercase__    ,							lowercase__    ,							lowercase__    ,							lowercase__ = 2    ,							lowercase__ = 2    ,							)							-> Any:
									super().__init__()

									SCREAMING_SNAKE_CASE					:			Tuple    	=       RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

									SCREAMING_SNAKE_CASE					:			str    	=       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__ )							-> List[Any]:
									SCREAMING_SNAKE_CASE					:			Tuple    	=       self.layers(lowercase__ )
									return hidden_state






class 				UpperCAmelCase (       nn.Module ):




				'''simple docstring'''







				def __init__(       self    ,							lowercase__ )							-> Dict:
									super().__init__()
									SCREAMING_SNAKE_CASE					:			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]    ,							) )
									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       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:
									SCREAMING_SNAKE_CASE					:			str    	=       () if output_hidden_states else None

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

														SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       stage_module(lowercase__ )

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




				'''simple docstring'''







				snake_case__							:   Any							=						RegNetConfig
				snake_case__							:   List[str]							=						"regnet"
				snake_case__							:   List[Any]							=						"pixel_values"
				snake_case__							:   Optional[Any]							=						True






				def       _UpperCamelCase    (       self    ,							lowercase__ )							-> Any:
									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 )							-> Tuple:
									if isinstance(lowercase__    ,							lowercase__ ):
														SCREAMING_SNAKE_CASE					:			Tuple    	=       value






_lowerCAmelCase  :Optional[Any]  				=    R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

_lowerCAmelCase  :Optional[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."   ,    _SCREAMING_SNAKE_CASE   ,    )
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




				'''simple docstring'''







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

									SCREAMING_SNAKE_CASE					:			List[str]    	=       self.embedder(lowercase__ )

									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.encoder(
									    lowercase__    ,							output_hidden_states=lowercase__    ,							return_dict=lowercase__ )

									SCREAMING_SNAKE_CASE					:			Dict    	=       encoder_outputs[0]

									SCREAMING_SNAKE_CASE					:			Dict    	=       self.pooler(lowercase__ )

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

									return BaseModelOutputWithPoolingAndNoAttention(
									    last_hidden_state=lowercase__    ,							pooler_output=lowercase__    ,							hidden_states=encoder_outputs.hidden_states    ,							)






@add_start_docstrings(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "   ,    _SCREAMING_SNAKE_CASE   ,    )
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 				UpperCAmelCase (       _SCREAMING_SNAKE_CASE ):




				'''simple docstring'''







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

									SCREAMING_SNAKE_CASE					:			Optional[int]    	=       self.regnet(lowercase__    ,							output_hidden_states=lowercase__    ,							return_dict=lowercase__ )

									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       outputs.pooler_output if return_dict else outputs[1]

									SCREAMING_SNAKE_CASE					:			List[Any]    	=       self.classifier(lowercase__ )

									SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       None

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

									if not return_dict:
														SCREAMING_SNAKE_CASE					:			List[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 )




'''simple docstring'''





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


_lowerCAmelCase  :List[Any]  				=    False






class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    pass






@nightly
@require_torch_gpu
class 				UpperCAmelCase (       unittest.TestCase ):




    '''simple docstring'''







    def       _UpperCamelCase    (       self )							-> Any:
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()






    def       _UpperCamelCase    (       self )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			Dict    	=       VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion' )
         # remove text_unet
         pipe.remove_unused_weights()
         pipe.to(lowercase__ )
         pipe.set_progress_bar_config(disable=lowercase__ )

         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       'A painting of a squirrel eating a burger '
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       torch.manual_seed(0 )
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       pipe(
             prompt=lowercase__    ,							generator=lowercase__    ,							guidance_scale=7.5    ,							num_inference_steps=2    ,							output_type='numpy' ).images

         with tempfile.TemporaryDirectory() as tmpdirname:
              pipe.save_pretrained(lowercase__ )
              SCREAMING_SNAKE_CASE					:			int    	=       VersatileDiffusionTextToImagePipeline.from_pretrained(lowercase__ )
         pipe.to(lowercase__ )
         pipe.set_progress_bar_config(disable=lowercase__ )

         SCREAMING_SNAKE_CASE					:			List[str]    	=       generator.manual_seed(0 )
         SCREAMING_SNAKE_CASE					:			List[str]    	=       pipe(
             prompt=lowercase__    ,							generator=lowercase__    ,							guidance_scale=7.5    ,							num_inference_steps=2    ,							output_type='numpy' ).images

         assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass"






    def       _UpperCamelCase    (       self )							-> Any:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       VersatileDiffusionTextToImagePipeline.from_pretrained(
             'shi-labs/versatile-diffusion'    ,							torch_dtype=torch.floataa )
         pipe.to(lowercase__ )
         pipe.set_progress_bar_config(disable=lowercase__ )

         SCREAMING_SNAKE_CASE					:			int    	=       'A painting of a squirrel eating a burger '
         SCREAMING_SNAKE_CASE					:			Tuple    	=       torch.manual_seed(0 )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       pipe(
             prompt=lowercase__    ,							generator=lowercase__    ,							guidance_scale=7.5    ,							num_inference_steps=50    ,							output_type='numpy' ).images

         SCREAMING_SNAKE_CASE					:			Dict    	=       image[0, 253:256, 253:256, -1]

         assert image.shape == (1, 512, 512, 3)
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8] )

         assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2



