


"""simple docstring"""


import unittest

from transformers import XLMConfig, 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, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        XLMForMultipleChoice,
        XLMForQuestionAnswering,
        XLMForQuestionAnsweringSimple,
        XLMForSequenceClassification,
        XLMForTokenClassification,
        XLMModel,
        XLMWithLMHeadModel,
    )
    from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST


class 			A__	:


     """simple docstring"""

     def __init__(	self:						int     ,		__a:						Any     ,		__a:						List[Any]=13     ,		__a:						Optional[Any]=7     ,		__a:						Optional[int]=True     ,		__a:						Any=True     ,		__a:						Optional[Any]=True     ,		__a:						Any=True     ,		__a:						int=True     ,		__a:						Optional[int]=False     ,		__a:						Dict=False     ,		__a:						List[Any]=False     ,		__a:						str=2     ,		__a:						List[str]=99     ,		__a:						int=0     ,		__a:						int=32     ,		__a:						List[Any]=5     ,		__a:						Tuple=4     ,		__a:						Dict=0.1     ,		__a:						Tuple=0.1     ,		__a:						str=512     ,		__a:						Optional[int]=2     ,		__a:						Tuple=0.02     ,		__a:						List[str]=2     ,		__a:						Optional[int]=4     ,		__a:						Union[str, Any]="last"     ,		__a:						Union[str, Any]=True     ,		__a:						Optional[Any]=None     ,		__a:						int=0     ,		)->    Dict:
           lowerCamelCase					:					List[str]        =	parent
           lowerCamelCase					:					Tuple        =	batch_size
           lowerCamelCase					:					Union[str, Any]        =	seq_length
           lowerCamelCase					:					Dict        =	is_training
           lowerCamelCase					:					Dict        =	use_input_lengths
           lowerCamelCase					:					List[str]        =	use_token_type_ids
           lowerCamelCase					:					Dict        =	use_labels
           lowerCamelCase					:					int        =	gelu_activation
           lowerCamelCase					:					List[str]        =	sinusoidal_embeddings
           lowerCamelCase					:					Tuple        =	causal
           lowerCamelCase					:					Tuple        =	asm
           lowerCamelCase					:					List[Any]        =	n_langs
           lowerCamelCase					:					List[Any]        =	vocab_size
           lowerCamelCase					:					List[Any]        =	n_special
           lowerCamelCase					:					Dict        =	hidden_size
           lowerCamelCase					:					str        =	num_hidden_layers
           lowerCamelCase					:					Optional[Any]        =	num_attention_heads
           lowerCamelCase					:					Dict        =	hidden_dropout_prob
           lowerCamelCase					:					Optional[Any]        =	attention_probs_dropout_prob
           lowerCamelCase					:					str        =	max_position_embeddings
           lowerCamelCase					:					List[Any]        =	type_sequence_label_size
           lowerCamelCase					:					Optional[int]        =	initializer_range
           lowerCamelCase					:					Optional[Any]        =	num_labels
           lowerCamelCase					:					str        =	num_choices
           lowerCamelCase					:					Tuple        =	summary_type
           lowerCamelCase					:					Tuple        =	use_proj
           lowerCamelCase					:					List[Any]        =	scope
           lowerCamelCase					:					int        =	bos_token_id

     def 		a__						(	self:						Optional[Any]	)->    str:
           lowerCamelCase					:					Any        =	ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size	)
           lowerCamelCase					:					List[str]        =	random_attention_mask([self.batch_size, self.seq_length]	)

           lowerCamelCase					:					Union[str, Any]        =	None
           if self.use_input_lengths:
                 lowerCamelCase					:					int        =	(
                     ids_tensor([self.batch_size]     ,		vocab_size=2	) + self.seq_length - 2
                 )  # small variation of seq_length

           lowerCamelCase					:					Optional[int]        =	None
           if self.use_token_type_ids:
                 lowerCamelCase					:					int        =	ids_tensor([self.batch_size, self.seq_length]     ,		self.n_langs	)

           lowerCamelCase					:					Optional[int]        =	None
           lowerCamelCase					:					str        =	None
           lowerCamelCase					:					Optional[Any]        =	None
           if self.use_labels:
                 lowerCamelCase					:					Optional[Any]        =	ids_tensor([self.batch_size]     ,		self.type_sequence_label_size	)
                 lowerCamelCase					:					Tuple        =	ids_tensor([self.batch_size, self.seq_length]     ,		self.num_labels	)
                 lowerCamelCase					:					Optional[Any]        =	ids_tensor([self.batch_size]     ,		2	).float()
                 lowerCamelCase					:					Optional[int]        =	ids_tensor([self.batch_size]     ,		self.num_choices	)

           lowerCamelCase					:					Optional[int]        =	self.get_config()

           return (
               config,
               input_ids,
               token_type_ids,
               input_lengths,
               sequence_labels,
               token_labels,
               is_impossible_labels,
               choice_labels,
               input_mask,
           )

     def 		a__						(	self:						Tuple	)->    List[Any]:
           return XLMConfig(
               vocab_size=self.vocab_size     ,		n_special=self.n_special     ,		emb_dim=self.hidden_size     ,		n_layers=self.num_hidden_layers     ,		n_heads=self.num_attention_heads     ,		dropout=self.hidden_dropout_prob     ,		attention_dropout=self.attention_probs_dropout_prob     ,		gelu_activation=self.gelu_activation     ,		sinusoidal_embeddings=self.sinusoidal_embeddings     ,		asm=self.asm     ,		causal=self.causal     ,		n_langs=self.n_langs     ,		max_position_embeddings=self.max_position_embeddings     ,		initializer_range=self.initializer_range     ,		summary_type=self.summary_type     ,		use_proj=self.use_proj     ,		num_labels=self.num_labels     ,		bos_token_id=self.bos_token_id     ,		)

     def 		a__						(	self:						int     ,		__a:						int     ,		__a:						Dict     ,		__a:						Any     ,		__a:						str     ,		__a:						Dict     ,		__a:						Optional[int]     ,		__a:						Optional[int]     ,		__a:						List[Any]     ,		__a:						Optional[Any]     ,		)->    Any:
           lowerCamelCase					:					Tuple        =	XLMModel(config=snake_case_	)
           model.to(snake_case_	)
           model.eval()
           lowerCamelCase					:					List[str]        =	model(snake_case_     ,		lengths=snake_case_     ,		langs=snake_case_	)
           lowerCamelCase					:					Optional[Any]        =	model(snake_case_     ,		langs=snake_case_	)
           lowerCamelCase					:					str        =	model(snake_case_	)
           self.parent.assertEqual(result.last_hidden_state.shape     ,		(self.batch_size, self.seq_length, self.hidden_size)	)

     def 		a__						(	self:						List[Any]     ,		__a:						List[Any]     ,		__a:						List[Any]     ,		__a:						Any     ,		__a:						Optional[int]     ,		__a:						int     ,		__a:						Tuple     ,		__a:						Dict     ,		__a:						Union[str, Any]     ,		__a:						str     ,		)->    Optional[Any]:
           lowerCamelCase					:					Optional[Any]        =	XLMWithLMHeadModel(snake_case_	)
           model.to(snake_case_	)
           model.eval()

           lowerCamelCase					:					int        =	model(snake_case_     ,		token_type_ids=snake_case_     ,		labels=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:						Any     ,		__a:						List[Any]     ,		__a:						List[str]     ,		__a:						Optional[int]     ,		__a:						Dict     ,		__a:						Union[str, Any]     ,		__a:						Dict     ,		__a:						int     ,		__a:						Optional[int]     ,		__a:						Optional[Any]     ,		)->    Tuple:
           lowerCamelCase					:					List[Any]        =	XLMForQuestionAnsweringSimple(snake_case_	)
           model.to(snake_case_	)
           model.eval()

           lowerCamelCase					:					Optional[int]        =	model(snake_case_	)

           lowerCamelCase					:					Tuple        =	model(snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_	)
           lowerCamelCase					:					Union[str, Any]        =	outputs
           self.parent.assertEqual(result.start_logits.shape     ,		(self.batch_size, self.seq_length)	)
           self.parent.assertEqual(result.end_logits.shape     ,		(self.batch_size, self.seq_length)	)

     def 		a__						(	self:						Optional[Any]     ,		__a:						Optional[Any]     ,		__a:						List[Any]     ,		__a:						int     ,		__a:						Union[str, Any]     ,		__a:						Dict     ,		__a:						Dict     ,		__a:						str     ,		__a:						int     ,		__a:						Optional[int]     ,		)->    Dict:
           lowerCamelCase					:					Union[str, Any]        =	XLMForQuestionAnswering(snake_case_	)
           model.to(snake_case_	)
           model.eval()

           lowerCamelCase					:					int        =	model(snake_case_	)

           lowerCamelCase					:					List[Any]        =	model(
               snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_     ,		cls_index=snake_case_     ,		is_impossible=snake_case_     ,		p_mask=snake_case_     ,		)

           lowerCamelCase					:					Optional[Any]        =	model(
               snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_     ,		cls_index=snake_case_     ,		is_impossible=snake_case_     ,		)

           (lowerCamelCase					)					:					Dict        =	result_with_labels.to_tuple()

           lowerCamelCase					:					Optional[Any]        =	model(snake_case_     ,		start_positions=snake_case_     ,		end_positions=snake_case_	)

           (lowerCamelCase					)					:					List[str]        =	result_with_labels.to_tuple()

           self.parent.assertEqual(result_with_labels.loss.shape     ,		()	)
           self.parent.assertEqual(result.start_top_log_probs.shape     ,		(self.batch_size, model.config.start_n_top)	)
           self.parent.assertEqual(result.start_top_index.shape     ,		(self.batch_size, model.config.start_n_top)	)
           self.parent.assertEqual(
               result.end_top_log_probs.shape     ,		(self.batch_size, model.config.start_n_top * model.config.end_n_top)	)
           self.parent.assertEqual(
               result.end_top_index.shape     ,		(self.batch_size, model.config.start_n_top * model.config.end_n_top)	)
           self.parent.assertEqual(result.cls_logits.shape     ,		(self.batch_size,)	)

     def 		a__						(	self:						Union[str, Any]     ,		__a:						List[Any]     ,		__a:						str     ,		__a:						List[Any]     ,		__a:						List[str]     ,		__a:						Dict     ,		__a:						Dict     ,		__a:						List[Any]     ,		__a:						Any     ,		__a:						str     ,		)->    Dict:
           lowerCamelCase					:					Union[str, Any]        =	XLMForSequenceClassification(snake_case_	)
           model.to(snake_case_	)
           model.eval()

           lowerCamelCase					:					Optional[int]        =	model(snake_case_	)
           lowerCamelCase					:					Union[str, Any]        =	model(snake_case_     ,		labels=snake_case_	)
           self.parent.assertEqual(result.loss.shape     ,		()	)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.type_sequence_label_size)	)

     def 		a__						(	self:						Dict     ,		__a:						Union[str, Any]     ,		__a:						List[Any]     ,		__a:						Union[str, Any]     ,		__a:						str     ,		__a:						Union[str, Any]     ,		__a:						str     ,		__a:						Tuple     ,		__a:						str     ,		__a:						int     ,		)->    Dict:
           lowerCamelCase					:					List[str]        =	self.num_labels
           lowerCamelCase					:					Union[str, Any]        =	XLMForTokenClassification(snake_case_	)
           model.to(snake_case_	)
           model.eval()

           lowerCamelCase					:					Optional[Any]        =	model(snake_case_     ,		attention_mask=snake_case_     ,		labels=snake_case_	)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.num_labels)	)

     def 		a__						(	self:						List[Any]     ,		__a:						Optional[int]     ,		__a:						str     ,		__a:						Dict     ,		__a:						Optional[int]     ,		__a:						Optional[Any]     ,		__a:						str     ,		__a:						Optional[Any]     ,		__a:						Any     ,		__a:						Dict     ,		)->    List[str]:
           lowerCamelCase					:					List[str]        =	self.num_choices
           lowerCamelCase					:					Union[str, Any]        =	XLMForMultipleChoice(config=snake_case_	)
           model.to(snake_case_	)
           model.eval()
           lowerCamelCase					:					int        =	input_ids.unsqueeze(1	).expand(-1     ,		self.num_choices     ,		-1	).contiguous()
           lowerCamelCase					:					int        =	token_type_ids.unsqueeze(1	).expand(-1     ,		self.num_choices     ,		-1	).contiguous()
           lowerCamelCase					:					List[str]        =	input_mask.unsqueeze(1	).expand(-1     ,		self.num_choices     ,		-1	).contiguous()
           lowerCamelCase					:					List[Any]        =	model(
               snake_case_     ,		attention_mask=snake_case_     ,		token_type_ids=snake_case_     ,		labels=snake_case_     ,		)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_choices)	)







     def 		a__						(	self:						Optional[Any]	)->    Tuple:
           lowerCamelCase					:					Dict        =	self.prepare_config_and_inputs()
           (
               lowerCamelCase					
           )					:					Any        =	config_and_inputs
           lowerCamelCase					:					str        =	{"input_ids": input_ids, "token_type_ids": token_type_ids, "lengths": input_lengths}
           return config, inputs_dict


@require_torch
class 			A__	(						_snake_case ,     _snake_case ,     _snake_case ,     unittest.TestCase):


     """simple docstring"""
     snake_case__	:					Dict													=(
         (
             XLMModel,
             XLMWithLMHeadModel,
             XLMForQuestionAnswering,
             XLMForSequenceClassification,
             XLMForQuestionAnsweringSimple,
             XLMForTokenClassification,
             XLMForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     snake_case__	:					Union[str, Any]													=(
         (XLMWithLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     snake_case__	:					Optional[int]													=(
         {
             """feature-extraction""": XLMModel,
             """fill-mask""": XLMWithLMHeadModel,
             """question-answering""": XLMForQuestionAnsweringSimple,
             """text-classification""": XLMForSequenceClassification,
             """text-generation""": XLMWithLMHeadModel,
             """token-classification""": XLMForTokenClassification,
             """zero-shot""": XLMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     def 		a__						(	self:						str     ,		__a:						Optional[int]     ,		__a:						Dict     ,		__a:						Union[str, Any]     ,		__a:						List[Any]     ,		__a:						int	)->    List[str]:
           if (
               pipeline_test_casse_name == "QAPipelineTests"
               and tokenizer_name is not None
               and not tokenizer_name.endswith("""Fast"""	)
           ):
                 # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
                 # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
                 # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
                 return True

           return False

     def 		a__						(	self:						Any     ,		__a:						Optional[Any]     ,		__a:						str     ,		__a:						int=False	)->    Tuple:
           lowerCamelCase					:					Optional[Any]        =	super()._prepare_for_class(snake_case_     ,		snake_case_     ,		return_labels=snake_case_	)

           if return_labels:
                 if model_class.__name__ == "XLMForQuestionAnswering":
                       lowerCamelCase					:					Any        =	torch.zeros(
                           self.model_tester.batch_size     ,		dtype=torch.long     ,		device=snake_case_	)
                       lowerCamelCase					:					Union[str, Any]        =	torch.zeros(
                           self.model_tester.batch_size     ,		dtype=torch.long     ,		device=snake_case_	)

           return inputs_dict

     def 		a__						(	self:						Optional[Any]	)->    Any:
           lowerCamelCase					:					int        =	XLMModelTester(self	)
           lowerCamelCase					:					Any        =	ConfigTester(self     ,		config_class=snake_case_     ,		emb_dim=37	)

     def 		a__						(	self:						List[Any]	)->    Tuple:
           self.config_tester.run_common_tests()

     def 		a__						(	self:						Tuple	)->    Optional[int]:
           lowerCamelCase					:					Tuple        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_model(*snake_case_	)

     def 		a__						(	self:						Optional[Any]	)->    int:
           lowerCamelCase					:					int        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_lm_head(*snake_case_	)

     def 		a__						(	self:						Optional[Any]	)->    Optional[Any]:
           lowerCamelCase					:					Union[str, Any]        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_simple_qa(*snake_case_	)

     def 		a__						(	self:						Optional[int]	)->    int:
           lowerCamelCase					:					str        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_qa(*snake_case_	)

     def 		a__						(	self:						List[str]	)->    Tuple:
           lowerCamelCase					:					Union[str, Any]        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_sequence_classif(*snake_case_	)

     def 		a__						(	self:						Tuple	)->    Union[str, Any]:
           lowerCamelCase					:					Any        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_token_classif(*snake_case_	)

     def 		a__						(	self:						str	)->    List[str]:
           lowerCamelCase					:					Dict        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_xlm_for_multiple_choice(*snake_case_	)

     def 		a__						(	self:						Any     ,		__a:						Optional[int]     ,		__a:						Optional[Any]     ,		__a:						Optional[int]     ,		__a:						int     ,		__a:						Dict     ,		__a:						Optional[int]=False     ,		__a:						Union[str, Any]=1	)->    Any:
           self.assertIsInstance(snake_case_     ,		snake_case_	)
           self.assertListEqual(
               [isinstance(snake_case_     ,		snake_case_	) for iter_attentions in attentions]     ,		[True] * len(snake_case_	)	)
           self.assertEqual(len(snake_case_	)     ,		(max_length - min_length) * num_beam_groups	)

           for idx, iter_attentions in enumerate(snake_case_	):
                 # adds PAD dummy token
                 lowerCamelCase					:					Optional[Any]        =	min_length + idx + 1
                 lowerCamelCase					:					Optional[Any]        =	min_length + idx + 1

                 lowerCamelCase					:					Any        =	(
                     batch_size * num_beam_groups,
                     config.num_attention_heads,
                     tgt_len,
                     src_len,
                 )
                 # check attn size
                 self.assertListEqual(
                     [layer_attention.shape for layer_attention in iter_attentions]     ,		[expected_shape] * len(snake_case_	)	)

     def 		a__						(	self:						Dict     ,		__a:						Any     ,		__a:						List[str]     ,		__a:						Any     ,		__a:						Dict     ,		__a:						int     ,		__a:						Dict=False     ,		__a:						Optional[int]=1	)->    Union[str, Any]:
           self.assertIsInstance(snake_case_     ,		snake_case_	)
           self.assertListEqual(
               [isinstance(snake_case_     ,		snake_case_	) for iter_hidden_states in hidden_states]     ,		[True] * len(snake_case_	)     ,		)
           self.assertEqual(len(snake_case_	)     ,		(max_length - min_length) * num_beam_groups	)

           for idx, iter_hidden_states in enumerate(snake_case_	):
                 # adds PAD dummy token
                 lowerCamelCase					:					Dict        =	min_length + idx + 1
                 lowerCamelCase					:					Optional[Any]        =	(batch_size * num_beam_groups, seq_len, config.hidden_size)
                 # check hidden size
                 self.assertListEqual(
                     [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]     ,		[expected_shape] * len(snake_case_	)     ,		)
           pass







     @slow
     def 		a__						(	self:						Dict	)->    str:
           for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCamelCase					:					Any        =	XLMModel.from_pretrained(snake_case_	)
                 self.assertIsNotNone(snake_case_	)


@require_torch
class 			A__	(						unittest.TestCase):


     """simple docstring"""

     @slow
     def 		a__						(	self:						str	)->    Any:
           lowerCamelCase					:					Optional[Any]        =	XLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""	)
           model.to(snake_case_	)
           lowerCamelCase					:					List[Any]        =	torch.tensor([[14, 447]]     ,		dtype=torch.long     ,		device=snake_case_	)  # the president
           lowerCamelCase					:					List[Any]        =	[
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
               14,
               447,
           ]  # the president the president the president the president the president the president the president the president the president the president
           # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
           lowerCamelCase					:					Any        =	model.generate(snake_case_     ,		do_sample=snake_case_	)
           self.assertListEqual(output_ids[0].cpu().numpy().tolist()     ,		snake_case_	)









"""simple docstring"""


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


     """simple docstring"""
     snake_case__	:					str													=StableDiffusionXLImgaImgPipeline
     snake_case__	:					Any													=TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width'''}
     snake_case__	:					Optional[int]													=PipelineTesterMixin.required_optional_params - {'''latents'''}
     snake_case__	:					Dict													=TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     snake_case__	:					Tuple													=IMAGE_TO_IMAGE_IMAGE_PARAMS
     snake_case__	:					List[str]													=IMAGE_TO_IMAGE_IMAGE_PARAMS

     def 		a__						(	self:						List[str]	)->    int:
           torch.manual_seed(0	)
           lowerCamelCase					:					Any        =	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     ,		)
           lowerCamelCase					:					Any        =	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	)
           lowerCamelCase					:					Any        =	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	)
           lowerCamelCase					:					Optional[Any]        =	CLIPTextConfig(
               bos_token_id=0     ,		eos_token_id=2     ,		hidden_size=32     ,		intermediate_size=37     ,		layer_norm_eps=1e-0_5     ,		num_attention_heads=4     ,		num_hidden_layers=5     ,		pad_token_id=1     ,		vocab_size=1_000     ,		hidden_act="""gelu"""     ,		projection_dim=32     ,		)
           lowerCamelCase					:					Dict        =	CLIPTextModel(__a	)
           lowerCamelCase					:					Union[str, Any]        =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""     ,		local_files_only=__a	)

           lowerCamelCase					:					Dict        =	CLIPTextModelWithProjection(__a	)
           lowerCamelCase					:					Optional[int]        =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""     ,		local_files_only=__a	)

           lowerCamelCase					:					str        =	{
               """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 		a__						(	self:						Any     ,		__a:						str     ,		__a:						Tuple=0	)->    Union[str, Any]:
           lowerCamelCase					:					List[Any]        =	floats_tensor((1, 3, 32, 32)     ,		rng=random.Random(__a	)	).to(__a	)
           lowerCamelCase					:					Any        =	image / 2 + 0.5
           if str(__a	).startswith("""mps"""	):
                 lowerCamelCase					:					Dict        =	torch.manual_seed(__a	)
           else:
                 lowerCamelCase					:					Tuple        =	torch.Generator(device=__a	).manual_seed(__a	)
           lowerCamelCase					:					Tuple        =	{
               """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 		a__						(	self:						Dict	)->    Optional[Any]:
           lowerCamelCase					:					Any        =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           lowerCamelCase					:					Union[str, Any]        =	self.get_dummy_components()
           lowerCamelCase					:					Optional[int]        =	StableDiffusionXLImgaImgPipeline(**__a	)
           lowerCamelCase					:					int        =	sd_pipe.to(__a	)
           sd_pipe.set_progress_bar_config(disable=__a	)

           lowerCamelCase					:					Optional[Any]        =	self.get_dummy_inputs(__a	)
           lowerCamelCase					:					Optional[int]        =	sd_pipe(**__a	).images
           lowerCamelCase					:					Tuple        =	image[0, -3:, -3:, -1]

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

           lowerCamelCase					:					Any        =	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 		a__						(	self:						Optional[int]	)->    Union[str, Any]:
           super().test_attention_slicing_forward_pass(expected_max_diff=3e-3	)

     def 		a__						(	self:						Optional[Any]	)->    str:
           super().test_inference_batch_single_identical(expected_max_diff=3e-3	)

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







     def 		a__						(	self:						List[Any]	)->    Union[str, Any]:
           lowerCamelCase					:					Tuple        =	self.get_dummy_components()
           lowerCamelCase					:					Union[str, Any]        =	StableDiffusionXLImgaImgPipeline(**__a	)
           lowerCamelCase					:					str        =	sd_pipe.to(__a	)
           lowerCamelCase					:					Any        =	sd_pipe.to(__a	)
           sd_pipe.set_progress_bar_config(disable=__a	)

           # forward without prompt embeds
           lowerCamelCase					:					Dict        =	self.get_dummy_inputs(__a	)
           lowerCamelCase					:					Any        =	3 * ["""this is a negative prompt"""]
           lowerCamelCase					:					Optional[int]        =	negative_prompt
           lowerCamelCase					:					Tuple        =	3 * [inputs["""prompt"""]]

           lowerCamelCase					:					List[Any]        =	sd_pipe(**__a	)
           lowerCamelCase					:					Optional[int]        =	output.images[0, -3:, -3:, -1]

           # forward with prompt embeds
           lowerCamelCase					:					Tuple        =	self.get_dummy_inputs(__a	)
           lowerCamelCase					:					List[Any]        =	3 * ["""this is a negative prompt"""]
           lowerCamelCase					:					Tuple        =	3 * [inputs.pop("""prompt"""	)]

           (
               (
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			
           )					:					Union[str, Any]        =	sd_pipe.encode_prompt(__a     ,		negative_prompt=__a	)

           lowerCamelCase					:					int        =	sd_pipe(
               **__a     ,		prompt_embeds=__a     ,		negative_prompt_embeds=__a     ,		pooled_prompt_embeds=__a     ,		negative_pooled_prompt_embeds=__a     ,		)
           lowerCamelCase					:					Union[str, Any]        =	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 			A__	(						unittest.TestCase):


     """simple docstring"""

     def 		a__						(	self:						Dict	)->    str:
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()

     def 		a__						(	self:						Union[str, Any]     ,		__a:						Any     ,		__a:						Any="cpu"     ,		__a:						str=torch.floataa     ,		__a:						Any=0	)->    Optional[Any]:
           lowerCamelCase					:					Optional[Any]        =	torch.Generator(device=__a	).manual_seed(__a	)
           lowerCamelCase					:					List[Any]        =	np.random.RandomState(__a	).standard_normal((1, 4, 64, 64)	)
           lowerCamelCase					:					List[str]        =	torch.from_numpy(__a	).to(device=__a     ,		dtype=__a	)
           lowerCamelCase					:					int        =	{
               """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 		a__						(	self:						Optional[int]	)->    List[str]:
           lowerCamelCase					:					Tuple        =	DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""	)
           pipe.to(__a	)
           pipe.set_progress_bar_config(disable=__a	)

           lowerCamelCase					:					Optional[int]        =	self.get_inputs(__a	)
           lowerCamelCase					:					Optional[Any]        =	pipe(**__a	).images
           lowerCamelCase					:					Dict        =	image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)
           lowerCamelCase					:					List[str]        =	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




