



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


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

# General docstring
A_						:    str				=      'RegNetConfig'

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

# Image classification docstring
A_						:    str				=      'facebook/regnet-y-040'
A_						:    int				=      'tabby, tabby cat'

A_						:    List[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				:       int     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int = 3     ,       _SCREAMING_SNAKE_CASE				:       int = 1     ,       _SCREAMING_SNAKE_CASE				:       int = 1     ,       _SCREAMING_SNAKE_CASE				:       Optional[str] = "relu"     ,       )						->	Dict:





					"""simple docstring"""


					super().__init__()
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     nn.Convad(
					    _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       kernel_size=_SCREAMING_SNAKE_CASE     ,       stride=_SCREAMING_SNAKE_CASE     ,       padding=kernel_size // 2     ,       groups=_SCREAMING_SNAKE_CASE     ,       bias=_SCREAMING_SNAKE_CASE     ,       )
					SCREAMING_SNAKE_CASE						:							int							=     nn.BatchNormad(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Optional[int]							=     ACTaFN[activation] if activation is not None else nn.Identity()







	def 			_lowerCAmelCase     (      self				:       str     ,       _SCREAMING_SNAKE_CASE				:       Any    )						->	List[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[str]							=     self.convolution(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Dict							=     self.normalization(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Any							=     self.activation(_SCREAMING_SNAKE_CASE    )
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       List[Any]     ,       _SCREAMING_SNAKE_CASE				:       RegNetConfig    )						->	Any:





					"""simple docstring"""


					super().__init__()
					SCREAMING_SNAKE_CASE						:							Dict							=     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 			_lowerCAmelCase     (      self				:       int     ,       _SCREAMING_SNAKE_CASE				:       Tuple    )						->	Tuple:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							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.'    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.embedder(_SCREAMING_SNAKE_CASE    )
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       Dict     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int = 2    )						->	int:





					"""simple docstring"""


					super().__init__()
					SCREAMING_SNAKE_CASE						:							List[Any]							=     nn.Convad(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       kernel_size=1     ,       stride=_SCREAMING_SNAKE_CASE     ,       bias=_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     nn.BatchNormad(_SCREAMING_SNAKE_CASE    )







	def 			_lowerCAmelCase     (      self				:       str     ,       _SCREAMING_SNAKE_CASE				:       Tensor    )						->	Tensor:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							int							=     self.convolution(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     self.normalization(_SCREAMING_SNAKE_CASE    )
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       str     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int    )						->	Union[str, Any]:





					"""simple docstring"""


					super().__init__()

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







	def 			_lowerCAmelCase     (      self				:       Optional[int]     ,       _SCREAMING_SNAKE_CASE				:       str    )						->	Optional[Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.pooler(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.attention(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Any							=     hidden_state * attention
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       List[Any]     ,       _SCREAMING_SNAKE_CASE				:       RegNetConfig     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int = 1    )						->	int:





					"""simple docstring"""


					super().__init__()
					SCREAMING_SNAKE_CASE						:							Tuple							=     in_channels != out_channels or stride != 1
					SCREAMING_SNAKE_CASE						:							int							=     max(1     ,       out_channels // config.groups_width    )
					SCREAMING_SNAKE_CASE						:							Tuple							=     (
					    RegNetShortCut(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       stride=_SCREAMING_SNAKE_CASE    ) if should_apply_shortcut else nn.Identity()
					)
					SCREAMING_SNAKE_CASE						:							Dict							=     nn.Sequential(
					    RegNetConvLayer(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       kernel_size=1     ,       activation=config.hidden_act    )     ,       RegNetConvLayer(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       stride=_SCREAMING_SNAKE_CASE     ,       groups=_SCREAMING_SNAKE_CASE     ,       activation=config.hidden_act    )     ,       RegNetConvLayer(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       kernel_size=1     ,       activation=_SCREAMING_SNAKE_CASE    )     ,       )
					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     ACTaFN[config.hidden_act]







	def 			_lowerCAmelCase     (      self				:       Dict     ,       _SCREAMING_SNAKE_CASE				:       Union[str, Any]    )						->	Dict:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Dict							=     hidden_state
					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.layer(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.shortcut(_SCREAMING_SNAKE_CASE    )
					hidden_state += residual
					SCREAMING_SNAKE_CASE						:							Any							=     self.activation(_SCREAMING_SNAKE_CASE    )
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       Tuple     ,       _SCREAMING_SNAKE_CASE				:       RegNetConfig     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int = 1    )						->	str:





					"""simple docstring"""


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







	def 			_lowerCAmelCase     (      self				:       str     ,       _SCREAMING_SNAKE_CASE				:       Any    )						->	List[str]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     hidden_state
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     self.layer(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[str]							=     self.shortcut(_SCREAMING_SNAKE_CASE    )
					hidden_state += residual
					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.activation(_SCREAMING_SNAKE_CASE    )
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       Tuple     ,       _SCREAMING_SNAKE_CASE				:       RegNetConfig     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int     ,       _SCREAMING_SNAKE_CASE				:       int = 2     ,       _SCREAMING_SNAKE_CASE				:       int = 2     ,       )						->	Optional[int]:





					"""simple docstring"""


					super().__init__()

					SCREAMING_SNAKE_CASE						:							Optional[int]							=     RegNetXLayer if config.layer_type == 'x' else RegNetYLayer

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







	def 			_lowerCAmelCase     (      self				:       Optional[int]     ,       _SCREAMING_SNAKE_CASE				:       Union[str, Any]    )						->	Union[str, Any]:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.layers(_SCREAMING_SNAKE_CASE    )
					return hidden_state




class     lowerCAmelCase__						(	nn.Module ):


	'''simple docstring'''







	def __init__(      self				:       List[str]     ,       _SCREAMING_SNAKE_CASE				:       RegNetConfig    )						->	Union[str, Any]:





					"""simple docstring"""


					super().__init__()
					SCREAMING_SNAKE_CASE						:							Tuple							=     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(
					        _SCREAMING_SNAKE_CASE     ,       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(_SCREAMING_SNAKE_CASE     ,       config.depths[1:]    ):
									self.stages.append(RegNetStage(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       depth=_SCREAMING_SNAKE_CASE    )    )







	def 			_lowerCAmelCase     (      self				:       Union[str, Any]     ,       _SCREAMING_SNAKE_CASE				:       Tensor     ,       _SCREAMING_SNAKE_CASE				:       bool = False     ,       _SCREAMING_SNAKE_CASE				:       bool = True    )						->	BaseModelOutputWithNoAttention:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[Any]							=     () 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						:							Optional[int]							=     stage_module(_SCREAMING_SNAKE_CASE    )

					if output_hidden_states:
									SCREAMING_SNAKE_CASE						:							List[str]							=     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=_SCREAMING_SNAKE_CASE     ,       hidden_states=_SCREAMING_SNAKE_CASE    )




class     lowerCAmelCase__						(	_lowerCamelCase ):


	'''simple docstring'''







	_SCREAMING_SNAKE_CASE	:		Optional[Any]  						= RegNetConfig
	_SCREAMING_SNAKE_CASE	:		str  						= '''regnet'''
	_SCREAMING_SNAKE_CASE	:		Dict  						= '''pixel_values'''
	_SCREAMING_SNAKE_CASE	:		Tuple  						= True





	def 			_lowerCAmelCase     (      self				:       Union[str, Any]     ,       _SCREAMING_SNAKE_CASE				:       List[Any]    )						->	str:





					"""simple docstring"""


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







	def 			_lowerCAmelCase     (      self				:       Tuple     ,       _SCREAMING_SNAKE_CASE				:       str     ,       _SCREAMING_SNAKE_CASE				:       Any=False    )						->	Any:





					"""simple docstring"""


					if isinstance(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    ):
									SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     value




A_						:    int				=      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_						:    List[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.'''							,							_lowerCamelCase							,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class     lowerCAmelCase__						(	_lowerCamelCase ):


	'''simple docstring'''







	def __init__(      self				:       Union[str, Any]     ,       _SCREAMING_SNAKE_CASE				:       str    )						->	List[str]:





					"""simple docstring"""


					super().__init__(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							List[str]							=     config
					SCREAMING_SNAKE_CASE						:							Tuple							=     RegNetEmbeddings(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Optional[Any]							=     RegNetEncoder(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							str							=     nn.AdaptiveAvgPoolad((1, 1)    )
					# Initialize weights and apply final processing
					self.post_init()







	@add_start_docstrings_to_model_forward(_SCREAMING_SNAKE_CASE    )
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC     ,       output_type=_SCREAMING_SNAKE_CASE     ,       config_class=_CONFIG_FOR_DOC     ,       modality='vision'     ,       expected_output=_EXPECTED_OUTPUT_SHAPE     ,       )
	def 			_lowerCAmelCase     (      self				:       List[Any]     ,       _SCREAMING_SNAKE_CASE				:       Tensor     ,       _SCREAMING_SNAKE_CASE				:       Optional[bool] = None     ,       _SCREAMING_SNAKE_CASE				:       Optional[bool] = None    )						->	BaseModelOutputWithPoolingAndNoAttention:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							List[Any]							=     (
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					SCREAMING_SNAKE_CASE						:							Dict							=     return_dict if return_dict is not None else self.config.use_return_dict

					SCREAMING_SNAKE_CASE						:							Dict							=     self.embedder(_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							str							=     self.encoder(
					    _SCREAMING_SNAKE_CASE     ,       output_hidden_states=_SCREAMING_SNAKE_CASE     ,       return_dict=_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     encoder_outputs[0]

					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.pooler(_SCREAMING_SNAKE_CASE    )

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

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


	'''simple docstring'''







	def __init__(      self				:       Optional[int]     ,       _SCREAMING_SNAKE_CASE				:       Any    )						->	Optional[int]:





					"""simple docstring"""


					super().__init__(_SCREAMING_SNAKE_CASE    )
					SCREAMING_SNAKE_CASE						:							Union[str, Any]							=     config.num_labels
					SCREAMING_SNAKE_CASE						:							Dict							=     RegNetModel(_SCREAMING_SNAKE_CASE    )
					# classification head
					SCREAMING_SNAKE_CASE						:							Any							=     nn.Sequential(
					    nn.Flatten()     ,       nn.Linear(config.hidden_sizes[-1]     ,       config.num_labels    ) if config.num_labels > 0 else nn.Identity()     ,       )
					# initialize weights and apply final processing
					self.post_init()







	@add_start_docstrings_to_model_forward(_SCREAMING_SNAKE_CASE    )
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT     ,       output_type=_SCREAMING_SNAKE_CASE     ,       config_class=_CONFIG_FOR_DOC     ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT     ,       )
	def 			_lowerCAmelCase     (      self				:       List[Any]     ,       _SCREAMING_SNAKE_CASE				:       Optional[torch.FloatTensor] = None     ,       _SCREAMING_SNAKE_CASE				:       Optional[torch.LongTensor] = None     ,       _SCREAMING_SNAKE_CASE				:       Optional[bool] = None     ,       _SCREAMING_SNAKE_CASE				:       Optional[bool] = None     ,       )						->	ImageClassifierOutputWithNoAttention:





					"""simple docstring"""


					SCREAMING_SNAKE_CASE						:							str							=     return_dict if return_dict is not None else self.config.use_return_dict

					SCREAMING_SNAKE_CASE						:							List[Any]							=     self.regnet(_SCREAMING_SNAKE_CASE     ,       output_hidden_states=_SCREAMING_SNAKE_CASE     ,       return_dict=_SCREAMING_SNAKE_CASE    )

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

					SCREAMING_SNAKE_CASE						:							Optional[int]							=     self.classifier(_SCREAMING_SNAKE_CASE    )

					SCREAMING_SNAKE_CASE						:							Optional[int]							=     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						:							str							=     'single_label_classification'
													else:
																	SCREAMING_SNAKE_CASE						:							Optional[int]							=     'multi_label_classification'
									if self.config.problem_type == "regression":
													SCREAMING_SNAKE_CASE						:							List[str]							=     MSELoss()
													if self.num_labels == 1:
																	SCREAMING_SNAKE_CASE						:							List[Any]							=     loss_fct(logits.squeeze()     ,       labels.squeeze()    )
													else:
																	SCREAMING_SNAKE_CASE						:							Dict							=     loss_fct(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )
									elif self.config.problem_type == "single_label_classification":
													SCREAMING_SNAKE_CASE						:							Optional[int]							=     CrossEntropyLoss()
													SCREAMING_SNAKE_CASE						:							Dict							=     loss_fct(logits.view(-1     ,       self.num_labels    )     ,       labels.view(-1    )    )
									elif self.config.problem_type == "multi_label_classification":
													SCREAMING_SNAKE_CASE						:							List[str]							=     BCEWithLogitsLoss()
													SCREAMING_SNAKE_CASE						:							List[Any]							=     loss_fct(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE    )

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

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










"""simple docstring"""

import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


A_						:    List[Any]				=      argparse.ArgumentParser('Stable Diffusion script with intel optimization', add_help=False)
parser.add_argument('--dpm', action='store_true', help='Enable DPMSolver or not')
parser.add_argument('--steps', default=None, type=int, help='Num inference steps')
A_						:    Tuple				=      parser.parse_args()


A_						:    Optional[int]				=      'cpu'
A_						:    Dict				=      'a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'

A_						:    List[str]				=      'path-to-your-trained-model'
A_						:    Any				=      StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
							A_						:    Tuple				=      DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
A_						:    Tuple				=      pipe.to(device)

# to channels last
A_						:    List[str]				=      pipe.unet.to(memory_format=torch.channels_last)
A_						:    List[str]				=      pipe.vae.to(memory_format=torch.channels_last)
A_						:    Optional[int]				=      pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
							A_						:    Union[str, Any]				=      pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
A_						:    List[Any]				=      torch.randn(2, 4, 64, 64)
A_						:    int				=      torch.rand(1) * 999
A_						:    int				=      torch.randn(2, 77, 768)
A_						:    Tuple				=      (sample, timestep, encoder_hidden_status)
try:
							A_						:    Any				=      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
							A_						:    List[str]				=      ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
A_						:    str				=      ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
A_						:    Optional[int]				=      ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
							A_						:    List[str]				=      ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
A_						:    Union[str, Any]				=      666
A_						:    Optional[Any]				=      torch.Generator(device).manual_seed(seed)
A_						:    Union[str, Any]				=      {'generator': generator}
if args.steps is not None:
							A_						:    int				=      args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
							A_						:    int				=      pipe(prompt, **generate_kwargs).images[0]

# save image
image.save('generated.png')





