




"""simple docstring"""







import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class       lowerCAmelCase__       (			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





				__UpperCAmelCase :     List[str]       =      DDIMPipeline
				__UpperCAmelCase :     List[str]       =      UNCONDITIONAL_IMAGE_GENERATION_PARAMS
				__UpperCAmelCase :     str       =      PipelineTesterMixin.required_optional_params - {
				    "num_images_per_prompt",
				    "latents",
				    "callback",
				    "callback_steps",
				}
				__UpperCAmelCase :     Union[str, Any]       =      UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
				__UpperCAmelCase :     str       =      False



				def       snake_case    (     self					:	Union[str, Any]						):
						torch.manual_seed(0						)
						__lowercase				:		List[str]												=				UNetaDModel(
						    block_out_channels=(3_2, 6_4)    ,   layers_per_block=2    ,   sample_size=3_2    ,   in_channels=3    ,   out_channels=3    ,   down_block_types=("DownBlock2D", "AttnDownBlock2D")    ,   up_block_types=("AttnUpBlock2D", "UpBlock2D")    ,   )
						__lowercase				:		Any												=				DDIMScheduler()
						__lowercase				:		Optional[Any]												=				{"unet": unet, "scheduler": scheduler}
						return components



				def       snake_case    (     self					:	Any    ,   lowercase__					:	Tuple    ,   lowercase__					:	Union[str, Any]=0						):
						if str(lowercase__						).startswith("mps"						):
								__lowercase				:		Optional[Any]												=				torch.manual_seed(lowercase__						)
						else:
								__lowercase				:		Optional[Any]												=				torch.Generator(device=lowercase__						).manual_seed(lowercase__						)
						__lowercase				:		Dict												=				{
						    "batch_size": 1,
						    "generator": generator,
						    "num_inference_steps": 2,
						    "output_type": "numpy",
						}
						return inputs



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		List[Any]												=				"cpu"

						__lowercase				:		Tuple												=				self.get_dummy_components()
						__lowercase				:		List[str]												=				self.pipeline_class(**lowercase__						)
						pipe.to(lowercase__						)
						pipe.set_progress_bar_config(disable=lowercase__						)

						__lowercase				:		Optional[int]												=				self.get_dummy_inputs(lowercase__						)
						__lowercase				:		Optional[int]												=				pipe(**lowercase__						).images
						__lowercase				:		List[Any]												=				image[0, -3:, -3:, -1]

						self.assertEqual(image.shape    ,   (1, 3_2, 3_2, 3)						)
						__lowercase				:		List[Any]												=				np.array(
						    [1.000e00, 5.717e-01, 4.717e-01, 1.000e00, 0.000e00, 1.000e00, 3.000e-04, 0.000e00, 9.000e-04]						)
						__lowercase				:		str												=				np.abs(image_slice.flatten() - expected_slice						).max()
						self.assertLessEqual(lowercase__    ,   1e-3						)



				def       snake_case    (     self					:	List[Any]						):
						super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3						)



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



				def       snake_case    (     self					:	Optional[int]						):
						super().test_save_load_optional_components(expected_max_difference=3e-3						)



				def       snake_case    (     self					:	Union[str, 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					:	Union[str, Any]						):
						__lowercase				:		List[str]												=				"google/ddpm-cifar10-32"

						__lowercase				:		List[Any]												=				UNetaDModel.from_pretrained(lowercase__						)
						__lowercase				:		Optional[Any]												=				DDIMScheduler()

						__lowercase				:		Tuple												=				DDIMPipeline(unet=lowercase__    ,   scheduler=lowercase__						)
						ddim.to(lowercase__						)
						ddim.set_progress_bar_config(disable=lowercase__						)

						__lowercase				:		Optional[Any]												=				torch.manual_seed(0						)
						__lowercase				:		List[str]												=				ddim(generator=lowercase__    ,   eta=0.0    ,   output_type="numpy"						).images

						__lowercase				:		List[Any]												=				image[0, -3:, -3:, -1]

						assert image.shape == (1, 3_2, 3_2, 3)
						__lowercase				:		Dict												=				np.array([0.1_7_2_3, 0.1_6_1_7, 0.1_6_0_0, 0.1_6_2_6, 0.1_4_9_7, 0.1_5_1_3, 0.1_5_0_5, 0.1_4_4_2, 0.1_4_5_3]						)

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



				def       snake_case    (     self					:	Dict						):
						__lowercase				:		List[str]												=				"google/ddpm-ema-bedroom-256"

						__lowercase				:		Tuple												=				UNetaDModel.from_pretrained(lowercase__						)
						__lowercase				:		Tuple												=				DDIMScheduler.from_pretrained(lowercase__						)

						__lowercase				:		Union[str, Any]												=				DDIMPipeline(unet=lowercase__    ,   scheduler=lowercase__						)
						ddpm.to(lowercase__						)
						ddpm.set_progress_bar_config(disable=lowercase__						)

						__lowercase				:		Optional[Any]												=				torch.manual_seed(0						)
						__lowercase				:		Optional[int]												=				ddpm(generator=lowercase__    ,   output_type="numpy"						).images

						__lowercase				:		List[str]												=				image[0, -3:, -3:, -1]

						assert image.shape == (1, 2_5_6, 2_5_6, 3)
						__lowercase				:		Tuple												=				np.array([0.0_0_6_0, 0.0_2_0_1, 0.0_3_4_4, 0.0_0_2_4, 0.0_0_1_8, 0.0_0_0_2, 0.0_0_2_2, 0.0_0_0_0, 0.0_0_6_9]						)

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






"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A   :  str					=      logging.get_logger(__name__)

__A   :  str					=      {
    'uclanlp/visualbert-vqa': 'https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json',
    'uclanlp/visualbert-vqa-pre': 'https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json',
    'uclanlp/visualbert-vqa-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json'
    ),
    'uclanlp/visualbert-vcr': 'https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json',
    'uclanlp/visualbert-vcr-pre': 'https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json',
    'uclanlp/visualbert-vcr-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json'
    ),
    'uclanlp/visualbert-nlvr2': 'https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json',
    'uclanlp/visualbert-nlvr2-pre': 'https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json',
    'uclanlp/visualbert-nlvr2-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json'
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}




class       lowerCAmelCase__       (			lowerCAmelCase_    ):


				"""simple docstring"""





				__UpperCAmelCase :     Tuple       =      "visual_bert"



				def __init__(     self					:	List[Any]    ,   lowercase__					:	Dict=3_0_5_2_2    ,   lowercase__					:	Any=7_6_8    ,   lowercase__					:	Dict=5_1_2    ,   lowercase__					:	Tuple=1_2    ,   lowercase__					:	Any=1_2    ,   lowercase__					:	Union[str, Any]=3_0_7_2    ,   lowercase__					:	Tuple="gelu"    ,   lowercase__					:	Dict=0.1    ,   lowercase__					:	Tuple=0.1    ,   lowercase__					:	str=5_1_2    ,   lowercase__					:	str=2    ,   lowercase__					:	Any=0.0_2    ,   lowercase__					:	Any=1e-12    ,   lowercase__					:	Any=False    ,   lowercase__					:	List[str]=True    ,   lowercase__					:	Dict=1    ,   lowercase__					:	List[Any]=0    ,   lowercase__					:	Tuple=2    ,   **lowercase__					:	Any    ,   ):
						super().__init__(pad_token_id=lowercase__    ,   bos_token_id=lowercase__    ,   eos_token_id=lowercase__    ,   **lowercase__						)

						__lowercase				:		Optional[int]												=				vocab_size
						__lowercase				:		Any												=				max_position_embeddings
						__lowercase				:		Optional[Any]												=				hidden_size
						__lowercase				:		List[str]												=				visual_embedding_dim
						__lowercase				:		Dict												=				num_hidden_layers
						__lowercase				:		Any												=				num_attention_heads
						__lowercase				:		str												=				intermediate_size
						__lowercase				:		Dict												=				hidden_act
						__lowercase				:		Any												=				hidden_dropout_prob
						__lowercase				:		Optional[int]												=				attention_probs_dropout_prob
						__lowercase				:		Dict												=				initializer_range
						__lowercase				:		str												=				type_vocab_size
						__lowercase				:		List[Any]												=				layer_norm_eps
						__lowercase				:		Tuple												=				bypass_transformer
						__lowercase				:		List[str]												=				special_visual_initialize
