


'''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__					(					a_      ,  unittest.TestCase   ):





			'''simple docstring'''

			A						:				List[str]					      =			KandinskyImgaImgPipeline
			A						:				List[str]					      =			['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''']
			A						:				Dict					      =			[
			    '''prompt''',
			    '''negative_prompt''',
			    '''image_embeds''',
			    '''negative_image_embeds''',
			    '''image''',
			]
			A						:				Tuple					      =			[
			    '''generator''',
			    '''height''',
			    '''width''',
			    '''strength''',
			    '''guidance_scale''',
			    '''negative_prompt''',
			    '''num_inference_steps''',
			    '''return_dict''',
			    '''guidance_scale''',
			    '''num_images_per_prompt''',
			    '''output_type''',
			    '''return_dict''',
			]
			A						:				Optional[Any]					      =			False
			@property
			def 					lowerCAmelCase     (  self							:			List[str]	)      ->		List[str]:
						return 32
			@property
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		Optional[int]:
						return 32
			@property
			def 					lowerCAmelCase     (  self							:			Any	)      ->		Union[str, Any]:
						return self.time_input_dim
			@property
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		List[str]:
						return self.time_input_dim * 4
			@property
			def 					lowerCAmelCase     (  self							:			Tuple	)      ->		Optional[int]:
						return 100
			@property
			def 					lowerCAmelCase     (  self							:			List[Any]	)      ->		Tuple:
						__A=	XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base'	)
						return tokenizer
			@property
			def 					lowerCAmelCase     (  self							:			Optional[int]	)      ->		Union[str, Any]:
						torch.manual_seed(0	)
						__A=	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=1_005			,    )

						__A=	MultilingualCLIP(lowerCAmelCase_	)
						__A=	text_encoder.eval()

						return text_encoder
			@property
			def 					lowerCAmelCase     (  self							:			Dict	)      ->		Optional[Any]:
						torch.manual_seed(0	)

						__A=	{
						    '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,
						}

						__A=	UNetaDConditionModel(**lowerCAmelCase_	)
						return model
			@property
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		Optional[Any]:
						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 					lowerCAmelCase     (  self							:			Tuple	)      ->		Optional[Any]:
						torch.manual_seed(0	)
						__A=	VQModel(**self.dummy_movq_kwargs	)
						return model
			def 					lowerCAmelCase     (  self							:			List[Any]	)      ->		Optional[Any]:
						__A=	self.dummy_text_encoder
						__A=	self.dummy_tokenizer
						__A=	self.dummy_unet
						__A=	self.dummy_movq

						__A=	{
						    'num_train_timesteps': 1_000,
						    'beta_schedule': 'linear',
						    'beta_start': 0.0_00_85,
						    'beta_end': 0.0_12,
						    'clip_sample': False,
						    'set_alpha_to_one': False,
						    'steps_offset': 0,
						    'prediction_type': 'epsilon',
						    'thresholding': False,
						}

						__A=	DDIMScheduler(**lowerCAmelCase_	)

						__A=	{
						    'text_encoder': text_encoder,
						    'tokenizer': tokenizer,
						    'unet': unet,
						    'scheduler': scheduler,
						    'movq': movq,
						}

						return components
			def 					lowerCAmelCase     (  self							:			List[str]			,    lowerCAmelCase_							:			int			,    lowerCAmelCase_							:			Any=0	)      ->		Union[str, Any]:
						__A=	floats_tensor((1, self.cross_attention_dim)			,    rng=random.Random(lowerCAmelCase_	)	).to(lowerCAmelCase_	)
						__A=	floats_tensor((1, self.cross_attention_dim)			,    rng=random.Random(seed + 1	)	).to(lowerCAmelCase_	)
						# create init_image
						__A=	floats_tensor((1, 3, 64, 64)			,    rng=random.Random(lowerCAmelCase_	)	).to(lowerCAmelCase_	)
						__A=	image.cpu().permute(0			,    2			,    3			,    1	)[0]
						__A=	Image.fromarray(np.uinta(lowerCAmelCase_	)	).convert('RGB'	).resize((256, 256)	)

						if str(lowerCAmelCase_	).startswith('mps'	):
									__A=	torch.manual_seed(lowerCAmelCase_	)
						else:
									__A=	torch.Generator(device=lowerCAmelCase_	).manual_seed(lowerCAmelCase_	)
						__A=	{
						    '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 					lowerCAmelCase     (  self							:			Tuple	)      ->		Union[str, Any]:
						__A=	'cpu'

						__A=	self.get_dummy_components()

						__A=	self.pipeline_class(**lowerCAmelCase_	)
						__A=	pipe.to(lowerCAmelCase_	)

						pipe.set_progress_bar_config(disable=lowerCAmelCase_	)

						__A=	pipe(**self.get_dummy_inputs(lowerCAmelCase_	)	)
						__A=	output.images

						__A=	pipe(
						    **self.get_dummy_inputs(lowerCAmelCase_	)			,    return_dict=lowerCAmelCase_			,    )[0]

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

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

						__A=	np.array(
						    [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33]	)
						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 					lowerCAmelCase     (  self							:			Dict	)      ->		Any:
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()

			def 					lowerCAmelCase     (  self							:			Optional[int]	)      ->		Union[str, Any]:
						__A=	load_numpy(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
						    '/kandinsky/kandinsky_img2img_frog.npy'	)

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

						__A=	KandinskyPriorPipeline.from_pretrained(
						    'kandinsky-community/kandinsky-2-1-prior'			,    torch_dtype=torch.floataa	)
						pipe_prior.to(lowerCAmelCase_	)

						__A=	KandinskyImgaImgPipeline.from_pretrained(
						    'kandinsky-community/kandinsky-2-1'			,    torch_dtype=torch.floataa	)
						__A=	pipeline.to(lowerCAmelCase_	)

						pipeline.set_progress_bar_config(disable=lowerCAmelCase_	)

						__A=	torch.Generator(device='cpu'	).manual_seed(0	)
						__A,	__A=	pipe_prior(
						    lowerCAmelCase_			,    generator=lowerCAmelCase_			,    num_inference_steps=5			,    negative_prompt=''			,    ).to_tuple()

						__A=	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'			,    )

						__A=	output.images[0]

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

						assert_mean_pixel_difference(lowerCAmelCase_			,    lowerCAmelCase_	)







'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
					from transformers import (
					    AutoConfig,
					    BertConfig,
					    GPTaConfig,
					    TaConfig,
					    TFAutoModel,
					    TFAutoModelForCausalLM,
					    TFAutoModelForMaskedLM,
					    TFAutoModelForPreTraining,
					    TFAutoModelForQuestionAnswering,
					    TFAutoModelForSeqaSeqLM,
					    TFAutoModelForSequenceClassification,
					    TFAutoModelWithLMHead,
					    TFBertForMaskedLM,
					    TFBertForPreTraining,
					    TFBertForQuestionAnswering,
					    TFBertForSequenceClassification,
					    TFBertModel,
					    TFGPTaLMHeadModel,
					    TFRobertaForMaskedLM,
					    TFTaForConditionalGeneration,
					)
					from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
					from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
					from transformers import (
					    AutoModel,
					    AutoModelForCausalLM,
					    AutoModelForMaskedLM,
					    AutoModelForPreTraining,
					    AutoModelForQuestionAnswering,
					    AutoModelForSeqaSeqLM,
					    AutoModelForSequenceClassification,
					    AutoModelWithLMHead,
					    BertForMaskedLM,
					    BertForPreTraining,
					    BertForQuestionAnswering,
					    BertForSequenceClassification,
					    BertModel,
					    GPTaLMHeadModel,
					    RobertaForMaskedLM,
					    TaForConditionalGeneration,
					)
@is_pt_tf_cross_test
class 						a__					(					unittest.TestCase   ):





			'''simple docstring'''

			@slow
			def 					lowerCAmelCase     (  self							:			List[str]	)      ->		List[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModel.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModel.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Tuple	)      ->		Optional[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForPreTraining.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			int	)      ->		Optional[int]:
						for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									__A,	__A=	TFAutoModelForCausalLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForCausalLM.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									__A,	__A=	AutoModelForCausalLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		Optional[Any]:
						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		Optional[int]:
						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									__A,	__A=	TFAutoModelForMaskedLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForMaskedLM.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									__A,	__A=	AutoModelForMaskedLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Tuple	)      ->		Dict:
						for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									__A,	__A=	TFAutoModelForSeqaSeqLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									__A,	__A=	AutoModelForSeqaSeqLM.from_pretrained(
									    lowerCAmelCase_			,    output_loading_info=lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		str:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			@slow
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		List[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
									__A=	AutoConfig.from_pretrained(lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)

									__A=	AutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
									self.assertIsNotNone(lowerCAmelCase_	)
									self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
			def 					lowerCAmelCase     (  self							:			int	)      ->		List[str]:
						__A=	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)

						__A=	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)

			def 					lowerCAmelCase     (  self							:			Optional[Any]	)      ->		Tuple:
						__A=	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_pt=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)

						__A=	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_			,    from_tf=lowerCAmelCase_	)
						self.assertIsInstance(lowerCAmelCase_			,    lowerCAmelCase_	)
						self.assertEqual(model.num_parameters()			,    14_410	)
						self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_	)			,    14_410	)



