


import gc
import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

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     (       __snake_case     ,		__snake_case     ,		unittest.TestCase					):

				"""simple docstring"""







				lowerCAmelCase					=			StableDiffusionXLImgaImgPipeline
				lowerCAmelCase					=			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'}
				lowerCAmelCase					=			PipelineTesterMixin.required_optional_params - {'latents'}
				lowerCAmelCase					=			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
				lowerCAmelCase					=			IMAGE_TO_IMAGE_IMAGE_PARAMS
				lowerCAmelCase					=			IMAGE_TO_IMAGE_IMAGE_PARAMS

				def   _UpperCAmelCase       (    self )						->					Dict:
					torch.manual_seed(0 )
					A             =					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""")					, attention_head_dim=(2, 4)					, use_linear_projection=a__					, addition_embed_type="""text_time"""					, addition_time_embed_dim=8					, transformer_layers_per_block=(1, 2)					, projection_class_embeddings_input_dim=80					, cross_attention_dim=64					, )
					A             =					EulerDiscreteScheduler(
					    beta_start=0.0_00_85					, beta_end=0.0_12					, steps_offset=1					, beta_schedule="""scaled_linear"""					, timestep_spacing="""leading"""					, )
					torch.manual_seed(0 )
					A             =					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					, sample_size=128					, )
					torch.manual_seed(0 )
					A             =					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					, hidden_act="""gelu"""					, projection_dim=32					, )
					A             =					CLIPTextModel(a__ )
					A             =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					, local_files_only=a__ )

					A             =					CLIPTextModelWithProjection(a__ )
					A             =					CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					, local_files_only=a__ )

					A             =					{
					    """unet""": unet,
					    """scheduler""": scheduler,
					    """vae""": vae,
					    """text_encoder""": text_encoder,
					    """tokenizer""": tokenizer,
					    """text_encoder_2""": text_encoder_a,
					    """tokenizer_2""": tokenizer_a,
					    # "safety_checker": None,
					    # "feature_extractor": None,
					}
					return components

				def   _UpperCAmelCase       (    self					, a__					, a__=0 )						->					List[Any]:
					A             =					floats_tensor((1, 3, 32, 32)					, rng=random.Random(a__ ) ).to(a__ )
					A             =					image / 2 + 0.5
					if str(a__ ).startswith("""mps""" ):
						A             =					torch.manual_seed(a__ )
					else:
						A             =					torch.Generator(device=a__ ).manual_seed(a__ )
					A             =					{
					    """prompt""": """A painting of a squirrel eating a burger""",
					    """image""": image,
					    """generator""": generator,
					    """num_inference_steps""": 2,
					    """guidance_scale""": 5.0,
					    """output_type""": """numpy""",
					    """strength""": 0.75,
					}
					return inputs

				def   _UpperCAmelCase       (    self )						->					int:
					A             =					"""cpu"""  # ensure determinism for the device-dependent torch.Generator
					A             =					self.get_dummy_components()
					A             =					StableDiffusionXLImgaImgPipeline(**a__ )
					A             =					sd_pipe.to(a__ )
					sd_pipe.set_progress_bar_config(disable=a__ )

					A             =					self.get_dummy_inputs(a__ )
					A             =					sd_pipe(**a__ ).images
					A             =					image[0, -3:, -3:, -1]

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

					A             =					np.array([0.46_56, 0.48_40, 0.44_39, 0.66_98, 0.55_74, 0.45_24, 0.57_99, 0.59_43, 0.51_65] )

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

				def   _UpperCAmelCase       (    self )						->					Union[str, Any]:
					super().test_attention_slicing_forward_pass(expected_max_diff=3e-3 )

				def   _UpperCAmelCase       (    self )						->					Dict:
					super().test_inference_batch_single_identical(expected_max_diff=3e-3 )

				def   _UpperCAmelCase       (    self )						->					str:
					pass

				def   _UpperCAmelCase       (    self )						->					Optional[int]:
					A             =					self.get_dummy_components()
					A             =					StableDiffusionXLImgaImgPipeline(**a__ )
					A             =					sd_pipe.to(a__ )
					A             =					sd_pipe.to(a__ )
					sd_pipe.set_progress_bar_config(disable=a__ )

					# forward without prompt embeds
					A             =					self.get_dummy_inputs(a__ )
					A             =					3 * ["""this is a negative prompt"""]
					A             =					negative_prompt
					A             =					3 * [inputs["""prompt"""]]

					A             =					sd_pipe(**a__ )
					A             =					output.images[0, -3:, -3:, -1]

					# forward with prompt embeds
					A             =					self.get_dummy_inputs(a__ )
					A             =					3 * ["""this is a negative prompt"""]
					A             =					3 * [inputs.pop("""prompt""" )]

					(
					    (
					    A
					)       ,      (
					    A
					)       ,      (
					    A
					)       ,      (
					    A
					)       ,      
					)             =					sd_pipe.encode_prompt(a__					, negative_prompt=a__ )

					A             =					sd_pipe(
					    **a__					, prompt_embeds=a__					, negative_prompt_embeds=a__					, pooled_prompt_embeds=a__					, negative_pooled_prompt_embeds=a__					, )
					A             =					output.images[0, -3:, -3:, -1]

					# make sure that it's equal
					assert np.abs(image_slice_a.flatten() - image_slice_a.flatten() ).max() < 1e-4






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

				"""simple docstring"""







				def   _UpperCAmelCase       (    self )						->					Tuple:
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()

				def   _UpperCAmelCase       (    self					, a__					, a__="cpu"					, a__=torch.floataa					, a__=0 )						->					int:
					A             =					torch.Generator(device=a__ ).manual_seed(a__ )
					A             =					np.random.RandomState(a__ ).standard_normal((1, 4, 64, 64) )
					A             =					torch.from_numpy(a__ ).to(device=a__					, dtype=a__ )
					A             =					{
					    """prompt""": """a photograph of an astronaut riding a horse""",
					    """latents""": latents,
					    """generator""": generator,
					    """num_inference_steps""": 3,
					    """guidance_scale""": 7.5,
					    """output_type""": """numpy""",
					}
					return inputs

				def   _UpperCAmelCase       (    self )						->					List[str]:
					A             =					DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base""" )
					pipe.to(a__ )
					pipe.set_progress_bar_config(disable=a__ )

					A             =					self.get_inputs(a__ )
					A             =					pipe(**a__ ).images
					A             =					image[0, -3:, -3:, -1].flatten()

					assert image.shape == (1, 512, 512, 3)
					A             =					np.array([0.4_94_93, 0.4_78_96, 0.4_07_98, 0.5_42_14, 0.5_32_12, 0.4_82_02, 0.4_76_56, 0.4_63_29, 0.4_85_06] )
					assert np.abs(image_slice - expected_slice ).max() < 7e-3







import unittest

from transformers import BigBirdConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
				import jax

				from transformers.models.big_bird.modeling_flax_big_bird import (
				    FlaxBigBirdForCausalLM,
				    FlaxBigBirdForMaskedLM,
				    FlaxBigBirdForMultipleChoice,
				    FlaxBigBirdForPreTraining,
				    FlaxBigBirdForQuestionAnswering,
				    FlaxBigBirdForSequenceClassification,
				    FlaxBigBirdForTokenClassification,
				    FlaxBigBirdModel,
				)






class    _UpperCamelCase     (       unittest.TestCase					):

				"""simple docstring"""







				def __init__(    self					, a__					, a__=2					, a__=56					, a__=True					, a__=True					, a__=True					, a__=True					, a__=99					, a__=32					, a__=2					, a__=2					, a__=7					, a__="gelu_new"					, a__=0.1					, a__=0.1					, a__=512					, a__=16					, a__=2					, a__=0.02					, a__=4					, a__="block_sparse"					, a__=True					, a__=False					, a__=2					, a__=3					, )						->					List[Any]:
					A             =					parent
					A             =					batch_size
					A             =					seq_length
					A             =					is_training
					A             =					use_attention_mask
					A             =					use_token_type_ids
					A             =					use_labels
					A             =					vocab_size
					A             =					hidden_size
					A             =					num_hidden_layers
					A             =					num_attention_heads
					A             =					intermediate_size
					A             =					hidden_act
					A             =					hidden_dropout_prob
					A             =					attention_probs_dropout_prob
					A             =					max_position_embeddings
					A             =					type_vocab_size
					A             =					type_sequence_label_size
					A             =					initializer_range
					A             =					num_choices

					A             =					rescale_embeddings
					A             =					attention_type
					A             =					use_bias
					A             =					block_size
					A             =					num_random_blocks

				def   _UpperCAmelCase       (    self )						->					List[Any]:
					A             =					ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size )

					A             =					None
					if self.use_attention_mask:
						A             =					random_attention_mask([self.batch_size, self.seq_length] )

					A             =					None
					if self.use_token_type_ids:
						A             =					ids_tensor([self.batch_size, self.seq_length]					, self.type_vocab_size )

					A             =					BigBirdConfig(
					    vocab_size=self.vocab_size					, hidden_size=self.hidden_size					, num_hidden_layers=self.num_hidden_layers					, num_attention_heads=self.num_attention_heads					, intermediate_size=self.intermediate_size					, hidden_act=self.hidden_act					, hidden_dropout_prob=self.hidden_dropout_prob					, attention_probs_dropout_prob=self.attention_probs_dropout_prob					, max_position_embeddings=self.max_position_embeddings					, type_vocab_size=self.type_vocab_size					, is_decoder=a__					, initializer_range=self.initializer_range					, attention_type=self.attention_type					, block_size=self.block_size					, num_random_blocks=self.num_random_blocks					, use_bias=self.use_bias					, rescale_embeddings=self.rescale_embeddings					, )

					return config, input_ids, token_type_ids, attention_mask

				def   _UpperCAmelCase       (    self )						->					Optional[int]:
					A             =					self.prepare_config_and_inputs()
					A       ,      A       ,      A       ,      A             =					config_and_inputs
					A             =					{
					    """input_ids""": input_ids,
					    """token_type_ids""": token_type_ids,
					    """attention_mask""": attention_mask,
					}
					return config, inputs_dict






@require_flax
class    _UpperCamelCase     (       __snake_case     ,		unittest.TestCase					):

				"""simple docstring"""







				lowerCAmelCase					=			(
				    (
				        FlaxBigBirdForCausalLM,
				        FlaxBigBirdModel,
				        FlaxBigBirdForPreTraining,
				        FlaxBigBirdForMaskedLM,
				        FlaxBigBirdForMultipleChoice,
				        FlaxBigBirdForQuestionAnswering,
				        FlaxBigBirdForSequenceClassification,
				        FlaxBigBirdForTokenClassification,
				    )
				    if is_flax_available()
				    else ()
				)

				lowerCAmelCase					=			False
				lowerCAmelCase					=			False

				def   _UpperCAmelCase       (    self )						->					Optional[int]:
					A             =					FlaxBigBirdModelTester(self )

				@slow
				# copied from `test_modeling_flax_common` because it takes much longer than other models
				def   _UpperCAmelCase       (    self )						->					Tuple:
					super().test_from_pretrained_save_pretrained()

				@slow
				# copied from `test_modeling_flax_common` because it takes much longer than other models
				def   _UpperCAmelCase       (    self )						->					Dict:
					super().test_from_pretrained_with_no_automatic_init()

				@slow
				# copied from `test_modeling_flax_common` because it takes much longer than other models
				def   _UpperCAmelCase       (    self )						->					str:
					super().test_no_automatic_init()

				@slow
				# copied from `test_modeling_flax_common` because it takes much longer than other models
				def   _UpperCAmelCase       (    self )						->					List[Any]:
					super().test_hidden_states_output()

				@slow
				def   _UpperCAmelCase       (    self )						->					str:
					for model_class_name in self.all_model_classes:
						A             =					model_class_name.from_pretrained("""google/bigbird-roberta-base""" )
						self.assertIsNotNone(a__ )

				def   _UpperCAmelCase       (    self )						->					Optional[int]:
					if self.test_attn_probs:
						super().test_attention_outputs()

				@slow
				# copied from `test_modeling_flax_common` because it takes much longer than other models
				def   _UpperCAmelCase       (    self )						->					List[Any]:
					A       ,      A             =					self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						with self.subTest(model_class.__name__ ):
							A             =					self._prepare_for_class(a__					, a__ )
							A             =					model_class(a__ )

							@jax.jit
							def model_jitted(a__					, a__=None					, **a__ ):
								return model(input_ids=a__					, attention_mask=a__					, **a__ )

							with self.subTest("""JIT Enabled""" ):
								A             =					model_jitted(**a__ ).to_tuple()

							with self.subTest("""JIT Disabled""" ):
								with jax.disable_jit():
									A             =					model_jitted(**a__ ).to_tuple()

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

				def   _UpperCAmelCase       (    self					, a__					, a__					, a__					, a__=1e-5					, a__="outputs"					, a__=None )						->					Tuple:
					# `bigbird_block_sparse_attention` in `FlaxBigBird` returns `attention_probs = None`, while in PyTorch version,
					# an effort was done to return `attention_probs` (yet to be verified).
					if name.startswith("""outputs.attentions""" ):
						return
					else:
						super().check_pt_flax_outputs(a__					, a__					, a__					, a__					, a__					, a__ )



