


'''simple docstring'''
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    PNDMScheduler,
    StableDiffusionLDMaDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import nightly, 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


enable_full_determinism()
class        lowercase     (							unittest.TestCase ):
		_a   			=    StableDiffusionLDMaDPipeline
		_a   			=    TEXT_TO_IMAGE_PARAMS
		_a   			=    TEXT_TO_IMAGE_BATCH_PARAMS
		_a   			=    TEXT_TO_IMAGE_IMAGE_PARAMS




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							torch.manual_seed(0							)
							UpperCamelCase__       :List[str]							 =			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   ,       )
							UpperCamelCase__       :Union[str, Any]							 =			DDIMScheduler(
							    beta_start=0.00085   ,       beta_end=0.012   ,       beta_schedule='''scaled_linear'''   ,       clip_sample=__UpperCAmelCase   ,       set_alpha_to_one=__UpperCAmelCase   ,       )
							torch.manual_seed(0							)
							UpperCamelCase__       :Optional[int]							 =			AutoencoderKL(
							    block_out_channels=[32, 64]   ,       in_channels=6   ,       out_channels=6   ,       down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,       up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,       latent_channels=4   ,       )
							torch.manual_seed(0							)
							UpperCamelCase__       :Optional[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=1000   ,       )
							UpperCamelCase__       :str							 =			CLIPTextModel(__UpperCAmelCase							)
							UpperCamelCase__       :Tuple							 =			CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							)

							UpperCamelCase__       :Optional[Any]							 =			{
							    '''unet''': unet,
							    '''scheduler''': scheduler,
							    '''vae''': vae,
							    '''text_encoder''': text_encoder,
							    '''tokenizer''': tokenizer,
							    '''safety_checker''': None,
							    '''feature_extractor''': None,
							}
							return components




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_=0							):

							'''simple docstring'''


							if str(__UpperCAmelCase							).startswith('''mps'''							):
												UpperCamelCase__       :str							 =			torch.manual_seed(__UpperCAmelCase							)
							else:
												UpperCamelCase__       :str							 =			torch.Generator(device=__UpperCAmelCase							).manual_seed(__UpperCAmelCase							)
							UpperCamelCase__       :List[str]							 =			{
							    '''prompt''': '''A painting of a squirrel eating a burger''',
							    '''generator''': generator,
							    '''num_inference_steps''': 2,
							    '''guidance_scale''': 6.0,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			'''cpu'''  # ensure determinism for the device-dependent torch.Generator

							UpperCamelCase__       :Dict							 =			self.get_dummy_components()
							UpperCamelCase__       :str							 =			StableDiffusionLDMaDPipeline(**__UpperCAmelCase							)
							UpperCamelCase__       :int							 =			ldmad_pipe.to(__UpperCAmelCase							)
							ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase							)

							UpperCamelCase__       :str							 =			self.get_dummy_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :str							 =			ldmad_pipe(**__UpperCAmelCase							)
							UpperCamelCase__ ,    UpperCamelCase__       :Optional[int]							 =			output.rgb, output.depth

							UpperCamelCase__       :Any							 =			rgb[0, -3:, -3:, -1]
							UpperCamelCase__       :List[str]							 =			depth[0, -3:, -1]

							assert rgb.shape == (1, 64, 64, 3)
							assert depth.shape == (1, 64, 64)

							UpperCamelCase__       :List[Any]							 =			np.array(
							    [0.37338176, 0.70247, 0.74203193, 0.51643604, 0.58256793, 0.60932136, 0.4181095, 0.48355877, 0.46535262]							)
							UpperCamelCase__       :Dict							 =			np.array([103.46727, 85.812004, 87.849236]							)

							assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb							).max() < 1e-2
							assert np.abs(image_slice_depth.flatten() - expected_slice_depth							).max() < 1e-2




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			self.get_dummy_components()
							UpperCamelCase__       :Optional[Any]							 =			StableDiffusionLDMaDPipeline(**__UpperCAmelCase							)
							UpperCamelCase__       :Optional[Any]							 =			ldmad_pipe.to(__UpperCAmelCase							)
							ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase							)

							UpperCamelCase__       :List[str]							 =			self.get_dummy_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :List[Any]							 =			3 * [inputs['''prompt''']]

							# forward
							UpperCamelCase__       :str							 =			ldmad_pipe(**__UpperCAmelCase							)
							UpperCamelCase__ ,    UpperCamelCase__       :Optional[int]							 =			output.rgb, output.depth
							UpperCamelCase__       :Union[str, Any]							 =			rgb_slice_a[0, -3:, -3:, -1]
							UpperCamelCase__       :Union[str, Any]							 =			depth_slice_a[0, -3:, -1]

							UpperCamelCase__       :Union[str, Any]							 =			self.get_dummy_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :Optional[Any]							 =			3 * [inputs.pop('''prompt'''							)]

							UpperCamelCase__       :List[Any]							 =			ldmad_pipe.tokenizer(
							    __UpperCAmelCase   ,       padding='''max_length'''   ,       max_length=ldmad_pipe.tokenizer.model_max_length   ,       truncation=__UpperCAmelCase   ,       return_tensors='''pt'''   ,       )
							UpperCamelCase__       :Dict							 =			text_inputs['''input_ids'''].to(__UpperCAmelCase							)

							UpperCamelCase__       :Optional[Any]							 =			ldmad_pipe.text_encoder(__UpperCAmelCase							)[0]

							UpperCamelCase__       :int							 =			prompt_embeds

							# forward
							UpperCamelCase__       :Optional[Any]							 =			ldmad_pipe(**__UpperCAmelCase							)
							UpperCamelCase__ ,    UpperCamelCase__       :Union[str, Any]							 =			output.rgb, output.depth
							UpperCamelCase__       :List[Any]							 =			rgb_slice_a[0, -3:, -3:, -1]
							UpperCamelCase__       :List[str]							 =			depth_slice_a[0, -3:, -1]

							assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten()							).max() < 1e-4
							assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten()							).max() < 1e-4




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Tuple							 =			'''cpu'''  # ensure determinism for the device-dependent torch.Generator
							UpperCamelCase__       :Optional[int]							 =			self.get_dummy_components()
							UpperCamelCase__       :Union[str, Any]							 =			PNDMScheduler(skip_prk_steps=__UpperCAmelCase							)
							UpperCamelCase__       :Optional[Any]							 =			StableDiffusionLDMaDPipeline(**__UpperCAmelCase							)
							UpperCamelCase__       :Any							 =			ldmad_pipe.to(__UpperCAmelCase							)
							ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase							)

							UpperCamelCase__       :Union[str, Any]							 =			self.get_dummy_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :Tuple							 =			'''french fries'''
							UpperCamelCase__       :Union[str, Any]							 =			ldmad_pipe(**__UpperCAmelCase   ,       negative_prompt=__UpperCAmelCase							)

							UpperCamelCase__ ,    UpperCamelCase__       :List[str]							 =			output.rgb, output.depth
							UpperCamelCase__       :Tuple							 =			rgb[0, -3:, -3:, -1]
							UpperCamelCase__       :Union[str, Any]							 =			depth[0, -3:, -1]

							assert rgb.shape == (1, 64, 64, 3)
							assert depth.shape == (1, 64, 64)

							UpperCamelCase__       :List[Any]							 =			np.array(
							    [0.37044, 0.71811503, 0.7223251, 0.48603675, 0.5638391, 0.6364948, 0.42833704, 0.4901315, 0.47926217]							)
							UpperCamelCase__       :Optional[Any]							 =			np.array([107.84738, 84.62802, 89.962135]							)
							assert np.abs(rgb_slice.flatten() - expected_slice_rgb							).max() < 1e-2
							assert np.abs(depth_slice.flatten() - expected_slice_depth							).max() < 1e-2



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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


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




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_="cpu"   ,       UpperCamelCase_=torch.floataa   ,       UpperCamelCase_=0							):

							'''simple docstring'''


							UpperCamelCase__       :str							 =			torch.Generator(device=__UpperCAmelCase							).manual_seed(__UpperCAmelCase							)
							UpperCamelCase__       :str							 =			np.random.RandomState(__UpperCAmelCase							).standard_normal((1, 4, 64, 64)							)
							UpperCamelCase__       :List[str]							 =			torch.from_numpy(__UpperCAmelCase							).to(device=__UpperCAmelCase   ,       dtype=__UpperCAmelCase							)
							UpperCamelCase__       :List[Any]							 =			{
							    '''prompt''': '''a photograph of an astronaut riding a horse''',
							    '''latents''': latents,
							    '''generator''': generator,
							    '''num_inference_steps''': 3,
							    '''guidance_scale''': 7.5,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d'''							)
							UpperCamelCase__       :Optional[Any]							 =			ldmad_pipe.to(__UpperCAmelCase							)
							ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase							)

							UpperCamelCase__       :Optional[Any]							 =			self.get_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :Tuple							 =			ldmad_pipe(**__UpperCAmelCase							)
							UpperCamelCase__ ,    UpperCamelCase__       :Optional[Any]							 =			output.rgb, output.depth
							UpperCamelCase__       :Union[str, Any]							 =			rgb[0, -3:, -3:, -1].flatten()
							UpperCamelCase__       :List[str]							 =			rgb[0, -3:, -1].flatten()

							assert rgb.shape == (1, 512, 512, 3)
							assert depth.shape == (1, 512, 512)

							UpperCamelCase__       :Optional[int]							 =			np.array(
							    [0.53805465, 0.56707305, 0.5486515, 0.57012236, 0.5814511, 0.56253487, 0.54843014, 0.55092263, 0.6459706]							)
							UpperCamelCase__       :Tuple							 =			np.array(
							    [0.9263781, 0.6678672, 0.5486515, 0.92202145, 0.67831135, 0.56253487, 0.9241694, 0.7551478, 0.6459706]							)
							assert np.abs(rgb_slice - expected_slice_rgb							).max() < 3e-3
							assert np.abs(depth_slice - expected_slice_depth							).max() < 3e-3



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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


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




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_="cpu"   ,       UpperCamelCase_=torch.floataa   ,       UpperCamelCase_=0							):

							'''simple docstring'''


							UpperCamelCase__       :List[Any]							 =			torch.Generator(device=__UpperCAmelCase							).manual_seed(__UpperCAmelCase							)
							UpperCamelCase__       :Union[str, Any]							 =			np.random.RandomState(__UpperCAmelCase							).standard_normal((1, 4, 64, 64)							)
							UpperCamelCase__       :Dict							 =			torch.from_numpy(__UpperCAmelCase							).to(device=__UpperCAmelCase   ,       dtype=__UpperCAmelCase							)
							UpperCamelCase__       :Optional[int]							 =			{
							    '''prompt''': '''a photograph of an astronaut riding a horse''',
							    '''latents''': latents,
							    '''generator''': generator,
							    '''num_inference_steps''': 50,
							    '''guidance_scale''': 7.5,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d'''							).to(__UpperCAmelCase							)
							ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase							)

							UpperCamelCase__       :str							 =			self.get_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :Any							 =			ldmad_pipe(**__UpperCAmelCase							)
							UpperCamelCase__ ,    UpperCamelCase__       :Dict							 =			output.rgb, output.depth

							UpperCamelCase__       :List[str]							 =			0.495586
							UpperCamelCase__       :Dict							 =			0.33795515
							UpperCamelCase__       :Any							 =			112.48518
							UpperCamelCase__       :Any							 =			98.489746
							assert np.abs(expected_rgb_mean - rgb.mean()							) < 1e-3
							assert np.abs(expected_rgb_std - rgb.std()							) < 1e-3
							assert np.abs(expected_depth_mean - depth.mean()							) < 1e-3
							assert np.abs(expected_depth_std - depth.std()							) < 1e-3




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :List[str]							 =			StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c'''							).to(__UpperCAmelCase							)
							ldmad_pipe.set_progress_bar_config(disable=__UpperCAmelCase							)

							UpperCamelCase__       :Any							 =			self.get_inputs(__UpperCAmelCase							)
							UpperCamelCase__       :Any							 =			ldmad_pipe(**__UpperCAmelCase							)
							UpperCamelCase__ ,    UpperCamelCase__       :Optional[Any]							 =			output.rgb, output.depth

							UpperCamelCase__       :int							 =			0.4194127
							UpperCamelCase__       :Dict							 =			0.35375586
							UpperCamelCase__       :Dict							 =			0.5638502
							UpperCamelCase__       :int							 =			0.34686103

							assert rgb.shape == (1, 512, 512, 3)
							assert depth.shape == (1, 512, 512, 1)
							assert np.abs(expected_rgb_mean - rgb.mean()							) < 1e-3
							assert np.abs(expected_rgb_std - rgb.std()							) < 1e-3
							assert np.abs(expected_depth_mean - depth.mean()							) < 1e-3
							assert np.abs(expected_depth_std - depth.std()							) < 1e-3



'''simple docstring'''
from collections import OrderedDict
from typing import Mapping

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


__snake_case											=					logging.get_logger(__name__)

__snake_case											=					{
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}
class        lowercase     (							A__ ):

		"""simple docstring"""

		_a   			=    'distilbert'
		_a   			=    {
		    'hidden_size': 'dim',
		    'num_attention_heads': 'n_heads',
		    'num_hidden_layers': 'n_layers',
		}




		def __init__(		self   ,       UpperCamelCase_=30522   ,       UpperCamelCase_=512   ,       UpperCamelCase_=False   ,       UpperCamelCase_=6   ,       UpperCamelCase_=12   ,       UpperCamelCase_=768   ,       UpperCamelCase_=4 * 768   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_="gelu"   ,       UpperCamelCase_=0.02   ,       UpperCamelCase_=0.1   ,       UpperCamelCase_=0.2   ,       UpperCamelCase_=0   ,       **UpperCamelCase_   ,       ):

							'''simple docstring'''


							UpperCamelCase__       :List[Any]							 =			vocab_size
							UpperCamelCase__       :Dict							 =			max_position_embeddings
							UpperCamelCase__       :str							 =			sinusoidal_pos_embds
							UpperCamelCase__       :Any							 =			n_layers
							UpperCamelCase__       :str							 =			n_heads
							UpperCamelCase__       :Tuple							 =			dim
							UpperCamelCase__       :str							 =			hidden_dim
							UpperCamelCase__       :Dict							 =			dropout
							UpperCamelCase__       :int							 =			attention_dropout
							UpperCamelCase__       :Optional[Any]							 =			activation
							UpperCamelCase__       :Optional[int]							 =			initializer_range
							UpperCamelCase__       :Union[str, Any]							 =			qa_dropout
							UpperCamelCase__       :Dict							 =			seq_classif_dropout
							super().__init__(**UpperCamelCase_   ,       pad_token_id=UpperCamelCase_							)



class        lowercase     (							A__ ):

		"""simple docstring"""

		@property
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							if self.task == "multiple-choice":
												UpperCamelCase__       :str							 =			{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
							else:
												UpperCamelCase__       :str							 =			{0: '''batch''', 1: '''sequence'''}
							return OrderedDict(
							    [
							        ('''input_ids''', dynamic_axis),
							        ('''attention_mask''', dynamic_axis),
							    ]							)