



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.stable_diffusion_safe import StableDiffusionPipelineSafe as StableDiffusionPipeline
from diffusers.utils import floats_tensor, nightly, torch_device
from diffusers.utils.testing_utils import require_torch_gpu



class     _UpperCAmelCase			(       unittest.TestCase     ):




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




     @property
     def 		snake_case_		(				self):
         A__		 =					1
         A__		 =					3
         A__		 =					(3_2, 3_2)

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




     @property
     def 		snake_case_		(				self):
         torch.manual_seed(0)
         A__		 =					UNetaDConditionModel(
             block_out_channels=(3_2, 6_4) ,						layers_per_block=2 ,						sample_size=3_2 ,						in_channels=4 ,						out_channels=4 ,						down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''') ,						up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''') ,						cross_attention_dim=3_2 ,						)
         return model




     @property
     def 		snake_case_		(				self):
         torch.manual_seed(0)
         A__		 =					AutoencoderKL(
             block_out_channels=[3_2, 6_4] ,						in_channels=3 ,						out_channels=3 ,						down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] ,						up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] ,						latent_channels=4 ,						)
         return model




     @property
     def 		snake_case_		(				self):
         torch.manual_seed(0)
         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 ,						)
         return CLIPTextModel(A_)




     @property
     def 		snake_case_		(				self):
         def extract(*a__ ,						**a__):



             class     _UpperCAmelCase			:




                  def __init__(				self):
                      A__		 =					torch.ones([0])




                  def 		snake_case_		(				self ,						a__):
                      self.pixel_values.to(A_)
                      return self







             return Out()

         return extract




     def 		snake_case_		(				self):
         A__		 =					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
         A__		 =					self.dummy_cond_unet
         A__		 =					DDIMScheduler(
             beta_start=0.0_0_0_8_5 ,						beta_end=0.0_1_2 ,						beta_schedule='''scaled_linear''' ,						clip_sample=A_ ,						set_alpha_to_one=A_ ,						)

         A__		 =					self.dummy_vae
         A__		 =					self.dummy_text_encoder
         A__		 =					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

         # make sure here that pndm scheduler skips prk
         A__		 =					StableDiffusionPipeline(
             unet=A_ ,						scheduler=A_ ,						vae=A_ ,						text_encoder=A_ ,						tokenizer=A_ ,						safety_checker=A_ ,						feature_extractor=self.dummy_extractor ,						)
         A__		 =					sd_pipe.to(A_)
         sd_pipe.set_progress_bar_config(disable=A_)

         A__		 =					'''A painting of a squirrel eating a burger'''

         A__		 =					torch.Generator(device=A_).manual_seed(0)
         A__		 =					sd_pipe([prompt] ,						generator=A_ ,						guidance_scale=6.0 ,						num_inference_steps=2 ,						output_type='''np''')
         A__		 =					output.images

         A__		 =					torch.Generator(device=A_).manual_seed(0)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=6.0 ,						num_inference_steps=2 ,						output_type='''np''' ,						return_dict=A_ ,						)[0]

         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 6_4, 6_4, 3)
         A__		 =					np.array([0.5_7_5_6, 0.6_1_1_8, 0.5_0_0_5, 0.5_0_4_1, 0.5_4_7_1, 0.4_7_2_6, 0.4_9_7_6, 0.4_8_6_5, 0.4_8_6_4])

         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 		snake_case_		(				self):
         A__		 =					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
         A__		 =					self.dummy_cond_unet
         A__		 =					PNDMScheduler(skip_prk_steps=A_)
         A__		 =					self.dummy_vae
         A__		 =					self.dummy_text_encoder
         A__		 =					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

         # make sure here that pndm scheduler skips prk
         A__		 =					StableDiffusionPipeline(
             unet=A_ ,						scheduler=A_ ,						vae=A_ ,						text_encoder=A_ ,						tokenizer=A_ ,						safety_checker=A_ ,						feature_extractor=self.dummy_extractor ,						)
         A__		 =					sd_pipe.to(A_)
         sd_pipe.set_progress_bar_config(disable=A_)

         A__		 =					'''A painting of a squirrel eating a burger'''
         A__		 =					torch.Generator(device=A_).manual_seed(0)
         A__		 =					sd_pipe([prompt] ,						generator=A_ ,						guidance_scale=6.0 ,						num_inference_steps=2 ,						output_type='''np''')

         A__		 =					output.images

         A__		 =					torch.Generator(device=A_).manual_seed(0)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=6.0 ,						num_inference_steps=2 ,						output_type='''np''' ,						return_dict=A_ ,						)[0]

         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					image_from_tuple[0, -3:, -3:, -1]

         assert image.shape == (1, 6_4, 6_4, 3)
         A__		 =					np.array([0.5_1_2_5, 0.5_7_1_6, 0.4_8_2_8, 0.5_0_6_0, 0.5_6_5_0, 0.4_7_6_8, 0.5_1_8_5, 0.4_8_9_5, 0.4_9_9_3])

         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 		snake_case_		(				self):
         A__		 =					StableDiffusionPipeline.from_pretrained(
             '''hf-internal-testing/tiny-stable-diffusion-lms-pipe''' ,						safety_checker=A_)
         assert isinstance(A_ ,						A_)
         assert isinstance(pipe.scheduler ,						A_)
         assert pipe.safety_checker is None

         A__		 =					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(A_)
             A__		 =					StableDiffusionPipeline.from_pretrained(A_)

         # sanity check that the pipeline still works
         assert pipe.safety_checker is None
         A__		 =					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 		snake_case_		(				self):
         A__		 =					self.dummy_cond_unet
         A__		 =					PNDMScheduler(skip_prk_steps=A_)
         A__		 =					self.dummy_vae
         A__		 =					self.dummy_text_encoder
         A__		 =					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

         # put models in fp16
         A__		 =					unet.half()
         A__		 =					vae.half()
         A__		 =					bert.half()

         # make sure here that pndm scheduler skips prk
         A__		 =					StableDiffusionPipeline(
             unet=A_ ,						scheduler=A_ ,						vae=A_ ,						text_encoder=A_ ,						tokenizer=A_ ,						safety_checker=A_ ,						feature_extractor=self.dummy_extractor ,						)
         A__		 =					sd_pipe.to(A_)
         sd_pipe.set_progress_bar_config(disable=A_)

         A__		 =					'''A painting of a squirrel eating a burger'''
         A__		 =					sd_pipe([prompt] ,						num_inference_steps=2 ,						output_type='''np''').images

         assert image.shape == (1, 6_4, 6_4, 3)







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




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




     def 		snake_case_		(				self):
         A__		 =					StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ,						safety_checker=A_)
         A__		 =					LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config)
         A__		 =					sd_pipe.to(A_)
         sd_pipe.set_progress_bar_config(disable=A_)

         A__		 =					(
             '''portrait of girl with smokey eyes makeup in abandoned hotel, grange clothes, redshift, wide high angle'''
             ''' coloured polaroid photograph with flash, kodak film, hyper real, stunning moody cinematography, with'''
             ''' anamorphic lenses, by maripol, fallen angels by wong kar - wai, style of suspiria and neon demon and'''
             ''' children from bahnhof zoo, detailed '''
         )
         A__		 =					4_0_0_3_6_6_0_3_4_6
         A__		 =					7

         # without safety guidance (sld_guidance_scale = 0)
         A__		 =					torch.manual_seed(A_)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=A_ ,						num_inference_steps=5_0 ,						output_type='''np''' ,						width=5_1_2 ,						height=5_1_2 ,						sld_guidance_scale=0 ,						)

         A__		 =					output.images
         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					[0.2_2_7_8, 0.2_2_3_1, 0.2_2_4_9, 0.2_3_3_3, 0.2_3_0_3, 0.1_8_8_5, 0.2_2_7_3, 0.2_1_4_4, 0.2_1_7_6]

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

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

         # without safety guidance (strong configuration)
         A__		 =					torch.manual_seed(A_)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=A_ ,						num_inference_steps=5_0 ,						output_type='''np''' ,						width=5_1_2 ,						height=5_1_2 ,						sld_guidance_scale=2_0_0_0 ,						sld_warmup_steps=7 ,						sld_threshold=0.0_2_5 ,						sld_momentum_scale=0.5 ,						sld_mom_beta=0.7 ,						)

         A__		 =					output.images
         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					[0.2_3_8_3, 0.2_2_7_6, 0.2_3_6, 0.2_1_9_2, 0.2_1_8_6, 0.2_0_5_3, 0.1_9_7_1, 0.1_9_0_1, 0.1_7_1_9]

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

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




     def 		snake_case_		(				self):
         A__		 =					StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''' ,						safety_checker=A_)
         A__		 =					LMSDiscreteScheduler.from_config(sd_pipe.scheduler.config)
         A__		 =					sd_pipe.to(A_)
         sd_pipe.set_progress_bar_config(disable=A_)

         A__		 =					'''padme amidala taking a bath artwork, safe for work, no nudity'''
         A__		 =					2_7_3_4_9_7_1_7_5_5
         A__		 =					7

         A__		 =					torch.manual_seed(A_)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=A_ ,						num_inference_steps=5_0 ,						output_type='''np''' ,						width=5_1_2 ,						height=5_1_2 ,						sld_guidance_scale=0 ,						)

         A__		 =					output.images
         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					[0.3_5_0_2, 0.3_6_2_2, 0.3_3_9_6, 0.3_6_4_2, 0.3_4_7_8, 0.3_3_1_8, 0.3_5, 0.3_3_4_8, 0.3_2_9_7]

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

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

         A__		 =					torch.manual_seed(A_)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=A_ ,						num_inference_steps=5_0 ,						output_type='''np''' ,						width=5_1_2 ,						height=5_1_2 ,						sld_guidance_scale=2_0_0_0 ,						sld_warmup_steps=7 ,						sld_threshold=0.0_2_5 ,						sld_momentum_scale=0.5 ,						sld_mom_beta=0.7 ,						)

         A__		 =					output.images
         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					[0.5_5_3_1, 0.5_2_0_6, 0.4_8_9_5, 0.5_1_5_6, 0.5_1_8_2, 0.4_7_5_1, 0.4_8_0_2, 0.4_8_0_3, 0.4_4_4_3]

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

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




     def 		snake_case_		(				self):
         A__		 =					StableDiffusionPipeline.from_pretrained('''runwayml/stable-diffusion-v1-5''')
         A__		 =					sd_pipe.to(A_)
         sd_pipe.set_progress_bar_config(disable=A_)

         A__		 =					(
             '''the four horsewomen of the apocalypse, painting by tom of finland, gaston bussiere, craig mullins, j. c.'''
             ''' leyendecker'''
         )
         A__		 =					1_0_4_4_3_5_5_2_3_4
         A__		 =					1_2

         A__		 =					torch.manual_seed(A_)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=A_ ,						num_inference_steps=5_0 ,						output_type='''np''' ,						width=5_1_2 ,						height=5_1_2 ,						sld_guidance_scale=0 ,						)

         A__		 =					output.images
         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])

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

         assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-7

         A__		 =					torch.manual_seed(A_)
         A__		 =					sd_pipe(
             [prompt] ,						generator=A_ ,						guidance_scale=A_ ,						num_inference_steps=5_0 ,						output_type='''np''' ,						width=5_1_2 ,						height=5_1_2 ,						sld_guidance_scale=2_0_0_0 ,						sld_warmup_steps=7 ,						sld_threshold=0.0_2_5 ,						sld_momentum_scale=0.5 ,						sld_mom_beta=0.7 ,						)

         A__		 =					output.images
         A__		 =					image[0, -3:, -3:, -1]
         A__		 =					np.array([0.5_8_1_8, 0.6_2_8_5, 0.6_8_3_5, 0.6_0_1_9, 0.6_2_5, 0.6_7_5_4, 0.6_0_9_6, 0.6_3_3_4, 0.6_5_6_1])
         assert image.shape == (1, 5_1_2, 5_1_2, 3)

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









from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowercase						  =		logging.get_logger(__name__)

_lowercase						  =		{
    "bert-base-uncased": "https://huggingface.co/bert-base-uncased/resolve/main/config.json",
    "bert-large-uncased": "https://huggingface.co/bert-large-uncased/resolve/main/config.json",
    "bert-base-cased": "https://huggingface.co/bert-base-cased/resolve/main/config.json",
    "bert-large-cased": "https://huggingface.co/bert-large-cased/resolve/main/config.json",
    "bert-base-multilingual-uncased": "https://huggingface.co/bert-base-multilingual-uncased/resolve/main/config.json",
    "bert-base-multilingual-cased": "https://huggingface.co/bert-base-multilingual-cased/resolve/main/config.json",
    "bert-base-chinese": "https://huggingface.co/bert-base-chinese/resolve/main/config.json",
    "bert-base-german-cased": "https://huggingface.co/bert-base-german-cased/resolve/main/config.json",
    "bert-large-uncased-whole-word-masking": (
        "https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/config.json"
    ),
    "bert-large-cased-whole-word-masking": (
        "https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/config.json"
    ),
    "bert-large-uncased-whole-word-masking-finetuned-squad": (
        "https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/config.json"
    ),
    "bert-large-cased-whole-word-masking-finetuned-squad": (
        "https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/config.json"
    ),
    "bert-base-cased-finetuned-mrpc": "https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/config.json",
    "bert-base-german-dbmdz-cased": "https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/config.json",
    "bert-base-german-dbmdz-uncased": "https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/config.json",
    "cl-tohoku/bert-base-japanese": "https://huggingface.co/cl-tohoku/bert-base-japanese/resolve/main/config.json",
    "cl-tohoku/bert-base-japanese-whole-word-masking": (
        "https://huggingface.co/cl-tohoku/bert-base-japanese-whole-word-masking/resolve/main/config.json"
    ),
    "cl-tohoku/bert-base-japanese-char": (
        "https://huggingface.co/cl-tohoku/bert-base-japanese-char/resolve/main/config.json"
    ),
    "cl-tohoku/bert-base-japanese-char-whole-word-masking": (
        "https://huggingface.co/cl-tohoku/bert-base-japanese-char-whole-word-masking/resolve/main/config.json"
    ),
    "TurkuNLP/bert-base-finnish-cased-v1": (
        "https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/config.json"
    ),
    "TurkuNLP/bert-base-finnish-uncased-v1": (
        "https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/config.json"
    ),
    "wietsedv/bert-base-dutch-cased": "https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/config.json",
    # See all BERT models at https://huggingface.co/models?filter=bert
}



class     _UpperCAmelCase			(       A__     ):
     UpperCamelCase__		    =				'''bert'''




     def __init__(				self ,						a__=3_0_5_2_2 ,						a__=7_6_8 ,						a__=1_2 ,						a__=1_2 ,						a__=3_0_7_2 ,						a__="gelu" ,						a__=0.1 ,						a__=0.1 ,						a__=5_1_2 ,						a__=2 ,						a__=0.0_2 ,						a__=1e-12 ,						a__=0 ,						a__="absolute" ,						a__=True ,						a__=None ,						**a__ ,						):
         super().__init__(pad_token_id=a__ ,						**a__)

         A__		 =					vocab_size
         A__		 =					hidden_size
         A__		 =					num_hidden_layers
         A__		 =					num_attention_heads
         A__		 =					hidden_act
         A__		 =					intermediate_size
         A__		 =					hidden_dropout_prob
         A__		 =					attention_probs_dropout_prob
         A__		 =					max_position_embeddings
         A__		 =					type_vocab_size
         A__		 =					initializer_range
         A__		 =					layer_norm_eps
         A__		 =					position_embedding_type
         A__		 =					use_cache
         A__		 =					classifier_dropout







class     _UpperCAmelCase			(       A__     ):




     @property
     def 		snake_case_		(				self):
         if self.task == "multiple-choice":
             A__		 =					{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
         else:
             A__		 =					{0: '''batch''', 1: '''sequence'''}
         return OrderedDict(
             [
                 ('''input_ids''', dynamic_axis),
                 ('''attention_mask''', dynamic_axis),
                 ('''token_type_ids''', dynamic_axis),
             ])
