

'''simple docstring'''





from typing import Optional

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

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


UpperCamelCase_    						=       logging.get_logger(__name__)

# General docstring
UpperCamelCase_    						=       """RegNetConfig"""

# Base docstring
UpperCamelCase_    						=       """facebook/regnet-y-040"""
UpperCamelCase_    						=       [1, 1088, 7, 7]

# Image classification docstring
UpperCamelCase_    						=       """facebook/regnet-y-040"""
UpperCamelCase_    						=       """tabby, tabby cat"""

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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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







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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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







				def  lowerCamelCase_	(       self     :		Union[str, Any]		,					UpperCAmelCase__     :		List[str]   ):
										'''simple docstring'''
										lowercase :							Optional[Any]    		=pixel_values.shape[1]
										if num_channels != self.num_channels:
																raise ValueError(
																    '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.'''   )
										lowercase :							List[Any]    		=self.embedder(UpperCAmelCase__   )
										return hidden_state





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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







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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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

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







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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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







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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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







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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







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

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

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







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





class 		__SCREAMING_SNAKE_CASE							(			nn.Module      ):







				def __init__(       self     :		int		,					UpperCAmelCase__     :		RegNetConfig   ):
										'''simple docstring'''
										super().__init__()
										lowercase :							str    		=nn.ModuleList([]   )
										# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
										self.stages.append(
										    RegNetStage(
										        UpperCAmelCase__		,					config.embedding_size		,					config.hidden_sizes[0]		,					stride=2 if config.downsample_in_first_stage else 1		,					depth=config.depths[0]		,					)   )
										lowercase :							List[str]    		=zip(config.hidden_sizes		,					config.hidden_sizes[1:]   )
										for (in_channels, out_channels), depth in zip(UpperCAmelCase__		,					config.depths[1:]   ):
																self.stages.append(RegNetStage(UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__		,					depth=UpperCAmelCase__   )   )







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

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

																lowercase :							int    		=stage_module(UpperCAmelCase__   )

										if output_hidden_states:
																lowercase :							Tuple    		=hidden_states + (hidden_state,)

										if not return_dict:
																return tuple(v for v in [hidden_state, hidden_states] if v is not None   )

										return BaseModelOutputWithNoAttention(last_hidden_state=UpperCAmelCase__		,					hidden_states=UpperCAmelCase__   )





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







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







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		Optional[int]=False   ):
										'''simple docstring'''
										if isinstance(UpperCAmelCase__		,					UpperCAmelCase__   ):
																lowercase :							int    		=value



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

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

UpperCamelCase_    						=       r"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.
"""





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







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







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

										lowercase :							Tuple    		=self.embedder(UpperCAmelCase__   )

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

										lowercase :							Optional[int]    		=encoder_outputs[0]

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

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

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





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







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







				@add_start_docstrings_to_model_forward(UpperCAmelCase__   )
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT		,					output_type=UpperCAmelCase__		,					config_class=_CONFIG_FOR_DOC		,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,					)
				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		Optional[torch.FloatTensor] = None		,					UpperCAmelCase__     :		Optional[torch.LongTensor] = None		,					UpperCAmelCase__     :		Optional[bool] = None		,					UpperCAmelCase__     :		Optional[bool] = None		,					):
										'''simple docstring'''
										lowercase :							Dict    		=return_dict if return_dict is not None else self.config.use_return_dict

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

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

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

										lowercase :							Any    		=None

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

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

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






'''simple docstring'''




import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()






class 							a__(   lowerCAmelCase__					,		unittest.TestCase      ):


			'''simple docstring'''






			UpperCAmelCase_     :	List[str]						=			KandinskyImgaImgPipeline
			UpperCAmelCase_     :	Union[str, Any]						=			['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''']
			UpperCAmelCase_     :	List[str]						=			[
			    '''prompt''',
			    '''negative_prompt''',
			    '''image_embeds''',
			    '''negative_image_embeds''',
			    '''image''',
			]
			UpperCAmelCase_     :	Dict						=			[
			    '''generator''',
			    '''height''',
			    '''width''',
			    '''strength''',
			    '''guidance_scale''',
			    '''negative_prompt''',
			    '''num_inference_steps''',
			    '''return_dict''',
			    '''guidance_scale''',
			    '''num_images_per_prompt''',
			    '''output_type''',
			    '''return_dict''',
			]
			UpperCAmelCase_     :	str						=			False



			@property
			def 				a_			(  self):




					"""simple docstring"""


					return 32



			@property
			def 				a_			(  self):




					"""simple docstring"""


					return 32



			@property
			def 				a_			(  self):




					"""simple docstring"""


					return self.time_input_dim



			@property
			def 				a_			(  self):




					"""simple docstring"""


					return self.time_input_dim * 4



			@property
			def 				a_			(  self):




					"""simple docstring"""


					return 100



			@property
			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base""")
					return tokenizer



			@property
			def 				a_			(  self):




					"""simple docstring"""


					torch.manual_seed(0)
					lowerCAmelCase     =    MCLIPConfig(
					    numDims=self.cross_attention_dim			,       transformerDimensions=self.text_embedder_hidden_size			,       hidden_size=self.text_embedder_hidden_size			,       intermediate_size=37			,       num_attention_heads=4			,       num_hidden_layers=5			,       vocab_size=1005			,       )

					lowerCAmelCase     =    MultilingualCLIP(__lowerCAmelCase)
					lowerCAmelCase     =    text_encoder.eval()

					return text_encoder



			@property
			def 				a_			(  self):




					"""simple docstring"""


					torch.manual_seed(0)

					lowerCAmelCase     =    {
					    """in_channels""": 4,
					    # Out channels is double in channels because predicts mean and variance
					    """out_channels""": 8,
					    """addition_embed_type""": """text_image""",
					    """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
					    """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
					    """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
					    """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
					    """layers_per_block""": 1,
					    """encoder_hid_dim""": self.text_embedder_hidden_size,
					    """encoder_hid_dim_type""": """text_image_proj""",
					    """cross_attention_dim""": self.cross_attention_dim,
					    """attention_head_dim""": 4,
					    """resnet_time_scale_shift""": """scale_shift""",
					    """class_embed_type""": None,
					}

					lowerCAmelCase     =    UNetaDConditionModel(**__lowerCAmelCase)
					return model



			@property
			def 				a_			(  self):




					"""simple docstring"""


					return {
					    "block_out_channels": [32, 64],
					    "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
					    "in_channels": 3,
					    "latent_channels": 4,
					    "layers_per_block": 1,
					    "norm_num_groups": 8,
					    "norm_type": "spatial",
					    "num_vq_embeddings": 12,
					    "out_channels": 3,
					    "up_block_types": [
					        "AttnUpDecoderBlock2D",
					        "UpDecoderBlock2D",
					    ],
					    "vq_embed_dim": 4,
					}



			@property
			def 				a_			(  self):




					"""simple docstring"""


					torch.manual_seed(0)
					lowerCAmelCase     =    VQModel(**self.dummy_movq_kwargs)
					return model



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.dummy_text_encoder
					lowerCAmelCase     =    self.dummy_tokenizer
					lowerCAmelCase     =    self.dummy_unet
					lowerCAmelCase     =    self.dummy_movq

					lowerCAmelCase     =    {
					    """num_train_timesteps""": 1000,
					    """beta_schedule""": """linear""",
					    """beta_start""": 0.00085,
					    """beta_end""": 0.012,
					    """clip_sample""": False,
					    """set_alpha_to_one""": False,
					    """steps_offset""": 0,
					    """prediction_type""": """epsilon""",
					    """thresholding""": False,
					}

					lowerCAmelCase     =    DDIMScheduler(**__lowerCAmelCase)

					lowerCAmelCase     =    {
					    """text_encoder""": text_encoder,
					    """tokenizer""": tokenizer,
					    """unet""": unet,
					    """scheduler""": scheduler,
					    """movq""": movq,
					}

					return components



			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase=0):




					"""simple docstring"""


					lowerCAmelCase     =    floats_tensor((1, self.cross_attention_dim)			,       rng=random.Random(__lowerCAmelCase)).to(__lowerCAmelCase)
					lowerCAmelCase     =    floats_tensor((1, self.cross_attention_dim)			,       rng=random.Random(seed + 1)).to(__lowerCAmelCase)
					# create init_image
					lowerCAmelCase     =    floats_tensor((1, 3, 64, 64)			,       rng=random.Random(__lowerCAmelCase)).to(__lowerCAmelCase)
					lowerCAmelCase     =    image.cpu().permute(0			,       2			,       3			,       1)[0]
					lowerCAmelCase     =    Image.fromarray(np.uinta(__lowerCAmelCase)).convert("""RGB""").resize((256, 256))

					if str(__lowerCAmelCase).startswith("""mps"""):
							lowerCAmelCase     =    torch.manual_seed(__lowerCAmelCase)
					else:
							lowerCAmelCase     =    torch.Generator(device=__lowerCAmelCase).manual_seed(__lowerCAmelCase)
					lowerCAmelCase     =    {
					    """prompt""": """horse""",
					    """image""": init_image,
					    """image_embeds""": image_embeds,
					    """negative_image_embeds""": negative_image_embeds,
					    """generator""": generator,
					    """height""": 64,
					    """width""": 64,
					    """num_inference_steps""": 10,
					    """guidance_scale""": 7.0,
					    """strength""": 0.2,
					    """output_type""": """np""",
					}
					return inputs







			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    """cpu"""

					lowerCAmelCase     =    self.get_dummy_components()

					lowerCAmelCase     =    self.pipeline_class(**__lowerCAmelCase)
					lowerCAmelCase     =    pipe.to(__lowerCAmelCase)

					pipe.set_progress_bar_config(disable=__lowerCAmelCase)

					lowerCAmelCase     =    pipe(**self.get_dummy_inputs(__lowerCAmelCase))
					lowerCAmelCase     =    output.images

					lowerCAmelCase     =    pipe(
					    **self.get_dummy_inputs(__lowerCAmelCase)			,       return_dict=__lowerCAmelCase			,       )[0]

					lowerCAmelCase     =    image[0, -3:, -3:, -1]
					lowerCAmelCase     =    image_from_tuple[0, -3:, -3:, -1]

					assert image.shape == (1, 64, 64, 3)

					lowerCAmelCase     =    np.array(
					    [0.61474943, 0.6073539, 0.43308544, 0.5928269, 0.47493595, 0.46755973, 0.4613838, 0.45368797, 0.50119233])
					assert (
					    np.abs(image_slice.flatten() - expected_slice).max() < 1E-2
					), f" expected_slice {expected_slice}, but got {image_slice.flatten()}"
					assert (
					    np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 1E-2
					), f" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"







@slow
@require_torch_gpu
class 							a__(   unittest.TestCase      ):


			'''simple docstring'''






			def 				a_			(  self):




					"""simple docstring"""


					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()







			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    load_numpy(
					    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
					    """/kandinsky/kandinsky_img2img_frog.npy""")

					lowerCAmelCase     =    load_image(
					    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png""")
					lowerCAmelCase     =    """A red cartoon frog, 4k"""

					lowerCAmelCase     =    KandinskyPriorPipeline.from_pretrained(
					    """kandinsky-community/kandinsky-2-1-prior"""			,       torch_dtype=torch.floataa)
					pipe_prior.to(__lowerCAmelCase)

					lowerCAmelCase     =    KandinskyImgaImgPipeline.from_pretrained(
					    """kandinsky-community/kandinsky-2-1"""			,       torch_dtype=torch.floataa)
					lowerCAmelCase     =    pipeline.to(__lowerCAmelCase)

					pipeline.set_progress_bar_config(disable=__lowerCAmelCase)

					lowerCAmelCase     =    torch.Generator(device="""cpu""").manual_seed(0)
					lowerCAmelCase   ,	lowerCAmelCase     =    pipe_prior(
					    __lowerCAmelCase			,       generator=__lowerCAmelCase			,       num_inference_steps=5			,       negative_prompt=""""""			,       ).to_tuple()

					lowerCAmelCase     =    pipeline(
					    __lowerCAmelCase			,       image=__lowerCAmelCase			,       image_embeds=__lowerCAmelCase			,       negative_image_embeds=__lowerCAmelCase			,       generator=__lowerCAmelCase			,       num_inference_steps=100			,       height=768			,       width=768			,       strength=0.2			,       output_type="""np"""			,       )

					lowerCAmelCase     =    output.images[0]

					assert image.shape == (768, 768, 3)

					assert_mean_pixel_difference(__lowerCAmelCase			,       __lowerCAmelCase)




