


'''simple docstring'''





import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, 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    ):
		_a			:						Dict    			=			KandinskyPipeline
		_a			:						str    			=			[
		    'prompt',
		    'image_embeds',
		    'negative_image_embeds',
		]
		_a			:						Optional[int]    			=			['prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds']
		_a			:						Union[str, Any]    			=			[
		    'generator',
		    'height',
		    'width',
		    'latents',
		    'guidance_scale',
		    'negative_prompt',
		    'num_inference_steps',
		    'return_dict',
		    'guidance_scale',
		    'num_images_per_prompt',
		    'output_type',
		    'return_dict',
		]
		_a			:						Optional[int]    			=			False
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									return 32
		@property
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									return 32
		@property
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									return self.time_input_dim
		@property
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									return self.time_input_dim * 4
		@property
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									return 100
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									_UpperCAmelCase					=    XLMRobertaTokenizerFast.from_pretrained('YiYiXu/tiny-random-mclip-base'		)
									return tokenizer
		@property
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									torch.manual_seed(0		)
									_UpperCAmelCase					=    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    ,					)

									_UpperCAmelCase					=    MultilingualCLIP(__a		)
									_UpperCAmelCase					=    text_encoder.eval()

									return text_encoder
		@property
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									torch.manual_seed(0		)

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

									_UpperCAmelCase					=    UNetaDConditionModel(**__a		)
									return model
		@property
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""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 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									torch.manual_seed(0		)
									_UpperCAmelCase					=    VQModel(**self.dummy_movq_kwargs		)
									return model
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    self.dummy_text_encoder
									_UpperCAmelCase					=    self.dummy_tokenizer
									_UpperCAmelCase					=    self.dummy_unet
									_UpperCAmelCase					=    self.dummy_movq

									_UpperCAmelCase					=    DDIMScheduler(
									    num_train_timesteps=1000    ,					beta_schedule='linear'    ,					beta_start=0.00085    ,					beta_end=0.012    ,					clip_sample=__a    ,					set_alpha_to_one=__a    ,					steps_offset=1    ,					prediction_type='epsilon'    ,					thresholding=__a    ,					)

									_UpperCAmelCase					=    {
									    'text_encoder': text_encoder,
									    'tokenizer': tokenizer,
									    'unet': unet,
									    'scheduler': scheduler,
									    'movq': movq,
									}
									return components
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=0		)				-> Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=    floats_tensor((1, self.cross_attention_dim)    ,					rng=random.Random(__a		)		).to(__a		)
									_UpperCAmelCase					=    floats_tensor((1, self.cross_attention_dim)    ,					rng=random.Random(seed + 1		)		).to(__a		)
									if str(__a		).startswith('mps'		):
																_UpperCAmelCase					=    torch.manual_seed(__a		)
									else:
																_UpperCAmelCase					=    torch.Generator(device=__a		).manual_seed(__a		)
									_UpperCAmelCase					=    {
									    'prompt': 'horse',
									    'image_embeds': image_embeds,
									    'negative_image_embeds': negative_image_embeds,
									    'generator': generator,
									    'height': 64,
									    'width': 64,
									    'guidance_scale': 4.0,
									    'num_inference_steps': 2,
									    'output_type': 'np',
									}
									return inputs






		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    'cpu'

									_UpperCAmelCase					=    self.get_dummy_components()

									_UpperCAmelCase					=    self.pipeline_class(**__a		)
									_UpperCAmelCase					=    pipe.to(__a		)

									pipe.set_progress_bar_config(disable=__a		)

									_UpperCAmelCase					=    pipe(**self.get_dummy_inputs(__a		)		)
									_UpperCAmelCase					=    output.images

									_UpperCAmelCase					=    pipe(
									    **self.get_dummy_inputs(__a		)    ,					return_dict=__a    ,					)[0]

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

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

									_UpperCAmelCase					=    np.array(
									    [0.328663, 1.0, 0.23216873, 1.0, 0.92717564, 0.4639046, 0.96894777, 0.31713378, 0.6293953]		)

									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    ):
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





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






		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    load_numpy(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
									    '/kandinsky/kandinsky_text2img_cat_fp16.npy'		)

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

									_UpperCAmelCase					=    KandinskyPipeline.from_pretrained('kandinsky-community/kandinsky-2-1'    ,					torch_dtype=torch.floataa		)
									_UpperCAmelCase					=    pipeline.to(__a		)
									pipeline.set_progress_bar_config(disable=__a		)

									_UpperCAmelCase					=    'red cat, 4k photo'

									_UpperCAmelCase					=    torch.Generator(device='cuda'		).manual_seed(0		)
									_UpperCAmelCase					=    pipe_prior(
									    __a    ,					generator=__a    ,					num_inference_steps=5    ,					negative_prompt=''    ,					).to_tuple()

									_UpperCAmelCase					=    torch.Generator(device='cuda'		).manual_seed(0		)
									_UpperCAmelCase					=    pipeline(
									    __a    ,					image_embeds=__a    ,					negative_image_embeds=__a    ,					generator=__a    ,					num_inference_steps=100    ,					output_type='np'    ,					)

									_UpperCAmelCase					=    output.images[0]

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

									assert_mean_pixel_difference(__a    ,					__a		)




import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor

class 	__a   (       unittest.TestCase    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=32    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=10    ,					_SCREAMING_SNAKE_CASE=[10, 20, 30, 40]    ,					_SCREAMING_SNAKE_CASE=[1, 1, 2, 1]    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE=True    ,					_SCREAMING_SNAKE_CASE="relu"    ,					_SCREAMING_SNAKE_CASE=3    ,					_SCREAMING_SNAKE_CASE=None    ,					)				-> Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=    parent
									_UpperCAmelCase					=    batch_size
									_UpperCAmelCase					=    image_size
									_UpperCAmelCase					=    num_channels
									_UpperCAmelCase					=    embeddings_size
									_UpperCAmelCase					=    hidden_sizes
									_UpperCAmelCase					=    depths
									_UpperCAmelCase					=    is_training
									_UpperCAmelCase					=    use_labels
									_UpperCAmelCase					=    hidden_act
									_UpperCAmelCase					=    num_labels
									_UpperCAmelCase					=    scope
									_UpperCAmelCase					=    len(_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

									_UpperCAmelCase					=    self.get_config()

									return config, pixel_values
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									return ResNetConfig(
									    num_channels=self.num_channels    ,					embeddings_size=self.embeddings_size    ,					hidden_sizes=self.hidden_sizes    ,					depths=self.depths    ,					hidden_act=self.hidden_act    ,					num_labels=self.num_labels    ,					image_size=self.image_size    ,					)
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    FlaxResNetModel(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    model(_SCREAMING_SNAKE_CASE		)

									# Output shape (b, c, h, w)
									self.parent.assertEqual(
									    result.last_hidden_state.shape    ,					(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,					)
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> Any:




									"""simple docstring"""





									_UpperCAmelCase					=    self.num_labels
									_UpperCAmelCase					=    FlaxResNetForImageClassification(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)		)






		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									_UpperCAmelCase					=    self.prepare_config_and_inputs()
									_UpperCAmelCase	, _UpperCAmelCase					=    config_and_inputs
									_UpperCAmelCase					=    {'pixel_values': pixel_values}
									return config, inputs_dict



@require_flax
class 	__a   (       UpperCAmelCase					,   unittest.TestCase    ):
		_a			:						Any    			=			(FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

		_a			:						Dict    			=			False
		_a			:						Optional[Any]    			=			False
		_a			:						Optional[Any]    			=			False
		def 			UpperCAmelCase__       (    self		)				-> None:




									"""simple docstring"""





									_UpperCAmelCase					=    FlaxResNetModelTester(self		)
									_UpperCAmelCase					=    ConfigTester(self    ,					config_class=_SCREAMING_SNAKE_CASE    ,					has_text_modality=_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									self.create_and_test_config_common_properties()
									self.config_tester.create_and_test_config_to_json_string()
									self.config_tester.create_and_test_config_to_json_file()
									self.config_tester.create_and_test_config_from_and_save_pretrained()
									self.config_tester.create_and_test_config_with_num_labels()
									self.config_tester.check_config_can_be_init_without_params()
									self.config_tester.check_config_arguments_init()
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									return
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE		)
		@unittest.skip(reason='ResNet does not use inputs_embeds'		)
		def 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""simple docstring"""





									pass
		@unittest.skip(reason='ResNet does not support input and output embeddings'		)
		def 			UpperCAmelCase__       (    self		)				-> Optional[int]:




									"""simple docstring"""





									pass
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase	, _UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																_UpperCAmelCase					=    model_class(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase					=    inspect.signature(model.__call__		)
																# signature.parameters is an OrderedDict => so arg_names order is deterministic
																_UpperCAmelCase					=    [*signature.parameters.keys()]

																_UpperCAmelCase					=    ['pixel_values']
																self.assertListEqual(arg_names[:1]    ,					_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									def check_hidden_states_output(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    model_class(_SCREAMING_SNAKE_CASE		)

																_UpperCAmelCase					=    model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)		)

																_UpperCAmelCase					=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																_UpperCAmelCase					=    self.model_tester.num_stages
																self.assertEqual(len(_SCREAMING_SNAKE_CASE		)    ,					expected_num_stages + 1		)
		@unittest.skip(reason='ResNet does not use feedforward chunking'		)
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									pass






		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase	, _UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__		):
																							_UpperCAmelCase					=    self._prepare_for_class(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase					=    model_class(_SCREAMING_SNAKE_CASE		)

																							@jax.jit
																							def model_jitted(_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE		):
																														return model(pixel_values=_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE		)

																							with self.subTest('JIT Enabled'		):
																														_UpperCAmelCase					=    model_jitted(**_SCREAMING_SNAKE_CASE		).to_tuple()

																							with self.subTest('JIT Disabled'		):
																														with jax.disable_jit():
																																					_UpperCAmelCase					=    model_jitted(**_SCREAMING_SNAKE_CASE		).to_tuple()

																							self.assertEqual(len(_SCREAMING_SNAKE_CASE		)    ,					len(_SCREAMING_SNAKE_CASE		)		)
																							for jitted_output, output in zip(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		):
																														self.assertEqual(jitted_output.shape    ,					output.shape		)



def  lowerCAmelCase__				(    )						->	Optional[int]:


							'''simple docstring'''





							_UpperCAmelCase					=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
							return image







@require_flax
class 	__a   (       unittest.TestCase    ):
		@cached_property
		def 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





									return AutoImageProcessor.from_pretrained('microsoft/resnet-50'		) if is_vision_available() else None






		@slow
		def 			UpperCAmelCase__       (    self		)				-> List[str]:




									"""simple docstring"""





									_UpperCAmelCase					=    FlaxResNetForImageClassification.from_pretrained('microsoft/resnet-50'		)

									_UpperCAmelCase					=    self.default_image_processor
									_UpperCAmelCase					=    prepare_img()
									_UpperCAmelCase					=    image_processor(images=_SCREAMING_SNAKE_CASE    ,					return_tensors='np'		)

									_UpperCAmelCase					=    model(**_SCREAMING_SNAKE_CASE		)

									# verify the logits
									_UpperCAmelCase					=    (1, 1000)
									self.assertEqual(outputs.logits.shape    ,					_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase					=    jnp.array([-11.1069, -9.7877, -8.3777]		)

									self.assertTrue(jnp.allclose(outputs.logits[0, :3]    ,					_SCREAMING_SNAKE_CASE    ,					atol=1e-4		)		)
