





import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 (
	    MODEL_FOR_PRETRAINING_MAPPING,
	    MegatronBertForCausalLM,
	    MegatronBertForMaskedLM,
	    MegatronBertForMultipleChoice,
	    MegatronBertForNextSentencePrediction,
	    MegatronBertForPreTraining,
	    MegatronBertForQuestionAnswering,
	    MegatronBertForSequenceClassification,
	    MegatronBertForTokenClassification,
	    MegatronBertModel,
	)




class 	UpperCamelCase_							:


							def __init__(				self     :int				,					__A     :Optional[Any]				,					__A     :Any=13				,					__A     :Optional[int]=7				,					__A     :str=True				,					__A     :Tuple=True				,					__A     :Union[str, Any]=True				,					__A     :int=True				,					__A     :Any=99				,					__A     :Union[str, Any]=64				,					__A     :int=32				,					__A     :Tuple=5				,					__A     :int=4				,					__A     :Tuple=37				,					__A     :Optional[Any]="gelu"				,					__A     :Union[str, Any]=0.1				,					__A     :Any=0.1				,					__A     :str=512				,					__A     :Tuple=16				,					__A     :Optional[Any]=2				,					__A     :Tuple=0.0_2				,					__A     :int=3				,					__A     :Union[str, Any]=4				,					__A     :List[Any]=None				,					) -> Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   parent
									SCREAMING_SNAKE_CASE__								=   batch_size
									SCREAMING_SNAKE_CASE__								=   seq_length
									SCREAMING_SNAKE_CASE__								=   is_training
									SCREAMING_SNAKE_CASE__								=   use_input_mask
									SCREAMING_SNAKE_CASE__								=   use_token_type_ids
									SCREAMING_SNAKE_CASE__								=   use_labels
									SCREAMING_SNAKE_CASE__								=   vocab_size
									SCREAMING_SNAKE_CASE__								=   hidden_size
									SCREAMING_SNAKE_CASE__								=   embedding_size
									SCREAMING_SNAKE_CASE__								=   num_hidden_layers
									SCREAMING_SNAKE_CASE__								=   num_attention_heads
									SCREAMING_SNAKE_CASE__								=   intermediate_size
									SCREAMING_SNAKE_CASE__								=   hidden_act
									SCREAMING_SNAKE_CASE__								=   hidden_dropout_prob
									SCREAMING_SNAKE_CASE__								=   attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE__								=   max_position_embeddings
									SCREAMING_SNAKE_CASE__								=   type_vocab_size
									SCREAMING_SNAKE_CASE__								=   type_sequence_label_size
									SCREAMING_SNAKE_CASE__								=   initializer_range
									SCREAMING_SNAKE_CASE__								=   num_labels
									SCREAMING_SNAKE_CASE__								=   num_choices
									SCREAMING_SNAKE_CASE__								=   scope


							def   _snake_case	(				self     :Dict		) -> Optional[Any]:



									"""simple docstring"""



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

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

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

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

									SCREAMING_SNAKE_CASE__								=   self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


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



									"""simple docstring"""



									return MegatronBertConfig(
									    vocab_size=self.vocab_size				,					hidden_size=self.hidden_size				,					num_hidden_layers=self.num_hidden_layers				,					num_attention_heads=self.num_attention_heads				,					intermediate_size=self.intermediate_size				,					embedding_size=self.embedding_size				,					hidden_act=self.hidden_act				,					hidden_dropout_prob=self.hidden_dropout_prob				,					attention_probs_dropout_prob=self.attention_probs_dropout_prob				,					max_position_embeddings=self.max_position_embeddings				,					type_vocab_size=self.type_vocab_size				,					is_decoder=__A				,					initializer_range=self.initializer_range				,					)


							def   _snake_case	(				self     :Optional[int]				,					__A     :Any				,					__A     :Union[str, Any]				,					__A     :Union[str, Any]				,					__A     :int				,					__A     :str				,					__A     :List[Any]				,					__A     :List[str]		) -> Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertModel(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(__A				,					attention_mask=__A				,					token_type_ids=__A		)
									SCREAMING_SNAKE_CASE__								=   model(__A				,					token_type_ids=__A		)
									SCREAMING_SNAKE_CASE__								=   model(__A		)

									self.parent.assertEqual(result.last_hidden_state.shape				,					(self.batch_size, self.seq_length, self.hidden_size)		)
									self.parent.assertEqual(result.pooler_output.shape				,					(self.batch_size, self.hidden_size)		)


							def   _snake_case	(				self     :List[Any]				,					__A     :List[str]				,					__A     :Optional[Any]				,					__A     :Optional[int]				,					__A     :List[str]				,					__A     :List[Any]				,					__A     :int				,					__A     :Any		) -> str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertForMaskedLM(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(__A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A		)
									self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.seq_length, self.vocab_size)		)


							def   _snake_case	(				self     :List[Any]				,					__A     :Any				,					__A     :Tuple				,					__A     :str				,					__A     :str				,					__A     :List[str]				,					__A     :Union[str, Any]				,					__A     :Optional[int]		) -> int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertForCausalLM(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(__A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A		)
									self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.seq_length, self.vocab_size)		)


							def   _snake_case	(				self     :Tuple				,					__A     :int				,					__A     :Optional[int]				,					__A     :Union[str, Any]				,					__A     :Union[str, Any]				,					__A     :int				,					__A     :Union[str, Any]				,					__A     :str		) -> Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertForNextSentencePrediction(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(
									    __A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A				,					)
									self.parent.assertEqual(result.logits.shape				,					(self.batch_size, 2)		)


							def   _snake_case	(				self     :Union[str, Any]				,					__A     :str				,					__A     :Any				,					__A     :Union[str, Any]				,					__A     :List[Any]				,					__A     :Any				,					__A     :str				,					__A     :Dict		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertForPreTraining(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(
									    __A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A				,					next_sentence_label=__A				,					)
									self.parent.assertEqual(result.prediction_logits.shape				,					(self.batch_size, self.seq_length, self.vocab_size)		)
									self.parent.assertEqual(result.seq_relationship_logits.shape				,					(self.batch_size, 2)		)


							def   _snake_case	(				self     :Optional[Any]				,					__A     :int				,					__A     :Optional[Any]				,					__A     :List[Any]				,					__A     :Tuple				,					__A     :Any				,					__A     :Union[str, Any]				,					__A     :Any		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertForQuestionAnswering(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(
									    __A				,					attention_mask=__A				,					token_type_ids=__A				,					start_positions=__A				,					end_positions=__A				,					)
									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   _snake_case	(				self     :str				,					__A     :int				,					__A     :List[Any]				,					__A     :Tuple				,					__A     :Optional[Any]				,					__A     :Optional[int]				,					__A     :Dict				,					__A     :Optional[int]		) -> str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.num_labels
									SCREAMING_SNAKE_CASE__								=   MegatronBertForSequenceClassification(__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(__A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A		)
									self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.num_labels)		)


							def   _snake_case	(				self     :Optional[Any]				,					__A     :List[Any]				,					__A     :List[Any]				,					__A     :Any				,					__A     :Tuple				,					__A     :Any				,					__A     :Optional[int]				,					__A     :str		) -> List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.num_labels
									SCREAMING_SNAKE_CASE__								=   MegatronBertForTokenClassification(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   model(__A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A		)
									self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.seq_length, self.num_labels)		)


							def   _snake_case	(				self     :Optional[Any]				,					__A     :int				,					__A     :List[Any]				,					__A     :List[str]				,					__A     :Any				,					__A     :List[Any]				,					__A     :Any				,					__A     :Optional[int]		) -> int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.num_choices
									SCREAMING_SNAKE_CASE__								=   MegatronBertForMultipleChoice(config=__A		)
									model.to(__A		)
									model.eval()
									SCREAMING_SNAKE_CASE__								=   input_ids.unsqueeze(1		).expand(-1				,					self.num_choices				,					-1		).contiguous()
									SCREAMING_SNAKE_CASE__								=   token_type_ids.unsqueeze(1		).expand(-1				,					self.num_choices				,					-1		).contiguous()
									SCREAMING_SNAKE_CASE__								=   input_mask.unsqueeze(1		).expand(-1				,					self.num_choices				,					-1		).contiguous()
									SCREAMING_SNAKE_CASE__								=   model(
									    __A				,					attention_mask=__A				,					token_type_ids=__A				,					labels=__A				,					)
									self.parent.assertEqual(result.logits.shape				,					(self.batch_size, self.num_choices)		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.prepare_config_and_inputs()
									(
									    (
									    SCREAMING_SNAKE_CASE__
									)						,    (
									    SCREAMING_SNAKE_CASE__
									)						,    (
									    SCREAMING_SNAKE_CASE__
									)						,    (
									    SCREAMING_SNAKE_CASE__
									)						,    (
									    SCREAMING_SNAKE_CASE__
									)						,    (
									    SCREAMING_SNAKE_CASE__
									)						,    (
									    SCREAMING_SNAKE_CASE__
									)						,    
									)								=   config_and_inputs
									SCREAMING_SNAKE_CASE__								=   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
									return config, inputs_dict




@require_torch
class 	UpperCamelCase_							(  UpperCamelCase__					,	UpperCamelCase__					,	unittest.TestCase     ):
							lowerCamelCase_							     =       (
							    (
							        MegatronBertModel,
							        MegatronBertForMaskedLM,
							        MegatronBertForCausalLM,
							        MegatronBertForMultipleChoice,
							        MegatronBertForNextSentencePrediction,
							        MegatronBertForPreTraining,
							        MegatronBertForQuestionAnswering,
							        MegatronBertForSequenceClassification,
							        MegatronBertForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowerCamelCase_							     =       (
							    {
							        "feature-extraction": MegatronBertModel,
							        "fill-mask": MegatronBertForMaskedLM,
							        "question-answering": MegatronBertForQuestionAnswering,
							        "text-classification": MegatronBertForSequenceClassification,
							        "text-generation": MegatronBertForCausalLM,
							        "token-classification": MegatronBertForTokenClassification,
							        "zero-shot": MegatronBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowerCamelCase_							     =       True
							# test_resize_embeddings = False
							lowerCamelCase_							     =       False


							def   _snake_case	(				self     :int				,					__A     :Tuple				,					__A     :List[str]				,					__A     :int=False		) -> int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   super()._prepare_for_class(__A				,					__A				,					return_labels=__A		)

									if return_labels:
											if model_class in get_values(__A		):
													SCREAMING_SNAKE_CASE__								=   torch.zeros(
													    (self.model_tester.batch_size, self.model_tester.seq_length)				,					dtype=torch.long				,					device=__A		)
													SCREAMING_SNAKE_CASE__								=   torch.zeros(
													    self.model_tester.batch_size				,					dtype=torch.long				,					device=__A		)
									return inputs_dict


							def   _snake_case	(				self     :Tuple		) -> List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   MegatronBertModelTester(self		)
									SCREAMING_SNAKE_CASE__								=   ConfigTester(self				,					config_class=__A				,					hidden_size=37		)


							def   _snake_case	(				self     :List[Any]		) -> Optional[int]:



									"""simple docstring"""



									self.config_tester.run_common_tests()


							def   _snake_case	(				self     :int		) -> Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_model(*__A		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__A		)


							def   _snake_case	(				self     :Optional[Any]		) -> Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__A		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__A		)


							def   _snake_case	(				self     :Optional[Any]		) -> Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_pretraining(*__A		)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_question_answering(*__A		)


							def   _snake_case	(				self     :Tuple		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__A		)


							def   _snake_case	(				self     :Optional[int]		) -> Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_megatron_bert_for_token_classification(*__A		)






def 					SCREAMING_SNAKE_CASE__				(							UpperCamelCase__:				List[str]     ):
		return torch.tensor(
		    UpperCamelCase__       , dtype=torch.long       , device=UpperCamelCase__       , )


_lowerCamelCase 					=      1e-4




@require_torch
@require_sentencepiece
@require_tokenizers
class 	UpperCamelCase_							(  unittest.TestCase     ):


							@slow
							@unittest.skip("""Model is not available."""		)
							def   _snake_case	(				self     :int		) -> List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   """nvidia/megatron-bert-uncased-345m"""
									if "MYDIR" in os.environ:
											SCREAMING_SNAKE_CASE__								=   os.path.join(os.environ["""MYDIR"""]				,					__A		)
									SCREAMING_SNAKE_CASE__								=   MegatronBertModel.from_pretrained(__A		)
									model.to(__A		)
									model.half()
									SCREAMING_SNAKE_CASE__								=   _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]		)
									with torch.no_grad():
											SCREAMING_SNAKE_CASE__								=   model(__A		)[0]
									SCREAMING_SNAKE_CASE__								=   torch.Size((1, 9, 1024)		)
									self.assertEqual(output.shape				,					__A		)

									SCREAMING_SNAKE_CASE__								=   [-0.6_0_4_0, -0.2_5_1_7, -0.1_0_2_5, 0.3_4_2_0, -0.6_7_5_8, -0.0_0_1_7, -0.1_0_8_9, -0.1_9_9_0, 0.5_7_2_8]
									for ii in range(3		):
											for jj in range(3		):
													SCREAMING_SNAKE_CASE__								=   output[0, ii, jj]
													SCREAMING_SNAKE_CASE__								=   expected[3 * ii + jj]
													SCREAMING_SNAKE_CASE__								=   """ii={} jj={} a={} b={}""".format(__A				,					__A				,					__A				,					__A		)
													self.assertTrue(math.isclose(__A				,					__A				,					rel_tol=__A				,					abs_tol=__A		)				,					msg=__A		)



import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







def SCREAMING_SNAKE_CASE	(			__lowerCAmelCase				)    ->				List[Any]:
  snake_case__						= [tensor.shape for tensor in tensor_list]
  return all(shape == shapes[0] for shape in shapes[1:]				)
class 		__magic_name__						(snake_case_   ,snake_case_   ,snake_case_   ,unittest.TestCase				):





     '''simple docstring'''



     __lowercase				:							Dict	   =       StableDiffusionLatentUpscalePipeline
     __lowercase				:							List[str]	   =       TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
         'height',
         'width',
         'cross_attention_kwargs',
         'negative_prompt_embeds',
         'prompt_embeds',
     }
     __lowercase				:							List[Any]	   =       PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'}
     __lowercase				:							Any	   =       TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     __lowercase				:							int	   =       frozenset(
         []				)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
     __lowercase				:							List[Any]	   =       frozenset([]				)

     __lowercase				:							Any	   =       True

     @property
     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       snake_case__						= 1
       snake_case__						= 4
       snake_case__						= (16, 16)

       snake_case__						= floats_tensor((batch_size, num_channels) + sizes     ,		rng=random.Random(0   )   ).to(_a   )
       return image

     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       torch.manual_seed(0   )
       snake_case__						= UNetaDConditionModel(
           act_fn='''gelu'''     ,		attention_head_dim=8     ,		norm_num_groups=_a     ,		block_out_channels=[32, 32, 64, 64]     ,		time_cond_proj_dim=1_60     ,		conv_in_kernel=1     ,		conv_out_kernel=1     ,		cross_attention_dim=32     ,		down_block_types=(
               '''KDownBlock2D''',
               '''KCrossAttnDownBlock2D''',
               '''KCrossAttnDownBlock2D''',
               '''KCrossAttnDownBlock2D''',
           )     ,		in_channels=8     ,		mid_block_type=_a     ,		only_cross_attention=_a     ,		out_channels=5     ,		resnet_time_scale_shift='''scale_shift'''     ,		time_embedding_type='''fourier'''     ,		timestep_post_act='''gelu'''     ,		up_block_types=('''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KCrossAttnUpBlock2D''', '''KUpBlock2D''')     ,		)
       snake_case__						= AutoencoderKL(
           block_out_channels=[32, 32, 64, 64]     ,		in_channels=3     ,		out_channels=3     ,		down_block_types=[
               '''DownEncoderBlock2D''',
               '''DownEncoderBlock2D''',
               '''DownEncoderBlock2D''',
               '''DownEncoderBlock2D''',
           ]     ,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']     ,		latent_channels=4     ,		)
       snake_case__						= EulerDiscreteScheduler(prediction_type='''sample'''   )
       snake_case__						= 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=10_00     ,		hidden_act='''quick_gelu'''     ,		projection_dim=5_12     ,		)
       snake_case__						= CLIPTextModel(_a   )
       snake_case__						= CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   )

       snake_case__						= {
           '''unet''': model.eval(),
           '''vae''': vae.eval(),
           '''scheduler''': scheduler,
           '''text_encoder''': text_encoder,
           '''tokenizer''': tokenizer,
       }

       return components

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]     ,		_a:Optional[Any]     ,		_a:List[str]=0   ):
       if str(_a   ).startswith('''mps'''   ):
         snake_case__						= torch.manual_seed(_a   )
       else:
         snake_case__						= torch.Generator(device=_a   ).manual_seed(_a   )
       snake_case__						= {
           '''prompt''': '''A painting of a squirrel eating a burger''',
           '''image''': self.dummy_image.cpu(),
           '''generator''': generator,
           '''num_inference_steps''': 2,
           '''output_type''': '''numpy''',
       }
       return inputs

     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       snake_case__						= '''cpu'''

       snake_case__						= self.get_dummy_components()
       snake_case__						= self.pipeline_class(**_a   )
       pipe.to(_a   )
       pipe.set_progress_bar_config(disable=_a   )

       snake_case__						= self.get_dummy_inputs(_a   )
       snake_case__						= pipe(**_a   ).images
       snake_case__						= image[0, -3:, -3:, -1]

       self.assertEqual(image.shape     ,		(1, 2_56, 2_56, 3)   )
       snake_case__						= np.array(
           [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055]   )
       snake_case__						= np.abs(image_slice.flatten() - expected_slice   ).max()
       self.assertLessEqual(_a     ,		1e-3   )

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

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       super().test_cpu_offload_forward_pass(expected_max_diff=3e-3   )

     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       super().test_inference_batch_single_identical(expected_max_diff=7e-3   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple   ):
       super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3e-3   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       super().test_save_load_local(expected_max_difference=3e-3   )

     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       super().test_save_load_optional_components(expected_max_difference=3e-3   )







     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       snake_case__						= [
           '''DDIMScheduler''',
           '''DDPMScheduler''',
           '''PNDMScheduler''',
           '''HeunDiscreteScheduler''',
           '''EulerAncestralDiscreteScheduler''',
           '''KDPM2DiscreteScheduler''',
           '''KDPM2AncestralDiscreteScheduler''',
           '''DPMSolverSDEScheduler''',
       ]
       snake_case__						= self.get_dummy_components()
       snake_case__						= self.pipeline_class(**_a   )

       # make sure that PNDM does not need warm-up
       pipe.scheduler.register_to_config(skip_prk_steps=_a   )

       pipe.to(_a   )
       pipe.set_progress_bar_config(disable=_a   )
       snake_case__						= self.get_dummy_inputs(_a   )
       snake_case__						= 2

       snake_case__						= []
       for scheduler_enum in KarrasDiffusionSchedulers:
         if scheduler_enum.name in skip_schedulers:
           # no sigma schedulers are not supported
           # no schedulers
           continue

         snake_case__						= getattr(_a     ,		scheduler_enum.name   )
         snake_case__						= scheduler_cls.from_config(pipe.scheduler.config   )
         snake_case__						= pipe(**_a   )[0]
         outputs.append(_a   )

       assert check_same_shape(_a   )




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





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]   ):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       snake_case__						= torch.manual_seed(33   )

       snake_case__						= StableDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''     ,		torch_dtype=torch.floataa   )
       pipe.to('''cuda'''   )

       snake_case__						= StableDiffusionLatentUpscalePipeline.from_pretrained(
           '''stabilityai/sd-x2-latent-upscaler'''     ,		torch_dtype=torch.floataa   )
       upscaler.to('''cuda'''   )

       snake_case__						= '''a photo of an astronaut high resolution, unreal engine, ultra realistic'''

       snake_case__						= pipe(_a     ,		generator=_a     ,		output_type='''latent'''   ).images

       snake_case__						= upscaler(
           prompt=_a     ,		image=_a     ,		num_inference_steps=20     ,		guidance_scale=0     ,		generator=_a     ,		output_type='''np'''     ,		).images[0]

       snake_case__						= load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy'''   )
       assert np.abs((expected_image - image).mean()   ) < 5e-2







     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       snake_case__						= torch.manual_seed(33   )

       snake_case__						= StableDiffusionLatentUpscalePipeline.from_pretrained(
           '''stabilityai/sd-x2-latent-upscaler'''     ,		torch_dtype=torch.floataa   )
       upscaler.to('''cuda'''   )

       snake_case__						= '''the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas'''

       snake_case__						= load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png'''   )

       snake_case__						= upscaler(
           prompt=_a     ,		image=_a     ,		num_inference_steps=20     ,		guidance_scale=0     ,		generator=_a     ,		output_type='''np'''     ,		).images[0]

       snake_case__						= load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy'''   )
       assert np.abs((expected_image - image).max()   ) < 5e-2

