


import gc
import random
import tempfile
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.semantic_stable_diffusion import SemanticStableDiffusionPipeline as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()



class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







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







         """simple docstring"""







         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()




      @property
      def    UpperCAmelCase							(		self)					->						List[Any]:







         """simple docstring"""







         UpperCamelCase__ :			str 						=1
         UpperCamelCase__ :			List[Any] 						=3
         UpperCamelCase__ :			int 						=(32, 32)

         UpperCamelCase__ :			Optional[int] 						=floats_tensor((batch_size, num_channels) + sizes   ,			rng=random.Random(0)).to(__SCREAMING_SNAKE_CASE)
         return image




      @property
      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         torch.manual_seed(0)
         UpperCamelCase__ :			int 						=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   ,			)
         return model




      @property
      def    UpperCAmelCase							(		self)					->						Optional[int]:







         """simple docstring"""







         torch.manual_seed(0)
         UpperCamelCase__ :			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   ,			)
         return model




      @property
      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         torch.manual_seed(0)
         UpperCamelCase__ :			Any 						=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   ,			)
         return CLIPTextModel(__SCREAMING_SNAKE_CASE)




      @property
      def    UpperCAmelCase							(		self)					->						Union[str, Any]:







         """simple docstring"""







         def extract(*__SCREAMING_SNAKE_CASE   ,			**__SCREAMING_SNAKE_CASE):



            class 							lowercase__:






                  '''simple docstring'''







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







                     """simple docstring"""







                     UpperCamelCase__ :			Dict 						=torch.ones([0])




                  def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE)					->						Union[str, Any]:







                     """simple docstring"""







                     self.pixel_values.to(__SCREAMING_SNAKE_CASE)
                     return self




            return Out()

         return extract




      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						="cpu"  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase__ :			Any 						=self.dummy_cond_unet
         UpperCamelCase__ :			Dict 						=DDIMScheduler(
             beta_start=0.0_00_85   ,			beta_end=0.0_12   ,			beta_schedule="scaled_linear"   ,			clip_sample=__SCREAMING_SNAKE_CASE   ,			set_alpha_to_one=__SCREAMING_SNAKE_CASE   ,			)

         UpperCamelCase__ :			str 						=self.dummy_vae
         UpperCamelCase__ :			List[Any] 						=self.dummy_text_encoder
         UpperCamelCase__ :			str 						=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

         # make sure here that pndm scheduler skips prk
         UpperCamelCase__ :			Dict 						=StableDiffusionPipeline(
             unet=__SCREAMING_SNAKE_CASE   ,			scheduler=__SCREAMING_SNAKE_CASE   ,			vae=__SCREAMING_SNAKE_CASE   ,			text_encoder=__SCREAMING_SNAKE_CASE   ,			tokenizer=__SCREAMING_SNAKE_CASE   ,			safety_checker=__SCREAMING_SNAKE_CASE   ,			feature_extractor=self.dummy_extractor   ,			)
         UpperCamelCase__ :			List[str] 						=sd_pipe.to(__SCREAMING_SNAKE_CASE)
         sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Union[str, Any] 						="A painting of a squirrel eating a burger"

         UpperCamelCase__ :			Tuple 						=torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(0)
         UpperCamelCase__ :			Tuple 						=sd_pipe([prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=6.0   ,			num_inference_steps=2   ,			output_type="np")
         UpperCamelCase__ :			Any 						=output.images

         UpperCamelCase__ :			str 						=torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(0)
         UpperCamelCase__ :			int 						=sd_pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=6.0   ,			num_inference_steps=2   ,			output_type="np"   ,			return_dict=__SCREAMING_SNAKE_CASE   ,			)[0]

         UpperCamelCase__ :			Any 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			Tuple 						=image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 64, 64, 3)
         UpperCamelCase__ :			Optional[Any] 						=np.array([0.57_53, 0.61_14, 0.50_01, 0.50_34, 0.54_70, 0.47_29, 0.49_71, 0.48_67, 0.48_67])

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




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__ :			Dict 						="cpu"  # ensure determinism for the device-dependent torch.Generator
         UpperCamelCase__ :			Optional[int] 						=self.dummy_cond_unet
         UpperCamelCase__ :			Optional[int] 						=PNDMScheduler(skip_prk_steps=__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			List[Any] 						=self.dummy_vae
         UpperCamelCase__ :			Tuple 						=self.dummy_text_encoder
         UpperCamelCase__ :			List[Any] 						=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

         # make sure here that pndm scheduler skips prk
         UpperCamelCase__ :			Tuple 						=StableDiffusionPipeline(
             unet=__SCREAMING_SNAKE_CASE   ,			scheduler=__SCREAMING_SNAKE_CASE   ,			vae=__SCREAMING_SNAKE_CASE   ,			text_encoder=__SCREAMING_SNAKE_CASE   ,			tokenizer=__SCREAMING_SNAKE_CASE   ,			safety_checker=__SCREAMING_SNAKE_CASE   ,			feature_extractor=self.dummy_extractor   ,			)
         UpperCamelCase__ :			str 						=sd_pipe.to(__SCREAMING_SNAKE_CASE)
         sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Optional[Any] 						="A painting of a squirrel eating a burger"
         UpperCamelCase__ :			Any 						=torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(0)
         UpperCamelCase__ :			Union[str, Any] 						=sd_pipe([prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=6.0   ,			num_inference_steps=2   ,			output_type="np")

         UpperCamelCase__ :			Optional[Any] 						=output.images

         UpperCamelCase__ :			List[Any] 						=torch.Generator(device=__SCREAMING_SNAKE_CASE).manual_seed(0)
         UpperCamelCase__ :			Tuple 						=sd_pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=6.0   ,			num_inference_steps=2   ,			output_type="np"   ,			return_dict=__SCREAMING_SNAKE_CASE   ,			)[0]

         UpperCamelCase__ :			Optional[Any] 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			str 						=image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 64, 64, 3)
         UpperCamelCase__ :			List[Any] 						=np.array([0.51_22, 0.57_12, 0.48_25, 0.50_53, 0.56_46, 0.47_69, 0.51_79, 0.48_94, 0.49_94])

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




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







         """simple docstring"""







         UpperCamelCase__ :			List[Any] 						=StableDiffusionPipeline.from_pretrained(
             "hf-internal-testing/tiny-stable-diffusion-lms-pipe"   ,			safety_checker=__SCREAMING_SNAKE_CASE)
         assert isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)
         assert isinstance(pipe.scheduler   ,			__SCREAMING_SNAKE_CASE)
         assert pipe.safety_checker is None

         UpperCamelCase__ :			int 						=pipe("example prompt"   ,			num_inference_steps=2).images[0]
         assert image is not None

         # check that there's no error when saving a pipeline with one of the models being None
         with tempfile.TemporaryDirectory() as tmpdirname:
            pipe.save_pretrained(__SCREAMING_SNAKE_CASE)
            UpperCamelCase__ :			Union[str, Any] 						=StableDiffusionPipeline.from_pretrained(__SCREAMING_SNAKE_CASE)

         # sanity check that the pipeline still works
         assert pipe.safety_checker is None
         UpperCamelCase__ :			Optional[Any] 						=pipe("example prompt"   ,			num_inference_steps=2).images[0]
         assert image is not None




      @unittest.skipIf(torch_device != "cuda"   ,			"This test requires a GPU")
      def    UpperCAmelCase							(		self)					->						List[Any]:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.dummy_cond_unet
         UpperCamelCase__ :			Optional[Any] 						=PNDMScheduler(skip_prk_steps=__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Any 						=self.dummy_vae
         UpperCamelCase__ :			int 						=self.dummy_text_encoder
         UpperCamelCase__ :			Any 						=CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

         # put models in fp16
         UpperCamelCase__ :			str 						=unet.half()
         UpperCamelCase__ :			List[Any] 						=vae.half()
         UpperCamelCase__ :			Tuple 						=bert.half()

         # make sure here that pndm scheduler skips prk
         UpperCamelCase__ :			Tuple 						=StableDiffusionPipeline(
             unet=__SCREAMING_SNAKE_CASE   ,			scheduler=__SCREAMING_SNAKE_CASE   ,			vae=__SCREAMING_SNAKE_CASE   ,			text_encoder=__SCREAMING_SNAKE_CASE   ,			tokenizer=__SCREAMING_SNAKE_CASE   ,			safety_checker=__SCREAMING_SNAKE_CASE   ,			feature_extractor=self.dummy_extractor   ,			)
         UpperCamelCase__ :			Union[str, Any] 						=sd_pipe.to(__SCREAMING_SNAKE_CASE)
         sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			int 						="A painting of a squirrel eating a burger"
         UpperCamelCase__ :			Optional[int] 						=sd_pipe([prompt]   ,			num_inference_steps=2   ,			output_type="np").images

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




@nightly
@require_torch_gpu
class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







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







         """simple docstring"""







         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()




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







         """simple docstring"""







         UpperCamelCase__ :			Dict 						="cuda"
         UpperCamelCase__ :			Union[str, Any] 						=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
         UpperCamelCase__ :			str 						=pipe.to(__SCREAMING_SNAKE_CASE)
         pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Dict 						="a photo of a cat"
         UpperCamelCase__ :			List[str] 						={
             "editing_prompt": ["sunglasses"],
             "reverse_editing_direction": [False],
             "edit_warmup_steps": 10,
             "edit_guidance_scale": 6,
             "edit_threshold": 0.95,
             "edit_momentum_scale": 0.5,
             "edit_mom_beta": 0.6,
         }

         UpperCamelCase__ :			Any 						=3
         UpperCamelCase__ :			str 						=7

         # no sega enabled
         UpperCamelCase__ :			str 						=torch.Generator(__SCREAMING_SNAKE_CASE)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Union[str, Any] 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			)

         UpperCamelCase__ :			int 						=output.images
         UpperCamelCase__ :			Tuple 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			List[str] 						=[
             0.34_67_31_13,
             0.38_49_27_33,
             0.37_59_73_52,
             0.34_08_63_35,
             0.35_65_07_48,
             0.35_57_92_05,
             0.3_38_47_63,
             0.34_34_02_36,
             0.3_57_32_71,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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

         # with sega enabled
         # generator = torch.manual_seed(seed)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Any 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			**__SCREAMING_SNAKE_CASE   ,			)

         UpperCamelCase__ :			Tuple 						=output.images
         UpperCamelCase__ :			Any 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			Dict 						=[
             0.41_88_78_26,
             0.37_72_87_66,
             0.30_13_82_72,
             0.41_41_63_35,
             0.41_66_49_85,
             0.36_28_33_92,
             0.36_19_12_46,
             0.43_36_44_65,
             0.43_00_17_32,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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




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







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						="cuda"
         UpperCamelCase__ :			Optional[Any] 						=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
         UpperCamelCase__ :			Union[str, Any] 						=pipe.to(__SCREAMING_SNAKE_CASE)
         pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Tuple 						="an image of a crowded boulevard, realistic, 4k"
         UpperCamelCase__ :			Optional[Any] 						={
             "editing_prompt": "crowd, crowded, people",
             "reverse_editing_direction": True,
             "edit_warmup_steps": 10,
             "edit_guidance_scale": 8.3,
             "edit_threshold": 0.9,
             "edit_momentum_scale": 0.5,
             "edit_mom_beta": 0.6,
         }

         UpperCamelCase__ :			Any 						=9
         UpperCamelCase__ :			Dict 						=7

         # no sega enabled
         UpperCamelCase__ :			List[Any] 						=torch.Generator(__SCREAMING_SNAKE_CASE)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			List[Any] 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			)

         UpperCamelCase__ :			Optional[int] 						=output.images
         UpperCamelCase__ :			int 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			Tuple 						=[
             0.43_49_79_98,
             0.91_81_40_65,
             0.7_54_07_39,
             0.55_58_02_05,
             0.8_46_72_65,
             0.5_38_96_91,
             0.62_57_45_06,
             0.58_89_77_63,
             0.50_92_67_57,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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

         # with sega enabled
         # generator = torch.manual_seed(seed)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Union[str, Any] 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			**__SCREAMING_SNAKE_CASE   ,			)

         UpperCamelCase__ :			Union[str, Any] 						=output.images
         UpperCamelCase__ :			Any 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			Dict 						=[
             0.3_08_97_19,
             0.30_50_01_44,
             0.29_01_60_42,
             0.30_63_09_64,
             0.32_56_87,
             0.29_41_92_25,
             0.2_90_80_91,
             0.28_72_35_98,
             0.27_69_62_94,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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




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







         """simple docstring"""







         UpperCamelCase__ :			Any 						="cuda"
         UpperCamelCase__ :			Union[str, Any] 						=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
         UpperCamelCase__ :			List[Any] 						=pipe.to(__SCREAMING_SNAKE_CASE)
         pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Dict 						="a castle next to a river"
         UpperCamelCase__ :			str 						={
             "editing_prompt": ["boat on a river, boat", "monet, impression, sunrise"],
             "reverse_editing_direction": False,
             "edit_warmup_steps": [15, 18],
             "edit_guidance_scale": 6,
             "edit_threshold": [0.9, 0.8],
             "edit_momentum_scale": 0.5,
             "edit_mom_beta": 0.6,
         }

         UpperCamelCase__ :			Optional[int] 						=48
         UpperCamelCase__ :			List[Any] 						=7

         # no sega enabled
         UpperCamelCase__ :			Tuple 						=torch.Generator(__SCREAMING_SNAKE_CASE)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			str 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			)

         UpperCamelCase__ :			List[str] 						=output.images
         UpperCamelCase__ :			Optional[int] 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			str 						=[
             0.75_16_35_55,
             0.76_03_71_45,
             0.6_17_85,
             0.9_18_96_73,
             0.8_62_77_01,
             0.85_18_96_94,
             0.8_51_28_13,
             0.87_01_20_76,
             0.8_31_28_57,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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

         # with sega enabled
         # generator = torch.manual_seed(seed)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Tuple 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			**__SCREAMING_SNAKE_CASE   ,			)

         UpperCamelCase__ :			List[Any] 						=output.images
         UpperCamelCase__ :			Optional[Any] 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			int 						=[
             0.73_55_33_65,
             0.7_53_72_71,
             0.74_34_19_05,
             0.66_48_03_56,
             0.6_47_29_25,
             0.63_03_94_16,
             0.64_81_29_05,
             0.6_74_97_17,
             0.6_51_71_02,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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




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







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						="cuda"
         UpperCamelCase__ :			Optional[int] 						=StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5"   ,			torch_dtype=torch.floataa)
         UpperCamelCase__ :			Union[str, Any] 						=pipe.to(__SCREAMING_SNAKE_CASE)
         pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Tuple 						="a photo of a cat"
         UpperCamelCase__ :			Tuple 						={
             "editing_prompt": ["sunglasses"],
             "reverse_editing_direction": [False],
             "edit_warmup_steps": 10,
             "edit_guidance_scale": 6,
             "edit_threshold": 0.95,
             "edit_momentum_scale": 0.5,
             "edit_mom_beta": 0.6,
         }

         UpperCamelCase__ :			int 						=3
         UpperCamelCase__ :			List[Any] 						=7

         # no sega enabled
         UpperCamelCase__ :			List[str] 						=torch.Generator(__SCREAMING_SNAKE_CASE)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Dict 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			)

         UpperCamelCase__ :			Tuple 						=output.images
         UpperCamelCase__ :			int 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			Any 						=[
             0.34_88_76_95,
             0.3_87_69_53,
             0.3_75,
             0.34_42_38_28,
             0.3_58_15_43,
             0.35_71_77_73,
             0.3_38_37_89,
             0.34_57_03_12,
             0.35_93_75,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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

         # with sega enabled
         # generator = torch.manual_seed(seed)
         generator.manual_seed(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			List[str] 						=pipe(
             [prompt]   ,			generator=__SCREAMING_SNAKE_CASE   ,			guidance_scale=__SCREAMING_SNAKE_CASE   ,			num_inference_steps=50   ,			output_type="np"   ,			width=5_12   ,			height=5_12   ,			**__SCREAMING_SNAKE_CASE   ,			)

         UpperCamelCase__ :			int 						=output.images
         UpperCamelCase__ :			Union[str, Any] 						=image[0, -3:, -3:, -1]
         UpperCamelCase__ :			Union[str, Any] 						=[
             0.42_28_51_56,
             0.36_91_40_62,
             0.29_07_71_48,
             0.42_04_10_16,
             0.41_91_89_45,
             0.35_49_80_47,
             0.3_61_81_64,
             0.4_42_38_28,
             0.43_11_52_34,
         ]

         assert image.shape == (1, 5_12, 5_12, 3)

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





import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import 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 (
         ErnieMForInformationExtraction,
         ErnieMForMultipleChoice,
         ErnieMForQuestionAnswering,
         ErnieMForSequenceClassification,
         ErnieMForTokenClassification,
         ErnieMModel,
     )
     from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST



class 							lowercase__:






      '''simple docstring'''







      def __init__(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE=13   ,			__SCREAMING_SNAKE_CASE=7   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=99   ,			__SCREAMING_SNAKE_CASE=32   ,			__SCREAMING_SNAKE_CASE=5   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=37   ,			__SCREAMING_SNAKE_CASE="gelu"   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=5_12   ,			__SCREAMING_SNAKE_CASE=16   ,			__SCREAMING_SNAKE_CASE=2   ,			__SCREAMING_SNAKE_CASE=0.02   ,			__SCREAMING_SNAKE_CASE=3   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=None   ,			)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=parent
         UpperCamelCase__ :			Tuple 						=batch_size
         UpperCamelCase__ :			Union[str, Any] 						=seq_length
         UpperCamelCase__ :			Union[str, Any] 						=is_training
         UpperCamelCase__ :			Tuple 						=use_input_mask
         UpperCamelCase__ :			Union[str, Any] 						=use_labels
         UpperCamelCase__ :			Union[str, Any] 						=vocab_size
         UpperCamelCase__ :			List[Any] 						=hidden_size
         UpperCamelCase__ :			Dict 						=num_hidden_layers
         UpperCamelCase__ :			Any 						=num_attention_heads
         UpperCamelCase__ :			List[str] 						=intermediate_size
         UpperCamelCase__ :			List[Any] 						=hidden_act
         UpperCamelCase__ :			Tuple 						=hidden_dropout_prob
         UpperCamelCase__ :			Any 						=attention_probs_dropout_prob
         UpperCamelCase__ :			Tuple 						=max_position_embeddings
         UpperCamelCase__ :			Dict 						=type_vocab_size
         UpperCamelCase__ :			Tuple 						=type_sequence_label_size
         UpperCamelCase__ :			Tuple 						=initializer_range
         UpperCamelCase__ :			List[str] 						=num_labels
         UpperCamelCase__ :			Any 						=num_choices
         UpperCamelCase__ :			Tuple 						=scope




      def    UpperCAmelCase							(		self)					->						int:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

         UpperCamelCase__ :			Any 						=None
         if self.use_input_mask:
            UpperCamelCase__ :			int 						=random_attention_mask([self.batch_size, self.seq_length])

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

         UpperCamelCase__ :			Tuple 						=self.get_config()

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




      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

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

         return config, input_ids, input_mask




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







         """simple docstring"""







         return ErnieMConfig(
             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   ,			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   ,			initializer_range=self.initializer_range   ,			)




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=ErnieMModel(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			int 						=model(__SCREAMING_SNAKE_CASE   ,			return_dict=__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						List[Any]:







         """simple docstring"""







         UpperCamelCase__ :			str 						=ErnieMForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Dict 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			start_positions=__SCREAMING_SNAKE_CASE   ,			end_positions=__SCREAMING_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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=ErnieMForInformationExtraction(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			int 						=torch.ones_like(__SCREAMING_SNAKE_CASE   ,			dtype=torch.floataa)
         UpperCamelCase__ :			Union[str, Any] 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			start_positions=__SCREAMING_SNAKE_CASE   ,			end_positions=__SCREAMING_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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			List[Any] 						=self.num_labels
         UpperCamelCase__ :			List[Any] 						=ErnieMForSequenceClassification(__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Union[str, Any] 						=model(__SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_labels))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=self.num_labels
         UpperCamelCase__ :			int 						=ErnieMForTokenClassification(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         input_ids.to(__SCREAMING_SNAKE_CASE)
         input_mask.to(__SCREAMING_SNAKE_CASE)
         token_labels.to(__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Tuple 						=model(__SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE)

         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.num_labels))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







         UpperCamelCase__ :			List[Any] 						=self.num_choices
         UpperCamelCase__ :			List[str] 						=ErnieMForMultipleChoice(config=__SCREAMING_SNAKE_CASE)
         model.to(__SCREAMING_SNAKE_CASE)
         model.eval()
         UpperCamelCase__ :			Optional[Any] 						=input_ids.unsqueeze(1).expand(-1   ,			self.num_choices   ,			-1).contiguous()
         UpperCamelCase__ :			int 						=input_mask.unsqueeze(1).expand(-1   ,			self.num_choices   ,			-1).contiguous()
         UpperCamelCase__ :			Optional[Any] 						=model(
             __SCREAMING_SNAKE_CASE   ,			attention_mask=__SCREAMING_SNAKE_CASE   ,			labels=__SCREAMING_SNAKE_CASE   ,			)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_choices))




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







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=self.prepare_config_and_inputs()
         (
             (
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	(
             UpperCamelCase__
         )						,	
         ) :			int 						=config_and_inputs
         UpperCamelCase__ :			Tuple 						={"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict




@require_torch
class 							lowercase__(						snake_case__			,							snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					(
          (
              ErnieMModel,
              ErnieMForMultipleChoice,
              ErnieMForQuestionAnswering,
              ErnieMForSequenceClassification,
              ErnieMForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      snake_case__       =					()
      snake_case__       =					(
          {
              '''feature-extraction''': ErnieMModel,
              '''question-answering''': ErnieMForQuestionAnswering,
              '''text-classification''': ErnieMForSequenceClassification,
              '''token-classification''': ErnieMForTokenClassification,
              '''zero-shot''': ErnieMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case__       =					False




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







         if pipeline_test_casse_name == "QAPipelineTests":
            return True

         return False




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







         """simple docstring"""







         UpperCamelCase__ :			Dict 						=ErnieMModelTester(self)
         UpperCamelCase__ :			int 						=ConfigTester(self   ,			config_class=__SCREAMING_SNAKE_CASE   ,			hidden_size=37)




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







         """simple docstring"""







         self.config_tester.run_common_tests()




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







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			int 						=self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCamelCase__ :			List[str] 						=type
            self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_information_extraction(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			Optional[Any] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE)




      @slow
      def    UpperCAmelCase							(		self)					->						Tuple:







         """simple docstring"""







         for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__ :			Optional[Any] 						=ErnieMModel.from_pretrained(__SCREAMING_SNAKE_CASE)
            self.assertIsNotNone(__SCREAMING_SNAKE_CASE)




@require_torch
class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







      @slow
      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=ErnieMModel.from_pretrained("susnato/ernie-m-base_pytorch")
         model.eval()
         UpperCamelCase__ :			int 						=torch.tensor([[0, 1, 2, 3, 4, 5]])
         UpperCamelCase__ :			List[Any] 						=model(__SCREAMING_SNAKE_CASE)[0]

         # TODO Replace vocab size
         UpperCamelCase__ :			List[Any] 						=7_68

         UpperCamelCase__ :			Dict 						=torch.Size((1, 6, hidden_size))
         self.assertEqual(output.shape   ,			__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Union[str, Any] 						=torch.tensor(
             [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]])

         self.assertTrue(torch.allclose(output[:, :3, :3]   ,			__SCREAMING_SNAKE_CASE   ,			atol=1E-3))

