
import unittest
from unittest.util import safe_repr

from transformers import AutoTokenizer, RwkvConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
				    RwkvForCausalLM,
				    RwkvModel,
				)
				from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_0
else:
				lowercase__							:							int													=     False






class     SCREAMING_SNAKE_CASE__      :

							"""simple docstring"""






							def __init__(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=14       ,		SCREAMING_SNAKE_CASE_=7       ,		SCREAMING_SNAKE_CASE_=True       ,		SCREAMING_SNAKE_CASE_=False       ,		SCREAMING_SNAKE_CASE_=True       ,		SCREAMING_SNAKE_CASE_=True       ,		SCREAMING_SNAKE_CASE_=True       ,		SCREAMING_SNAKE_CASE_=99       ,		SCREAMING_SNAKE_CASE_=32       ,		SCREAMING_SNAKE_CASE_=5       ,		SCREAMING_SNAKE_CASE_=37       ,		SCREAMING_SNAKE_CASE_="gelu"       ,		SCREAMING_SNAKE_CASE_=0.1       ,		SCREAMING_SNAKE_CASE_=0.1       ,		SCREAMING_SNAKE_CASE_=512       ,		SCREAMING_SNAKE_CASE_=16       ,		SCREAMING_SNAKE_CASE_=2       ,		SCREAMING_SNAKE_CASE_=3       ,		SCREAMING_SNAKE_CASE_=4       ,		SCREAMING_SNAKE_CASE_=None       ,		)->				Tuple:





													'''simple docstring'''






													__UpperCamelCase       					=     parent
													__UpperCamelCase       					=     batch_size
													__UpperCamelCase       					=     seq_length
													__UpperCamelCase       					=     is_training
													__UpperCamelCase       					=     use_token_type_ids
													__UpperCamelCase       					=     use_input_mask
													__UpperCamelCase       					=     use_labels
													__UpperCamelCase       					=     use_mc_token_ids
													__UpperCamelCase       					=     vocab_size
													__UpperCamelCase       					=     hidden_size
													__UpperCamelCase       					=     num_hidden_layers
													__UpperCamelCase       					=     intermediate_size
													__UpperCamelCase       					=     hidden_act
													__UpperCamelCase       					=     hidden_dropout_prob
													__UpperCamelCase       					=     attention_probs_dropout_prob
													__UpperCamelCase       					=     max_position_embeddings
													__UpperCamelCase       					=     type_vocab_size
													__UpperCamelCase       					=     type_sequence_label_size
													__UpperCamelCase       					=     num_labels
													__UpperCamelCase       					=     num_choices
													__UpperCamelCase       					=     scope
													__UpperCamelCase       					=     vocab_size - 1
													__UpperCamelCase       					=     vocab_size - 1
													__UpperCamelCase       					=     vocab_size - 1



							def       A__						(					self     )->				str:





													'''simple docstring'''






													return RwkvConfig.from_pretrained('''sgugger/rwkv-4-pile-7b'''     )



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_=False       ,		SCREAMING_SNAKE_CASE_=False       ,		SCREAMING_SNAKE_CASE_=False     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size     )

													__UpperCamelCase       					=     None
													if self.use_input_mask:
																			__UpperCamelCase       					=     random_attention_mask([self.batch_size, self.seq_length]     )

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

													__UpperCamelCase       					=     None
													if self.use_mc_token_ids:
																			__UpperCamelCase       					=     ids_tensor([self.batch_size, self.num_choices]       ,		self.seq_length     )

													__UpperCamelCase       					=     None
													__UpperCamelCase       					=     None
													__UpperCamelCase       					=     None
													if self.use_labels:
																			__UpperCamelCase       					=     ids_tensor([self.batch_size]       ,		self.type_sequence_label_size     )
																			__UpperCamelCase       					=     ids_tensor([self.batch_size, self.seq_length]       ,		self.num_labels     )
																			__UpperCamelCase       					=     ids_tensor([self.batch_size]       ,		self.num_choices     )

													__UpperCamelCase       					=     self.get_config(
													    gradient_checkpointing=SCREAMING_SNAKE_CASE_       ,		scale_attn_by_inverse_layer_idx=SCREAMING_SNAKE_CASE_       ,		reorder_and_upcast_attn=SCREAMING_SNAKE_CASE_       ,		)

													return (
													    config,
													    input_ids,
													    input_mask,
													    None,
													    token_type_ids,
													    mc_token_ids,
													    sequence_labels,
													    token_labels,
													    choice_labels,
													)



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_=False       ,		SCREAMING_SNAKE_CASE_=False       ,		SCREAMING_SNAKE_CASE_=False     )->				Dict:





													'''simple docstring'''






													return RwkvConfig(
													    vocab_size=self.vocab_size       ,		hidden_size=self.hidden_size       ,		num_hidden_layers=self.num_hidden_layers       ,		intermediate_size=self.intermediate_size       ,		activation_function=self.hidden_act       ,		resid_pdrop=self.hidden_dropout_prob       ,		attn_pdrop=self.attention_probs_dropout_prob       ,		n_positions=self.max_position_embeddings       ,		type_vocab_size=self.type_vocab_size       ,		use_cache=SCREAMING_SNAKE_CASE_       ,		bos_token_id=self.bos_token_id       ,		eos_token_id=self.eos_token_id       ,		pad_token_id=self.pad_token_id       ,		gradient_checkpointing=SCREAMING_SNAKE_CASE_       ,		scale_attn_by_inverse_layer_idx=SCREAMING_SNAKE_CASE_       ,		reorder_and_upcast_attn=SCREAMING_SNAKE_CASE_       ,		)



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





													'''simple docstring'''






													__UpperCamelCase       					=     self.get_config()
													__UpperCamelCase       					=     300
													return config



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





													'''simple docstring'''






													(
													    (
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							
													)       					=     self.prepare_config_and_inputs()

													__UpperCamelCase       					=     floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
													__UpperCamelCase       					=     ids_tensor([self.batch_size, self.seq_length]       ,		vocab_size=2     )

													return (
													    config,
													    input_ids,
													    input_mask,
													    head_mask,
													    token_type_ids,
													    sequence_labels,
													    token_labels,
													    choice_labels,
													    encoder_hidden_states,
													    encoder_attention_mask,
													)



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		*SCREAMING_SNAKE_CASE_     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     True
													__UpperCamelCase       					=     RwkvModel(config=SCREAMING_SNAKE_CASE_     )
													model.to(SCREAMING_SNAKE_CASE_     )
													model.eval()

													__UpperCamelCase       					=     model(SCREAMING_SNAKE_CASE_     )

													self.parent.assertEqual(result.last_hidden_state.shape       ,		(self.batch_size, self.seq_length, self.hidden_size)     )
													self.parent.assertEqual(len(result.hidden_states     )       ,		config.num_hidden_layers + 1     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     RwkvForCausalLM(SCREAMING_SNAKE_CASE_     )
													model.to(SCREAMING_SNAKE_CASE_     )
													model.eval()

													__UpperCamelCase       					=     model(SCREAMING_SNAKE_CASE_       ,		labels=SCREAMING_SNAKE_CASE_     )
													self.parent.assertEqual(result.loss.shape       ,		()     )
													self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.vocab_size)     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     RwkvModel(config=SCREAMING_SNAKE_CASE_     )
													model.to(SCREAMING_SNAKE_CASE_     )
													model.eval()

													__UpperCamelCase       					=     model(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     outputs.last_hidden_state

													__UpperCamelCase       					=     model(input_ids[:, :2]     )
													__UpperCamelCase       					=     outputs.last_hidden_state

													# Using the state computed on the first inputs, we will get the same output
													__UpperCamelCase       					=     model(input_ids[:, 2:]       ,		state=outputs.state     )
													__UpperCamelCase       					=     outputs.last_hidden_state

													self.parent.assertTrue(torch.allclose(torch.cat([output_one, output_two]       ,		dim=1     )       ,		SCREAMING_SNAKE_CASE_       ,		atol=1E-5     )     )



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		*SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=False     )->				List[Any]:





													'''simple docstring'''






													__UpperCamelCase       					=     RwkvForCausalLM(SCREAMING_SNAKE_CASE_     )
													model.to(SCREAMING_SNAKE_CASE_     )
													if gradient_checkpointing:
																			model.gradient_checkpointing_enable()

													__UpperCamelCase       					=     model(SCREAMING_SNAKE_CASE_       ,		labels=SCREAMING_SNAKE_CASE_     )
													self.parent.assertEqual(result.loss.shape       ,		()     )
													self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.seq_length, self.vocab_size)     )
													result.loss.backward()



							def       A__						(					self     )->				int:





													'''simple docstring'''






													__UpperCamelCase       					=     self.prepare_config_and_inputs()

													(
													    (
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							(
													    __UpperCamelCase
													)					,							
													)       					=     config_and_inputs

													__UpperCamelCase       					=     {'''input_ids''': input_ids}

													return config, inputs_dict






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0				,						reason='See https://github.com/huggingface/transformers/pull/24204'      )
@require_torch
class     SCREAMING_SNAKE_CASE__      (							SCREAMING_SNAKE_CASE_				,						SCREAMING_SNAKE_CASE_				,						SCREAMING_SNAKE_CASE_				,						unittest.TestCase      ):

							"""simple docstring"""






							_snake_case       						=					(RwkvModel, RwkvForCausalLM) if is_torch_available() else ()
							_snake_case       						=					(
							    {'feature-extraction': RwkvModel, 'text-generation': RwkvForCausalLM} if is_torch_available() else {}
							)
							# all_generative_model_classes = (RwkvForCausalLM,) if is_torch_available() else ()
							_snake_case       						=					False
							_snake_case       						=					False
							_snake_case       						=					False
							_snake_case       						=					False
							_snake_case       						=					False  # Rwkv does not support head masking



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





													'''simple docstring'''






													__UpperCamelCase       					=     RwkvModelTester(self     )
													__UpperCamelCase       					=     ConfigTester(
													    self       ,		config_class=SCREAMING_SNAKE_CASE_       ,		n_embd=37       ,		common_properties=['''hidden_size''', '''num_hidden_layers''']     )



							def       A__						(					self       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_=None     )->				List[str]:





													'''simple docstring'''






													if isinstance(SCREAMING_SNAKE_CASE_       ,		torch.Tensor     ):
																			__UpperCamelCase					,							__UpperCamelCase       					=     member.max().item(), member.min().item()
													elif isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ) or isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ):
																			__UpperCamelCase					,							__UpperCamelCase       					=     max(SCREAMING_SNAKE_CASE_     ), min(SCREAMING_SNAKE_CASE_     )

													if not isinstance(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     ):
																			raise TypeError('''container should be a list or tuple'''     )
													elif len(SCREAMING_SNAKE_CASE_     ) != 2:
																			raise ValueError('''container should have 2 elements'''     )

													__UpperCamelCase					,							__UpperCamelCase       					=     container

													__UpperCamelCase       					=     (min_value >= expected_min) and (max_value <= expected_max)

													if not is_inside_interval:
																			__UpperCamelCase       					=     '''%s not found in %s''' % (safe_repr(SCREAMING_SNAKE_CASE_     ), safe_repr(SCREAMING_SNAKE_CASE_     ))
																			self.fail(self._formatMessage(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )     )



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





													'''simple docstring'''






													self.config_tester.run_common_tests()



							def       A__						(					self     )->				str:





													'''simple docstring'''






													__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_rwkv_model(*SCREAMING_SNAKE_CASE_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_causl_lm(*SCREAMING_SNAKE_CASE_     )



							def       A__						(					self     )->				Tuple:





													'''simple docstring'''






													__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_state_equivalency(*SCREAMING_SNAKE_CASE_     )



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





													'''simple docstring'''






													__UpperCamelCase					,							__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																			__UpperCamelCase       					=     model_class(config=SCREAMING_SNAKE_CASE_     )
																			for name, param in model.named_parameters():
																									if "time_decay" in name:
																															if param.requires_grad:
																																					self.assertTrue(param.data.max().item() == 3.0     )
																																					self.assertTrue(param.data.min().item() == -5.0     )
																									elif "time_first" in name:
																															if param.requires_grad:
																																					# check if it's a ones like
																																					self.assertTrue(torch.allclose(param.data       ,		torch.ones_like(param.data     )       ,		atol=1E-5       ,		rtol=1E-5     )     )
																									elif any(x in name for x in ['''time_mix_key''', '''time_mix_receptance''']     ):
																															if param.requires_grad:
																																					self.assertInterval(
																																					    param.data       ,		[0.0, 1.0]       ,		msg=F"Parameter {name} of model {model_class} seems not properly initialized"       ,		)
																									elif "time_mix_value" in name:
																															if param.requires_grad:
																																					self.assertInterval(
																																					    param.data       ,		[0.0, 1.3]       ,		msg=F"Parameter {name} of model {model_class} seems not properly initialized"       ,		)



							def       A__						(					self     )->				str:





													'''simple docstring'''






													__UpperCamelCase					,							__UpperCamelCase       					=     self.model_tester.prepare_config_and_inputs_for_common()
													__UpperCamelCase       					=     True

													__UpperCamelCase       					=     getattr(self.model_tester       ,		'''seq_length'''       ,		SCREAMING_SNAKE_CASE_     )

													for model_class in self.all_model_classes:
																			__UpperCamelCase       					=     True
																			__UpperCamelCase       					=     False
																			__UpperCamelCase       					=     True
																			__UpperCamelCase       					=     model_class(SCREAMING_SNAKE_CASE_     )
																			model.to(SCREAMING_SNAKE_CASE_     )
																			model.eval()

																			__UpperCamelCase       					=     self._prepare_for_class(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )
																			__UpperCamelCase       					=     inputs['''input_ids'''].shape[0]
																			with torch.no_grad():
																									__UpperCamelCase       					=     model(**SCREAMING_SNAKE_CASE_     )
																			__UpperCamelCase       					=     outputs.attentions
																			self.assertEqual(len(SCREAMING_SNAKE_CASE_     )       ,		self.model_tester.num_hidden_layers     )

																			# check that output_attentions also work using config
																			del inputs_dict["output_attentions"]
																			__UpperCamelCase       					=     True
																			__UpperCamelCase       					=     model_class(SCREAMING_SNAKE_CASE_     )
																			model.to(SCREAMING_SNAKE_CASE_     )
																			model.eval()

																			__UpperCamelCase       					=     self._prepare_for_class(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )
																			__UpperCamelCase       					=     inputs['''input_ids'''].shape[0]
																			with torch.no_grad():
																									__UpperCamelCase       					=     model(**SCREAMING_SNAKE_CASE_     )
																			__UpperCamelCase       					=     outputs.attentions
																			self.assertEqual(len(SCREAMING_SNAKE_CASE_     )       ,		self.model_tester.num_hidden_layers     )

																			self.assertListEqual(
																			    list(attentions[0].shape[-3:]     )       ,		[batch_size, seq_len, config.hidden_size]       ,		)
																			__UpperCamelCase       					=     len(SCREAMING_SNAKE_CASE_     )

																			# Check attention is always last and order is fine
																			__UpperCamelCase       					=     True
																			__UpperCamelCase       					=     True
																			__UpperCamelCase       					=     model_class(SCREAMING_SNAKE_CASE_     )
																			model.to(SCREAMING_SNAKE_CASE_     )
																			model.eval()

																			__UpperCamelCase       					=     self._prepare_for_class(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )
																			__UpperCamelCase       					=     inputs['''input_ids'''].shape[0]
																			with torch.no_grad():
																									__UpperCamelCase       					=     model(**SCREAMING_SNAKE_CASE_     )

																			__UpperCamelCase       					=     1
																			self.assertEqual(out_len + added_hidden_states       ,		len(SCREAMING_SNAKE_CASE_     )     )

																			__UpperCamelCase       					=     outputs.attentions

																			self.assertEqual(len(SCREAMING_SNAKE_CASE_     )       ,		self.model_tester.num_hidden_layers     )
																			self.assertListEqual(
																			    list(self_attentions[0].shape[-3:]     )       ,		[batch_size, seq_len, config.hidden_size]       ,		)



							@slow
							def       A__						(					self     )->				int:





													'''simple docstring'''






													for model_name in RWKV_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__UpperCamelCase       					=     RwkvModel.from_pretrained(SCREAMING_SNAKE_CASE_     )
																			self.assertIsNotNone(SCREAMING_SNAKE_CASE_     )






@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0				,						reason='See https://github.com/huggingface/transformers/pull/24204'      )
@slow
class     SCREAMING_SNAKE_CASE__      (							unittest.TestCase      ):

							"""simple docstring"""






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





													'''simple docstring'''






													__UpperCamelCase       					=     '''RWKV/rwkv-4-169m-pile'''
													__UpperCamelCase       					=     AutoTokenizer.from_pretrained(self.model_id     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''Hello my name is Jasmine and I am a newbie to the'''
													__UpperCamelCase       					=     RwkvForCausalLM.from_pretrained(self.model_id     ).to(SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     self.tokenizer('''Hello my name is'''       ,		return_tensors='''pt'''     ).input_ids.to(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     model.generate(SCREAMING_SNAKE_CASE_       ,		max_new_tokens=10     )
													__UpperCamelCase       					=     self.tokenizer.decode(output[0].tolist()     )

													self.assertEqual(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )



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





													'''simple docstring'''






													__UpperCamelCase       					=     '''Hello my name is Jasmine and I am a newbie to the'''

													__UpperCamelCase       					=     self.tokenizer('''Hello my name is'''       ,		return_tensors='''pt'''     ).input_ids.to(SCREAMING_SNAKE_CASE_     )
													__UpperCamelCase       					=     RwkvForCausalLM.from_pretrained(self.model_id       ,		torch_dtype=torch.bfloataa     ).to(SCREAMING_SNAKE_CASE_     )

													__UpperCamelCase       					=     model.generate(SCREAMING_SNAKE_CASE_       ,		max_new_tokens=10     )
													__UpperCamelCase       					=     self.tokenizer.decode(output[0].tolist()     )

													self.assertEqual(SCREAMING_SNAKE_CASE_       ,		SCREAMING_SNAKE_CASE_     )






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




