
from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TFFunnelBaseModel,
     TFFunnelForMaskedLM,
     TFFunnelForMultipleChoice,
     TFFunnelForPreTraining,
     TFFunnelForQuestionAnswering,
     TFFunnelForSequenceClassification,
     TFFunnelForTokenClassification,
     TFFunnelModel,
 )


class 						__lowerCAmelCase	:



      """simple docstring"""







      def __init__(      self		:       List[Any]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       Dict=13				,       _snake_case		:       Dict=7				,       _snake_case		:       Dict=True				,       _snake_case		:       List[str]=True				,       _snake_case		:       Union[str, Any]=True				,       _snake_case		:       Optional[Any]=True				,       _snake_case		:       Any=99				,       _snake_case		:       Optional[int]=[1, 1, 2]				,       _snake_case		:       List[str]=1				,       _snake_case		:       List[Any]=32				,       _snake_case		:       Union[str, Any]=4				,       _snake_case		:       Any=8				,       _snake_case		:       str=37				,       _snake_case		:       Union[str, Any]="gelu_new"				,       _snake_case		:       List[str]=0.1				,       _snake_case		:       Dict=0.1				,       _snake_case		:       List[str]=0.0				,       _snake_case		:       Optional[Any]=512				,       _snake_case		:       Optional[Any]=3				,       _snake_case		:       Optional[Any]=0.02				,       _snake_case		:       List[Any]=3				,       _snake_case		:       int=4				,       _snake_case		:       Optional[int]=None				,       _snake_case		:       Tuple=False				,       ):
          __lowercase		:			Union[str, Any]	    =       parent
          __lowercase		:			Tuple	    =       batch_size
          __lowercase		:			Dict	    =       seq_length
          __lowercase		:			int	    =       is_training
          __lowercase		:			Optional[Any]	    =       use_input_mask
          __lowercase		:			List[str]	    =       use_token_type_ids
          __lowercase		:			Tuple	    =       use_labels
          __lowercase		:			List[str]	    =       vocab_size
          __lowercase		:			Any	    =       block_sizes
          __lowercase		:			Optional[Any]	    =       num_decoder_layers
          __lowercase		:			int	    =       d_model
          __lowercase		:			Union[str, Any]	    =       n_head
          __lowercase		:			Tuple	    =       d_head
          __lowercase		:			int	    =       d_inner
          __lowercase		:			Union[str, Any]	    =       hidden_act
          __lowercase		:			Dict	    =       hidden_dropout
          __lowercase		:			Dict	    =       attention_dropout
          __lowercase		:			List[str]	    =       activation_dropout
          __lowercase		:			Any	    =       max_position_embeddings
          __lowercase		:			List[Any]	    =       type_vocab_size
          __lowercase		:			Optional[Any]	    =       2
          __lowercase		:			List[Any]	    =       num_labels
          __lowercase		:			Optional[Any]	    =       num_choices
          __lowercase		:			List[str]	    =       scope
          __lowercase		:			Union[str, Any]	    =       initializer_std

          # Used in the tests to check the size of the first attention layer
          __lowercase		:			Optional[int]	    =       n_head
          # Used in the tests to check the size of the first hidden state
          __lowercase		:			List[Any]	    =       self.d_model
          # Used in the tests to check the number of output hidden states/attentions
          __lowercase		:			Union[str, Any]	    =       sum(self.block_sizes				) + (0 if base else self.num_decoder_layers)
          # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
          # the last hidden state of the first block (which is the first hidden state of the decoder).
          if not base:
              __lowercase		:			Optional[int]	    =       self.num_hidden_layers + 2




      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Dict	    =       ids_tensor([self.batch_size, self.seq_length]				,       self.vocab_size				)

          __lowercase		:			Union[str, Any]	    =       None
          if self.use_input_mask:
              __lowercase		:			Any	    =       random_attention_mask([self.batch_size, self.seq_length]				)

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

          __lowercase		:			Optional[Any]	    =       None
          __lowercase		:			Optional[int]	    =       None
          __lowercase		:			Optional[int]	    =       None
          if self.use_labels:
              __lowercase		:			List[str]	    =       ids_tensor([self.batch_size]				,       self.type_sequence_label_size				)
              __lowercase		:			Optional[Any]	    =       ids_tensor([self.batch_size, self.seq_length]				,       self.num_labels				)
              __lowercase		:			Union[str, Any]	    =       ids_tensor([self.batch_size]				,       self.num_choices				)

          __lowercase		:			Union[str, Any]	    =       FunnelConfig(
              vocab_size=self.vocab_size				,       block_sizes=self.block_sizes				,       num_decoder_layers=self.num_decoder_layers				,       d_model=self.d_model				,       n_head=self.n_head				,       d_head=self.d_head				,       d_inner=self.d_inner				,       hidden_act=self.hidden_act				,       hidden_dropout=self.hidden_dropout				,       attention_dropout=self.attention_dropout				,       activation_dropout=self.activation_dropout				,       max_position_embeddings=self.max_position_embeddings				,       type_vocab_size=self.type_vocab_size				,       initializer_std=self.initializer_std				,       )

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




      def 		snake_case_			(      self		:       List[str]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       Any				,       _snake_case		:       Dict				,       _snake_case		:       List[Any]				,       _snake_case		:       Tuple				,       _snake_case		:       Tuple				,       ):
          __lowercase		:			Any	    =       TFFunnelModel(config=_snake_case				)
          __lowercase		:			Tuple	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Tuple	    =       model(_snake_case				)

          __lowercase		:			List[str]	    =       [input_ids, input_mask]
          __lowercase		:			Tuple	    =       model(_snake_case				)

          __lowercase		:			List[Any]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.d_model)				)

          __lowercase		:			Any	    =       False
          __lowercase		:			List[Any]	    =       TFFunnelModel(config=_snake_case				)
          __lowercase		:			int	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.d_model)				)

          __lowercase		:			Dict	    =       False
          __lowercase		:			int	    =       TFFunnelModel(config=_snake_case				)
          __lowercase		:			List[Any]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.d_model)				)




      def 		snake_case_			(      self		:       Dict				,       _snake_case		:       Optional[int]				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Any				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       ):
          __lowercase		:			int	    =       TFFunnelBaseModel(config=_snake_case				)
          __lowercase		:			List[str]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Optional[int]	    =       model(_snake_case				)

          __lowercase		:			str	    =       [input_ids, input_mask]
          __lowercase		:			Optional[Any]	    =       model(_snake_case				)

          __lowercase		:			List[str]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, 2, self.d_model)				)

          __lowercase		:			int	    =       False
          __lowercase		:			List[str]	    =       TFFunnelBaseModel(config=_snake_case				)
          __lowercase		:			Dict	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, 3, self.d_model)				)

          __lowercase		:			List[str]	    =       False
          __lowercase		:			Dict	    =       TFFunnelBaseModel(config=_snake_case				)
          __lowercase		:			List[str]	    =       model(_snake_case				)
          self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, 2, self.d_model)				)




      def 		snake_case_			(      self		:       List[Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       Dict				,       _snake_case		:       Dict				,       _snake_case		:       List[Any]				,       _snake_case		:       Tuple				,       _snake_case		:       int				,       ):
          __lowercase		:			Dict	    =       TFFunnelForPreTraining(config=_snake_case				)
          __lowercase		:			str	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			List[Any]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length)				)




      def 		snake_case_			(      self		:       Optional[Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       List[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Any				,       _snake_case		:       List[str]				,       _snake_case		:       Union[str, Any]				,       ):
          __lowercase		:			Any	    =       TFFunnelForMaskedLM(config=_snake_case				)
          __lowercase		:			Optional[int]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Optional[int]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.vocab_size)				)




      def 		snake_case_			(      self		:       List[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       Any				,       _snake_case		:       Dict				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Dict				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       List[str]				,       ):
          __lowercase		:			Union[str, Any]	    =       self.num_labels
          __lowercase		:			List[str]	    =       TFFunnelForSequenceClassification(config=_snake_case				)
          __lowercase		:			int	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Any	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_labels)				)




      def 		snake_case_			(      self		:       Any				,       _snake_case		:       List[Any]				,       _snake_case		:       str				,       _snake_case		:       Any				,       _snake_case		:       List[Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Tuple				,       _snake_case		:       Union[str, Any]				,       ):
          __lowercase		:			Dict	    =       self.num_choices
          __lowercase		:			Union[str, Any]	    =       TFFunnelForMultipleChoice(config=_snake_case				)
          __lowercase		:			Optional[int]	    =       tf.tile(tf.expand_dims(_snake_case				,       1				)				,       (1, self.num_choices, 1)				)
          __lowercase		:			Dict	    =       tf.tile(tf.expand_dims(_snake_case				,       1				)				,       (1, self.num_choices, 1)				)
          __lowercase		:			Optional[int]	    =       tf.tile(tf.expand_dims(_snake_case				,       1				)				,       (1, self.num_choices, 1)				)
          __lowercase		:			Optional[Any]	    =       {
              '''input_ids''': multiple_choice_inputs_ids,
              '''attention_mask''': multiple_choice_input_mask,
              '''token_type_ids''': multiple_choice_token_type_ids,
          }
          __lowercase		:			Tuple	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.num_choices)				)




      def 		snake_case_			(      self		:       List[str]				,       _snake_case		:       Tuple				,       _snake_case		:       Dict				,       _snake_case		:       Optional[Any]				,       _snake_case		:       Optional[int]				,       _snake_case		:       List[str]				,       _snake_case		:       str				,       _snake_case		:       Any				,       ):
          __lowercase		:			List[Any]	    =       self.num_labels
          __lowercase		:			Tuple	    =       TFFunnelForTokenClassification(config=_snake_case				)
          __lowercase		:			Optional[int]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Optional[int]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length, self.num_labels)				)




      def 		snake_case_			(      self		:       Union[str, Any]				,       _snake_case		:       str				,       _snake_case		:       Optional[int]				,       _snake_case		:       str				,       _snake_case		:       List[str]				,       _snake_case		:       List[Any]				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       List[str]				,       ):
          __lowercase		:			Optional[Any]	    =       TFFunnelForQuestionAnswering(config=_snake_case				)
          __lowercase		:			List[str]	    =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
          __lowercase		:			Union[str, Any]	    =       model(_snake_case				)
          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		:       List[Any]				):
          __lowercase		:			Tuple	    =       self.prepare_config_and_inputs()
          (
              (
              __lowercase
          )    ,      (
              __lowercase
          )    ,      (
              __lowercase
          )    ,      (
              __lowercase
          )    ,      (
              __lowercase
          )    ,      (
              __lowercase
          )    ,      (
              __lowercase
          )    ,      
          )		:			Any	    =       config_and_inputs
          __lowercase		:			int	    =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
          return config, inputs_dict






@require_tf
class 						__lowerCAmelCase	(	lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Dict        =    (
          (
              TFFunnelModel,
              TFFunnelForMaskedLM,
              TFFunnelForPreTraining,
              TFFunnelForQuestionAnswering,
              TFFunnelForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      A__	:			Any        =    (
          {
              '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
              '''fill-mask''': TFFunnelForMaskedLM,
              '''question-answering''': TFFunnelForQuestionAnswering,
              '''text-classification''': TFFunnelForSequenceClassification,
              '''token-classification''': TFFunnelForTokenClassification,
              '''zero-shot''': TFFunnelForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      A__	:			Optional[Any]        =    False
      A__	:			Optional[Any]        =    False




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			Union[str, Any]	    =       TFFunnelModelTester(self				)
          __lowercase		:			Union[str, Any]	    =       ConfigTester(self				,       config_class=_snake_case				)




      def 		snake_case_			(      self		:       List[str]				):
          self.config_tester.run_common_tests()




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			List[str]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_snake_case				)




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			List[str]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Dict	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Dict	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_snake_case				)




      def 		snake_case_			(      self		:       Any				):
          __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_snake_case				)






@require_tf
class 						__lowerCAmelCase	(	lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			List[Any]        =    (
          (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
      )
      A__	:			List[Any]        =    False
      A__	:			Any        =    False




      def 		snake_case_			(      self		:       List[str]				):
          __lowercase		:			Tuple	    =       TFFunnelModelTester(self				,       base=_snake_case				)
          __lowercase		:			str	    =       ConfigTester(self				,       config_class=_snake_case				)




      def 		snake_case_			(      self		:       Dict				):
          self.config_tester.run_common_tests()




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			Union[str, Any]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_base_model(*_snake_case				)




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase		:			int	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_snake_case				)




      def 		snake_case_			(      self		:       int				):
          __lowercase		:			Union[str, Any]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*_snake_case				)




import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

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


enable_full_determinism()


class 						__lowerCAmelCase	(	lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			int        =    StableDiffusionSAGPipeline
      A__	:			Union[str, Any]        =    TEXT_TO_IMAGE_PARAMS
      A__	:			Dict        =    TEXT_TO_IMAGE_BATCH_PARAMS
      A__	:			List[str]        =    TEXT_TO_IMAGE_IMAGE_PARAMS
      A__	:			int        =    TEXT_TO_IMAGE_IMAGE_PARAMS
      A__	:			List[str]        =    False




      def 		snake_case_			(      self		:       Optional[Any]				):
          torch.manual_seed(0				)
          __lowercase		:			Optional[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''')				,       cross_attention_dim=32				,       )
          __lowercase		:			int	    =       DDIMScheduler(
              beta_start=0.0_00_85				,       beta_end=0.0_12				,       beta_schedule='''scaled_linear'''				,       clip_sample=_snake_case				,       set_alpha_to_one=_snake_case				,       )
          torch.manual_seed(0				)
          __lowercase		:			List[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				,       )
          torch.manual_seed(0				)
          __lowercase		:			Tuple	    =       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=1000				,       )
          __lowercase		:			List[Any]	    =       CLIPTextModel(_snake_case				)
          __lowercase		:			List[Any]	    =       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''				)

          __lowercase		:			int	    =       {
              '''unet''': unet,
              '''scheduler''': scheduler,
              '''vae''': vae,
              '''text_encoder''': text_encoder,
              '''tokenizer''': tokenizer,
              '''safety_checker''': None,
              '''feature_extractor''': None,
          }
          return components




      def 		snake_case_			(      self		:       Tuple				,       _snake_case		:       List[str]				,       _snake_case		:       List[str]=0				):
          if str(_snake_case				).startswith('''mps'''				):
              __lowercase		:			Optional[int]	    =       torch.manual_seed(_snake_case				)
          else:
              __lowercase		:			Tuple	    =       torch.Generator(device=_snake_case				).manual_seed(_snake_case				)
          __lowercase		:			Tuple	    =       {
              '''prompt''': '''.''',
              '''generator''': generator,
              '''num_inference_steps''': 2,
              '''guidance_scale''': 1.0,
              '''sag_scale''': 1.0,
              '''output_type''': '''numpy''',
          }
          return inputs




      def 		snake_case_			(      self		:       Any				):
          super().test_inference_batch_single_identical(expected_max_diff=3E-3				)






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



      """simple docstring"""







      def 		snake_case_			(      self		:       int				):
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()




      def 		snake_case_			(      self		:       Dict				):
          __lowercase		:			Optional[int]	    =       StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''				)
          __lowercase		:			Optional[int]	    =       sag_pipe.to(_snake_case				)
          sag_pipe.set_progress_bar_config(disable=_snake_case				)

          __lowercase		:			List[str]	    =       '''.'''
          __lowercase		:			List[str]	    =       torch.manual_seed(0				)
          __lowercase		:			List[str]	    =       sag_pipe(
              [prompt]				,       generator=_snake_case				,       guidance_scale=7.5				,       sag_scale=1.0				,       num_inference_steps=20				,       output_type='''np'''				)

          __lowercase		:			Optional[Any]	    =       output.images

          __lowercase		:			Dict	    =       image[0, -3:, -3:, -1]

          assert image.shape == (1, 512, 512, 3)
          __lowercase		:			int	    =       np.array([0.15_68, 0.17_38, 0.16_95, 0.16_93, 0.15_07, 0.17_05, 0.15_47, 0.17_51, 0.19_49]				)

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




      def 		snake_case_			(      self		:       Optional[int]				):
          __lowercase		:			Dict	    =       StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''				)
          __lowercase		:			Any	    =       sag_pipe.to(_snake_case				)
          sag_pipe.set_progress_bar_config(disable=_snake_case				)

          __lowercase		:			List[str]	    =       '''.'''
          __lowercase		:			List[str]	    =       torch.manual_seed(0				)
          __lowercase		:			int	    =       sag_pipe(
              [prompt]				,       generator=_snake_case				,       guidance_scale=7.5				,       sag_scale=1.0				,       num_inference_steps=20				,       output_type='''np'''				)

          __lowercase		:			str	    =       output.images

          __lowercase		:			Tuple	    =       image[0, -3:, -3:, -1]

          assert image.shape == (1, 512, 512, 3)
          __lowercase		:			Any	    =       np.array([0.34_59, 0.28_76, 0.25_37, 0.30_02, 0.26_71, 0.21_60, 0.30_26, 0.22_62, 0.23_71]				)

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




      def 		snake_case_			(      self		:       int				):
          __lowercase		:			List[str]	    =       StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''				)
          __lowercase		:			Tuple	    =       sag_pipe.to(_snake_case				)
          sag_pipe.set_progress_bar_config(disable=_snake_case				)

          __lowercase		:			Union[str, Any]	    =       '''.'''
          __lowercase		:			Tuple	    =       torch.manual_seed(0				)
          __lowercase		:			Optional[int]	    =       sag_pipe(
              [prompt]				,       width=768				,       height=512				,       generator=_snake_case				,       guidance_scale=7.5				,       sag_scale=1.0				,       num_inference_steps=20				,       output_type='''np'''				,       )

          __lowercase		:			Dict	    =       output.images

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


