
"""simple docstring"""




import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
				import os

				# The slow tests are often failing with OOM error on GPU
				# This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
				# but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
				lowercase__							:							List[Any]													=     "platform"

				import jax
				import jax.numpy as jnp

				from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer




def 		A_	(							snake_case							:				Union[str, Any]	, snake_case							:				Any	, snake_case							:				Union[str, Any]=None	, snake_case							:				Optional[Any]=None	) ->      Any:



						'''simple docstring'''

						if attention_mask is None:
												__UpperCamelCase       					=     np.where(input_ids != config.pad_token_id	, 1	, 0	)
						return {
						    "input_ids": input_ids,
						    "attention_mask": attention_mask,
						}






@require_flax
class     SCREAMING_SNAKE_CASE__      :

							"""simple docstring"""






							def __init__(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=13       ,		SCREAMING_SNAKE_CASE_=7       ,		SCREAMING_SNAKE_CASE_=True       ,		SCREAMING_SNAKE_CASE_=False       ,		SCREAMING_SNAKE_CASE_=99       ,		SCREAMING_SNAKE_CASE_=16       ,		SCREAMING_SNAKE_CASE_=2       ,		SCREAMING_SNAKE_CASE_=4       ,		SCREAMING_SNAKE_CASE_=4       ,		SCREAMING_SNAKE_CASE_="gelu"       ,		SCREAMING_SNAKE_CASE_=0.1       ,		SCREAMING_SNAKE_CASE_=0.1       ,		SCREAMING_SNAKE_CASE_=20       ,		SCREAMING_SNAKE_CASE_=2       ,		SCREAMING_SNAKE_CASE_=1       ,		SCREAMING_SNAKE_CASE_=0       ,		SCREAMING_SNAKE_CASE_=16       ,		SCREAMING_SNAKE_CASE_=16       ,		SCREAMING_SNAKE_CASE_=0.0_2       ,		)->				List[Any]:





													'''simple docstring'''






													__UpperCamelCase       					=     parent
													__UpperCamelCase       					=     batch_size
													__UpperCamelCase       					=     seq_length
													__UpperCamelCase       					=     is_training
													__UpperCamelCase       					=     use_labels
													__UpperCamelCase       					=     vocab_size
													__UpperCamelCase       					=     hidden_size
													__UpperCamelCase       					=     num_hidden_layers
													__UpperCamelCase       					=     num_attention_heads
													__UpperCamelCase       					=     intermediate_size
													__UpperCamelCase       					=     hidden_act
													__UpperCamelCase       					=     hidden_dropout_prob
													__UpperCamelCase       					=     attention_probs_dropout_prob
													__UpperCamelCase       					=     max_position_embeddings
													__UpperCamelCase       					=     eos_token_id
													__UpperCamelCase       					=     pad_token_id
													__UpperCamelCase       					=     bos_token_id
													__UpperCamelCase       					=     embed_dim
													__UpperCamelCase       					=     word_embed_proj_dim
													__UpperCamelCase       					=     initializer_range
													__UpperCamelCase       					=     False



							def       A__						(					self     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     np.clip(ids_tensor([self.batch_size, self.seq_length - 1]       ,		self.vocab_size     )       ,		3       ,		self.vocab_size     )
													__UpperCamelCase       					=     np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)       ,		dtype=np.intaa     ))       ,		-1     )

													__UpperCamelCase       					=     OPTConfig(
													    vocab_size=self.vocab_size       ,		hidden_size=self.hidden_size       ,		num_hidden_layers=self.num_hidden_layers       ,		num_attention_heads=self.num_attention_heads       ,		ffn_dim=self.intermediate_size       ,		dropout=self.hidden_dropout_prob       ,		attention_dropout=self.attention_probs_dropout_prob       ,		max_position_embeddings=self.max_position_embeddings       ,		eos_token_id=self.eos_token_id       ,		bos_token_id=self.bos_token_id       ,		pad_token_id=self.pad_token_id       ,		embed_dim=self.embed_dim       ,		is_encoder_decoder=UpperCamelCase_       ,		word_embed_proj_dim=self.word_embed_proj_dim       ,		initializer_range=self.initializer_range       ,		use_cache=UpperCamelCase_       ,		)
													__UpperCamelCase       					=     prepare_opt_inputs_dict(UpperCamelCase_       ,		UpperCamelCase_     )
													return config, inputs_dict



							def       A__						(					self     )->				Any:





													'''simple docstring'''






													__UpperCamelCase					,							__UpperCamelCase       					=     self.prepare_config_and_inputs()
													return config, inputs_dict



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )->				Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase       					=     20
													__UpperCamelCase       					=     model_class_name(UpperCamelCase_     )

													__UpperCamelCase       					=     inputs_dict['''input_ids''']
													__UpperCamelCase       					=     inputs_dict['''attention_mask''']

													__UpperCamelCase       					=     model.init_cache(input_ids.shape[0]       ,		UpperCamelCase_     )
													__UpperCamelCase       					=     jnp.ones((input_ids.shape[0], max_length)       ,		dtype='''i4'''     )

													__UpperCamelCase       					=     jnp.broadcast_to(
													    jnp.arange(input_ids.shape[-1] - 1     )[None, :]       ,		(input_ids.shape[0], input_ids.shape[-1] - 1)       ,		)
													__UpperCamelCase       					=     model(
													    input_ids[:, :-1]       ,		attention_mask=UpperCamelCase_       ,		past_key_values=UpperCamelCase_       ,		position_ids=UpperCamelCase_       ,		)

													__UpperCamelCase       					=     jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]       ,		dtype='''i4'''     )
													__UpperCamelCase       					=     model(
													    input_ids[:, -1:]       ,		attention_mask=UpperCamelCase_       ,		past_key_values=outputs_cache.past_key_values       ,		position_ids=UpperCamelCase_       ,		)

													__UpperCamelCase       					=     model(UpperCamelCase_     )

													__UpperCamelCase       					=     np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])     )     )
													self.parent.assertTrue(diff < 1E-3       ,		msg=F"Max diff is {diff}"     )



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )->				int:





													'''simple docstring'''






													__UpperCamelCase       					=     20
													__UpperCamelCase       					=     model_class_name(UpperCamelCase_     )

													__UpperCamelCase					,							__UpperCamelCase       					=     (
													    inputs_dict['''input_ids'''],
													    inputs_dict['''attention_mask'''],
													)

													__UpperCamelCase       					=     jnp.concatenate(
													    [
													        attention_mask,
													        jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])     ),
													    ]       ,		axis=-1       ,		)

													__UpperCamelCase       					=     model.init_cache(input_ids.shape[0]       ,		UpperCamelCase_     )
													__UpperCamelCase       					=     jnp.broadcast_to(
													    jnp.arange(input_ids.shape[-1] - 1     )[None, :]       ,		(input_ids.shape[0], input_ids.shape[-1] - 1)       ,		)

													__UpperCamelCase       					=     model(
													    input_ids[:, :-1]       ,		attention_mask=UpperCamelCase_       ,		past_key_values=UpperCamelCase_       ,		position_ids=UpperCamelCase_       ,		)
													__UpperCamelCase       					=     jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]       ,		dtype='''i4'''     )
													__UpperCamelCase       					=     model(
													    input_ids[:, -1:]       ,		past_key_values=outputs_cache.past_key_values       ,		attention_mask=UpperCamelCase_       ,		position_ids=UpperCamelCase_       ,		)

													__UpperCamelCase       					=     model(UpperCamelCase_       ,		attention_mask=UpperCamelCase_     )

													__UpperCamelCase       					=     np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])     )     )
													self.parent.assertTrue(diff < 1E-3       ,		msg=F"Max diff is {diff}"     )






@require_flax
class     SCREAMING_SNAKE_CASE__      (							lowercase_				,						unittest.TestCase				,						lowercase_      ):

							"""simple docstring"""






							_snake_case       						=					(FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
							_snake_case       						=					() if is_flax_available() else ()



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





													'''simple docstring'''






													__UpperCamelCase       					=     FlaxOPTModelTester(self     )



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





													'''simple docstring'''






													__UpperCamelCase					,							__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs()
													for model_class in self.all_model_classes:
																			self.model_tester.check_use_cache_forward(UpperCamelCase_       ,		UpperCamelCase_       ,		UpperCamelCase_     )



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





													'''simple docstring'''






													__UpperCamelCase					,							__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs()
													for model_class in self.all_model_classes:
																			self.model_tester.check_use_cache_forward_with_attn_mask(UpperCamelCase_       ,		UpperCamelCase_       ,		UpperCamelCase_     )



							@slow
							def       A__						(					self     )->				Any:





													'''simple docstring'''






													for model_class_name in self.all_model_classes:
																			__UpperCamelCase       					=     model_class_name.from_pretrained('''facebook/opt-125m'''     )
																			__UpperCamelCase       					=     np.ones((1, 1)     ) * model.config.eos_token_id
																			__UpperCamelCase       					=     model(UpperCamelCase_     )
																			self.assertIsNotNone(UpperCamelCase_     )






@require_sentencepiece
@require_flax
class     SCREAMING_SNAKE_CASE__      (							unittest.TestCase      ):

							"""simple docstring"""






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





													'''simple docstring'''






													__UpperCamelCase       					=     FlaxOPTModel.from_pretrained('''facebook/opt-350m'''     )
													__UpperCamelCase       					=     jnp.array([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]     )
													__UpperCamelCase       					=     model(input_ids=UpperCamelCase_     ).last_hidden_state
													__UpperCamelCase       					=     (1, 11, 512)
													self.assertEqual(output.shape       ,		UpperCamelCase_     )
													__UpperCamelCase       					=     jnp.array(
													    [[-0.2_8_6_7, -1.9_2_5_6, -0.3_0_6_2], [-1.2_7_1_1, -0.1_3_3_7, -0.1_8_9_7], [0.4_1_0_9, 0.1_1_8_7, -1.3_1_4_2]]     )
													self.assertTrue(jnp.allclose(output[:, :3, :3]       ,		UpperCamelCase_       ,		atol=4E-2     )     )






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

							"""simple docstring"""






							def       A__						(					self     )->				int:





													'''simple docstring'''






													super().setUp()
													__UpperCamelCase       					=     '''facebook/opt-350m'''



							def       A__						(					self     )->				int:





													'''simple docstring'''






													__UpperCamelCase       					=     FlaxOPTForCausalLM.from_pretrained(self.path_model     )
													__UpperCamelCase       					=     GPTaTokenizer.from_pretrained(self.path_model     )

													__UpperCamelCase       					=     [
													    '''Today is a beautiful day and I want to''',
													    '''In the city of''',
													    '''Paris is the capital of France and''',
													    '''Computers and mobile phones have taken''',
													]
													# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
													__UpperCamelCase       					=     tokenizer(UpperCamelCase_       ,		return_tensors='''jax'''       ,		padding=UpperCamelCase_       ,		add_special_tokens=UpperCamelCase_     )
													__UpperCamelCase       					=     model(inputs.input_ids       ,		attention_mask=inputs.attention_mask     )[0].mean(axis=-1     )
													__UpperCamelCase       					=     jnp.array(
													    [
													        [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
													        [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
													        [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
													        [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
													    ]     )
													self.assertTrue(jnp.allclose(UpperCamelCase_       ,		UpperCamelCase_       ,		atol=4E-2     )     )

													__UpperCamelCase       					=     jax.jit(UpperCamelCase_     )
													__UpperCamelCase       					=     model(inputs.input_ids       ,		attention_mask=inputs.attention_mask     )[0].mean(axis=-1     )
													self.assertTrue(jnp.allclose(UpperCamelCase_       ,		UpperCamelCase_       ,		atol=4E-2     )     )






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

							"""simple docstring"""






							@property
							def       A__						(					self     )->				Optional[Any]:





													'''simple docstring'''






													return [
													    "Today is a beautiful day and I want",
													    "In the city of",
													    "Paris is the capital of France and",
													    "Computers and mobile phones have taken",
													]



							def       A__						(					self     )->				Any:





													'''simple docstring'''






													__UpperCamelCase       					=     '''facebook/opt-125m'''

													__UpperCamelCase       					=     [
													    '''Today is a beautiful day and I want to''',
													    '''In the city of New York, the city''',
													    '''Paris is the capital of France and the capital''',
													    '''Computers and mobile phones have taken over the''',
													]

													__UpperCamelCase       					=     []

													__UpperCamelCase       					=     FlaxOPTForCausalLM.from_pretrained(UpperCamelCase_     )
													__UpperCamelCase       					=     GPTaTokenizer.from_pretrained(UpperCamelCase_     )

													for prompt in self.prompts:
																			__UpperCamelCase       					=     tokenizer(UpperCamelCase_       ,		return_tensors='''jax'''     ).input_ids

																			__UpperCamelCase       					=     model.generate(UpperCamelCase_       ,		max_length=10     )
																			__UpperCamelCase       					=     generated_ids[0]

																			__UpperCamelCase       					=     tokenizer.batch_decode(UpperCamelCase_       ,		skip_special_tokens=UpperCamelCase_     )
																			predicted_outputs += generated_string

													self.assertListEqual(UpperCamelCase_       ,		UpperCamelCase_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''facebook/opt-350m'''

													__UpperCamelCase       					=     [
													    '''Today is a beautiful day and I want to''',
													    '''In the city of San Francisco, the city''',
													    '''Paris is the capital of France and the capital''',
													    '''Computers and mobile phones have taken over the''',
													]

													__UpperCamelCase       					=     []
													__UpperCamelCase       					=     FlaxOPTForCausalLM.from_pretrained(UpperCamelCase_     )
													__UpperCamelCase       					=     GPTaTokenizer.from_pretrained(UpperCamelCase_     )

													for prompt in self.prompts:
																			__UpperCamelCase       					=     tokenizer(UpperCamelCase_       ,		return_tensors='''jax'''     ).input_ids

																			__UpperCamelCase       					=     model.generate(UpperCamelCase_       ,		max_length=10     )
																			__UpperCamelCase       					=     generated_ids[0]

																			__UpperCamelCase       					=     tokenizer.batch_decode(UpperCamelCase_       ,		skip_special_tokens=UpperCamelCase_     )
																			predicted_outputs += generated_string

													self.assertListEqual(UpperCamelCase_       ,		UpperCamelCase_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''facebook/opt-125m'''
													__UpperCamelCase       					=     [
													    '''Today is a beautiful day and I want to thank''',
													    '''In the city of Rome Canaver Canaver Canaver Canaver''',
													]
													__UpperCamelCase       					=     FlaxOPTForCausalLM.from_pretrained(UpperCamelCase_     )
													__UpperCamelCase       					=     GPTaTokenizer.from_pretrained(UpperCamelCase_     )
													__UpperCamelCase       					=     tokenizer(
													    [
													        '''Today is a beautiful day and I want to''',
													        '''In the city of''',
													    ]       ,		return_tensors='''jax'''       ,		padding=UpperCamelCase_       ,		)

													__UpperCamelCase       					=     jax.jit(model.generate     )

													__UpperCamelCase       					=     jit_generate(inputs['''input_ids''']       ,		attention_mask=inputs['''attention_mask''']     ).sequences

													__UpperCamelCase       					=     tokenizer.batch_decode(UpperCamelCase_       ,		skip_special_tokens=UpperCamelCase_     )

													self.assertIsNotNone(UpperCamelCase_       ,		UpperCamelCase_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''facebook/opt-350m'''

													__UpperCamelCase       					=     GPTaTokenizer.from_pretrained(UpperCamelCase_     )
													__UpperCamelCase       					=     FlaxOPTForCausalLM.from_pretrained(UpperCamelCase_     )

													__UpperCamelCase       					=     '''left'''

													# use different length sentences to test batching
													__UpperCamelCase       					=     [
													    '''Hello, my dog is a little''',
													    '''Today, I''',
													]

													__UpperCamelCase       					=     tokenizer(UpperCamelCase_       ,		return_tensors='''jax'''       ,		padding=UpperCamelCase_     )
													__UpperCamelCase       					=     inputs['''input_ids''']

													__UpperCamelCase       					=     model.generate(input_ids=UpperCamelCase_       ,		attention_mask=inputs['''attention_mask''']       ,		trace=UpperCamelCase_     )

													__UpperCamelCase       					=     tokenizer(sentences[0]       ,		return_tensors='''jax'''     ).input_ids
													__UpperCamelCase       					=     model.generate(input_ids=UpperCamelCase_     )

													__UpperCamelCase       					=     inputs_non_padded.shape[-1] - inputs['''attention_mask'''][-1].sum()
													__UpperCamelCase       					=     tokenizer(sentences[1]       ,		return_tensors='''jax'''     ).input_ids
													__UpperCamelCase       					=     model.generate(input_ids=UpperCamelCase_       ,		max_length=model.config.max_length - num_paddings     )

													__UpperCamelCase       					=     tokenizer.batch_decode(outputs[0]       ,		skip_special_tokens=UpperCamelCase_     )
													__UpperCamelCase       					=     tokenizer.decode(output_non_padded[0][0]       ,		skip_special_tokens=UpperCamelCase_     )
													__UpperCamelCase       					=     tokenizer.decode(output_padded[0][0]       ,		skip_special_tokens=UpperCamelCase_     )

													__UpperCamelCase       					=     [
													    '''Hello, my dog is a little bit of a dork.\nI\'m a little bit''',
													    '''Today, I was in the middle of a conversation with a friend about the''',
													]
													self.assertListEqual(UpperCamelCase_       ,		UpperCamelCase_     )
													self.assertListEqual(UpperCamelCase_       ,		[non_padded_sentence, padded_sentence]     )






import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class     SCREAMING_SNAKE_CASE__      (							SCREAMING_SNAKE_CASE_				,						SCREAMING_SNAKE_CASE_				,						unittest.TestCase      ):

							"""simple docstring"""






							_snake_case       						=					StableDiffusionParadigmsPipeline
							_snake_case       						=					TEXT_TO_IMAGE_PARAMS
							_snake_case       						=					TEXT_TO_IMAGE_BATCH_PARAMS
							_snake_case       						=					TEXT_TO_IMAGE_IMAGE_PARAMS
							_snake_case       						=					TEXT_TO_IMAGE_IMAGE_PARAMS



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





													'''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       ,		attention_head_dim=(2, 4)       ,		use_linear_projection=SCREAMING_SNAKE_CASE_       ,		)
													__UpperCamelCase       					=     DDIMParallelScheduler(
													    beta_start=0.0_0_0_8_5       ,		beta_end=0.0_1_2       ,		beta_schedule='''scaled_linear'''       ,		clip_sample=SCREAMING_SNAKE_CASE_       ,		set_alpha_to_one=SCREAMING_SNAKE_CASE_       ,		)
													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       ,		sample_size=128       ,		)
													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       ,		hidden_act='''gelu'''       ,		projection_dim=512       ,		)
													__UpperCamelCase       					=     CLIPTextModel(SCREAMING_SNAKE_CASE_     )
													__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       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=0     )->				List[Any]:





													'''simple docstring'''






													if str(SCREAMING_SNAKE_CASE_     ).startswith('''mps'''     ):
																			__UpperCamelCase       					=     torch.manual_seed(SCREAMING_SNAKE_CASE_     )
													else:
																			__UpperCamelCase       					=     torch.Generator(device=SCREAMING_SNAKE_CASE_     ).manual_seed(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     {
													    '''prompt''': '''a photograph of an astronaut riding a horse''',
													    '''generator''': generator,
													    '''num_inference_steps''': 10,
													    '''guidance_scale''': 6.0,
													    '''output_type''': '''numpy''',
													    '''parallel''': 3,
													    '''debug''': True,
													}
													return inputs



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
													__UpperCamelCase       					=     self.get_dummy_components()
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe.to(SCREAMING_SNAKE_CASE_     )
													sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe(**SCREAMING_SNAKE_CASE_     ).images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1]
													assert image.shape == (1, 64, 64, 3)

													__UpperCamelCase       					=     np.array([0.4_7_7_3, 0.5_4_1_7, 0.4_7_2_3, 0.4_9_2_5, 0.5_6_3_1, 0.4_7_5_2, 0.5_2_4_0, 0.4_9_3_5, 0.5_0_2_3]     )

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



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
													__UpperCamelCase       					=     self.get_dummy_components()
													torch.manual_seed(0     )
													__UpperCamelCase       					=     DDPMParallelScheduler()
													torch.manual_seed(0     )
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe.to(SCREAMING_SNAKE_CASE_     )
													sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe(**SCREAMING_SNAKE_CASE_     ).images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1]
													assert image.shape == (1, 64, 64, 3)

													__UpperCamelCase       					=     np.array([0.3_5_7_3, 0.4_4_2_0, 0.4_9_6_0, 0.4_7_9_9, 0.3_7_9_6, 0.3_8_7_9, 0.4_8_1_9, 0.4_3_6_5, 0.4_4_6_8]     )

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



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





													'''simple docstring'''






													super().test_inference_batch_consistent(batch_sizes=[1, 2]     )



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





													'''simple docstring'''






													super().test_inference_batch_single_identical(batch_size=2       ,		expected_max_diff=3E-3     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''cpu'''  # ensure determinism for the device-dependent torch.Generator
													__UpperCamelCase       					=     self.get_dummy_components()
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline(**SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     sd_pipe.to(SCREAMING_SNAKE_CASE_     )
													sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     '''french fries'''
													__UpperCamelCase       					=     sd_pipe(**SCREAMING_SNAKE_CASE_       ,		negative_prompt=SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     output.images
													__UpperCamelCase       					=     image[0, -3:, -3:, -1]

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

													__UpperCamelCase       					=     np.array([0.4_7_7_1, 0.5_4_2_0, 0.4_6_8_3, 0.4_9_1_8, 0.5_6_3_6, 0.4_7_2_5, 0.5_2_3_0, 0.4_9_2_3, 0.5_0_1_5]     )

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






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

							"""simple docstring"""






							def       A__						(					self     )->				Any:





													'''simple docstring'''






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



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_=0     )->				Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase       					=     torch.Generator(device=SCREAMING_SNAKE_CASE_     ).manual_seed(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     {
													    '''prompt''': '''a photograph of an astronaut riding a horse''',
													    '''generator''': generator,
													    '''num_inference_steps''': 10,
													    '''guidance_scale''': 7.5,
													    '''output_type''': '''numpy''',
													    '''parallel''': 3,
													    '''debug''': True,
													}
													return inputs



							def       A__						(					self     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     '''stabilityai/stable-diffusion-2-base'''
													__UpperCamelCase       					=     DDIMParallelScheduler.from_pretrained(SCREAMING_SNAKE_CASE_       ,		subfolder='''scheduler'''     )
													__UpperCamelCase       					=     StableDiffusionParadigmsPipeline.from_pretrained(SCREAMING_SNAKE_CASE_       ,		scheduler=SCREAMING_SNAKE_CASE_       ,		safety_checker=SCREAMING_SNAKE_CASE_     )
													pipe.to(SCREAMING_SNAKE_CASE_     )
													pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_     )
													pipe.enable_attention_slicing()

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

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

													__UpperCamelCase       					=     np.array([0.9_6_2_2, 0.9_6_0_2, 0.9_7_4_8, 0.9_5_9_1, 0.9_6_3_0, 0.9_6_9_1, 0.9_6_6_1, 0.9_6_3_1, 0.9_7_4_1]     )

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




