






import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
     FlaubertForMultipleChoice,
     FlaubertForQuestionAnswering,
     FlaubertForQuestionAnsweringSimple,
     FlaubertForSequenceClassification,
     FlaubertForTokenClassification,
     FlaubertModel,
     FlaubertWithLMHeadModel,
 )
 from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST







class 						_UpperCAmelCase	(	A__		):




      """simple docstring"""



      def __init__( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase=13     ,						_lowerCAmelCase=7     ,						_lowerCAmelCase=True     ,						_lowerCAmelCase=True     ,						_lowerCAmelCase=True     ,						_lowerCAmelCase=True     ,						_lowerCAmelCase=True     ,						_lowerCAmelCase=False     ,						_lowerCAmelCase=False     ,						_lowerCAmelCase=False     ,						_lowerCAmelCase=2     ,						_lowerCAmelCase=99     ,						_lowerCAmelCase=0     ,						_lowerCAmelCase=32     ,						_lowerCAmelCase=5     ,						_lowerCAmelCase=4     ,						_lowerCAmelCase=0.1     ,						_lowerCAmelCase=0.1     ,						_lowerCAmelCase=512     ,						_lowerCAmelCase=12     ,						_lowerCAmelCase=2     ,						_lowerCAmelCase=0.02     ,						_lowerCAmelCase=3     ,						_lowerCAmelCase=4     ,						_lowerCAmelCase="last"     ,						_lowerCAmelCase=None     ,						_lowerCAmelCase=None     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :Any   				=					parent
          lowerCAmelCase__ :Union[str, Any]   				=					batch_size
          lowerCAmelCase__ :Optional[int]   				=					seq_length
          lowerCAmelCase__ :str   				=					is_training
          lowerCAmelCase__ :Tuple   				=					use_input_lengths
          lowerCAmelCase__ :Union[str, Any]   				=					use_token_type_ids
          lowerCAmelCase__ :List[str]   				=					use_labels
          lowerCAmelCase__ :List[Any]   				=					gelu_activation
          lowerCAmelCase__ :int   				=					sinusoidal_embeddings
          lowerCAmelCase__ :Any   				=					causal
          lowerCAmelCase__ :Dict   				=					asm
          lowerCAmelCase__ :List[str]   				=					n_langs
          lowerCAmelCase__ :str   				=					vocab_size
          lowerCAmelCase__ :List[str]   				=					n_special
          lowerCAmelCase__ :Dict   				=					hidden_size
          lowerCAmelCase__ :Optional[int]   				=					num_hidden_layers
          lowerCAmelCase__ :Dict   				=					num_attention_heads
          lowerCAmelCase__ :Dict   				=					hidden_dropout_prob
          lowerCAmelCase__ :List[Any]   				=					attention_probs_dropout_prob
          lowerCAmelCase__ :Union[str, Any]   				=					max_position_embeddings
          lowerCAmelCase__ :List[Any]   				=					type_vocab_size
          lowerCAmelCase__ :int   				=					type_sequence_label_size
          lowerCAmelCase__ :str   				=					initializer_range
          lowerCAmelCase__ :Dict   				=					num_labels
          lowerCAmelCase__ :Optional[int]   				=					num_choices
          lowerCAmelCase__ :List[Any]   				=					summary_type
          lowerCAmelCase__ :Tuple   				=					use_proj
          lowerCAmelCase__ :List[Any]   				=					scope
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size		)
          lowerCAmelCase__ :Optional[int]   				=					random_attention_mask([self.batch_size, self.seq_length]		)

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

          lowerCAmelCase__ :int   				=					None
          if self.use_token_type_ids:
              lowerCAmelCase__ :List[Any]   				=					ids_tensor([self.batch_size, self.seq_length]     ,						self.n_langs		)

          lowerCAmelCase__ :str   				=					None
          lowerCAmelCase__ :List[str]   				=					None
          lowerCAmelCase__ :List[str]   				=					None
          if self.use_labels:
              lowerCAmelCase__ :Union[str, Any]   				=					ids_tensor([self.batch_size]     ,						self.type_sequence_label_size		)
              lowerCAmelCase__ :int   				=					ids_tensor([self.batch_size, self.seq_length]     ,						self.num_labels		)
              lowerCAmelCase__ :Dict   				=					ids_tensor([self.batch_size]     ,						2		).float()
              lowerCAmelCase__ :List[str]   				=					ids_tensor([self.batch_size]     ,						self.num_choices		)

          lowerCAmelCase__ :List[str]   				=					self.get_config()

          return (
              config,
              input_ids,
              token_type_ids,
              input_lengths,
              sequence_labels,
              token_labels,
              is_impossible_labels,
              choice_labels,
              input_mask,
          )
      def       snake_case_   ( self		):



          '''simple docstring'''
          return FlaubertConfig(
              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     ,						)
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :Any   				=					FlaubertModel(config=a__		)
          model.to(a__		)
          model.eval()
          lowerCAmelCase__ :Dict   				=					model(a__     ,						lengths=a__     ,						langs=a__		)
          lowerCAmelCase__ :List[str]   				=					model(a__     ,						langs=a__		)
          lowerCAmelCase__ :Tuple   				=					model(a__		)
          self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)		)
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					FlaubertWithLMHeadModel(a__		)
          model.to(a__		)
          model.eval()

          lowerCAmelCase__ :Any   				=					model(a__     ,						token_type_ids=a__     ,						labels=a__		)
          self.parent.assertEqual(result.loss.shape     ,						()		)
          self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.seq_length, self.vocab_size)		)
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					FlaubertForQuestionAnsweringSimple(a__		)
          model.to(a__		)
          model.eval()

          lowerCAmelCase__ :str   				=					model(a__		)

          lowerCAmelCase__ :Tuple   				=					model(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     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :Optional[Any]   				=					FlaubertForQuestionAnswering(a__		)
          model.to(a__		)
          model.eval()

          lowerCAmelCase__ :Tuple   				=					model(a__		)

          lowerCAmelCase__ :Any   				=					model(
              a__     ,						start_positions=a__     ,						end_positions=a__     ,						cls_index=a__     ,						is_impossible=a__     ,						p_mask=a__     ,						)

          lowerCAmelCase__ :Tuple   				=					model(
              a__     ,						start_positions=a__     ,						end_positions=a__     ,						cls_index=a__     ,						is_impossible=a__     ,						)

          ((lowerCAmelCase__						)				,						) :str   				=					result_with_labels.to_tuple()

          lowerCAmelCase__ :Optional[int]   				=					model(a__     ,						start_positions=a__     ,						end_positions=a__		)

          ((lowerCAmelCase__						)				,						) :int   				=					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       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :int   				=					FlaubertForSequenceClassification(a__		)
          model.to(a__		)
          model.eval()

          lowerCAmelCase__ :str   				=					model(a__		)
          lowerCAmelCase__ :int   				=					model(a__     ,						labels=a__		)

          self.parent.assertEqual(result.loss.shape     ,						()		)
          self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.type_sequence_label_size)		)
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :Tuple   				=					self.num_labels
          lowerCAmelCase__ :List[str]   				=					FlaubertForTokenClassification(a__		)
          model.to(a__		)
          model.eval()

          lowerCAmelCase__ :Any   				=					model(a__     ,						attention_mask=a__     ,						labels=a__		)
          self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.seq_length, self.num_labels)		)
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						):



          '''simple docstring'''
          lowerCAmelCase__ :Tuple   				=					self.num_choices
          lowerCAmelCase__ :Optional[int]   				=					FlaubertForMultipleChoice(config=a__		)
          model.to(a__		)
          model.eval()
          lowerCAmelCase__ :List[Any]   				=					input_ids.unsqueeze(1		).expand(-1     ,						self.num_choices     ,						-1		).contiguous()
          lowerCAmelCase__ :List[Any]   				=					token_type_ids.unsqueeze(1		).expand(-1     ,						self.num_choices     ,						-1		).contiguous()
          lowerCAmelCase__ :Union[str, Any]   				=					input_mask.unsqueeze(1		).expand(-1     ,						self.num_choices     ,						-1		).contiguous()
          lowerCAmelCase__ :Tuple   				=					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		):



          '''simple docstring'''
          lowerCAmelCase__ :Tuple   				=					self.prepare_config_and_inputs()
          (
              (
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,(
              lowerCAmelCase__						
          )				,						
          ) :Dict   				=					config_and_inputs
          lowerCAmelCase__ :Union[str, Any]   				=					{
              "input_ids": input_ids,
              "token_type_ids": token_type_ids,
              "lengths": input_lengths,
              "attention_mask": input_mask,
          }
          return config, inputs_dict







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




      """simple docstring"""



      A    =     (
          (
              FlaubertModel,
              FlaubertWithLMHeadModel,
              FlaubertForQuestionAnswering,
              FlaubertForQuestionAnsweringSimple,
              FlaubertForSequenceClassification,
              FlaubertForTokenClassification,
              FlaubertForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      A    =     (
          {
              '''feature-extraction''': FlaubertModel,
              '''fill-mask''': FlaubertWithLMHeadModel,
              '''question-answering''': FlaubertForQuestionAnsweringSimple,
              '''text-classification''': FlaubertForSequenceClassification,
              '''token-classification''': FlaubertForTokenClassification,
              '''zero-shot''': FlaubertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase		):



          '''simple docstring'''
          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       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase=False		):



          '''simple docstring'''
          lowerCAmelCase__ :Optional[int]   				=					super()._prepare_for_class(a__     ,						a__     ,						return_labels=a__		)

          if return_labels:
              if model_class.__name__ == "FlaubertForQuestionAnswering":
                  lowerCAmelCase__ :Tuple   				=					torch.zeros(
                      self.model_tester.batch_size     ,						dtype=torch.long     ,						device=a__		)
                  lowerCAmelCase__ :Dict   				=					torch.zeros(
                      self.model_tester.batch_size     ,						dtype=torch.long     ,						device=a__		)

          return inputs_dict
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					FlaubertModelTester(self		)
          lowerCAmelCase__ :Optional[int]   				=					ConfigTester(self     ,						config_class=a__     ,						emb_dim=37		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          self.config_tester.run_common_tests()
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[Any]   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_model(*a__		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_lm_head(*a__		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[Any]   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_simple_qa(*a__		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Optional[Any]   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_qa(*a__		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_sequence_classif(*a__		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Any   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_token_classif(*a__		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Any   				=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_flaubert_multiple_choice(*a__		)
      @slow
      def       snake_case_   ( self		):



          '''simple docstring'''
          for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowerCAmelCase__ :Any   				=					FlaubertModel.from_pretrained(a__		)
              self.assertIsNotNone(a__		)



      @slow
      @require_torch_gpu
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__				,lowerCAmelCase__ :Union[str, Any]   				=					self.model_tester.prepare_config_and_inputs_for_common()
          for model_class in self.all_model_classes:
              # FlauBertForMultipleChoice behaves incorrectly in JIT environments.
              if model_class == FlaubertForMultipleChoice:
                  return

              lowerCAmelCase__ :Optional[int]   				=					True
              lowerCAmelCase__ :Any   				=					model_class(config=a__		)

              lowerCAmelCase__ :List[Any]   				=					self._prepare_for_class(a__     ,						a__		)
              lowerCAmelCase__ :Optional[int]   				=					torch.jit.trace(
                  a__     ,						(inputs_dict["input_ids"].to("cpu"		), inputs_dict["attention_mask"].to("cpu"		))		)

              with tempfile.TemporaryDirectory() as tmp:
                  torch.jit.save(a__     ,						os.path.join(a__     ,						"traced_model.pt"		)		)
                  lowerCAmelCase__ :str   				=					torch.jit.load(os.path.join(a__     ,						"traced_model.pt"		)     ,						map_location=a__		)
                  loaded(inputs_dict["input_ids"].to(a__		)     ,						inputs_dict["attention_mask"].to(a__		)		)







@require_torch
class 						_UpperCAmelCase	(	unittest.TestCase		):




      """simple docstring"""



      @slow
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Union[str, Any]   				=					FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"		)
          lowerCAmelCase__ :int   				=					torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
          with torch.no_grad():
              lowerCAmelCase__ :List[str]   				=					model(a__		)[0]
          lowerCAmelCase__ :List[Any]   				=					torch.Size((1, 11, 768)		)
          self.assertEqual(output.shape     ,						a__		)
          lowerCAmelCase__ :str   				=					torch.tensor(
              [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]		)

          self.assertTrue(torch.allclose(output[:, :3, :3]     ,						a__     ,						atol=1e-4		)		)








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 		lowerCAmelCase__     (       UpperCamelCase_					:							Union[str, Any]						)->	List[str]:
    A__					=       [tensor.shape for tensor in tensor_list]
    return all(shape == shapes[0] for shape in shapes[1:]						)



class      _UpperCAmelCase      (							A__      ,   A__      ,   A__      ,   unittest.TestCase  ):
     UpperCamelCase__		     =					StableDiffusionLatentUpscalePipeline
     UpperCamelCase__		     =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
         '''height''',
         '''width''',
         '''cross_attention_kwargs''',
         '''negative_prompt_embeds''',
         '''prompt_embeds''',
     }
     UpperCamelCase__		     =					PipelineTesterMixin.required_optional_params - {'''num_images_per_prompt'''}
     UpperCamelCase__		     =					TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     UpperCamelCase__		     =					frozenset(
         []  )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
     UpperCamelCase__		     =					frozenset([]  )

     UpperCamelCase__		     =					True




     @property
     def 		snake_case_		(					self):
         A__					=       1
         A__					=       4
         A__					=       (1_6, 1_6)

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




     def 		snake_case_		(					self):
         torch.manual_seed(0)
         A__					=       UNetaDConditionModel(
             act_fn='''gelu'''			,    attention_head_dim=8			,    norm_num_groups=a__			,    block_out_channels=[3_2, 3_2, 6_4, 6_4]			,    time_cond_proj_dim=1_6_0			,    conv_in_kernel=1			,    conv_out_kernel=1			,    cross_attention_dim=3_2			,    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''')			,    )
         A__					=       AutoencoderKL(
             block_out_channels=[3_2, 3_2, 6_4, 6_4]			,    in_channels=3			,    out_channels=3			,    down_block_types=[
                 '''DownEncoderBlock2D''',
                 '''DownEncoderBlock2D''',
                 '''DownEncoderBlock2D''',
                 '''DownEncoderBlock2D''',
             ]			,    up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']			,    latent_channels=4			,    )
         A__					=       EulerDiscreteScheduler(prediction_type='''sample''')
         A__					=       CLIPTextConfig(
             bos_token_id=0			,    eos_token_id=2			,    hidden_size=3_2			,    intermediate_size=3_7			,    layer_norm_eps=1e-05			,    num_attention_heads=4			,    num_hidden_layers=5			,    pad_token_id=1			,    vocab_size=1_0_0_0			,    hidden_act='''quick_gelu'''			,    projection_dim=5_1_2			,    )
         A__					=       CLIPTextModel(a__)
         A__					=       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

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

         return components




     def 		snake_case_		(					self			,    a__			,    a__=0):
         if str(a__).startswith('''mps'''):
             A__					=       torch.manual_seed(a__)
         else:
             A__					=       torch.Generator(device=a__).manual_seed(a__)
         A__					=       {
             '''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 		snake_case_		(					self):
         A__					=       '''cpu'''

         A__					=       self.get_dummy_components()
         A__					=       self.pipeline_class(**a__)
         pipe.to(a__)
         pipe.set_progress_bar_config(disable=a__)

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

         self.assertEqual(image.shape			,    (1, 2_5_6, 2_5_6, 3))
         A__					=       np.array(
             [0.4_7_2_2_2_4_1_2, 0.4_1_9_2_1_6_3_3, 0.4_4_7_1_7_4_3_4, 0.4_6_8_7_4_1_9_2, 0.4_2_5_8_8_2_5_8, 0.4_6_1_5_0_7_2_6, 0.4_6_7_7_5_3_4, 0.4_5_5_8_3_8_3_2, 0.4_8_5_7_9_0_5_5])
         A__					=       np.abs(image_slice.flatten() - expected_slice).max()
         self.assertLessEqual(a__			,    1e-3)




     def 		snake_case_		(					self):
         super().test_attention_slicing_forward_pass(expected_max_diff=7e-3)




     def 		snake_case_		(					self):
         super().test_cpu_offload_forward_pass(expected_max_diff=3e-3)




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




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




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




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




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




     def 		snake_case_		(					self):
         A__					=       [
             '''DDIMScheduler''',
             '''DDPMScheduler''',
             '''PNDMScheduler''',
             '''HeunDiscreteScheduler''',
             '''EulerAncestralDiscreteScheduler''',
             '''KDPM2DiscreteScheduler''',
             '''KDPM2AncestralDiscreteScheduler''',
             '''DPMSolverSDEScheduler''',
         ]
         A__					=       self.get_dummy_components()
         A__					=       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__)
         A__					=       self.get_dummy_inputs(a__)
         A__					=       2

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

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

         assert check_same_shape(a__)







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




     def 		snake_case_		(					self):
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()




     def 		snake_case_		(					self):
         A__					=       torch.manual_seed(3_3)

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

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

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

         A__					=       pipe(a__			,    generator=a__			,    output_type='''latent''').images

         A__					=       upscaler(
             prompt=a__			,    image=a__			,    num_inference_steps=2_0			,    guidance_scale=0			,    generator=a__			,    output_type='''np'''			,    ).images[0]

         A__					=       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 		snake_case_		(					self):
         A__					=       torch.manual_seed(3_3)

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

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

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

         A__					=       upscaler(
             prompt=a__			,    image=a__			,    num_inference_steps=2_0			,    guidance_scale=0			,    generator=a__			,    output_type='''np'''			,    ).images[0]

         A__					=       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
