






import os
import tempfile
import unittest

import numpy as np

from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax, slow


if is_flax_available():
			import jax
			import jax.numpy as jnp
			from flax.jax_utils import replicate
			from flax.training.common_utils import shard

			from diffusers import FlaxDDIMScheduler, FlaxDiffusionPipeline, FlaxStableDiffusionPipeline




@require_flax
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



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




											'''simple docstring'''



											with tempfile.TemporaryDirectory() as tmpdirname:
																# pipeline has Flax weights
																__a					:						Optional[Any]      =					FlaxDiffusionPipeline.from_pretrained(
																    "hf-internal-testing/tiny-stable-diffusion-pipe" ,					safety_checker=__UpperCAmelCase ,					cache_dir=__UpperCAmelCase   )

																__a					:						Optional[Any]      =					[t[-1] for t in os.walk(os.path.join(__UpperCAmelCase ,					os.listdir(__UpperCAmelCase   )[0] ,					"snapshots"   )   )]
																__a					:						Dict      =					[item for sublist in all_root_files for item in sublist]

																# None of the downloaded files should be a PyTorch file even if we have some here:
																# https://huggingface.co/hf-internal-testing/tiny-stable-diffusion-pipe/blob/main/unet/diffusion_pytorch_model.bin
																assert not any(f.endswith(".bin"   ) for f in files   )




@slow
@require_flax
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



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




											'''simple docstring'''



											__a      ,			__a					:						int      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "hf-internal-testing/tiny-stable-diffusion-pipe" ,					safety_checker=__UpperCAmelCase   )

											__a					:						Optional[Any]      =					(
											    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
											    " field, close up, split lighting, cinematic"
											)

											__a					:						Tuple      =					jax.random.PRNGKey(0   )
											__a					:						Optional[int]      =					4

											__a					:						Optional[Any]      =					jax.device_count()
											__a					:						Tuple      =					num_samples * [prompt]
											__a					:						Dict      =					pipeline.prepare_inputs(__UpperCAmelCase   )

											# shard inputs and rng
											__a					:						List[str]      =					replicate(__UpperCAmelCase   )
											__a					:						Dict      =					jax.random.split(__UpperCAmelCase ,					__UpperCAmelCase   )
											__a					:						Tuple      =					shard(__UpperCAmelCase   )

											__a					:						str      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images

											assert images.shape == (num_samples, 1, 64, 64, 3)
											if jax.device_count() == 8:
																assert np.abs(np.abs(images[0, 0, :2, :2, -2:] ,					dtype=np.floataa   ).sum() - 4.1_51_47_45   ) < 1E-3
																assert np.abs(np.abs(__UpperCAmelCase ,					dtype=np.floataa   ).sum() - 4_99_47.8_75   ) < 5E-1

											__a					:						int      =					pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:]   )   )   )
											assert len(__UpperCAmelCase   ) == num_samples



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




											'''simple docstring'''



											__a      ,			__a					:						List[Any]      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "CompVis/stable-diffusion-v1-4" ,					revision="flax" ,					safety_checker=__UpperCAmelCase   )

											__a					:						Optional[int]      =					(
											    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
											    " field, close up, split lighting, cinematic"
											)

											__a					:						Optional[int]      =					jax.random.PRNGKey(0   )
											__a					:						Optional[int]      =					50

											__a					:						Optional[int]      =					jax.device_count()
											__a					:						Optional[int]      =					num_samples * [prompt]
											__a					:						str      =					pipeline.prepare_inputs(__UpperCAmelCase   )

											# shard inputs and rng
											__a					:						Optional[Any]      =					replicate(__UpperCAmelCase   )
											__a					:						int      =					jax.random.split(__UpperCAmelCase ,					__UpperCAmelCase   )
											__a					:						List[str]      =					shard(__UpperCAmelCase   )

											__a					:						List[str]      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images

											assert images.shape == (num_samples, 1, 512, 512, 3)
											if jax.device_count() == 8:
																assert np.abs((np.abs(images[0, 0, :2, :2, -2:] ,					dtype=np.floataa   ).sum() - 0.05_65_24_01)   ) < 1E-3
																assert np.abs((np.abs(__UpperCAmelCase ,					dtype=np.floataa   ).sum() - 2_38_38_08.2)   ) < 5E-1



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




											'''simple docstring'''



											__a      ,			__a					:						Optional[int]      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "CompVis/stable-diffusion-v1-4" ,					revision="bf16" ,					dtype=jnp.bfloataa ,					safety_checker=__UpperCAmelCase   )

											__a					:						Tuple      =					(
											    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
											    " field, close up, split lighting, cinematic"
											)

											__a					:						Optional[Any]      =					jax.random.PRNGKey(0   )
											__a					:						List[Any]      =					50

											__a					:						Union[str, Any]      =					jax.device_count()
											__a					:						int      =					num_samples * [prompt]
											__a					:						Optional[int]      =					pipeline.prepare_inputs(__UpperCAmelCase   )

											# shard inputs and rng
											__a					:						List[str]      =					replicate(__UpperCAmelCase   )
											__a					:						str      =					jax.random.split(__UpperCAmelCase ,					__UpperCAmelCase   )
											__a					:						Dict      =					shard(__UpperCAmelCase   )

											__a					:						Any      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images

											assert images.shape == (num_samples, 1, 512, 512, 3)
											if jax.device_count() == 8:
																assert np.abs((np.abs(images[0, 0, :2, :2, -2:] ,					dtype=np.floataa   ).sum() - 0.04_00_39_06)   ) < 1E-3
																assert np.abs((np.abs(__UpperCAmelCase ,					dtype=np.floataa   ).sum() - 2_37_35_16.75)   ) < 5E-1



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




											'''simple docstring'''



											__a      ,			__a					:						Optional[int]      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "CompVis/stable-diffusion-v1-4" ,					revision="bf16" ,					dtype=jnp.bfloataa   )

											__a					:						Optional[Any]      =					(
											    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
											    " field, close up, split lighting, cinematic"
											)

											__a					:						List[str]      =					jax.random.PRNGKey(0   )
											__a					:						Dict      =					50

											__a					:						Any      =					jax.device_count()
											__a					:						Dict      =					num_samples * [prompt]
											__a					:						int      =					pipeline.prepare_inputs(__UpperCAmelCase   )

											# shard inputs and rng
											__a					:						Optional[int]      =					replicate(__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					jax.random.split(__UpperCAmelCase ,					__UpperCAmelCase   )
											__a					:						Optional[int]      =					shard(__UpperCAmelCase   )

											__a					:						Optional[Any]      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images

											assert images.shape == (num_samples, 1, 512, 512, 3)
											if jax.device_count() == 8:
																assert np.abs((np.abs(images[0, 0, :2, :2, -2:] ,					dtype=np.floataa   ).sum() - 0.04_00_39_06)   ) < 1E-3
																assert np.abs((np.abs(__UpperCAmelCase ,					dtype=np.floataa   ).sum() - 2_37_35_16.75)   ) < 5E-1



						def        UpperCAmelCase__					(self:					int   )    ->     Any:




											'''simple docstring'''



											__a					:						int      =					FlaxDDIMScheduler(
											    beta_start=0.0_00_85 ,					beta_end=0.0_12 ,					beta_schedule="scaled_linear" ,					set_alpha_to_one=__UpperCAmelCase ,					steps_offset=1 ,					)

											__a      ,			__a					:						Optional[int]      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "CompVis/stable-diffusion-v1-4" ,					revision="bf16" ,					dtype=jnp.bfloataa ,					scheduler=__UpperCAmelCase ,					safety_checker=__UpperCAmelCase ,					)
											__a					:						Union[str, Any]      =					scheduler.create_state()

											__a					:						Tuple      =					scheduler_state

											__a					:						Any      =					(
											    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
											    " field, close up, split lighting, cinematic"
											)

											__a					:						Any      =					jax.random.PRNGKey(0   )
											__a					:						List[Any]      =					50

											__a					:						str      =					jax.device_count()
											__a					:						Any      =					num_samples * [prompt]
											__a					:						Optional[int]      =					pipeline.prepare_inputs(__UpperCAmelCase   )

											# shard inputs and rng
											__a					:						Dict      =					replicate(__UpperCAmelCase   )
											__a					:						Optional[Any]      =					jax.random.split(__UpperCAmelCase ,					__UpperCAmelCase   )
											__a					:						List[str]      =					shard(__UpperCAmelCase   )

											__a					:						Optional[Any]      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images

											assert images.shape == (num_samples, 1, 512, 512, 3)
											if jax.device_count() == 8:
																assert np.abs((np.abs(images[0, 0, :2, :2, -2:] ,					dtype=np.floataa   ).sum() - 0.0_45_04_39_45)   ) < 1E-3
																assert np.abs((np.abs(__UpperCAmelCase ,					dtype=np.floataa   ).sum() - 2_34_76_93.5)   ) < 5E-1



						def        UpperCAmelCase__					(self:					Any   )    ->     str:




											'''simple docstring'''



											__a					:						Dict      =					(
											    "A cinematic film still of Morgan Freeman starring as Jimi Hendrix, portrait, 40mm lens, shallow depth of"
											    " field, close up, split lighting, cinematic"
											)

											__a					:						str      =					jax.device_count()
											__a					:						Tuple      =					num_samples * [prompt]
											__a					:						Union[str, Any]      =					jax.random.split(jax.random.PRNGKey(0   ) ,					__UpperCAmelCase   )

											__a      ,			__a					:						Any      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "CompVis/stable-diffusion-v1-4" ,					revision="bf16" ,					dtype=jnp.bfloataa ,					safety_checker=__UpperCAmelCase ,					)

											__a					:						Any      =					replicate(__UpperCAmelCase   )
											__a					:						Optional[Any]      =					pipeline.prepare_inputs(__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					shard(__UpperCAmelCase   )
											__a					:						Optional[Any]      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images
											assert images.shape == (num_samples, 1, 512, 512, 3)
											__a					:						Dict      =					images[2, 0, 256, 10:17, 1]

											# With memory efficient attention
											__a      ,			__a					:						Optional[Any]      =					FlaxStableDiffusionPipeline.from_pretrained(
											    "CompVis/stable-diffusion-v1-4" ,					revision="bf16" ,					dtype=jnp.bfloataa ,					safety_checker=__UpperCAmelCase ,					use_memory_efficient_attention=__UpperCAmelCase ,					)

											__a					:						Optional[Any]      =					replicate(__UpperCAmelCase   )
											__a					:						Optional[int]      =					pipeline.prepare_inputs(__UpperCAmelCase   )
											__a					:						Dict      =					shard(__UpperCAmelCase   )
											__a					:						str      =					pipeline(__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					jit=__UpperCAmelCase   ).images
											assert images_eff.shape == (num_samples, 1, 512, 512, 3)
											__a					:						int      =					images[2, 0, 256, 10:17, 1]

											# I checked the results visually and they are very similar. However, I saw that the max diff is `1` and the `sum`
											# over the 8 images is exactly `256`, which is very suspicious. Testing a random slice for now.
											assert abs(slice_eff - slice   ).max() < 1E-2













from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
			import tensorflow as tf

			from transformers import TFBlipTextModel
			from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class 							snake_case_       :

						"""simple docstring"""



						def __init__(self:					Optional[Any] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					int=12 ,					__UpperCAmelCase:					Dict=7 ,					__UpperCAmelCase:					str=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Optional[int]=99 ,					__UpperCAmelCase:					str=32 ,					__UpperCAmelCase:					Any=32 ,					__UpperCAmelCase:					int=2 ,					__UpperCAmelCase:					List[str]=4 ,					__UpperCAmelCase:					Tuple=37 ,					__UpperCAmelCase:					Optional[int]=0.1 ,					__UpperCAmelCase:					List[Any]=0.1 ,					__UpperCAmelCase:					Dict=512 ,					__UpperCAmelCase:					Union[str, Any]=0.02 ,					__UpperCAmelCase:					List[Any]=0 ,					__UpperCAmelCase:					str=None ,					)    ->     Tuple:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					parent
											__a					:						Dict      =					batch_size
											__a					:						Optional[Any]      =					seq_length
											__a					:						int      =					is_training
											__a					:						Union[str, Any]      =					use_input_mask
											__a					:						str      =					use_labels
											__a					:						Tuple      =					vocab_size
											__a					:						Dict      =					hidden_size
											__a					:						Optional[int]      =					projection_dim
											__a					:						Tuple      =					num_hidden_layers
											__a					:						Dict      =					num_attention_heads
											__a					:						Any      =					intermediate_size
											__a					:						Optional[int]      =					dropout
											__a					:						Dict      =					attention_dropout
											__a					:						List[Any]      =					max_position_embeddings
											__a					:						Any      =					initializer_range
											__a					:						Tuple      =					scope
											__a					:						Dict      =					bos_token_id



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




											'''simple docstring'''



											__a					:						Optional[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

											__a					:						Any      =					None
											if self.use_input_mask:
																__a					:						Tuple      =					random_attention_mask([self.batch_size, self.seq_length]   )

											if input_mask is not None:
																__a					:						Tuple      =					input_mask.numpy()
																__a      ,			__a					:						int      =					input_mask.shape
																__a					:						Dict      =					np.random.randint(1 ,					seq_length - 1 ,					size=(batch_size,)   )
																for batch_idx, start_index in enumerate(__UpperCAmelCase   ):
																					__a					:						Any      =					1
																					__a					:						int      =					0

											__a					:						Union[str, Any]      =					self.get_config()

											return config, input_ids, tf.convert_to_tensor(__UpperCAmelCase   )



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




											'''simple docstring'''



											return BlipTextConfig(
											    vocab_size=self.vocab_size ,					hidden_size=self.hidden_size ,					projection_dim=self.projection_dim ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					dropout=self.dropout ,					attention_dropout=self.attention_dropout ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					bos_token_id=self.bos_token_id ,					)



						def        UpperCAmelCase__					(self:					List[str] ,					__UpperCAmelCase:					Any ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					str   )    ->     List[str]:




											'''simple docstring'''



											__a					:						int      =					TFBlipTextModel(config=__UpperCAmelCase   )
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					training=__UpperCAmelCase   )
											__a					:						Any      =					model(__UpperCAmelCase ,					training=__UpperCAmelCase   )
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )
											self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.hidden_size)   )



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




											'''simple docstring'''



											__a					:						Tuple      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a					:						Dict      =					config_and_inputs
											__a					:						Union[str, Any]      =					{"input_ids": input_ids, "attention_mask": input_mask}
											return config, inputs_dict




@require_tf
class 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (TFBlipTextModel,) if is_tf_available() else ()
						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



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




											'''simple docstring'''



											__a					:						str      =					BlipTextModelTester(self   )
											__a					:						str      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					str   )    ->     str:




											'''simple docstring'''



											self.config_tester.run_common_tests()



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




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											pass



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




											'''simple docstring'''



											pass



						@unittest.skip(reason="Blip does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					Any   )    ->     Dict:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     List[str]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     int:




											'''simple docstring'''



											pass



						@slow
						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Tuple:




											'''simple docstring'''



											for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a					:						List[str]      =					TFBlipTextModel.from_pretrained(__UpperCAmelCase   )
																self.assertIsNotNone(__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					Optional[Any]=True   )    ->     Any:




											'''simple docstring'''



											super().test_pt_tf_model_equivalence(allow_missing_keys=__UpperCAmelCase   )





