


'''simple docstring'''



import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, CycleDiffusionPipeline, DDIMScheduler, UNetaDConditionModel
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, skip_mps

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class 	UpperCamelCase__(       lowerCAmelCase						,    lowerCAmelCase						,    unittest.TestCase	):
				__magic_name__    :				Tuple  		=	CycleDiffusionPipeline
				__magic_name__    :				int  		=	TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
				    "negative_prompt",
				    "height",
				    "width",
				    "negative_prompt_embeds",
				}
				__magic_name__    :				str  		=	PipelineTesterMixin.required_optional_params - {"latents"}
				__magic_name__    :				Union[str, Any]  		=	TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"source_prompt"}	)
				__magic_name__    :				List[str]  		=	IMAGE_TO_IMAGE_IMAGE_PARAMS
				__magic_name__    :				Optional[int]  		=	IMAGE_TO_IMAGE_IMAGE_PARAMS




				def 					a__(		self      :			List[Any]				)->					int:

										"""simple docstring"""




										torch.manual_seed(0				)
										UpperCAmelCase					    =     UNetaDConditionModel(
										    block_out_channels=(32, 64)				,   layers_per_block=2				,   sample_size=32				,   in_channels=4				,   out_channels=4				,   down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')				,   up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')				,   cross_attention_dim=32				,   )
										UpperCAmelCase					    =     DDIMScheduler(
										    beta_start=0.00085				,   beta_end=0.012				,   beta_schedule='''scaled_linear'''				,   num_train_timesteps=1000				,   clip_sample=lowerCAmelCase				,   set_alpha_to_one=lowerCAmelCase				,   )
										torch.manual_seed(0				)
										UpperCAmelCase					    =     AutoencoderKL(
										    block_out_channels=[32, 64]				,   in_channels=3				,   out_channels=3				,   down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']				,   up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']				,   latent_channels=4				,   )
										torch.manual_seed(0				)
										UpperCAmelCase					    =     CLIPTextConfig(
										    bos_token_id=0				,   eos_token_id=2				,   hidden_size=32				,   intermediate_size=37				,   layer_norm_eps=1E-05				,   num_attention_heads=4				,   num_hidden_layers=5				,   pad_token_id=1				,   vocab_size=1000				,   )
										UpperCAmelCase					    =     CLIPTextModel(lowerCAmelCase				)
										UpperCAmelCase					    =     CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''				)

										UpperCAmelCase					    =     {
										    '''unet''': unet,
										    '''scheduler''': scheduler,
										    '''vae''': vae,
										    '''text_encoder''': text_encoder,
										    '''tokenizer''': tokenizer,
										    '''safety_checker''': None,
										    '''feature_extractor''': None,
										}
										return components




				def 					a__(		self      :			Tuple				,   lowerCAmelCase      :			Union[str, Any]				,   lowerCAmelCase      :			Optional[int]=0				)->					Optional[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     floats_tensor((1, 3, 32, 32)				,   rng=random.Random(lowerCAmelCase				)				).to(lowerCAmelCase				)
										UpperCAmelCase					    =     image / 2 + 0.5
										if str(lowerCAmelCase				).startswith('''mps'''				):
																UpperCAmelCase					    =     torch.manual_seed(lowerCAmelCase				)
										else:
																UpperCAmelCase					    =     torch.Generator(device=lowerCAmelCase				).manual_seed(lowerCAmelCase				)
										UpperCAmelCase					    =     {
										    '''prompt''': '''An astronaut riding an elephant''',
										    '''source_prompt''': '''An astronaut riding a horse''',
										    '''image''': image,
										    '''generator''': generator,
										    '''num_inference_steps''': 2,
										    '''eta''': 0.1,
										    '''strength''': 0.8,
										    '''guidance_scale''': 3,
										    '''source_guidance_scale''': 1,
										    '''output_type''': '''numpy''',
										}
										return inputs




				def 					a__(		self      :			Optional[int]				)->					Optional[Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     '''cpu'''  # ensure determinism for the device-dependent torch.Generator

										UpperCAmelCase					    =     self.get_dummy_components()
										UpperCAmelCase					    =     CycleDiffusionPipeline(**lowerCAmelCase				)
										UpperCAmelCase					    =     pipe.to(lowerCAmelCase				)
										pipe.set_progress_bar_config(disable=lowerCAmelCase				)

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

										UpperCAmelCase					    =     images[0, -3:, -3:, -1]

										assert images.shape == (1, 32, 32, 3)
										UpperCAmelCase					    =     np.array([0.4459, 0.4943, 0.4544, 0.6643, 0.5474, 0.4327, 0.5701, 0.5959, 0.5179]				)

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




				@unittest.skipIf(torch_device != '''cuda'''				,   '''This test requires a GPU'''				)
				def 					a__(		self      :			Any				)->					Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     self.get_dummy_components()
										for name, module in components.items():
																if hasattr(lowerCAmelCase				,   '''half'''				):
																						UpperCAmelCase					    =     module.half()
										UpperCAmelCase					    =     CycleDiffusionPipeline(**lowerCAmelCase				)
										UpperCAmelCase					    =     pipe.to(lowerCAmelCase				)
										pipe.set_progress_bar_config(disable=lowerCAmelCase				)

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

										UpperCAmelCase					    =     images[0, -3:, -3:, -1]

										assert images.shape == (1, 32, 32, 3)
										UpperCAmelCase					    =     np.array([0.3506, 0.4543, 0.446, 0.4575, 0.5195, 0.4155, 0.5273, 0.518, 0.4116]				)

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




				@skip_mps
				def 					a__(		self      :			int				)->					Union[str, Any]:

										"""simple docstring"""




										return super().test_save_load_local()




				@unittest.skip('''non-deterministic pipeline'''				)
				def 					a__(		self      :			Optional[int]				)->					Tuple:

										"""simple docstring"""




										return super().test_inference_batch_single_identical()




				@skip_mps
				def 					a__(		self      :			Optional[int]				)->					int:

										"""simple docstring"""




										return super().test_dict_tuple_outputs_equivalent()




				@skip_mps
				def 					a__(		self      :			str				)->					Union[str, Any]:

										"""simple docstring"""




										return super().test_save_load_optional_components()





				@skip_mps
				def 					a__(		self      :			Optional[int]				)->					Any:

										"""simple docstring"""




										return super().test_attention_slicing_forward_pass()







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




				def 					a__(		self      :			Optional[Any]				)->					Optional[Any]:

										"""simple docstring"""




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




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

										"""simple docstring"""




										UpperCAmelCase					    =     load_image(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
										    '''/cycle-diffusion/black_colored_car.png'''				)
										UpperCAmelCase					    =     load_numpy(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car_fp16.npy'''				)
										UpperCAmelCase					    =     init_image.resize((512, 512)				)

										UpperCAmelCase					    =     '''CompVis/stable-diffusion-v1-4'''
										UpperCAmelCase					    =     DDIMScheduler.from_pretrained(lowerCAmelCase				,   subfolder='''scheduler'''				)
										UpperCAmelCase					    =     CycleDiffusionPipeline.from_pretrained(
										    lowerCAmelCase				,   scheduler=lowerCAmelCase				,   safety_checker=lowerCAmelCase				,   torch_dtype=torch.floataa				,   revision='''fp16'''				)

										pipe.to(lowerCAmelCase				)
										pipe.set_progress_bar_config(disable=lowerCAmelCase				)
										pipe.enable_attention_slicing()

										UpperCAmelCase					    =     '''A black colored car'''
										UpperCAmelCase					    =     '''A blue colored car'''

										UpperCAmelCase					    =     torch.manual_seed(0				)
										UpperCAmelCase					    =     pipe(
										    prompt=lowerCAmelCase				,   source_prompt=lowerCAmelCase				,   image=lowerCAmelCase				,   num_inference_steps=100				,   eta=0.1				,   strength=0.85				,   guidance_scale=3				,   source_guidance_scale=1				,   generator=lowerCAmelCase				,   output_type='''np'''				,   )
										UpperCAmelCase					    =     output.images

										# the values aren't exactly equal, but the images look the same visually
										assert np.abs(image - expected_image				).max() < 5E-1





				def 					a__(		self      :			Any				)->					List[str]:

										"""simple docstring"""




										UpperCAmelCase					    =     load_image(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
										    '''/cycle-diffusion/black_colored_car.png'''				)
										UpperCAmelCase					    =     load_numpy(
										    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/cycle-diffusion/blue_colored_car.npy'''				)
										UpperCAmelCase					    =     init_image.resize((512, 512)				)

										UpperCAmelCase					    =     '''CompVis/stable-diffusion-v1-4'''
										UpperCAmelCase					    =     DDIMScheduler.from_pretrained(lowerCAmelCase				,   subfolder='''scheduler'''				)
										UpperCAmelCase					    =     CycleDiffusionPipeline.from_pretrained(lowerCAmelCase				,   scheduler=lowerCAmelCase				,   safety_checker=lowerCAmelCase				)

										pipe.to(lowerCAmelCase				)
										pipe.set_progress_bar_config(disable=lowerCAmelCase				)
										pipe.enable_attention_slicing()

										UpperCAmelCase					    =     '''A black colored car'''
										UpperCAmelCase					    =     '''A blue colored car'''

										UpperCAmelCase					    =     torch.manual_seed(0				)
										UpperCAmelCase					    =     pipe(
										    prompt=lowerCAmelCase				,   source_prompt=lowerCAmelCase				,   image=lowerCAmelCase				,   num_inference_steps=100				,   eta=0.1				,   strength=0.85				,   guidance_scale=3				,   source_guidance_scale=1				,   generator=lowerCAmelCase				,   output_type='''np'''				,   )
										UpperCAmelCase					    =     output.images

										assert np.abs(image - expected_image				).max() < 2E-2









'''simple docstring'''



import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD






class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			str				)->					Dict:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[Any]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			int				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet, attn in zip(self.resnets				,   self.attentions				):
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Any:

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=lowerCAmelCase				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)
										UpperCAmelCase					    =     resnets

										if self.add_downsample:
																UpperCAmelCase					    =     FlaxDownsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			List[str]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			List[str]=True				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     ()

										for resnet in self.resnets:
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																output_states += (hidden_states,)

										if self.add_downsample:
																UpperCAmelCase					    =     self.downsamplers_a(lowerCAmelCase				)
																output_states += (hidden_states,)

										return hidden_states, output_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			List[str]				)->					Tuple:

										"""simple docstring"""




										UpperCAmelCase					    =     []
										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.out_channels				,   n_heads=self.num_attention_heads				,   d_head=self.out_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   only_cross_attention=self.only_cross_attention				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			Optional[int]				,   lowerCAmelCase      :			List[str]				,   lowerCAmelCase      :			Any				,   lowerCAmelCase      :			Union[str, Any]=True				)->					Optional[int]:

										"""simple docstring"""




										for resnet, attn in zip(self.resnets				,   self.attentions				):
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	True
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			Optional[int]				)->					str:

										"""simple docstring"""




										UpperCAmelCase					    =     []

										for i in range(self.num_layers				):
																UpperCAmelCase					    =     self.in_channels if (i == self.num_layers - 1) else self.out_channels
																UpperCAmelCase					    =     self.prev_output_channel if i == 0 else self.out_channels

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=resnet_in_channels + res_skip_channels				,   out_channels=self.out_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets

										if self.add_upsample:
																UpperCAmelCase					    =     FlaxUpsampleaD(self.out_channels				,   dtype=self.dtype				)





				def __call__(		self      :			Union[str, Any]				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Union[str, Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict=True				)->					Tuple:

										"""simple docstring"""




										for resnet in self.resnets:
																# pop res hidden states
																UpperCAmelCase					    =     res_hidden_states_tuple[-1]
																UpperCAmelCase					    =     res_hidden_states_tuple[:-1]
																UpperCAmelCase					    =     jnp.concatenate((hidden_states, res_hidden_states)				,   axis=-1				)

																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										if self.add_upsample:
																UpperCAmelCase					    =     self.upsamplers_a(lowerCAmelCase				)

										return hidden_states







class 	UpperCamelCase__(       nn.Module	):
				__magic_name__    :				int
				__magic_name__    :				float  		=	0.0
				__magic_name__    :				int  		=	1
				__magic_name__    :				int  		=	1
				__magic_name__    :				bool  		=	False
				__magic_name__    :				bool  		=	False
				__magic_name__    :				jnp.dtype  		=	jnp.floataa




				def 					a__(		self      :			int				)->					Optional[int]:

										"""simple docstring"""




										UpperCAmelCase					    =     [
										    FlaxResnetBlockaD(
										        in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
										]

										UpperCAmelCase					    =     []

										for _ in range(self.num_layers				):
																UpperCAmelCase					    =     FlaxTransformeraDModel(
																    in_channels=self.in_channels				,   n_heads=self.num_attention_heads				,   d_head=self.in_channels // self.num_attention_heads				,   depth=1				,   use_linear_projection=self.use_linear_projection				,   use_memory_efficient_attention=self.use_memory_efficient_attention				,   dtype=self.dtype				,   )
																attentions.append(lowerCAmelCase				)

																UpperCAmelCase					    =     FlaxResnetBlockaD(
																    in_channels=self.in_channels				,   out_channels=self.in_channels				,   dropout_prob=self.dropout				,   dtype=self.dtype				,   )
																resnets.append(lowerCAmelCase				)

										UpperCAmelCase					    =     resnets
										UpperCAmelCase					    =     attentions





				def __call__(		self      :			Dict				,   lowerCAmelCase      :			Optional[Any]				,   lowerCAmelCase      :			Tuple				,   lowerCAmelCase      :			Dict				,   lowerCAmelCase      :			Any=True				)->					Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase					    =     self.resnets[0](lowerCAmelCase				,   lowerCAmelCase				)
										for attn, resnet in zip(self.attentions				,   self.resnets[1:]				):
																UpperCAmelCase					    =     attn(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)
																UpperCAmelCase					    =     resnet(lowerCAmelCase				,   lowerCAmelCase				,   deterministic=lowerCAmelCase				)

										return hidden_states





